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"
20 #include "Dimension.h"
21 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "LaTeXFeatures.h"
28 #include "MetricsInfo.h"
29 #include "OutputParams.h"
30 #include "texstream.h"
31 #include "TextClass.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/Translator.h"
38 #include "frontends/Application.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
51 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
52 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
54 PhantomTranslator const init_phantomtranslator()
56 PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
57 translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
58 translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
63 PhantomTranslatorLoc const init_phantomtranslator_loc()
65 PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
66 translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
67 translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
72 PhantomTranslator const & phantomtranslator()
74 static PhantomTranslator const translator =
75 init_phantomtranslator();
80 PhantomTranslatorLoc const & phantomtranslator_loc()
82 static PhantomTranslatorLoc const translator =
83 init_phantomtranslator_loc();
90 InsetPhantomParams::InsetPhantomParams()
95 void InsetPhantomParams::write(ostream & os) const
97 string const label = phantomtranslator().find(type);
98 os << "Phantom " << label << "\n";
102 void InsetPhantomParams::read(Lexer & lex)
107 type = phantomtranslator().find(label);
111 /////////////////////////////////////////////////////////////////////
115 /////////////////////////////////////////////////////////////////////
117 InsetPhantom::InsetPhantom(Buffer * buf, string const & label)
118 : InsetCollapsible(buf)
121 params_.type = phantomtranslator().find(label);
125 InsetPhantom::~InsetPhantom()
127 hideDialogs("phantom", this);
131 docstring InsetPhantom::layoutName() const
133 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
137 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
140 InsetCollapsible::draw(pi, x, y);
142 // draw the inset marker
143 drawMarkers(pi, x, y);
146 static int const arrow_size = 4;
147 ColorCode const origcol = pi.base.font.color();
148 pi.base.font.setColor(Color_special);
149 pi.base.font.setColor(origcol);
150 Dimension const dim = dimension(*pi.base.bv);
152 if (params_.type == InsetPhantomParams::Phantom ||
153 params_.type == InsetPhantomParams::VPhantom) {
166 int const x2 = x + dim.wid / 2;
167 int const x1 = x2 - arrow_size;
168 int const x3 = x2 + arrow_size;
170 int const y1 = y - dim.asc;
171 int const y2 = y1 + arrow_size;
172 int const y4 = y + dim.des;
173 int const y3 = y4 - arrow_size;
176 pi.pain.line(x2, y1, x1, y2, Color_added_space);
177 pi.pain.line(x2, y1, x3, y2, Color_added_space);
180 pi.pain.line(x2, y4, x1, y3, Color_added_space);
181 pi.pain.line(x2, y4, x3, y3, Color_added_space);
184 pi.pain.line(x2, y1, x2, y4, Color_added_space);
187 if (params_.type == InsetPhantomParams::Phantom ||
188 params_.type == InsetPhantomParams::HPhantom) {
191 // y2--- <---------------->
197 x += leftOffset(pi.base.bv);
199 int const x2 = x + arrow_size;
200 int const x4 = x + dim.wid - leftOffset(pi.base.bv) - rightOffset(pi.base.bv);
201 int const x3 = x4 - arrow_size;
203 int const y2 = y + (dim.des - dim.asc) / 2;
204 int const y1 = y2 - arrow_size;
205 int const y3 = y2 + arrow_size;
208 pi.pain.line(x1, y2, x2, y3, Color_added_space);
209 pi.pain.line(x1, y2, x2, y1, Color_added_space);
212 pi.pain.line(x4, y2, x3, y3, Color_added_space);
213 pi.pain.line(x4, y2, x3, y1, Color_added_space);
216 pi.pain.line(x1, y2, x4, y2, Color_added_space);
221 void InsetPhantom::write(ostream & os) const
224 InsetCollapsible::write(os);
228 void InsetPhantom::read(Lexer & lex)
231 InsetCollapsible::read(lex);
235 void InsetPhantom::setButtonLabel()
237 docstring const label = phantomtranslator_loc().find(params_.type);
242 bool InsetPhantom::showInsetDialog(BufferView * bv) const
244 bv->showDialog("phantom", params2string(params()),
245 const_cast<InsetPhantom *>(this));
250 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
252 switch (cmd.action()) {
254 case LFUN_INSET_MODIFY:
255 cur.recordUndoInset(this);
256 string2params(to_utf8(cmd.argument()), params_);
258 cur.forceBufferUpdate();
261 case LFUN_INSET_DIALOG_UPDATE:
262 cur.bv().updateDialog("phantom", params2string(params()));
266 InsetCollapsible::doDispatch(cur, cmd);
272 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
273 FuncStatus & flag) const
275 switch (cmd.action()) {
277 case LFUN_INSET_MODIFY:
278 if (cmd.getArg(0) == "phantom") {
279 InsetPhantomParams params;
280 string2params(to_utf8(cmd.argument()), params);
281 flag.setOnOff(params_.type == params.type);
283 flag.setEnabled(true);
286 case LFUN_INSET_DIALOG_UPDATE:
287 flag.setEnabled(true);
291 return InsetCollapsible::getStatus(cur, cmd, flag);
296 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
298 docstring const res = phantomtranslator_loc().find(params_.type);
299 return toolTipText(res + from_ascii(": "));
303 void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
305 if (runparams.moving_arg)
308 switch (params_.type) {
309 case InsetPhantomParams::Phantom:
312 case InsetPhantomParams::HPhantom:
315 case InsetPhantomParams::VPhantom:
322 InsetCollapsible::latex(os, runparams);
327 int InsetPhantom::plaintext(odocstringstream & os,
328 OutputParams const & runparams, size_t max_length) const
330 switch (params_.type) {
331 case InsetPhantomParams::Phantom:
332 os << '[' << buffer().B_("phantom") << ":";
334 case InsetPhantomParams::HPhantom:
335 os << '[' << buffer().B_("hphantom") << ":";
337 case InsetPhantomParams::VPhantom:
338 os << '[' << buffer().B_("vphantom") << ":";
341 os << '[' << buffer().B_("phantom") << ":";
344 InsetCollapsible::plaintext(os, runparams, max_length);
347 return PLAINTEXT_NEWLINE;
351 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
354 switch (params_.type) {
355 case InsetPhantomParams::Phantom:
356 case InsetPhantomParams::HPhantom:
357 case InsetPhantomParams::VPhantom:
359 cmdname = from_ascii("phantom");
362 os << "<" + cmdname + ">";
363 int const i = InsetCollapsible::docbook(os, runparams);
364 os << "</" + cmdname + ">";
370 docstring InsetPhantom::xhtml(XHTMLStream &, OutputParams const &) const
375 string InsetPhantom::contextMenuName() const
377 return "context-phantom";
381 string InsetPhantom::params2string(InsetPhantomParams const & params)
384 data << "phantom" << ' ';
390 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
392 params = InsetPhantomParams();
397 istringstream data(in);
400 lex.setContext("InsetPhantom::string2params");
401 lex >> "phantom" >> "Phantom";