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"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "LaTeXFeatures.h"
26 #include "MetricsInfo.h"
27 #include "OutputParams.h"
28 #include "texstream.h"
29 #include "TextClass.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/Translator.h"
36 #include "frontends/Application.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
49 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
50 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
52 PhantomTranslator const init_phantomtranslator()
54 PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
55 translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
56 translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
61 PhantomTranslatorLoc const init_phantomtranslator_loc()
63 PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
64 translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
65 translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
70 PhantomTranslator const & phantomtranslator()
72 static PhantomTranslator const translator =
73 init_phantomtranslator();
78 PhantomTranslatorLoc const & phantomtranslator_loc()
80 static PhantomTranslatorLoc const translator =
81 init_phantomtranslator_loc();
88 InsetPhantomParams::InsetPhantomParams()
93 void InsetPhantomParams::write(ostream & os) const
95 string const label = phantomtranslator().find(type);
96 os << "Phantom " << label << "\n";
100 void InsetPhantomParams::read(Lexer & lex)
105 type = phantomtranslator().find(label);
109 /////////////////////////////////////////////////////////////////////
113 /////////////////////////////////////////////////////////////////////
115 InsetPhantom::InsetPhantom(Buffer * buf, string const & label)
116 : InsetCollapsible(buf)
119 params_.type = phantomtranslator().find(label);
123 InsetPhantom::~InsetPhantom()
125 hideDialogs("phantom", this);
129 docstring InsetPhantom::layoutName() const
131 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
135 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
138 InsetCollapsible::draw(pi, x, y);
140 // draw the inset marker
141 drawMarkers(pi, x, y);
144 static int const arrow_size = 4;
145 ColorCode const origcol = pi.base.font.color();
146 pi.base.font.setColor(Color_special);
147 pi.base.font.setColor(origcol);
148 Dimension const dim = dimension(*pi.base.bv);
150 if (params_.type == InsetPhantomParams::Phantom ||
151 params_.type == InsetPhantomParams::VPhantom) {
164 int const x2 = x + dim.wid / 2;
165 int const x1 = x2 - arrow_size;
166 int const x3 = x2 + arrow_size;
168 int const y1 = y - dim.asc;
169 int const y2 = y1 + arrow_size;
170 int const y4 = y + dim.des;
171 int const y3 = y4 - arrow_size;
174 pi.pain.line(x2, y1, x1, y2, Color_added_space);
175 pi.pain.line(x2, y1, x3, y2, Color_added_space);
178 pi.pain.line(x2, y4, x1, y3, Color_added_space);
179 pi.pain.line(x2, y4, x3, y3, Color_added_space);
182 pi.pain.line(x2, y1, x2, y4, Color_added_space);
185 if (params_.type == InsetPhantomParams::Phantom ||
186 params_.type == InsetPhantomParams::HPhantom) {
189 // y2--- <---------------->
195 x += leftOffset(pi.base.bv);
197 int const x2 = x + arrow_size;
198 int const x4 = x + dim.wid - leftOffset(pi.base.bv) - rightOffset(pi.base.bv);
199 int const x3 = x4 - arrow_size;
201 int const y2 = y + (dim.des - dim.asc) / 2;
202 int const y1 = y2 - arrow_size;
203 int const y3 = y2 + arrow_size;
206 pi.pain.line(x1, y2, x2, y3, Color_added_space);
207 pi.pain.line(x1, y2, x2, y1, Color_added_space);
210 pi.pain.line(x4, y2, x3, y3, Color_added_space);
211 pi.pain.line(x4, y2, x3, y1, Color_added_space);
214 pi.pain.line(x1, y2, x4, y2, Color_added_space);
219 void InsetPhantom::write(ostream & os) const
222 InsetCollapsible::write(os);
226 void InsetPhantom::read(Lexer & lex)
229 InsetCollapsible::read(lex);
233 void InsetPhantom::setButtonLabel()
235 docstring const label = phantomtranslator_loc().find(params_.type);
240 bool InsetPhantom::showInsetDialog(BufferView * bv) const
242 bv->showDialog("phantom", params2string(params()),
243 const_cast<InsetPhantom *>(this));
248 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
250 switch (cmd.action()) {
252 case LFUN_INSET_MODIFY:
253 cur.recordUndoInset(this);
254 string2params(to_utf8(cmd.argument()), params_);
256 cur.forceBufferUpdate();
259 case LFUN_INSET_DIALOG_UPDATE:
260 cur.bv().updateDialog("phantom", params2string(params()));
264 InsetCollapsible::doDispatch(cur, cmd);
270 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
271 FuncStatus & flag) const
273 switch (cmd.action()) {
275 case LFUN_INSET_MODIFY:
276 if (cmd.getArg(0) == "phantom") {
277 InsetPhantomParams params;
278 string2params(to_utf8(cmd.argument()), params);
279 flag.setOnOff(params_.type == params.type);
281 flag.setEnabled(true);
284 case LFUN_INSET_DIALOG_UPDATE:
285 flag.setEnabled(true);
289 return InsetCollapsible::getStatus(cur, cmd, flag);
294 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
296 docstring const res = phantomtranslator_loc().find(params_.type);
297 return toolTipText(res + from_ascii(": "));
301 void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
303 if (runparams.moving_arg)
306 switch (params_.type) {
307 case InsetPhantomParams::Phantom:
310 case InsetPhantomParams::HPhantom:
313 case InsetPhantomParams::VPhantom:
320 InsetCollapsible::latex(os, runparams);
325 int InsetPhantom::plaintext(odocstringstream & os,
326 OutputParams const & runparams, size_t max_length) const
328 switch (params_.type) {
329 case InsetPhantomParams::Phantom:
330 os << '[' << buffer().B_("phantom") << ":";
332 case InsetPhantomParams::HPhantom:
333 os << '[' << buffer().B_("hphantom") << ":";
335 case InsetPhantomParams::VPhantom:
336 os << '[' << buffer().B_("vphantom") << ":";
339 os << '[' << buffer().B_("phantom") << ":";
342 InsetCollapsible::plaintext(os, runparams, max_length);
345 return PLAINTEXT_NEWLINE;
349 void InsetPhantom::docbook(XMLStream &, OutputParams const &) const
355 docstring InsetPhantom::xhtml(XMLStream &, OutputParams const &) const
360 string InsetPhantom::contextMenuName() const
362 return "context-phantom";
366 string InsetPhantom::params2string(InsetPhantomParams const & params)
369 data << "phantom" << ' ';
375 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
377 params = InsetPhantomParams();
382 istringstream data(in);
385 lex.setContext("InsetPhantom::string2params");
386 lex >> "phantom" >> "Phantom";