]> git.lyx.org Git - lyx.git/blob - src/insets/InsetPhantom.cpp
This is just a giant renaming of member variables in FuncRequest,
[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                 string2params(to_utf8(cmd.argument()), params_);
263                 break;
264
265         case LFUN_INSET_DIALOG_UPDATE:
266                 cur.bv().updateDialog("phantom", params2string(params()));
267                 break;
268
269         default:
270                 InsetCollapsable::doDispatch(cur, cmd);
271                 break;
272         }
273 }
274
275
276 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
277                 FuncStatus & flag) const
278 {
279         switch (cmd.action_) {
280
281         case LFUN_INSET_MODIFY:
282                 if (cmd.getArg(0) == "phantom") {
283                         InsetPhantomParams params;
284                         string2params(to_utf8(cmd.argument()), params);
285                         flag.setOnOff(params_.type == params.type);
286                 }
287                 flag.setEnabled(true);
288                 return true;
289
290         case LFUN_INSET_DIALOG_UPDATE:
291                 flag.setEnabled(true);
292                 return true;
293
294         default:
295                 return InsetCollapsable::getStatus(cur, cmd, flag);
296         }
297 }
298
299
300 docstring InsetPhantom::toolTip(BufferView const &, int, int) const
301 {
302         OutputParams rp(&buffer().params().encoding());
303         odocstringstream ods;
304         InsetCollapsable::plaintext(ods, rp);
305         docstring content_tip = support::wrapParas(ods.str());
306         docstring res = phantomtranslator_loc().find(params_.type);
307         if (!content_tip.empty())
308                 res += from_ascii(": ") + "\n" + content_tip;
309         return res;
310 }
311
312
313 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams) const
314 {
315         if (params_.type == InsetPhantomParams::Phantom)
316                 os << "\\phantom{";
317         else if (params_.type == InsetPhantomParams::HPhantom)
318                 os << "\\hphantom{";
319         else if (params_.type == InsetPhantomParams::VPhantom)
320                 os << "\\vphantom{";
321         int const i = InsetCollapsable::latex(os, runparams);
322         os << "}";
323
324         return i;
325 }
326
327
328 int InsetPhantom::plaintext(odocstream & os,
329                             OutputParams const & runparams) const
330 {
331         if (params_.type == InsetPhantomParams::Phantom)
332                 os << '[' << buffer().B_("phantom") << ":";
333         else if (params_.type == InsetPhantomParams::HPhantom)
334                 os << '[' << buffer().B_("hphantom") << ":";
335         else if (params_.type == InsetPhantomParams::VPhantom)
336                 os << '[' << buffer().B_("vphantom") << ":";
337         InsetCollapsable::plaintext(os, runparams);
338         os << "]";
339
340         return PLAINTEXT_NEWLINE;
341 }
342
343
344 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
345 {
346         string cmdname;
347         if (params_.type == InsetPhantomParams::Phantom)
348                 cmdname = "phantom";
349         else if (params_.type == InsetPhantomParams::HPhantom)
350                 cmdname = "phantom";
351         else if (params_.type == InsetPhantomParams::VPhantom)
352                 cmdname = "phantom";
353         os << "<" + cmdname + ">";
354         int const i = InsetCollapsable::docbook(os, runparams);
355         os << "</" + cmdname + ">";
356
357         return i;
358 }
359
360
361 docstring InsetPhantom::xhtml(XHTMLStream &, OutputParams const &) const
362 {
363         return docstring();
364 }
365
366 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
367 {
368         return from_ascii("context-phantom");
369 }
370
371
372 string InsetPhantom::params2string(InsetPhantomParams const & params)
373 {
374         ostringstream data;
375         data << "phantom" << ' ';
376         params.write(data);
377         return data.str();
378 }
379
380
381 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
382 {
383         params = InsetPhantomParams();
384
385         if (in.empty())
386                 return;
387
388         istringstream data(in);
389         Lexer lex;
390         lex.setStream(data);
391         lex.setContext("InsetPhantom::string2params");
392         lex >> "phantom" >> "Phantom";
393
394         params.read(lex);
395 }
396
397
398 } // namespace lyx