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 "texstream.h"
32 #include "TextClass.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37 #include "support/Translator.h"
39 #include "frontends/Application.h"
40 #include "frontends/FontMetrics.h"
41 #include "frontends/Painter.h"
52 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
53 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
55 PhantomTranslator const init_phantomtranslator()
57 PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
58 translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
59 translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
64 PhantomTranslatorLoc const init_phantomtranslator_loc()
66 PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
67 translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
68 translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
73 PhantomTranslator const & phantomtranslator()
75 static PhantomTranslator const translator =
76 init_phantomtranslator();
81 PhantomTranslatorLoc const & phantomtranslator_loc()
83 static PhantomTranslatorLoc const translator =
84 init_phantomtranslator_loc();
91 InsetPhantomParams::InsetPhantomParams()
96 void InsetPhantomParams::write(ostream & os) const
98 string const label = phantomtranslator().find(type);
99 os << "Phantom " << label << "\n";
103 void InsetPhantomParams::read(Lexer & lex)
108 type = phantomtranslator().find(label);
112 /////////////////////////////////////////////////////////////////////
116 /////////////////////////////////////////////////////////////////////
118 InsetPhantom::InsetPhantom(Buffer * buf, string const & label)
119 : InsetCollapsible(buf)
122 params_.type = phantomtranslator().find(label);
126 InsetPhantom::~InsetPhantom()
128 hideDialogs("phantom", this);
132 docstring InsetPhantom::layoutName() const
134 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
138 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
141 InsetCollapsible::draw(pi, x, y);
143 // draw the inset marker
144 drawMarkers(pi, x, y);
147 static int const arrow_size = 4;
148 ColorCode const origcol = pi.base.font.color();
149 pi.base.font.setColor(Color_special);
150 pi.base.font.setColor(origcol);
151 Dimension const dim = dimension(*pi.base.bv);
153 if (params_.type == InsetPhantomParams::Phantom ||
154 params_.type == InsetPhantomParams::VPhantom) {
167 int const x2 = x + dim.wid / 2;
168 int const x1 = x2 - arrow_size;
169 int const x3 = x2 + arrow_size;
171 int const y1 = y - dim.asc;
172 int const y2 = y1 + arrow_size;
173 int const y4 = y + dim.des;
174 int const y3 = y4 - arrow_size;
177 pi.pain.line(x2, y1, x1, y2, Color_added_space);
178 pi.pain.line(x2, y1, x3, y2, Color_added_space);
181 pi.pain.line(x2, y4, x1, y3, Color_added_space);
182 pi.pain.line(x2, y4, x3, y3, Color_added_space);
185 pi.pain.line(x2, y1, x2, y4, Color_added_space);
188 if (params_.type == InsetPhantomParams::Phantom ||
189 params_.type == InsetPhantomParams::HPhantom) {
192 // y2--- <---------------->
198 x = x + TEXT_TO_INSET_OFFSET;
200 int const x2 = x + arrow_size;
201 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
202 int const x3 = x4 - arrow_size;
204 int const y2 = y + (dim.des - dim.asc) / 2;
205 int const y1 = y2 - arrow_size;
206 int const y3 = y2 + arrow_size;
209 pi.pain.line(x1, y2, x2, y3, Color_added_space);
210 pi.pain.line(x1, y2, x2, y1, Color_added_space);
213 pi.pain.line(x4, y2, x3, y3, Color_added_space);
214 pi.pain.line(x4, y2, x3, y1, Color_added_space);
217 pi.pain.line(x1, y2, x4, y2, Color_added_space);
222 void InsetPhantom::write(ostream & os) const
225 InsetCollapsible::write(os);
229 void InsetPhantom::read(Lexer & lex)
232 InsetCollapsible::read(lex);
236 void InsetPhantom::setButtonLabel()
238 docstring const label = phantomtranslator_loc().find(params_.type);
243 bool InsetPhantom::showInsetDialog(BufferView * bv) const
245 bv->showDialog("phantom", params2string(params()),
246 const_cast<InsetPhantom *>(this));
251 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
253 switch (cmd.action()) {
255 case LFUN_INSET_MODIFY:
256 cur.recordUndoInset(this);
257 string2params(to_utf8(cmd.argument()), params_);
259 cur.forceBufferUpdate();
262 case LFUN_INSET_DIALOG_UPDATE:
263 cur.bv().updateDialog("phantom", params2string(params()));
267 InsetCollapsible::doDispatch(cur, cmd);
273 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
274 FuncStatus & flag) const
276 switch (cmd.action()) {
278 case LFUN_INSET_MODIFY:
279 if (cmd.getArg(0) == "phantom") {
280 InsetPhantomParams params;
281 string2params(to_utf8(cmd.argument()), params);
282 flag.setOnOff(params_.type == params.type);
284 flag.setEnabled(true);
287 case LFUN_INSET_DIALOG_UPDATE:
288 flag.setEnabled(true);
292 return InsetCollapsible::getStatus(cur, cmd, flag);
297 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
299 docstring const res = phantomtranslator_loc().find(params_.type);
300 return toolTipText(res + from_ascii(": "));
304 void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
306 if (runparams.moving_arg)
309 switch (params_.type) {
310 case InsetPhantomParams::Phantom:
313 case InsetPhantomParams::HPhantom:
316 case InsetPhantomParams::VPhantom:
323 InsetCollapsible::latex(os, runparams);
328 int InsetPhantom::plaintext(odocstringstream & os,
329 OutputParams const & runparams, size_t max_length) const
331 switch (params_.type) {
332 case InsetPhantomParams::Phantom:
333 os << '[' << buffer().B_("phantom") << ":";
335 case InsetPhantomParams::HPhantom:
336 os << '[' << buffer().B_("hphantom") << ":";
338 case InsetPhantomParams::VPhantom:
339 os << '[' << buffer().B_("vphantom") << ":";
342 os << '[' << buffer().B_("phantom") << ":";
345 InsetCollapsible::plaintext(os, runparams, max_length);
348 return PLAINTEXT_NEWLINE;
352 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
355 switch (params_.type) {
356 case InsetPhantomParams::Phantom:
357 case InsetPhantomParams::HPhantom:
358 case InsetPhantomParams::VPhantom:
360 cmdname = from_ascii("phantom");
363 os << "<" + cmdname + ">";
364 int const i = InsetCollapsible::docbook(os, runparams);
365 os << "</" + cmdname + ">";
371 docstring InsetPhantom::xhtml(XHTMLStream &, OutputParams const &) const
376 string InsetPhantom::contextMenuName() const
378 return "context-phantom";
382 string InsetPhantom::params2string(InsetPhantomParams const & params)
385 data << "phantom" << ' ';
391 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
393 params = InsetPhantomParams();
398 istringstream data(in);
401 lex.setContext("InsetPhantom::string2params");
402 lex >> "phantom" >> "Phantom";