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