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 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 : InsetCollapsable(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::metrics(MetricsInfo & mi, Dimension & dim) const
139 InsetCollapsable::metrics(mi, dim);
141 // cache the inset dimension
142 setDimCache(mi, dim);
146 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
149 InsetCollapsable::draw(pi, x, y);
151 // draw the inset marker
152 drawMarkers(pi, x, y);
155 static int const arrow_size = 4;
156 ColorCode const origcol = pi.base.font.color();
157 pi.base.font.setColor(Color_special);
158 pi.base.font.setColor(origcol);
159 Dimension const dim = Inset::dimension(*pi.base.bv);
161 if (params_.type == InsetPhantomParams::Phantom ||
162 params_.type == InsetPhantomParams::VPhantom) {
175 int const x2 = x + dim.wid / 2;
176 int const x1 = x2 - arrow_size;
177 int const x3 = x2 + arrow_size;
179 int const y1 = y - dim.asc;
180 int const y2 = y1 + arrow_size;
181 int const y4 = y + dim.des;
182 int const y3 = y4 - arrow_size;
185 pi.pain.line(x2, y1, x1, y2, Color_added_space);
186 pi.pain.line(x2, y1, x3, y2, Color_added_space);
189 pi.pain.line(x2, y4, x1, y3, Color_added_space);
190 pi.pain.line(x2, y4, x3, y3, Color_added_space);
193 pi.pain.line(x2, y1, x2, y4, Color_added_space);
196 if (params_.type == InsetPhantomParams::Phantom ||
197 params_.type == InsetPhantomParams::HPhantom) {
200 // y2--- <---------------->
206 x = x + TEXT_TO_INSET_OFFSET;
208 int const x2 = x + arrow_size;
209 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
210 int const x3 = x4 - arrow_size;
212 int const y2 = y + (dim.des - dim.asc) / 2;
213 int const y1 = y2 - arrow_size;
214 int const y3 = y2 + arrow_size;
217 pi.pain.line(x1, y2, x2, y3, Color_added_space);
218 pi.pain.line(x1, y2, x2, y1, Color_added_space);
221 pi.pain.line(x4, y2, x3, y3, Color_added_space);
222 pi.pain.line(x4, y2, x3, y1, Color_added_space);
225 pi.pain.line(x1, y2, x4, y2, Color_added_space);
230 void InsetPhantom::write(ostream & os) const
233 InsetCollapsable::write(os);
237 void InsetPhantom::read(Lexer & lex)
240 InsetCollapsable::read(lex);
244 void InsetPhantom::setButtonLabel()
246 docstring const label = phantomtranslator_loc().find(params_.type);
251 bool InsetPhantom::showInsetDialog(BufferView * bv) const
253 bv->showDialog("phantom", params2string(params()),
254 const_cast<InsetPhantom *>(this));
259 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
261 switch (cmd.action()) {
263 case LFUN_INSET_MODIFY:
264 cur.recordUndoInset(ATOMIC_UNDO, this);
265 string2params(to_utf8(cmd.argument()), params_);
267 cur.forceBufferUpdate();
270 case LFUN_INSET_DIALOG_UPDATE:
271 cur.bv().updateDialog("phantom", params2string(params()));
275 InsetCollapsable::doDispatch(cur, cmd);
281 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
282 FuncStatus & flag) const
284 switch (cmd.action()) {
286 case LFUN_INSET_MODIFY:
287 if (cmd.getArg(0) == "phantom") {
288 InsetPhantomParams params;
289 string2params(to_utf8(cmd.argument()), params);
290 flag.setOnOff(params_.type == params.type);
292 flag.setEnabled(true);
295 case LFUN_INSET_DIALOG_UPDATE:
296 flag.setEnabled(true);
300 return InsetCollapsable::getStatus(cur, cmd, flag);
305 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
307 docstring const res = phantomtranslator_loc().find(params_.type);
308 return toolTipText(res + from_ascii(": "));
312 void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
314 if (runparams.moving_arg)
316 if (params_.type == InsetPhantomParams::Phantom)
318 else if (params_.type == InsetPhantomParams::HPhantom)
320 else if (params_.type == InsetPhantomParams::VPhantom)
322 InsetCollapsable::latex(os, runparams);
327 int InsetPhantom::plaintext(odocstringstream & os,
328 OutputParams const & runparams, size_t max_length) const
330 if (params_.type == InsetPhantomParams::Phantom)
331 os << '[' << buffer().B_("phantom") << ":";
332 else if (params_.type == InsetPhantomParams::HPhantom)
333 os << '[' << buffer().B_("hphantom") << ":";
334 else if (params_.type == InsetPhantomParams::VPhantom)
335 os << '[' << buffer().B_("vphantom") << ":";
336 InsetCollapsable::plaintext(os, runparams, max_length);
339 return PLAINTEXT_NEWLINE;
343 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
346 if (params_.type == InsetPhantomParams::Phantom)
348 else if (params_.type == InsetPhantomParams::HPhantom)
350 else if (params_.type == InsetPhantomParams::VPhantom)
352 os << "<" + cmdname + ">";
353 int const i = InsetCollapsable::docbook(os, runparams);
354 os << "</" + cmdname + ">";
360 docstring InsetPhantom::xhtml(XHTMLStream &, OutputParams const &) const
365 string InsetPhantom::contextMenuName() const
367 return "context-phantom";
371 string InsetPhantom::params2string(InsetPhantomParams const & params)
374 data << "phantom" << ' ';
380 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
382 params = InsetPhantomParams();
387 istringstream data(in);
390 lex.setContext("InsetPhantom::string2params");
391 lex >> "phantom" >> "Phantom";