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"
25 #include "MetricsInfo.h"
26 #include "texstream.h"
27 #include "TextClass.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/Translator.h"
34 #include "frontends/Application.h"
35 #include "frontends/FontMetrics.h"
36 #include "support/Lexer.h"
37 #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 const translator =
74 init_phantomtranslator();
79 PhantomTranslatorLoc const & phantomtranslator_loc()
81 static PhantomTranslatorLoc const translator =
82 init_phantomtranslator_loc();
89 InsetPhantomParams::InsetPhantomParams()
94 void InsetPhantomParams::write(ostream & os) const
96 string const label = phantomtranslator().find(type);
97 os << "Phantom " << label << "\n";
101 void InsetPhantomParams::read(Lexer & lex)
106 type = phantomtranslator().find(label);
110 /////////////////////////////////////////////////////////////////////
114 /////////////////////////////////////////////////////////////////////
116 InsetPhantom::InsetPhantom(Buffer * buf, string const & label)
117 : InsetCollapsible(buf)
120 params_.type = phantomtranslator().find(label);
124 InsetPhantom::~InsetPhantom()
126 hideDialogs("phantom", this);
130 docstring InsetPhantom::layoutName() const
132 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
136 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
139 InsetCollapsible::draw(pi, x, y);
141 // draw the inset marker
142 drawMarkers(pi, x, y);
145 static int const arrow_size = 4;
146 ColorCode const origcol = pi.base.font.color();
147 pi.base.font.setColor(Color_special);
148 pi.base.font.setColor(origcol);
149 Dimension const dim = dimension(*pi.base.bv);
151 if (params_.type == InsetPhantomParams::Phantom ||
152 params_.type == InsetPhantomParams::VPhantom) {
165 int const x2 = x + dim.wid / 2;
166 int const x1 = x2 - arrow_size;
167 int const x3 = x2 + arrow_size;
169 int const y1 = y - dim.asc;
170 int const y2 = y1 + arrow_size;
171 int const y4 = y + dim.des;
172 int const y3 = y4 - arrow_size;
175 pi.pain.line(x2, y1, x1, y2, Color_added_space);
176 pi.pain.line(x2, y1, x3, y2, Color_added_space);
179 pi.pain.line(x2, y4, x1, y3, Color_added_space);
180 pi.pain.line(x2, y4, x3, y3, Color_added_space);
183 pi.pain.line(x2, y1, x2, y4, Color_added_space);
186 if (params_.type == InsetPhantomParams::Phantom ||
187 params_.type == InsetPhantomParams::HPhantom) {
190 // y2--- <---------------->
196 x += leftOffset(pi.base.bv);
198 int const x2 = x + arrow_size;
199 int const x4 = x + dim.wid - leftOffset(pi.base.bv) - rightOffset(pi.base.bv);
200 int const x3 = x4 - arrow_size;
202 int const y2 = y + (dim.des - dim.asc) / 2;
203 int const y1 = y2 - arrow_size;
204 int const y3 = y2 + arrow_size;
207 pi.pain.line(x1, y2, x2, y3, Color_added_space);
208 pi.pain.line(x1, y2, x2, y1, Color_added_space);
211 pi.pain.line(x4, y2, x3, y3, Color_added_space);
212 pi.pain.line(x4, y2, x3, y1, Color_added_space);
215 pi.pain.line(x1, y2, x4, y2, Color_added_space);
220 void InsetPhantom::write(ostream & os) const
223 InsetCollapsible::write(os);
227 void InsetPhantom::read(Lexer & lex)
230 InsetCollapsible::read(lex);
234 void InsetPhantom::setButtonLabel()
236 docstring const label = phantomtranslator_loc().find(params_.type);
241 bool InsetPhantom::showInsetDialog(BufferView * bv) const
243 bv->showDialog("phantom", params2string(params()),
244 const_cast<InsetPhantom *>(this));
249 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
251 switch (cmd.action()) {
253 case LFUN_INSET_MODIFY:
254 cur.recordUndoInset(this);
255 string2params(to_utf8(cmd.argument()), params_);
257 cur.forceBufferUpdate();
260 case LFUN_INSET_DIALOG_UPDATE:
261 cur.bv().updateDialog("phantom", params2string(params()));
265 InsetCollapsible::doDispatch(cur, cmd);
271 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
272 FuncStatus & flag) const
274 switch (cmd.action()) {
276 case LFUN_INSET_MODIFY:
277 if (cmd.getArg(0) == "phantom") {
278 InsetPhantomParams params;
279 string2params(to_utf8(cmd.argument()), params);
280 flag.setOnOff(params_.type == params.type);
282 flag.setEnabled(true);
285 case LFUN_INSET_DIALOG_UPDATE:
286 flag.setEnabled(true);
290 return InsetCollapsible::getStatus(cur, cmd, flag);
295 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
297 docstring const res = phantomtranslator_loc().find(params_.type);
298 return toolTipText(res + from_ascii(": "));
302 void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
304 if (runparams.moving_arg)
307 switch (params_.type) {
308 case InsetPhantomParams::Phantom:
311 case InsetPhantomParams::HPhantom:
314 case InsetPhantomParams::VPhantom:
321 InsetCollapsible::latex(os, runparams);
326 int InsetPhantom::plaintext(odocstringstream & os,
327 OutputParams const & runparams, size_t max_length) const
329 switch (params_.type) {
330 case InsetPhantomParams::Phantom:
331 os << '[' << buffer().B_("phantom") << ":";
333 case InsetPhantomParams::HPhantom:
334 os << '[' << buffer().B_("hphantom") << ":";
336 case InsetPhantomParams::VPhantom:
337 os << '[' << buffer().B_("vphantom") << ":";
340 os << '[' << buffer().B_("phantom") << ":";
343 InsetCollapsible::plaintext(os, runparams, max_length);
346 return PLAINTEXT_NEWLINE;
350 void InsetPhantom::docbook(XMLStream &, OutputParams const &) const
356 docstring InsetPhantom::xhtml(XMLStream &, OutputParams const &) const
361 string InsetPhantom::contextMenuName() const
363 return "context-phantom";
367 string InsetPhantom::params2string(InsetPhantomParams const & params)
370 data << "phantom" << ' ';
376 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
378 params = InsetPhantomParams();
383 istringstream data(in);
386 lex.setContext("InsetPhantom::string2params");
387 lex >> "phantom" >> "Phantom";