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/lstrings.h"
36 #include "support/Translator.h"
38 #include "frontends/Application.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
51 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
52 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
54 PhantomTranslator const init_phantomtranslator()
56 PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
57 translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
58 translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
63 PhantomTranslatorLoc const init_phantomtranslator_loc()
65 PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
66 translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
67 translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
72 PhantomTranslator const & phantomtranslator()
74 static PhantomTranslator translator = init_phantomtranslator();
79 PhantomTranslatorLoc const & phantomtranslator_loc()
81 static PhantomTranslatorLoc translator = init_phantomtranslator_loc();
88 InsetPhantomParams::InsetPhantomParams()
93 void InsetPhantomParams::write(ostream & os) const
95 string const label = phantomtranslator().find(type);
96 os << "Phantom " << label << "\n";
100 void InsetPhantomParams::read(Lexer & lex)
105 type = phantomtranslator().find(label);
109 /////////////////////////////////////////////////////////////////////
113 /////////////////////////////////////////////////////////////////////
115 InsetPhantom::InsetPhantom(Buffer * buf, string const & label)
116 : InsetCollapsable(buf)
119 params_.type = phantomtranslator().find(label);
123 InsetPhantom::~InsetPhantom()
125 hideDialogs("phantom", this);
129 docstring InsetPhantom::name() const
131 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
135 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
137 InsetCollapsable::metrics(mi, dim);
139 // cache the inset dimension
140 setDimCache(mi, dim);
144 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
147 InsetCollapsable::draw(pi, x, y);
149 // draw the inset marker
150 drawMarkers(pi, x, y);
153 static int const arrow_size = 4;
154 ColorCode const origcol = pi.base.font.color();
155 pi.base.font.setColor(Color_special);
156 pi.base.font.setColor(origcol);
157 Dimension const dim = dimension(*pi.base.bv);
159 if (params_.type == InsetPhantomParams::Phantom ||
160 params_.type == InsetPhantomParams::VPhantom) {
173 int const x2 = x + dim.wid / 2;
174 int const x1 = x2 - arrow_size;
175 int const x3 = x2 + arrow_size;
177 int const y1 = y - dim.asc;
178 int const y2 = y1 + arrow_size;
179 int const y4 = y + dim.des;
180 int const y3 = y4 - arrow_size;
183 pi.pain.line(x2, y1, x1, y2, Color_added_space);
184 pi.pain.line(x2, y1, x3, y2, Color_added_space);
187 pi.pain.line(x2, y4, x1, y3, Color_added_space);
188 pi.pain.line(x2, y4, x3, y3, Color_added_space);
191 pi.pain.line(x2, y1, x2, y4, Color_added_space);
194 if (params_.type == InsetPhantomParams::Phantom ||
195 params_.type == InsetPhantomParams::HPhantom) {
198 // y2--- <---------------->
204 x = x + TEXT_TO_INSET_OFFSET;
206 int const x2 = x + arrow_size;
207 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
208 int const x3 = x4 - arrow_size;
210 int const y2 = y + (dim.des - dim.asc) / 2;
211 int const y1 = y2 - arrow_size;
212 int const y3 = y2 + arrow_size;
215 pi.pain.line(x1, y2, x2, y3, Color_added_space);
216 pi.pain.line(x1, y2, x2, y1, Color_added_space);
219 pi.pain.line(x4, y2, x3, y3, Color_added_space);
220 pi.pain.line(x4, y2, x3, y1, Color_added_space);
223 pi.pain.line(x1, y2, x4, y2, Color_added_space);
228 void InsetPhantom::write(ostream & os) const
231 InsetCollapsable::write(os);
235 void InsetPhantom::read(Lexer & lex)
238 InsetCollapsable::read(lex);
242 void InsetPhantom::setButtonLabel()
244 docstring const label = phantomtranslator_loc().find(params_.type);
249 bool InsetPhantom::showInsetDialog(BufferView * bv) const
251 bv->showDialog("phantom", params2string(params()),
252 const_cast<InsetPhantom *>(this));
257 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
259 switch (cmd.action()) {
261 case LFUN_INSET_MODIFY:
262 string2params(to_utf8(cmd.argument()), params_);
265 case LFUN_INSET_DIALOG_UPDATE:
266 cur.bv().updateDialog("phantom", params2string(params()));
270 InsetCollapsable::doDispatch(cur, cmd);
276 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
277 FuncStatus & flag) const
279 switch (cmd.action()) {
281 case LFUN_INSET_MODIFY:
282 if (cmd.getArg(0) == "phantom") {
283 InsetPhantomParams params;
284 string2params(to_utf8(cmd.argument()), params);
285 flag.setOnOff(params_.type == params.type);
287 flag.setEnabled(true);
290 case LFUN_INSET_DIALOG_UPDATE:
291 flag.setEnabled(true);
295 return InsetCollapsable::getStatus(cur, cmd, flag);
300 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
302 OutputParams rp(&buffer().params().encoding());
303 odocstringstream ods;
304 InsetCollapsable::plaintext(ods, rp);
305 docstring content_tip = support::wrapParas(ods.str());
306 docstring res = phantomtranslator_loc().find(params_.type);
307 if (!content_tip.empty())
308 res += from_ascii(": ") + "\n" + content_tip;
313 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams) const
315 if (params_.type == InsetPhantomParams::Phantom)
317 else if (params_.type == InsetPhantomParams::HPhantom)
319 else if (params_.type == InsetPhantomParams::VPhantom)
321 int const i = InsetCollapsable::latex(os, runparams);
328 int InsetPhantom::plaintext(odocstream & os,
329 OutputParams const & runparams) const
331 if (params_.type == InsetPhantomParams::Phantom)
332 os << '[' << buffer().B_("phantom") << ":";
333 else if (params_.type == InsetPhantomParams::HPhantom)
334 os << '[' << buffer().B_("hphantom") << ":";
335 else if (params_.type == InsetPhantomParams::VPhantom)
336 os << '[' << buffer().B_("vphantom") << ":";
337 InsetCollapsable::plaintext(os, runparams);
340 return PLAINTEXT_NEWLINE;
344 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
347 if (params_.type == InsetPhantomParams::Phantom)
349 else if (params_.type == InsetPhantomParams::HPhantom)
351 else if (params_.type == InsetPhantomParams::VPhantom)
353 os << "<" + cmdname + ">";
354 int const i = InsetCollapsable::docbook(os, runparams);
355 os << "</" + cmdname + ">";
361 docstring InsetPhantom::xhtml(XHTMLStream &, OutputParams const &) const
366 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
368 return from_ascii("context-phantom");
372 string InsetPhantom::params2string(InsetPhantomParams const & params)
375 data << "phantom" << ' ';
381 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
383 params = InsetPhantomParams();
388 istringstream data(in);
391 lex.setContext("InsetPhantom::string2params");
392 lex >> "phantom" >> "Phantom";