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