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 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
142 InsetCollapsable::metrics(mi, dim);
144 // cache the inset dimension
145 setDimCache(mi, dim);
149 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
152 InsetCollapsable::draw(pi, x, y);
154 // draw the inset marker
155 drawMarkers(pi, x, y);
158 static int const arrow_size = 4;
159 ColorCode const origcol = pi.base.font.color();
160 pi.base.font.setColor(Color_special);
161 pi.base.font.setColor(origcol);
162 Dimension const dim = dimension(*pi.base.bv);
164 if (params_.type == InsetPhantomParams::Phantom ||
165 params_.type == InsetPhantomParams::VPhantom) {
178 int const x2 = x + dim.wid / 2;
179 int const x1 = x2 - arrow_size;
180 int const x3 = x2 + arrow_size;
182 int const y1 = y - dim.asc;
183 int const y2 = y1 + arrow_size;
184 int const y4 = y + dim.des;
185 int const y3 = y4 - arrow_size;
188 pi.pain.line(x2, y1, x1, y2, Color_added_space);
189 pi.pain.line(x2, y1, x3, y2, Color_added_space);
192 pi.pain.line(x2, y4, x1, y3, Color_added_space);
193 pi.pain.line(x2, y4, x3, y3, Color_added_space);
196 pi.pain.line(x2, y1, x2, y4, Color_added_space);
199 if (params_.type == InsetPhantomParams::Phantom ||
200 params_.type == InsetPhantomParams::HPhantom) {
203 // y2--- <---------------->
209 x = x + TEXT_TO_INSET_OFFSET;
211 int const x2 = x + arrow_size;
212 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
213 int const x3 = x4 - arrow_size;
215 int const y2 = y + (dim.des - dim.asc) / 2;
216 int const y1 = y2 - arrow_size;
217 int const y3 = y2 + arrow_size;
220 pi.pain.line(x1, y2, x2, y3, Color_added_space);
221 pi.pain.line(x1, y2, x2, y1, Color_added_space);
224 pi.pain.line(x4, y2, x3, y3, Color_added_space);
225 pi.pain.line(x4, y2, x3, y1, Color_added_space);
228 pi.pain.line(x1, y2, x4, y2, Color_added_space);
233 void InsetPhantom::write(ostream & os) const
236 InsetCollapsable::write(os);
240 void InsetPhantom::read(Lexer & lex)
243 InsetCollapsable::read(lex);
247 void InsetPhantom::setButtonLabel()
249 docstring const label = phantomtranslator_loc().find(params_.type);
254 bool InsetPhantom::showInsetDialog(BufferView * bv) const
256 bv->showDialog("phantom", params2string(params()),
257 const_cast<InsetPhantom *>(this));
262 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
264 switch (cmd.action) {
266 case LFUN_INSET_MODIFY:
267 string2params(to_utf8(cmd.argument()), params_);
268 setLayout(buffer().params());
271 case LFUN_INSET_DIALOG_UPDATE:
272 cur.bv().updateDialog("phantom", params2string(params()));
276 InsetCollapsable::doDispatch(cur, cmd);
282 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
283 FuncStatus & flag) const
285 switch (cmd.action) {
287 case LFUN_INSET_MODIFY:
288 if (cmd.getArg(0) == "phantom") {
289 InsetPhantomParams params;
290 string2params(to_utf8(cmd.argument()), params);
291 flag.setOnOff(params_.type == params.type);
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 &, int, int) const
308 OutputParams rp(&buffer().params().encoding());
309 odocstringstream ods;
310 InsetCollapsable::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 docstring res = phantomtranslator_loc().find(params_.type);
316 if (!content_tip.empty())
317 res += from_ascii(": ") + content_tip;
322 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams) const
324 if (params_.type == InsetPhantomParams::Phantom)
326 else if (params_.type == InsetPhantomParams::HPhantom)
328 else if (params_.type == InsetPhantomParams::VPhantom)
330 int const i = InsetCollapsable::latex(os, runparams);
337 int InsetPhantom::plaintext(odocstream & os,
338 OutputParams const & runparams) const
340 if (params_.type == InsetPhantomParams::Phantom)
341 os << '[' << buffer().B_("phantom") << ":";
342 else if (params_.type == InsetPhantomParams::HPhantom)
343 os << '[' << buffer().B_("hphantom") << ":";
344 else if (params_.type == InsetPhantomParams::VPhantom)
345 os << '[' << buffer().B_("vphantom") << ":";
346 InsetCollapsable::plaintext(os, runparams);
349 return PLAINTEXT_NEWLINE;
353 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
356 if (params_.type == InsetPhantomParams::Phantom)
358 else if (params_.type == InsetPhantomParams::HPhantom)
360 else if (params_.type == InsetPhantomParams::VPhantom)
362 os << "<" + cmdname + ">";
363 int const i = InsetCollapsable::docbook(os, runparams);
364 os << "</" + cmdname + ">";
370 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
372 return from_ascii("context-phantom");
376 string InsetPhantom::params2string(InsetPhantomParams const & params)
379 data << "phantom" << ' ';
385 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
387 params = InsetPhantomParams();
392 istringstream data(in);
395 lex.setContext("InsetPhantom::string2params");
396 lex >> "phantom" >> "Phantom";