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