2 * \file InsetPhantom.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetPhantom.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "BufferParams.h"
21 #include "Dimension.h"
22 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetIterator.h"
27 #include "LaTeXFeatures.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "TextClass.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/Translator.h"
37 #include "frontends/Application.h"
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
50 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
51 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
53 PhantomTranslator const init_phantomtranslator()
55 PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
56 translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
57 translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
62 PhantomTranslatorLoc const init_phantomtranslator_loc()
64 PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
65 translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
66 translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
71 PhantomTranslator const & phantomtranslator()
73 static PhantomTranslator translator = init_phantomtranslator();
78 PhantomTranslatorLoc const & phantomtranslator_loc()
80 static PhantomTranslatorLoc translator = init_phantomtranslator_loc();
87 InsetPhantomParams::InsetPhantomParams()
92 void InsetPhantomParams::write(ostream & os) const
94 string const label = phantomtranslator().find(type);
95 os << "Phantom " << label << "\n";
99 void InsetPhantomParams::read(Lexer & lex)
104 type = phantomtranslator().find(label);
108 /////////////////////////////////////////////////////////////////////
112 /////////////////////////////////////////////////////////////////////
114 InsetPhantom::InsetPhantom(Buffer const & buf, string const & label)
115 : InsetCollapsable(buf)
118 params_.type = phantomtranslator().find(label);
122 InsetPhantom::~InsetPhantom()
124 hideDialogs("phantom", this);
128 docstring InsetPhantom::editMessage() const
130 return _("Opened Phantom Inset");
134 docstring InsetPhantom::name() const
136 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
140 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
142 InsetCollapsable::metrics(mi, dim);
144 // cache the inset dimension
145 setDimCache(mi, dim);
149 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
152 InsetCollapsable::draw(pi, x, y);
154 // draw the inset marker
155 drawMarkers(pi, x, y);
158 static int const arrow_size = 4;
159 ColorCode const origcol = pi.base.font.color();
160 pi.base.font.setColor(Color_special);
161 pi.base.font.setColor(origcol);
162 Dimension const dim = dimension(*pi.base.bv);
164 if (params_.type == InsetPhantomParams::Phantom ||
165 params_.type == InsetPhantomParams::VPhantom) {
178 int const x2 = x + dim.wid / 2;
179 int const x1 = x2 - arrow_size;
180 int const x3 = x2 + arrow_size;
182 int const y1 = y - dim.asc;
183 int const y2 = y1 + arrow_size;
184 int const y4 = y + dim.des;
185 int const y3 = y4 - arrow_size;
188 pi.pain.line(x2, y1, x1, y2, Color_added_space);
189 pi.pain.line(x2, y1, x3, y2, Color_added_space);
192 pi.pain.line(x2, y4, x1, y3, Color_added_space);
193 pi.pain.line(x2, y4, x3, y3, Color_added_space);
196 pi.pain.line(x2, y1, x2, y4, Color_added_space);
199 if (params_.type == InsetPhantomParams::Phantom ||
200 params_.type == InsetPhantomParams::HPhantom) {
203 // y2--- <---------------->
209 x = x + TEXT_TO_INSET_OFFSET;
211 int const x2 = x + arrow_size;
212 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
213 int const x3 = x4 - arrow_size;
215 int const y2 = y + (dim.des - dim.asc) / 2;
216 int const y1 = y2 - arrow_size;
217 int const y3 = y2 + arrow_size;
220 pi.pain.line(x1, y2, x2, y3, Color_added_space);
221 pi.pain.line(x1, y2, x2, y1, Color_added_space);
224 pi.pain.line(x4, y2, x3, y3, Color_added_space);
225 pi.pain.line(x4, y2, x3, y1, Color_added_space);
228 pi.pain.line(x1, y2, x4, y2, Color_added_space);
233 void InsetPhantom::write(ostream & os) const
236 InsetCollapsable::write(os);
240 void InsetPhantom::read(Lexer & lex)
243 InsetCollapsable::read(lex);
247 void InsetPhantom::setButtonLabel()
249 docstring const label = phantomtranslator_loc().find(params_.type);
254 bool InsetPhantom::showInsetDialog(BufferView * bv) const
256 bv->showDialog("phantom", params2string(params()),
257 const_cast<InsetPhantom *>(this));
262 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
264 switch (cmd.action) {
266 case LFUN_INSET_MODIFY:
267 string2params(to_utf8(cmd.argument()), params_);
268 setLayout(buffer().params());
271 case LFUN_INSET_DIALOG_UPDATE:
272 cur.bv().updateDialog("phantom", params2string(params()));
276 InsetCollapsable::doDispatch(cur, cmd);
282 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
283 FuncStatus & flag) const
285 switch (cmd.action) {
287 case LFUN_INSET_MODIFY:
288 flag.setEnabled(true);
291 case LFUN_INSET_DIALOG_UPDATE:
292 flag.setEnabled(true);
296 return InsetCollapsable::getStatus(cur, cmd, flag);
301 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
303 OutputParams rp(&buffer().params().encoding());
304 odocstringstream ods;
305 InsetCollapsable::plaintext(ods, rp);
306 docstring content_tip = ods.str();
307 // shorten it if necessary
308 if (content_tip.size() > 200)
309 content_tip = content_tip.substr(0, 200) + "...";
310 docstring res = phantomtranslator_loc().find(params_.type);
311 if (!content_tip.empty())
312 res += from_ascii(": ") + content_tip;
317 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams) const
319 if (params_.type == InsetPhantomParams::Phantom)
321 else if (params_.type == InsetPhantomParams::HPhantom)
323 else if (params_.type == InsetPhantomParams::VPhantom)
325 int const i = InsetCollapsable::latex(os, runparams);
332 int InsetPhantom::plaintext(odocstream & os,
333 OutputParams const & runparams) const
335 if (params_.type == InsetPhantomParams::Phantom)
336 os << '[' << buffer().B_("phantom") << ":";
337 else if (params_.type == InsetPhantomParams::HPhantom)
338 os << '[' << buffer().B_("hphantom") << ":";
339 else if (params_.type == InsetPhantomParams::VPhantom)
340 os << '[' << buffer().B_("vphantom") << ":";
341 InsetCollapsable::plaintext(os, runparams);
344 return PLAINTEXT_NEWLINE;
348 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
351 if (params_.type == InsetPhantomParams::Phantom)
353 else if (params_.type == InsetPhantomParams::HPhantom)
355 else if (params_.type == InsetPhantomParams::VPhantom)
357 os << "<" + cmdname + ">";
358 int const i = InsetCollapsable::docbook(os, runparams);
359 os << "</" + cmdname + ">";
365 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
367 return from_ascii("context-phantom");
371 string InsetPhantom::params2string(InsetPhantomParams const & params)
374 data << "phantom" << ' ';
380 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
382 params = InsetPhantomParams();
387 istringstream data(in);
390 lex.setContext("InsetPhantom::string2params");
391 lex >> "phantom" >> "Phantom";