]> git.lyx.org Git - features.git/blob - src/insets/InsetPhantom.cpp
* InsetPhantom.cpp (toolTip):
[features.git] / src / insets / InsetPhantom.cpp
1 /**
2  * \file InsetPhantom.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Uwe Stöhr
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetPhantom.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "BufferParams.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "Dimension.h"
22 #include "DispatchResult.h"
23 #include "Exporter.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetIterator.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "TextClass.h"
32
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/Translator.h"
36
37 #include "frontends/Application.h"
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 #include <algorithm>
42 #include <sstream>
43
44 using namespace std;
45
46 namespace lyx {
47
48 namespace {
49
50 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
51 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
52
53 PhantomTranslator const init_phantomtranslator()
54 {
55         PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
56         translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
57         translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
58         return translator;
59 }
60
61
62 PhantomTranslatorLoc const init_phantomtranslator_loc()
63 {
64         PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
65         translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
66         translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
67         return translator;
68 }
69
70
71 PhantomTranslator const & phantomtranslator()
72 {
73         static PhantomTranslator translator = init_phantomtranslator();
74         return translator;
75 }
76
77
78 PhantomTranslatorLoc const & phantomtranslator_loc()
79 {
80         static PhantomTranslatorLoc translator = init_phantomtranslator_loc();
81         return translator;
82 }
83
84 } // anon
85
86
87 InsetPhantomParams::InsetPhantomParams()
88         : type(Phantom)
89 {}
90
91
92 void InsetPhantomParams::write(ostream & os) const
93 {
94         string const label = phantomtranslator().find(type);
95         os << "Phantom " << label << "\n";
96 }
97
98
99 void InsetPhantomParams::read(Lexer & lex)
100 {
101         string label;
102         lex >> label;
103         if (lex)
104                 type = phantomtranslator().find(label);
105 }
106
107
108 /////////////////////////////////////////////////////////////////////
109 //
110 // InsetPhantom
111 //
112 /////////////////////////////////////////////////////////////////////
113
114 InsetPhantom::InsetPhantom(Buffer const & buf, string const & label)
115         : InsetCollapsable(buf)
116 {
117         setDrawFrame(false);
118         params_.type = phantomtranslator().find(label);
119 }
120
121
122 InsetPhantom::~InsetPhantom()
123 {
124         hideDialogs("phantom", this);
125 }
126
127
128 docstring InsetPhantom::editMessage() const
129 {
130         return _("Opened Phantom Inset");
131 }
132
133
134 docstring InsetPhantom::name() const 
135 {
136         return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
137 }
138
139
140 Inset::DisplayType InsetPhantom::display() const
141 {
142         return Inline;
143 }
144
145
146 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
147 {
148         InsetText::metrics(mi, dim);
149
150         // cache the inset dimension
151         setDimCache(mi, dim);
152 }
153
154
155 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
156 {
157         // draw the text
158         InsetText::draw(pi, x, y);
159
160         // draw the inset marker
161         drawMarkers(pi, x, y);
162         
163         // draw the arrow(s)
164         static int const arrow_size = 4;
165         ColorCode const origcol = pi.base.font.color();
166         pi.base.font.setColor(Color_special);
167         pi.base.font.setColor(origcol);
168         Dimension const dim = dimension(*pi.base.bv);
169
170         if (params_.type == InsetPhantomParams::Phantom ||
171                 params_.type == InsetPhantomParams::VPhantom) {
172                 // y1---------
173                 //           / \.
174                 // y2-----  / | \.
175                 //            |
176                 //            |
177                 // y3-----  \ | /
178                 //           \ /
179                 // y4---------
180                 //          | | |
181                 //         /  |  \.
182                 //        x1  x2 x3
183
184                 int const x2 = x + dim.wid / 2;
185                 int const x1 = x2 - arrow_size;
186                 int const x3 = x2 + arrow_size;
187
188                 int const y1 = y - dim.asc;
189                 int const y2 = y1 + arrow_size;
190                 int const y4 = y + dim.des;
191                 int const y3 = y4 - arrow_size;
192
193                 // top arrow
194                 pi.pain.line(x2, y1, x1, y2, Color_added_space);
195                 pi.pain.line(x2, y1, x3, y2, Color_added_space);
196
197                 // bottom arrow
198                 pi.pain.line(x2, y4, x1, y3, Color_added_space);
199                 pi.pain.line(x2, y4, x3, y3, Color_added_space);
200
201                 // joining line
202                 pi.pain.line(x2, y1, x2, y4, Color_added_space);
203         }
204
205         if (params_.type == InsetPhantomParams::Phantom ||
206                 params_.type == InsetPhantomParams::HPhantom) {
207                 // y1----   /          \.
208                 //        /              \.
209                 // y2--- <---------------->
210                 //        \              /
211                 // y3----   \          /
212                 //       |   |        |   |
213                 //      x1  x2       x3  x4
214
215                 x = x + TEXT_TO_INSET_OFFSET;
216                 int const x1 = x;
217                 int const x2 = x + arrow_size;
218                 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
219                 int const x3 = x4 - arrow_size;
220
221                 int const y2 = y + (dim.des - dim.asc) / 2;
222                 int const y1 = y2 - arrow_size;
223                 int const y3 = y2 + arrow_size;
224
225                 // left arrow
226                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
227                 pi.pain.line(x1, y2, x2, y1, Color_added_space);
228
229                 // right arrow
230                 pi.pain.line(x4, y2, x3, y3, Color_added_space);
231                 pi.pain.line(x4, y2, x3, y1, Color_added_space);
232
233                 // joining line
234                 pi.pain.line(x1, y2, x4, y2, Color_added_space);
235         }
236 }
237
238
239 void InsetPhantom::write(ostream & os) const
240 {
241         params_.write(os);
242         InsetCollapsable::write(os);
243 }
244
245
246 void InsetPhantom::read(Lexer & lex)
247 {
248         params_.read(lex);
249         InsetCollapsable::read(lex);
250 }
251
252
253 void InsetPhantom::setButtonLabel()
254 {
255         docstring const label = phantomtranslator_loc().find(params_.type);
256         setLabel(label);
257 }
258
259
260 bool InsetPhantom::showInsetDialog(BufferView * bv) const
261 {
262         bv->showDialog("phantom", params2string(params()),
263                 const_cast<InsetPhantom *>(this));
264         return true;
265 }
266
267
268 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
269 {
270         switch (cmd.action) {
271
272         case LFUN_INSET_MODIFY:
273                 string2params(to_utf8(cmd.argument()), params_);
274                 setLayout(buffer().params());
275                 break;
276
277         case LFUN_INSET_DIALOG_UPDATE:
278                 cur.bv().updateDialog("phantom", params2string(params()));
279                 break;
280
281         default:
282                 InsetCollapsable::doDispatch(cur, cmd);
283                 break;
284         }
285 }
286
287
288 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
289                 FuncStatus & flag) const
290 {
291         switch (cmd.action) {
292
293         case LFUN_INSET_MODIFY:
294                 flag.setEnabled(true);
295                 return true;
296
297         case LFUN_INSET_DIALOG_UPDATE:
298                 flag.setEnabled(true);
299                 return true;
300
301         default:
302                 return InsetCollapsable::getStatus(cur, cmd, flag);
303         }
304 }
305
306
307 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
308 {
309         OutputParams rp(&buffer().params().encoding());
310         odocstringstream ods;
311         InsetText::plaintext(ods, rp);
312         docstring content_tip = ods.str();
313         // shorten it if necessary
314         if (content_tip.size() > 200)
315                 content_tip = content_tip.substr(0, 200) + "...";
316         docstring res = phantomtranslator_loc().find(params_.type);
317         if (!content_tip.empty())
318                 res += from_ascii(": ") + content_tip;
319         return res;
320 }
321
322
323 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams_in) const
324 {
325         OutputParams runparams(runparams_in);
326         if (params_.type == InsetPhantomParams::Phantom)
327                 os << "\\phantom{";
328         else if (params_.type == InsetPhantomParams::HPhantom)
329                 os << "\\hphantom{";
330         else if (params_.type == InsetPhantomParams::VPhantom)
331                 os << "\\vphantom{";
332         int const i = InsetText::latex(os, runparams);
333         os << "}";
334         runparams_in.encoding = runparams.encoding;
335
336         return i + 2;
337 }
338
339
340 int InsetPhantom::plaintext(odocstream & os,
341                          OutputParams const & runparams_in) const
342 {
343         OutputParams runparams(runparams_in);
344         if (params_.type == InsetPhantomParams::Phantom)
345                 os << '[' << buffer().B_("phantom") << ":";
346         else if (params_.type == InsetPhantomParams::HPhantom)
347                 os << '[' << buffer().B_("hphantom") << ":";
348         else if (params_.type == InsetPhantomParams::VPhantom)
349                 os << '[' << buffer().B_("vphantom") << ":";
350         InsetText::plaintext(os, runparams);
351         os << "]";
352
353         return PLAINTEXT_NEWLINE;
354 }
355
356
357 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams_in) const
358 {
359         OutputParams runparams(runparams_in);
360         string cmdname;
361         if (params_.type == InsetPhantomParams::Phantom)
362                 cmdname = "phantom";
363         else if (params_.type == InsetPhantomParams::HPhantom)
364                 cmdname = "phantom";
365         else if (params_.type == InsetPhantomParams::VPhantom)
366                 cmdname = "phantom";
367         os << "<" + cmdname + ">";
368         int const i = InsetText::docbook(os, runparams);
369         os << "</" + cmdname + ">";
370
371         return i;
372 }
373
374
375 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
376 {
377         return from_ascii("context-phantom");
378 }
379
380
381 string InsetPhantom::params2string(InsetPhantomParams const & params)
382 {
383         ostringstream data;
384         data << "phantom" << ' ';
385         params.write(data);
386         return data.str();
387 }
388
389
390 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
391 {
392         params = InsetPhantomParams();
393
394         if (in.empty())
395                 return;
396
397         istringstream data(in);
398         Lexer lex;
399         lex.setStream(data);
400         lex.setContext("InsetPhantom::string2params");
401         lex >> "phantom" >> "Phantom";
402
403         params.read(lex);
404 }
405
406
407 } // namespace lyx