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)
117 params_.type = phantomtranslator().find(label);
121 InsetPhantom::~InsetPhantom()
123 hideDialogs("phantom", this);
127 docstring InsetPhantom::editMessage() const
129 return _("Opened Phantom Inset");
133 docstring InsetPhantom::name() const
135 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
139 Inset::DisplayType InsetPhantom::display() const
145 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
147 InsetText::metrics(mi, dim);
149 // cache the inset dimension
150 setDimCache(mi, dim);
154 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
157 InsetText::draw(pi, x, y);
159 // draw the inset marker
160 drawMarkers(pi, x, y);
163 static int const arrow_size = 4;
164 ColorCode const origcol = pi.base.font.color();
165 pi.base.font.setColor(Color_special);
166 pi.base.font.setColor(origcol);
167 Dimension const dim = dimension(*pi.base.bv);
169 if (params_.type == InsetPhantomParams::Phantom ||
170 params_.type == InsetPhantomParams::VPhantom) {
183 int const x2 = x + dim.wid / 2;
184 int const x1 = x2 - arrow_size;
185 int const x3 = x2 + arrow_size;
187 int const y1 = y - dim.asc;
188 int const y2 = y1 + arrow_size;
189 int const y4 = y + dim.des;
190 int const y3 = y4 - arrow_size;
193 pi.pain.line(x2, y1, x1, y2, Color_added_space);
194 pi.pain.line(x2, y1, x3, y2, Color_added_space);
197 pi.pain.line(x2, y4, x1, y3, Color_added_space);
198 pi.pain.line(x2, y4, x3, y3, Color_added_space);
201 pi.pain.line(x2, y1, x2, y4, Color_added_space);
204 if (params_.type == InsetPhantomParams::Phantom ||
205 params_.type == InsetPhantomParams::HPhantom) {
208 // y2--- <---------------->
214 x = x + TEXT_TO_INSET_OFFSET;
216 int const x2 = x + arrow_size;
217 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
218 int const x3 = x4 - arrow_size;
220 int const y2 = y + (dim.des - dim.asc) / 2;
221 int const y1 = y2 - arrow_size;
222 int const y3 = y2 + arrow_size;
225 pi.pain.line(x1, y2, x2, y3, Color_added_space);
226 pi.pain.line(x1, y2, x2, y1, Color_added_space);
229 pi.pain.line(x4, y2, x3, y3, Color_added_space);
230 pi.pain.line(x4, y2, x3, y1, Color_added_space);
233 pi.pain.line(x1, y2, x4, y2, Color_added_space);
238 void InsetPhantom::write(ostream & os) const
241 InsetCollapsable::write(os);
245 void InsetPhantom::read(Lexer & lex)
248 InsetCollapsable::read(lex);
252 void InsetPhantom::setButtonLabel()
254 docstring const label = phantomtranslator_loc().find(params_.type);
259 bool InsetPhantom::showInsetDialog(BufferView * bv) const
261 bv->showDialog("phantom", params2string(params()),
262 const_cast<InsetPhantom *>(this));
267 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
269 switch (cmd.action) {
271 case LFUN_INSET_MODIFY:
272 string2params(to_utf8(cmd.argument()), params_);
273 setLayout(buffer().params());
276 case LFUN_INSET_DIALOG_UPDATE:
277 cur.bv().updateDialog("phantom", params2string(params()));
281 InsetCollapsable::doDispatch(cur, cmd);
287 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
288 FuncStatus & flag) const
290 switch (cmd.action) {
292 case LFUN_INSET_MODIFY:
293 flag.setEnabled(true);
296 case LFUN_INSET_DIALOG_UPDATE:
297 flag.setEnabled(true);
301 return InsetCollapsable::getStatus(cur, cmd, flag);
306 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams_in) const
308 OutputParams runparams(runparams_in);
309 if (params_.type == InsetPhantomParams::Phantom)
311 else if (params_.type == InsetPhantomParams::HPhantom)
313 else if (params_.type == InsetPhantomParams::VPhantom)
315 int const i = InsetText::latex(os, runparams);
317 runparams_in.encoding = runparams.encoding;
323 int InsetPhantom::plaintext(odocstream & os,
324 OutputParams const & runparams_in) const
326 OutputParams runparams(runparams_in);
327 if (params_.type == InsetPhantomParams::Phantom)
328 os << '[' << buffer().B_("phantom") << ":";
329 else if (params_.type == InsetPhantomParams::HPhantom)
330 os << '[' << buffer().B_("hphantom") << ":";
331 else if (params_.type == InsetPhantomParams::VPhantom)
332 os << '[' << buffer().B_("vphantom") << ":";
333 InsetText::plaintext(os, runparams);
336 return PLAINTEXT_NEWLINE;
340 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams_in) const
342 OutputParams runparams(runparams_in);
344 if (params_.type == InsetPhantomParams::Phantom)
346 else if (params_.type == InsetPhantomParams::HPhantom)
348 else if (params_.type == InsetPhantomParams::VPhantom)
350 os << "<" + cmdname + ">";
351 int const i = InsetText::docbook(os, runparams);
352 os << "</" + cmdname + ">";
358 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
360 return from_ascii("context-phantom");
364 string InsetPhantom::params2string(InsetPhantomParams const & params)
367 data << "phantom" << ' ';
373 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
375 params = InsetPhantomParams();
380 istringstream data(in);
383 lex.setContext("InsetPhantom::string2params");
384 lex >> "phantom" >> "Phantom";