]> git.lyx.org Git - features.git/blob - src/insets/InsetPhantom.cpp
support for the LaTeX commands \*phantom, fileformat change
[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         params_.type = phantomtranslator().find(label);
118 }
119
120
121 InsetPhantom::~InsetPhantom()
122 {
123         hideDialogs("phantom", this);
124 }
125
126
127 docstring InsetPhantom::editMessage() const
128 {
129         return _("Opened Phantom Inset");
130 }
131
132
133 docstring InsetPhantom::name() const 
134 {
135         return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
136 }
137
138
139 Inset::DisplayType InsetPhantom::display() const
140 {
141         return Inline;
142 }
143
144
145 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
146 {
147         InsetText::metrics(mi, dim);
148
149         // cache the inset dimension
150         setDimCache(mi, dim);
151 }
152
153
154 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
155 {
156         // draw the text
157         InsetText::draw(pi, x, y);
158         
159         // draw the arrow(s)
160         static int const arrow_size = 4;
161         ColorCode const origcol = pi.base.font.color();
162         pi.base.font.setColor(Color_special);
163         pi.base.font.setColor(origcol);
164         Dimension const dim = dimension(*pi.base.bv);
165
166         if (params_.type == InsetPhantomParams::Phantom ||
167                 params_.type == InsetPhantomParams::VPhantom) {
168                 // y1---------
169                 //           / \.
170                 // y2-----  / | \.
171                 //            |
172                 //            |
173                 // y3-----  \ | /
174                 //           \ /
175                 // y4---------
176                 //          | | |
177                 //         /  |  \.
178                 //        x1  x2 x3
179
180                 int const x2 = x + dim.wid / 2;
181                 int const x1 = x2 - arrow_size;
182                 int const x3 = x2 + arrow_size;
183
184                 int const y1 = y - dim.asc;
185                 int const y2 = y1 + arrow_size;
186                 int const y4 = y + dim.des;
187                 int const y3 = y4 - arrow_size;
188
189                 // top arrow
190                 pi.pain.line(x2, y1, x1, y2, Color_added_space);
191                 pi.pain.line(x2, y1, x3, y2, Color_added_space);
192
193                 // bottom arrow
194                 pi.pain.line(x2, y4, x1, y3, Color_added_space);
195                 pi.pain.line(x2, y4, x3, y3, Color_added_space);
196
197                 // joining line
198                 pi.pain.line(x2, y1, x2, y4, Color_added_space);
199         }
200
201         if (params_.type == InsetPhantomParams::Phantom ||
202                 params_.type == InsetPhantomParams::HPhantom) {
203                 // y1----   /          \.
204                 //        /              \.
205                 // y2--- <---------------->
206                 //        \              /
207                 // y3----   \          /
208                 //       |   |        |   |
209                 //      x1  x2       x3  x4
210
211                 int const x1 = x;
212                 int const x2 = x + arrow_size;
213                 int const x4 = x + dim.wid;
214                 int const x3 = x4 - arrow_size;
215
216                 int const y2 = y + (dim.des - dim.asc) / 2;
217                 int const y1 = y2 - arrow_size;
218                 int const y3 = y2 + arrow_size;
219
220                 // left arrow
221                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
222                 pi.pain.line(x1, y2, x2, y1, Color_added_space);
223
224                 // right arrow
225                 pi.pain.line(x4, y2, x3, y3, Color_added_space);
226                 pi.pain.line(x4, y2, x3, y1, Color_added_space);
227
228                 // joining line
229                 pi.pain.line(x1, y2, x4, y2, Color_added_space);
230         }
231
232         drawMarkers(pi, x, y);
233 }
234
235
236 void InsetPhantom::write(ostream & os) const
237 {
238         params_.write(os);
239         InsetCollapsable::write(os);
240 }
241
242
243 void InsetPhantom::read(Lexer & lex)
244 {
245         params_.read(lex);
246         InsetCollapsable::read(lex);
247 }
248
249
250 void InsetPhantom::setButtonLabel()
251 {
252         docstring const label = phantomtranslator_loc().find(params_.type);
253         setLabel(label);
254 }
255
256
257 bool InsetPhantom::showInsetDialog(BufferView * bv) const
258 {
259         bv->showDialog("phantom", params2string(params()),
260                 const_cast<InsetPhantom *>(this));
261         return true;
262 }
263
264
265 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
266 {
267         switch (cmd.action) {
268
269         case LFUN_INSET_MODIFY:
270                 string2params(to_utf8(cmd.argument()), params_);
271                 setLayout(buffer().params());
272                 break;
273
274         case LFUN_INSET_DIALOG_UPDATE:
275                 cur.bv().updateDialog("phantom", params2string(params()));
276                 break;
277
278         default:
279                 InsetCollapsable::doDispatch(cur, cmd);
280                 break;
281         }
282 }
283
284
285 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
286                 FuncStatus & flag) const
287 {
288         switch (cmd.action) {
289
290         case LFUN_INSET_MODIFY:
291                 flag.setEnabled(true);
292                 return true;
293
294         case LFUN_INSET_DIALOG_UPDATE:
295                 flag.setEnabled(true);
296                 return true;
297
298         default:
299                 return InsetCollapsable::getStatus(cur, cmd, flag);
300         }
301 }
302
303
304 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams_in) const
305 {
306         OutputParams runparams(runparams_in);
307         if (params_.type == InsetPhantomParams::Phantom)
308                 os << "\\phantom{";
309         else if (params_.type == InsetPhantomParams::HPhantom)
310                 os << "\\hphantom{";
311         else if (params_.type == InsetPhantomParams::VPhantom)
312                 os << "\\vphantom{";
313         int const i = InsetText::latex(os, runparams);
314         os << "}";
315         runparams_in.encoding = runparams.encoding;
316
317         return i + 2;
318 }
319
320
321 int InsetPhantom::plaintext(odocstream & os,
322                          OutputParams const & runparams_in) const
323 {
324         OutputParams runparams(runparams_in);
325         if (params_.type == InsetPhantomParams::Phantom)
326                 os << '[' << buffer().B_("phantom") << ":";
327         else if (params_.type == InsetPhantomParams::HPhantom)
328                 os << '[' << buffer().B_("hphantom") << ":";
329         else if (params_.type == InsetPhantomParams::VPhantom)
330                 os << '[' << buffer().B_("vphantom") << ":";
331         InsetText::plaintext(os, runparams);
332         os << "]";
333
334         return PLAINTEXT_NEWLINE;
335 }
336
337
338 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams_in) const
339 {
340         OutputParams runparams(runparams_in);
341         string cmdname;
342         if (params_.type == InsetPhantomParams::Phantom)
343                 cmdname = "phantom";
344         else if (params_.type == InsetPhantomParams::HPhantom)
345                 cmdname = "phantom";
346         else if (params_.type == InsetPhantomParams::VPhantom)
347                 cmdname = "phantom";
348         os << "<" + cmdname + ">";
349         int const i = InsetText::docbook(os, runparams);
350         os << "</" + cmdname + ">";
351
352         return i;
353 }
354
355
356 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
357 {
358         return from_ascii("context-phantom");
359 }
360
361
362 string InsetPhantom::params2string(InsetPhantomParams const & params)
363 {
364         ostringstream data;
365         data << "phantom" << ' ';
366         params.write(data);
367         return data.str();
368 }
369
370
371 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
372 {
373         params = InsetPhantomParams();
374
375         if (in.empty())
376                 return;
377
378         istringstream data(in);
379         Lexer lex;
380         lex.setStream(data);
381         lex.setContext("InsetPhantom::string2params");
382         lex >> "phantom" >> "Phantom";
383
384         params.read(lex);
385 }
386
387
388 } // namespace lyx