]> git.lyx.org Git - lyx.git/blob - src/insets/InsetPhantom.cpp
Fix bug #6315: counters in insets that don't produce output have ghost values.
[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 "TextClass.h"
32
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/Translator.h"
37
38 #include "frontends/Application.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
41
42 #include <algorithm>
43 #include <sstream>
44
45 using namespace std;
46
47 namespace lyx {
48
49 namespace {
50
51 typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
52 typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
53
54 PhantomTranslator const init_phantomtranslator()
55 {
56         PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
57         translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
58         translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
59         return translator;
60 }
61
62
63 PhantomTranslatorLoc const init_phantomtranslator_loc()
64 {
65         PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
66         translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
67         translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
68         return translator;
69 }
70
71
72 PhantomTranslator const & phantomtranslator()
73 {
74         static PhantomTranslator translator = init_phantomtranslator();
75         return translator;
76 }
77
78
79 PhantomTranslatorLoc const & phantomtranslator_loc()
80 {
81         static PhantomTranslatorLoc translator = init_phantomtranslator_loc();
82         return translator;
83 }
84
85 } // anon
86
87
88 InsetPhantomParams::InsetPhantomParams()
89         : type(Phantom)
90 {}
91
92
93 void InsetPhantomParams::write(ostream & os) const
94 {
95         string const label = phantomtranslator().find(type);
96         os << "Phantom " << label << "\n";
97 }
98
99
100 void InsetPhantomParams::read(Lexer & lex)
101 {
102         string label;
103         lex >> label;
104         if (lex)
105                 type = phantomtranslator().find(label);
106 }
107
108
109 /////////////////////////////////////////////////////////////////////
110 //
111 // InsetPhantom
112 //
113 /////////////////////////////////////////////////////////////////////
114
115 InsetPhantom::InsetPhantom(Buffer * buf, string const & label)
116         : InsetCollapsable(buf)
117 {
118         setDrawFrame(false);
119         params_.type = phantomtranslator().find(label);
120 }
121
122
123 InsetPhantom::~InsetPhantom()
124 {
125         hideDialogs("phantom", this);
126 }
127
128
129 docstring InsetPhantom::name() const 
130 {
131         return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
132 }
133
134
135 void InsetPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
136 {
137         InsetCollapsable::metrics(mi, dim);
138
139         // cache the inset dimension
140         setDimCache(mi, dim);
141 }
142
143
144 void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
145 {
146         // draw the text
147         InsetCollapsable::draw(pi, x, y);
148
149         // draw the inset marker
150         drawMarkers(pi, x, y);
151         
152         // draw the arrow(s)
153         static int const arrow_size = 4;
154         ColorCode const origcol = pi.base.font.color();
155         pi.base.font.setColor(Color_special);
156         pi.base.font.setColor(origcol);
157         Dimension const dim = dimension(*pi.base.bv);
158
159         if (params_.type == InsetPhantomParams::Phantom ||
160                 params_.type == InsetPhantomParams::VPhantom) {
161                 // y1---------
162                 //           / \.
163                 // y2-----  / | \.
164                 //            |
165                 //            |
166                 // y3-----  \ | /
167                 //           \ /
168                 // y4---------
169                 //          | | |
170                 //         /  |  \.
171                 //        x1  x2 x3
172
173                 int const x2 = x + dim.wid / 2;
174                 int const x1 = x2 - arrow_size;
175                 int const x3 = x2 + arrow_size;
176
177                 int const y1 = y - dim.asc;
178                 int const y2 = y1 + arrow_size;
179                 int const y4 = y + dim.des;
180                 int const y3 = y4 - arrow_size;
181
182                 // top arrow
183                 pi.pain.line(x2, y1, x1, y2, Color_added_space);
184                 pi.pain.line(x2, y1, x3, y2, Color_added_space);
185
186                 // bottom arrow
187                 pi.pain.line(x2, y4, x1, y3, Color_added_space);
188                 pi.pain.line(x2, y4, x3, y3, Color_added_space);
189
190                 // joining line
191                 pi.pain.line(x2, y1, x2, y4, Color_added_space);
192         }
193
194         if (params_.type == InsetPhantomParams::Phantom ||
195                 params_.type == InsetPhantomParams::HPhantom) {
196                 // y1----   /          \.
197                 //        /              \.
198                 // y2--- <---------------->
199                 //        \              /
200                 // y3----   \          /
201                 //       |   |        |   |
202                 //      x1  x2       x3  x4
203
204                 x = x + TEXT_TO_INSET_OFFSET;
205                 int const x1 = x;
206                 int const x2 = x + arrow_size;
207                 int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
208                 int const x3 = x4 - arrow_size;
209
210                 int const y2 = y + (dim.des - dim.asc) / 2;
211                 int const y1 = y2 - arrow_size;
212                 int const y3 = y2 + arrow_size;
213
214                 // left arrow
215                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
216                 pi.pain.line(x1, y2, x2, y1, Color_added_space);
217
218                 // right arrow
219                 pi.pain.line(x4, y2, x3, y3, Color_added_space);
220                 pi.pain.line(x4, y2, x3, y1, Color_added_space);
221
222                 // joining line
223                 pi.pain.line(x1, y2, x4, y2, Color_added_space);
224         }
225 }
226
227
228 void InsetPhantom::write(ostream & os) const
229 {
230         params_.write(os);
231         InsetCollapsable::write(os);
232 }
233
234
235 void InsetPhantom::read(Lexer & lex)
236 {
237         params_.read(lex);
238         InsetCollapsable::read(lex);
239 }
240
241
242 void InsetPhantom::setButtonLabel()
243 {
244         docstring const label = phantomtranslator_loc().find(params_.type);
245         setLabel(label);
246 }
247
248
249 bool InsetPhantom::showInsetDialog(BufferView * bv) const
250 {
251         bv->showDialog("phantom", params2string(params()),
252                 const_cast<InsetPhantom *>(this));
253         return true;
254 }
255
256
257 void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
258 {
259         switch (cmd.action()) {
260
261         case LFUN_INSET_MODIFY:
262                 cur.recordUndoInset(ATOMIC_UNDO, this);
263                 string2params(to_utf8(cmd.argument()), params_);
264                 break;
265
266         case LFUN_INSET_DIALOG_UPDATE:
267                 cur.bv().updateDialog("phantom", params2string(params()));
268                 break;
269
270         default:
271                 InsetCollapsable::doDispatch(cur, cmd);
272                 break;
273         }
274 }
275
276
277 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
278                 FuncStatus & flag) const
279 {
280         switch (cmd.action()) {
281
282         case LFUN_INSET_MODIFY:
283                 if (cmd.getArg(0) == "phantom") {
284                         InsetPhantomParams params;
285                         string2params(to_utf8(cmd.argument()), params);
286                         flag.setOnOff(params_.type == params.type);
287                 }
288                 flag.setEnabled(true);
289                 return true;
290
291         case LFUN_INSET_DIALOG_UPDATE:
292                 flag.setEnabled(true);
293                 return true;
294
295         default:
296                 return InsetCollapsable::getStatus(cur, cmd, flag);
297         }
298 }
299
300
301 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
302 {
303         docstring const res = phantomtranslator_loc().find(params_.type);
304         return toolTipText(res + from_ascii(": "));
305 }
306
307
308 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams) const
309 {
310         if (params_.type == InsetPhantomParams::Phantom)
311                 os << "\\phantom{";
312         else if (params_.type == InsetPhantomParams::HPhantom)
313                 os << "\\hphantom{";
314         else if (params_.type == InsetPhantomParams::VPhantom)
315                 os << "\\vphantom{";
316         int const i = InsetCollapsable::latex(os, runparams);
317         os << "}";
318
319         return i;
320 }
321
322
323 int InsetPhantom::plaintext(odocstream & os,
324                             OutputParams const & runparams) const
325 {
326         if (params_.type == InsetPhantomParams::Phantom)
327                 os << '[' << buffer().B_("phantom") << ":";
328         else if (params_.type == InsetPhantomParams::HPhantom)
329                 os << '[' << buffer().B_("hphantom") << ":";
330         else if (params_.type == InsetPhantomParams::VPhantom)
331                 os << '[' << buffer().B_("vphantom") << ":";
332         InsetCollapsable::plaintext(os, runparams);
333         os << "]";
334
335         return PLAINTEXT_NEWLINE;
336 }
337
338
339 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
340 {
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 = InsetCollapsable::docbook(os, runparams);
350         os << "</" + cmdname + ">";
351
352         return i;
353 }
354
355
356 docstring InsetPhantom::xhtml(XHTMLStream &, OutputParams const &) const
357 {
358         return docstring();
359 }
360
361 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
362 {
363         return from_ascii("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