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