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