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);
160 static int const arrow_size = 4;
161 ColorCode const origcol = pi.base.font.color();
162 pi.base.font.setColor(Color_special);
163 pi.base.font.setColor(origcol);
164 Dimension const dim = dimension(*pi.base.bv);
166 if (params_.type == InsetPhantomParams::Phantom ||
167 params_.type == InsetPhantomParams::VPhantom) {
180 int const x2 = x + dim.wid / 2;
181 int const x1 = x2 - arrow_size;
182 int const x3 = x2 + arrow_size;
184 int const y1 = y - dim.asc;
185 int const y2 = y1 + arrow_size;
186 int const y4 = y + dim.des;
187 int const y3 = y4 - arrow_size;
190 pi.pain.line(x2, y1, x1, y2, Color_added_space);
191 pi.pain.line(x2, y1, x3, y2, Color_added_space);
194 pi.pain.line(x2, y4, x1, y3, Color_added_space);
195 pi.pain.line(x2, y4, x3, y3, Color_added_space);
198 pi.pain.line(x2, y1, x2, y4, Color_added_space);
201 if (params_.type == InsetPhantomParams::Phantom ||
202 params_.type == InsetPhantomParams::HPhantom) {
205 // y2--- <---------------->
212 int const x2 = x + arrow_size;
213 int const x4 = x + dim.wid;
214 int const x3 = x4 - arrow_size;
216 int const y2 = y + (dim.des - dim.asc) / 2;
217 int const y1 = y2 - arrow_size;
218 int const y3 = y2 + arrow_size;
221 pi.pain.line(x1, y2, x2, y3, Color_added_space);
222 pi.pain.line(x1, y2, x2, y1, Color_added_space);
225 pi.pain.line(x4, y2, x3, y3, Color_added_space);
226 pi.pain.line(x4, y2, x3, y1, Color_added_space);
229 pi.pain.line(x1, y2, x4, y2, Color_added_space);
232 drawMarkers(pi, x, y);
236 void InsetPhantom::write(ostream & os) const
239 InsetCollapsable::write(os);
243 void InsetPhantom::read(Lexer & lex)
246 InsetCollapsable::read(lex);
250 void InsetPhantom::setButtonLabel()
252 docstring const label = phantomtranslator_loc().find(params_.type);
257 bool InsetPhantom::showInsetDialog(BufferView * bv) const
259 bv->showDialog("phantom", params2string(params()),
260 const_cast<InsetPhantom *>(this));
265 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
267 switch (cmd.action) {
269 case LFUN_INSET_MODIFY:
270 string2params(to_utf8(cmd.argument()), params_);
271 setLayout(buffer().params());
274 case LFUN_INSET_DIALOG_UPDATE:
275 cur.bv().updateDialog("phantom", params2string(params()));
279 InsetCollapsable::doDispatch(cur, cmd);
285 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
286 FuncStatus & flag) const
288 switch (cmd.action) {
290 case LFUN_INSET_MODIFY:
291 flag.setEnabled(true);
294 case LFUN_INSET_DIALOG_UPDATE:
295 flag.setEnabled(true);
299 return InsetCollapsable::getStatus(cur, cmd, flag);
304 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams_in) const
306 OutputParams runparams(runparams_in);
307 if (params_.type == InsetPhantomParams::Phantom)
309 else if (params_.type == InsetPhantomParams::HPhantom)
311 else if (params_.type == InsetPhantomParams::VPhantom)
313 int const i = InsetText::latex(os, runparams);
315 runparams_in.encoding = runparams.encoding;
321 int InsetPhantom::plaintext(odocstream & os,
322 OutputParams const & runparams_in) const
324 OutputParams runparams(runparams_in);
325 if (params_.type == InsetPhantomParams::Phantom)
326 os << '[' << buffer().B_("phantom") << ":";
327 else if (params_.type == InsetPhantomParams::HPhantom)
328 os << '[' << buffer().B_("hphantom") << ":";
329 else if (params_.type == InsetPhantomParams::VPhantom)
330 os << '[' << buffer().B_("vphantom") << ":";
331 InsetText::plaintext(os, runparams);
334 return PLAINTEXT_NEWLINE;
338 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams_in) const
340 OutputParams runparams(runparams_in);
342 if (params_.type == InsetPhantomParams::Phantom)
344 else if (params_.type == InsetPhantomParams::HPhantom)
346 else if (params_.type == InsetPhantomParams::VPhantom)
348 os << "<" + cmdname + ">";
349 int const i = InsetText::docbook(os, runparams);
350 os << "</" + cmdname + ">";
356 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
358 return from_ascii("context-phantom");
362 string InsetPhantom::params2string(InsetPhantomParams const & params)
365 data << "phantom" << ' ';
371 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
373 params = InsetPhantomParams();
378 istringstream data(in);
381 lex.setContext("InsetPhantom::string2params");
382 lex >> "phantom" >> "Phantom";