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 const & buf, string const & label)
116 : InsetCollapsable(buf)
119 params_.type = phantomtranslator().find(label);
123 InsetPhantom::~InsetPhantom()
125 hideDialogs("phantom", this);
129 docstring InsetPhantom::editMessage() const
131 return _("Opened Phantom Inset");
135 docstring InsetPhantom::name() const
137 return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
141 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
143 InsetCollapsable::metrics(mi, dim);
145 // cache the inset dimension
146 setDimCache(mi, dim);
150 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
153 InsetCollapsable::draw(pi, x, y);
155 // draw the inset marker
156 drawMarkers(pi, x, y);
159 static int const arrow_size = 4;
160 ColorCode const origcol = pi.base.font.color();
161 pi.base.font.setColor(Color_special);
162 pi.base.font.setColor(origcol);
163 Dimension const dim = dimension(*pi.base.bv);
165 if (params_.type == InsetPhantomParams::Phantom ||
166 params_.type == InsetPhantomParams::VPhantom) {
179 int const x2 = x + dim.wid / 2;
180 int const x1 = x2 - arrow_size;
181 int const x3 = x2 + arrow_size;
183 int const y1 = y - dim.asc;
184 int const y2 = y1 + arrow_size;
185 int const y4 = y + dim.des;
186 int const y3 = y4 - arrow_size;
189 pi.pain.line(x2, y1, x1, y2, Color_added_space);
190 pi.pain.line(x2, y1, x3, y2, Color_added_space);
193 pi.pain.line(x2, y4, x1, y3, Color_added_space);
194 pi.pain.line(x2, y4, x3, y3, Color_added_space);
197 pi.pain.line(x2, y1, x2, y4, Color_added_space);
200 if (params_.type == InsetPhantomParams::Phantom ||
201 params_.type == InsetPhantomParams::HPhantom) {
204 // y2--- <---------------->
210 x = x + TEXT_TO_INSET_OFFSET;
212 int const x2 = x + arrow_size;
213 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
214 int const x3 = x4 - arrow_size;
216 int const y2 = y + (dim.des - dim.asc) / 2;
217 int const y1 = y2 - arrow_size;
218 int const y3 = y2 + arrow_size;
221 pi.pain.line(x1, y2, x2, y3, Color_added_space);
222 pi.pain.line(x1, y2, x2, y1, Color_added_space);
225 pi.pain.line(x4, y2, x3, y3, Color_added_space);
226 pi.pain.line(x4, y2, x3, y1, Color_added_space);
229 pi.pain.line(x1, y2, x4, y2, Color_added_space);
234 void InsetPhantom::write(ostream & os) const
237 InsetCollapsable::write(os);
241 void InsetPhantom::read(Lexer & lex)
244 InsetCollapsable::read(lex);
248 void InsetPhantom::setButtonLabel()
250 docstring const label = phantomtranslator_loc().find(params_.type);
255 bool InsetPhantom::showInsetDialog(BufferView * bv) const
257 bv->showDialog("phantom", params2string(params()),
258 const_cast<InsetPhantom *>(this));
263 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
265 switch (cmd.action) {
267 case LFUN_INSET_MODIFY:
268 string2params(to_utf8(cmd.argument()), params_);
269 setLayout(buffer().params());
272 case LFUN_INSET_DIALOG_UPDATE:
273 cur.bv().updateDialog("phantom", params2string(params()));
277 InsetCollapsable::doDispatch(cur, cmd);
283 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
284 FuncStatus & flag) const
286 switch (cmd.action) {
288 case LFUN_INSET_MODIFY:
289 if (cmd.getArg(0) == "phantom") {
290 InsetPhantomParams params;
291 string2params(to_utf8(cmd.argument()), params);
292 flag.setOnOff(params_.type == params.type);
294 flag.setEnabled(true);
297 case LFUN_INSET_DIALOG_UPDATE:
298 flag.setEnabled(true);
302 return InsetCollapsable::getStatus(cur, cmd, flag);
307 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
309 OutputParams rp(&buffer().params().encoding());
310 odocstringstream ods;
311 InsetCollapsable::plaintext(ods, rp);
312 docstring content_tip = support::wrapParas(ods.str());
313 docstring res = phantomtranslator_loc().find(params_.type);
314 if (!content_tip.empty())
315 res += from_ascii(": ") + "\n" + content_tip;
320 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams) const
322 if (params_.type == InsetPhantomParams::Phantom)
324 else if (params_.type == InsetPhantomParams::HPhantom)
326 else if (params_.type == InsetPhantomParams::VPhantom)
328 int const i = InsetCollapsable::latex(os, runparams);
335 int InsetPhantom::plaintext(odocstream & os,
336 OutputParams const & runparams) const
338 if (params_.type == InsetPhantomParams::Phantom)
339 os << '[' << buffer().B_("phantom") << ":";
340 else if (params_.type == InsetPhantomParams::HPhantom)
341 os << '[' << buffer().B_("hphantom") << ":";
342 else if (params_.type == InsetPhantomParams::VPhantom)
343 os << '[' << buffer().B_("vphantom") << ":";
344 InsetCollapsable::plaintext(os, runparams);
347 return PLAINTEXT_NEWLINE;
351 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
354 if (params_.type == InsetPhantomParams::Phantom)
356 else if (params_.type == InsetPhantomParams::HPhantom)
358 else if (params_.type == InsetPhantomParams::VPhantom)
360 os << "<" + cmdname + ">";
361 int const i = InsetCollapsable::docbook(os, runparams);
362 os << "</" + cmdname + ">";
368 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
370 return from_ascii("context-phantom");
374 string InsetPhantom::params2string(InsetPhantomParams const & params)
377 data << "phantom" << ' ';
383 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
385 params = InsetPhantomParams();
390 istringstream data(in);
393 lex.setContext("InsetPhantom::string2params");
394 lex >> "phantom" >> "Phantom";