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 "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 translator = init_phantomtranslator();
79 PhantomTranslatorLoc const & phantomtranslator_loc()
81 static PhantomTranslatorLoc translator = 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 : InsetCollapsable(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::metrics(MetricsInfo & mi, Dimension & dim) const
137 InsetCollapsable::metrics(mi, dim);
139 // cache the inset dimension
140 setDimCache(mi, dim);
144 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
147 InsetCollapsable::draw(pi, x, y);
149 // draw the inset marker
150 drawMarkers(pi, x, y);
153 static int const arrow_size = 4;
154 ColorCode const origcol = pi.base.font.color();
155 pi.base.font.setColor(Color_special);
156 pi.base.font.setColor(origcol);
157 Dimension const dim = Inset::dimension(*pi.base.bv);
159 if (params_.type == InsetPhantomParams::Phantom ||
160 params_.type == InsetPhantomParams::VPhantom) {
173 int const x2 = x + dim.wid / 2;
174 int const x1 = x2 - arrow_size;
175 int const x3 = x2 + arrow_size;
177 int const y1 = y - dim.asc;
178 int const y2 = y1 + arrow_size;
179 int const y4 = y + dim.des;
180 int const y3 = y4 - arrow_size;
183 pi.pain.line(x2, y1, x1, y2, Color_added_space);
184 pi.pain.line(x2, y1, x3, y2, Color_added_space);
187 pi.pain.line(x2, y4, x1, y3, Color_added_space);
188 pi.pain.line(x2, y4, x3, y3, Color_added_space);
191 pi.pain.line(x2, y1, x2, y4, Color_added_space);
194 if (params_.type == InsetPhantomParams::Phantom ||
195 params_.type == InsetPhantomParams::HPhantom) {
198 // y2--- <---------------->
204 x = x + TEXT_TO_INSET_OFFSET;
206 int const x2 = x + arrow_size;
207 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
208 int const x3 = x4 - arrow_size;
210 int const y2 = y + (dim.des - dim.asc) / 2;
211 int const y1 = y2 - arrow_size;
212 int const y3 = y2 + arrow_size;
215 pi.pain.line(x1, y2, x2, y3, Color_added_space);
216 pi.pain.line(x1, y2, x2, y1, Color_added_space);
219 pi.pain.line(x4, y2, x3, y3, Color_added_space);
220 pi.pain.line(x4, y2, x3, y1, Color_added_space);
223 pi.pain.line(x1, y2, x4, y2, Color_added_space);
228 void InsetPhantom::write(ostream & os) const
231 InsetCollapsable::write(os);
235 void InsetPhantom::read(Lexer & lex)
238 InsetCollapsable::read(lex);
242 void InsetPhantom::setButtonLabel()
244 docstring const label = phantomtranslator_loc().find(params_.type);
249 bool InsetPhantom::showInsetDialog(BufferView * bv) const
251 bv->showDialog("phantom", params2string(params()),
252 const_cast<InsetPhantom *>(this));
257 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
259 switch (cmd.action()) {
261 case LFUN_INSET_MODIFY:
262 cur.recordUndoInset(ATOMIC_UNDO, this);
263 string2params(to_utf8(cmd.argument()), params_);
265 cur.forceBufferUpdate();
268 case LFUN_INSET_DIALOG_UPDATE:
269 cur.bv().updateDialog("phantom", params2string(params()));
273 InsetCollapsable::doDispatch(cur, cmd);
279 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
280 FuncStatus & flag) const
282 switch (cmd.action()) {
284 case LFUN_INSET_MODIFY:
285 if (cmd.getArg(0) == "phantom") {
286 InsetPhantomParams params;
287 string2params(to_utf8(cmd.argument()), params);
288 flag.setOnOff(params_.type == params.type);
290 flag.setEnabled(true);
293 case LFUN_INSET_DIALOG_UPDATE:
294 flag.setEnabled(true);
298 return InsetCollapsable::getStatus(cur, cmd, flag);
303 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
305 docstring const res = phantomtranslator_loc().find(params_.type);
306 return toolTipText(res + from_ascii(": "));
310 void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
312 if (params_.type == InsetPhantomParams::Phantom)
314 else if (params_.type == InsetPhantomParams::HPhantom)
316 else if (params_.type == InsetPhantomParams::VPhantom)
318 InsetCollapsable::latex(os, runparams);
323 int InsetPhantom::plaintext(odocstringstream & os,
324 OutputParams const & runparams, size_t max_length) const
326 if (params_.type == InsetPhantomParams::Phantom)
327 os << '[' << buffer().B_("phantom") << ":";
328 else if (params_.type == InsetPhantomParams::HPhantom)
329 os << '[' << buffer().B_("hphantom") << ":";
330 else if (params_.type == InsetPhantomParams::VPhantom)
331 os << '[' << buffer().B_("vphantom") << ":";
332 InsetCollapsable::plaintext(os, runparams, max_length);
335 return PLAINTEXT_NEWLINE;
339 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
342 if (params_.type == InsetPhantomParams::Phantom)
344 else if (params_.type == InsetPhantomParams::HPhantom)
346 else if (params_.type == InsetPhantomParams::VPhantom)
348 os << "<" + cmdname + ">";
349 int const i = InsetCollapsable::docbook(os, runparams);
350 os << "</" + cmdname + ">";
356 docstring InsetPhantom::xhtml(XHTMLStream &, 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";