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 docstring InsetPhantom::toolTip(BufferView const & bv, int x, int y) const
308 OutputParams rp(&buffer().params().encoding());
309 odocstringstream ods;
310 InsetText::plaintext(ods, rp);
311 docstring content_tip = ods.str();
312 // shorten it if necessary
313 if (content_tip.size() > 200)
314 content_tip = content_tip.substr(0, 200) + "...";
315 if (params_.type == InsetPhantomParams::Phantom)
316 return from_ascii("Phantom: ") + content_tip;
317 if (params_.type == InsetPhantomParams::HPhantom)
318 return from_ascii("HPhantom: ") + content_tip;
319 if (params_.type == InsetPhantomParams::VPhantom)
320 return from_ascii("VPhantom: ") + content_tip;
324 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams_in) const
326 OutputParams runparams(runparams_in);
327 if (params_.type == InsetPhantomParams::Phantom)
329 else if (params_.type == InsetPhantomParams::HPhantom)
331 else if (params_.type == InsetPhantomParams::VPhantom)
333 int const i = InsetText::latex(os, runparams);
335 runparams_in.encoding = runparams.encoding;
341 int InsetPhantom::plaintext(odocstream & os,
342 OutputParams const & runparams_in) const
344 OutputParams runparams(runparams_in);
345 if (params_.type == InsetPhantomParams::Phantom)
346 os << '[' << buffer().B_("phantom") << ":";
347 else if (params_.type == InsetPhantomParams::HPhantom)
348 os << '[' << buffer().B_("hphantom") << ":";
349 else if (params_.type == InsetPhantomParams::VPhantom)
350 os << '[' << buffer().B_("vphantom") << ":";
351 InsetText::plaintext(os, runparams);
354 return PLAINTEXT_NEWLINE;
358 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams_in) const
360 OutputParams runparams(runparams_in);
362 if (params_.type == InsetPhantomParams::Phantom)
364 else if (params_.type == InsetPhantomParams::HPhantom)
366 else if (params_.type == InsetPhantomParams::VPhantom)
368 os << "<" + cmdname + ">";
369 int const i = InsetText::docbook(os, runparams);
370 os << "</" + cmdname + ">";
376 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
378 return from_ascii("context-phantom");
382 string InsetPhantom::params2string(InsetPhantomParams const & params)
385 data << "phantom" << ' ';
391 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
393 params = InsetPhantomParams();
398 istringstream data(in);
401 lex.setContext("InsetPhantom::string2params");
402 lex >> "phantom" >> "Phantom";