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 "texstream.h"
28 #include "TextClass.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33 #include "support/Translator.h"
35 #include "frontends/Application.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
48 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
49 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
51 PhantomTranslator const init_phantomtranslator()
53 PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
54 translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
55 translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
60 PhantomTranslatorLoc const init_phantomtranslator_loc()
62 PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
63 translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
64 translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
69 PhantomTranslator const & phantomtranslator()
71 static PhantomTranslator const translator =
72 init_phantomtranslator();
77 PhantomTranslatorLoc const & phantomtranslator_loc()
79 static PhantomTranslatorLoc const translator =
80 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 * buf, string const & label)
115 : InsetCollapsible(buf)
118 params_.type = phantomtranslator().find(label);
122 InsetPhantom::~InsetPhantom()
124 hideDialogs("phantom", this);
128 docstring InsetPhantom::layoutName() const
130 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
134 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
137 InsetCollapsible::draw(pi, x, y);
139 // draw the inset marker
140 drawMarkers(pi, x, y);
143 static int const arrow_size = 4;
144 ColorCode const origcol = pi.base.font.color();
145 pi.base.font.setColor(Color_special);
146 pi.base.font.setColor(origcol);
147 Dimension const dim = dimension(*pi.base.bv);
149 if (params_.type == InsetPhantomParams::Phantom ||
150 params_.type == InsetPhantomParams::VPhantom) {
163 int const x2 = x + dim.wid / 2;
164 int const x1 = x2 - arrow_size;
165 int const x3 = x2 + arrow_size;
167 int const y1 = y - dim.asc;
168 int const y2 = y1 + arrow_size;
169 int const y4 = y + dim.des;
170 int const y3 = y4 - arrow_size;
173 pi.pain.line(x2, y1, x1, y2, Color_added_space);
174 pi.pain.line(x2, y1, x3, y2, Color_added_space);
177 pi.pain.line(x2, y4, x1, y3, Color_added_space);
178 pi.pain.line(x2, y4, x3, y3, Color_added_space);
181 pi.pain.line(x2, y1, x2, y4, Color_added_space);
184 if (params_.type == InsetPhantomParams::Phantom ||
185 params_.type == InsetPhantomParams::HPhantom) {
188 // y2--- <---------------->
194 x += leftOffset(pi.base.bv);
196 int const x2 = x + arrow_size;
197 int const x4 = x + dim.wid - leftOffset(pi.base.bv) - rightOffset(pi.base.bv);
198 int const x3 = x4 - arrow_size;
200 int const y2 = y + (dim.des - dim.asc) / 2;
201 int const y1 = y2 - arrow_size;
202 int const y3 = y2 + arrow_size;
205 pi.pain.line(x1, y2, x2, y3, Color_added_space);
206 pi.pain.line(x1, y2, x2, y1, Color_added_space);
209 pi.pain.line(x4, y2, x3, y3, Color_added_space);
210 pi.pain.line(x4, y2, x3, y1, Color_added_space);
213 pi.pain.line(x1, y2, x4, y2, Color_added_space);
218 void InsetPhantom::write(ostream & os) const
221 InsetCollapsible::write(os);
225 void InsetPhantom::read(Lexer & lex)
228 InsetCollapsible::read(lex);
232 void InsetPhantom::setButtonLabel()
234 docstring const label = phantomtranslator_loc().find(params_.type);
239 bool InsetPhantom::showInsetDialog(BufferView * bv) const
241 bv->showDialog("phantom", params2string(params()),
242 const_cast<InsetPhantom *>(this));
247 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
249 switch (cmd.action()) {
251 case LFUN_INSET_MODIFY:
252 cur.recordUndoInset(this);
253 string2params(to_utf8(cmd.argument()), params_);
255 cur.forceBufferUpdate();
258 case LFUN_INSET_DIALOG_UPDATE:
259 cur.bv().updateDialog("phantom", params2string(params()));
263 InsetCollapsible::doDispatch(cur, cmd);
269 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
270 FuncStatus & flag) const
272 switch (cmd.action()) {
274 case LFUN_INSET_MODIFY:
275 if (cmd.getArg(0) == "phantom") {
276 InsetPhantomParams params;
277 string2params(to_utf8(cmd.argument()), params);
278 flag.setOnOff(params_.type == params.type);
280 flag.setEnabled(true);
283 case LFUN_INSET_DIALOG_UPDATE:
284 flag.setEnabled(true);
288 return InsetCollapsible::getStatus(cur, cmd, flag);
293 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
295 docstring const res = phantomtranslator_loc().find(params_.type);
296 return toolTipText(res + from_ascii(": "));
300 void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
302 if (runparams.moving_arg)
305 switch (params_.type) {
306 case InsetPhantomParams::Phantom:
309 case InsetPhantomParams::HPhantom:
312 case InsetPhantomParams::VPhantom:
319 InsetCollapsible::latex(os, runparams);
324 int InsetPhantom::plaintext(odocstringstream & os,
325 OutputParams const & runparams, size_t max_length) const
327 switch (params_.type) {
328 case InsetPhantomParams::Phantom:
329 os << '[' << buffer().B_("phantom") << ":";
331 case InsetPhantomParams::HPhantom:
332 os << '[' << buffer().B_("hphantom") << ":";
334 case InsetPhantomParams::VPhantom:
335 os << '[' << buffer().B_("vphantom") << ":";
338 os << '[' << buffer().B_("phantom") << ":";
341 InsetCollapsible::plaintext(os, runparams, max_length);
344 return PLAINTEXT_NEWLINE;
348 void InsetPhantom::docbook(XMLStream &, OutputParams const &) const
354 docstring InsetPhantom::xhtml(XMLStream &, OutputParams const &) const
359 string InsetPhantom::contextMenuName() const
361 return "context-phantom";
365 string InsetPhantom::params2string(InsetPhantomParams const & params)
368 data << "phantom" << ' ';
374 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
376 params = InsetPhantomParams();
381 istringstream data(in);
384 lex.setContext("InsetPhantom::string2params");
385 lex >> "phantom" >> "Phantom";