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