]> git.lyx.org Git - lyx.git/blob - src/insets/InsetPhantom.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[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/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                 setLayout();
270                 break;
271
272         case LFUN_INSET_DIALOG_UPDATE:
273                 cur.bv().updateDialog("phantom", params2string(params()));
274                 break;
275
276         default:
277                 InsetCollapsable::doDispatch(cur, cmd);
278                 break;
279         }
280 }
281
282
283 bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
284                 FuncStatus & flag) const
285 {
286         switch (cmd.action) {
287
288         case LFUN_INSET_MODIFY:
289                 if (cmd.getArg(0) == "phantom") {
290                         InsetPhantomParams params;
291                         string2params(to_utf8(cmd.argument()), params);
292                         flag.setOnOff(params_.type == params.type);
293                 }
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 &, int, int) const
308 {
309         OutputParams rp(&buffer().params().encoding());
310         odocstringstream ods;
311         InsetCollapsable::plaintext(ods, rp);
312         docstring content_tip = support::wrapParas(ods.str());
313         docstring res = phantomtranslator_loc().find(params_.type);
314         if (!content_tip.empty())
315                 res += from_ascii(": ") + "\n" + content_tip;
316         return res;
317 }
318
319
320 int InsetPhantom::latex(odocstream & os, OutputParams const & runparams) const
321 {
322         if (params_.type == InsetPhantomParams::Phantom)
323                 os << "\\phantom{";
324         else if (params_.type == InsetPhantomParams::HPhantom)
325                 os << "\\hphantom{";
326         else if (params_.type == InsetPhantomParams::VPhantom)
327                 os << "\\vphantom{";
328         int const i = InsetCollapsable::latex(os, runparams);
329         os << "}";
330
331         return i;
332 }
333
334
335 int InsetPhantom::plaintext(odocstream & os,
336                             OutputParams const & runparams) const
337 {
338         if (params_.type == InsetPhantomParams::Phantom)
339                 os << '[' << buffer().B_("phantom") << ":";
340         else if (params_.type == InsetPhantomParams::HPhantom)
341                 os << '[' << buffer().B_("hphantom") << ":";
342         else if (params_.type == InsetPhantomParams::VPhantom)
343                 os << '[' << buffer().B_("vphantom") << ":";
344         InsetCollapsable::plaintext(os, runparams);
345         os << "]";
346
347         return PLAINTEXT_NEWLINE;
348 }
349
350
351 int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
352 {
353         string cmdname;
354         if (params_.type == InsetPhantomParams::Phantom)
355                 cmdname = "phantom";
356         else if (params_.type == InsetPhantomParams::HPhantom)
357                 cmdname = "phantom";
358         else if (params_.type == InsetPhantomParams::VPhantom)
359                 cmdname = "phantom";
360         os << "<" + cmdname + ">";
361         int const i = InsetCollapsable::docbook(os, runparams);
362         os << "</" + cmdname + ">";
363
364         return i;
365 }
366
367
368 docstring InsetPhantom::xhtml(odocstream &, OutputParams const &) const
369 {
370         return docstring();
371 }
372
373 docstring InsetPhantom::contextMenu(BufferView const &, int, int) const
374 {
375         return from_ascii("context-phantom");
376 }
377
378
379 string InsetPhantom::params2string(InsetPhantomParams const & params)
380 {
381         ostringstream data;
382         data << "phantom" << ' ';
383         params.write(data);
384         return data.str();
385 }
386
387
388 void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
389 {
390         params = InsetPhantomParams();
391
392         if (in.empty())
393                 return;
394
395         istringstream data(in);
396         Lexer lex;
397         lex.setStream(data);
398         lex.setContext("InsetPhantom::string2params");
399         lex >> "phantom" >> "Phantom";
400
401         params.read(lex);
402 }
403
404
405 } // namespace lyx