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 Inset::DisplayType InsetPhantom::display() const
146 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
148 InsetText::metrics(mi, dim);
150 // cache the inset dimension
151 setDimCache(mi, dim);
155 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
158 InsetText::draw(pi, x, y);
160 // draw the inset marker
161 drawMarkers(pi, x, y);
164 static int const arrow_size = 4;
165 ColorCode const origcol = pi.base.font.color();
166 pi.base.font.setColor(Color_special);
167 pi.base.font.setColor(origcol);
168 Dimension const dim = dimension(*pi.base.bv);
170 if (params_.type == InsetPhantomParams::Phantom ||
171 params_.type == InsetPhantomParams::VPhantom) {
184 int const x2 = x + dim.wid / 2;
185 int const x1 = x2 - arrow_size;
186 int const x3 = x2 + arrow_size;
188 int const y1 = y - dim.asc;
189 int const y2 = y1 + arrow_size;
190 int const y4 = y + dim.des;
191 int const y3 = y4 - arrow_size;
194 pi.pain.line(x2, y1, x1, y2, Color_added_space);
195 pi.pain.line(x2, y1, x3, y2, Color_added_space);
198 pi.pain.line(x2, y4, x1, y3, Color_added_space);
199 pi.pain.line(x2, y4, x3, y3, Color_added_space);
202 pi.pain.line(x2, y1, x2, y4, Color_added_space);
205 if (params_.type == InsetPhantomParams::Phantom ||
206 params_.type == InsetPhantomParams::HPhantom) {
209 // y2--- <---------------->
215 x = x + TEXT_TO_INSET_OFFSET;
217 int const x2 = x + arrow_size;
218 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
219 int const x3 = x4 - arrow_size;
221 int const y2 = y + (dim.des - dim.asc) / 2;
222 int const y1 = y2 - arrow_size;
223 int const y3 = y2 + arrow_size;
226 pi.pain.line(x1, y2, x2, y3, Color_added_space);
227 pi.pain.line(x1, y2, x2, y1, Color_added_space);
230 pi.pain.line(x4, y2, x3, y3, Color_added_space);
231 pi.pain.line(x4, y2, x3, y1, Color_added_space);
234 pi.pain.line(x1, y2, x4, y2, Color_added_space);
239 void InsetPhantom::write(ostream & os) const
242 InsetCollapsable::write(os);
246 void InsetPhantom::read(Lexer & lex)
249 InsetCollapsable::read(lex);
253 void InsetPhantom::setButtonLabel()
255 docstring const label = phantomtranslator_loc().find(params_.type);
260 bool InsetPhantom::showInsetDialog(BufferView * bv) const
262 bv->showDialog("phantom", params2string(params()),
263 const_cast<InsetPhantom *>(this));
268 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
270 switch (cmd.action) {
272 case LFUN_INSET_MODIFY:
273 string2params(to_utf8(cmd.argument()), params_);
274 setLayout(buffer().params());
277 case LFUN_INSET_DIALOG_UPDATE:
278 cur.bv().updateDialog("phantom", params2string(params()));
282 InsetCollapsable::doDispatch(cur, cmd);
288 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
289 FuncStatus & flag) const
291 switch (cmd.action) {
293 case LFUN_INSET_MODIFY:
294 flag.setEnabled(true);
297 case LFUN_INSET_DIALOG_UPDATE:
298 flag.setEnabled(true);
302 return InsetCollapsable::getStatus(cur, cmd, flag);
307 docstring InsetPhantom::toolTip(BufferView const & bv, int x, int y) const
309 OutputParams rp(&buffer().params().encoding());
310 odocstringstream ods;
311 InsetText::plaintext(ods, rp);
312 docstring content_tip = ods.str();
313 // shorten it if necessary
314 if (content_tip.size() > 200)
315 content_tip = content_tip.substr(0, 200) + "...";
316 if (params_.type == InsetPhantomParams::Phantom)
317 return from_ascii("Phantom: ") + content_tip;
318 if (params_.type == InsetPhantomParams::HPhantom)
319 return from_ascii("HPhantom: ") + content_tip;
320 if (params_.type == InsetPhantomParams::VPhantom)
321 return from_ascii("VPhantom: ") + content_tip;
325 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams_in) const
327 OutputParams runparams(runparams_in);
328 if (params_.type == InsetPhantomParams::Phantom)
330 else if (params_.type == InsetPhantomParams::HPhantom)
332 else if (params_.type == InsetPhantomParams::VPhantom)
334 int const i = InsetText::latex(os, runparams);
336 runparams_in.encoding = runparams.encoding;
342 int InsetPhantom::plaintext(odocstream & os,
343 OutputParams const & runparams_in) const
345 OutputParams runparams(runparams_in);
346 if (params_.type == InsetPhantomParams::Phantom)
347 os << '[' << buffer().B_("phantom") << ":";
348 else if (params_.type == InsetPhantomParams::HPhantom)
349 os << '[' << buffer().B_("hphantom") << ":";
350 else if (params_.type == InsetPhantomParams::VPhantom)
351 os << '[' << buffer().B_("vphantom") << ":";
352 InsetText::plaintext(os, runparams);
355 return PLAINTEXT_NEWLINE;
359 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams_in) const
361 OutputParams runparams(runparams_in);
363 if (params_.type == InsetPhantomParams::Phantom)
365 else if (params_.type == InsetPhantomParams::HPhantom)
367 else if (params_.type == InsetPhantomParams::VPhantom)
369 os << "<" + cmdname + ">";
370 int const i = InsetText::docbook(os, runparams);
371 os << "</" + cmdname + ">";
377 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
379 return from_ascii("context-phantom");
383 string InsetPhantom::params2string(InsetPhantomParams const & params)
386 data << "phantom" << ' ';
392 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
394 params = InsetPhantomParams();
399 istringstream data(in);
402 lex.setContext("InsetPhantom::string2params");
403 lex >> "phantom" >> "Phantom";