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