]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
make dispatch_result_t ctor of DispatchResult explicit
[lyx.git] / src / insets / insetbox.C
1 /**
2  * \file insetbox.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "insetbox.h"
16
17 #include "BufferView.h"
18 #include "dispatchresult.h"
19 #include "debug.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "LaTeXFeatures.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27
28 #include "support/std_sstream.h"
29
30 using std::auto_ptr;
31 using std::string;
32 using std::istringstream;
33 using std::ostream;
34 using std::ostringstream;
35 using std::endl;
36
37
38 namespace {
39
40 BoxTranslator const init_boxtranslator() {
41         BoxTranslator translator("Boxed", InsetBox::Boxed);
42         translator.addPair("Frameless", InsetBox::Frameless);
43         translator.addPair("ovalbox", InsetBox::ovalbox);
44         translator.addPair("Ovalbox", InsetBox::Ovalbox);
45         translator.addPair("Shadowbox", InsetBox::Shadowbox);
46         translator.addPair("Doublebox",InsetBox::Doublebox);
47         return translator;
48 }
49
50
51 BoxTranslator const init_boxtranslator_loc() {
52         BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
53         translator.addPair(_("Frameless"), InsetBox::Frameless);
54         translator.addPair(_("ovalbox"), InsetBox::ovalbox);
55         translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
56         translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
57         translator.addPair(_("Doublebox"), InsetBox::Doublebox);
58         return translator;
59 }
60
61
62 BoxTranslator const & boxtranslator() {
63         static BoxTranslator translator = init_boxtranslator();
64         return translator;
65 }
66
67
68 BoxTranslator const & boxtranslator_loc() {
69         static BoxTranslator translator = init_boxtranslator_loc();
70         return translator;
71 }
72
73 } // anon
74
75
76 void InsetBox::init()
77 {
78         setInsetName("Box");
79         setButtonLabel();
80 }
81
82
83 InsetBox::InsetBox(BufferParams const & bp, string const & label)
84         : InsetCollapsable(bp), params_(label)
85 {
86         init();
87 }
88
89
90 InsetBox::InsetBox(InsetBox const & in)
91         : InsetCollapsable(in), params_(in.params_)
92 {
93         init();
94 }
95
96
97 InsetBox::~InsetBox()
98 {
99         InsetBoxMailer mailer(*this);
100         mailer.hideDialog();
101 }
102
103
104 auto_ptr<InsetBase> InsetBox::clone() const
105 {
106         return auto_ptr<InsetBase>(new InsetBox(*this));
107 }
108
109
110 string const InsetBox::editMessage() const
111 {
112         return _("Opened Box Inset");
113 }
114
115
116 void InsetBox::write(Buffer const & buf, ostream & os) const
117 {
118         params_.write(os);
119         InsetCollapsable::write(buf, os);
120 }
121
122
123 void InsetBox::read(Buffer const & buf, LyXLex & lex)
124 {
125         params_.read(lex);
126         InsetCollapsable::read(buf, lex);
127         setButtonLabel();
128 }
129
130
131 void InsetBox::setButtonLabel()
132 {
133         LyXFont font(LyXFont::ALL_SANE);
134         font.decSize();
135         font.decSize();
136
137         BoxType btype = boxtranslator().find(params_.type);
138         if (btype == Frameless) {
139                 if (params_.use_parbox)
140                         setLabel(_("Parbox"));
141                 else
142                         setLabel(_("Minipage"));
143         } else
144                 setLabel(boxtranslator_loc().find(btype));
145
146         font.setColor(LColor::foreground);
147         setBackgroundColor(LColor::background);
148         setLabelFont(font);
149 }
150
151
152 void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
153 {
154         InsetCollapsable::metrics(mi, dim);
155         if (params_.inner_box && isOpen())
156                 dim.wid = mi.base.textwidth;
157         dim_ = dim;
158 }
159
160
161 bool InsetBox::showInsetDialog(BufferView * bv) const
162 {
163         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
164         return true;
165 }
166
167
168 DispatchResult
169 InsetBox::priv_dispatch(FuncRequest const & cmd,
170                         idx_type & idx, pos_type & pos)
171 {
172         BufferView * bv = cmd.view();
173
174         switch (cmd.action) {
175
176         case LFUN_INSET_MODIFY: {
177                 InsetBoxMailer::string2params(cmd.argument, params_);
178                 setButtonLabel();
179                 bv->updateInset(this);
180                 return DispatchResult(DISPATCHED);
181         }
182         case LFUN_INSET_DIALOG_UPDATE:
183                 InsetBoxMailer(*this).updateDialog(bv);
184                 return DispatchResult(DISPATCHED);
185
186         case LFUN_MOUSE_RELEASE:
187                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
188                         InsetBoxMailer(*this).showDialog(bv);
189                         return DispatchResult(DISPATCHED);
190                 }
191                 // fallthrough:
192
193         default:
194                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
195         }
196 }
197
198
199 int InsetBox::latex(Buffer const & buf, ostream & os,
200                                 LatexRunParams const & runparams) const
201 {
202         BoxType btype = boxtranslator().find(params_.type);
203
204         string width_string = params_.width.asLatexString();
205         bool stdwidth(false);
206         if (params_.inner_box &&
207                         (width_string.find("1.0\\columnwidth") != string::npos
208                         || width_string.find("1.0\\textwidth") != string::npos)) {
209                 stdwidth = true;
210                 switch (btype) {
211                 case Frameless:
212                         break;
213                 case Boxed:
214                         width_string += " - 2\\fboxsep - 2\\fboxrule";
215                         break;
216                 case ovalbox:
217                         width_string += " - 2\\fboxsep - 0.8pt";
218                         break;
219                 case Ovalbox:
220                         width_string += " - 2\\fboxsep - 1.6pt";
221                         break;
222                 case Shadowbox:
223                         // Shadow falls outside right margin... opinions?
224                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
225                         break;
226                 case Doublebox:
227                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
228                         break;
229                 }
230         }
231
232         int i = 0;
233         os << "%\n";
234         // Adapt to column/text width correctly also if paragraphs indented:
235         if (stdwidth)
236                 os << "\\noindent";
237
238         switch (btype) {
239         case Frameless:
240                 break;
241         case Boxed:
242                 os << "\\framebox";
243                 if (!params_.inner_box) {
244                         os << "{\\makebox";
245                         // Special widths, see usrguide §3.5
246                         if (params_.special != "none") {
247                                 os << "[" << params_.width.value()
248                                    << "\\" << params_.special << "]";
249                         } else
250                                 os << "[" << width_string << "]";
251                         if (params_.hor_pos != 'c')
252                                 os << "[" << params_.hor_pos << "]";
253                 }
254
255                 os << "{";
256                 break;
257         case ovalbox:
258                 os << "\\ovalbox{";
259                 break;
260         case Ovalbox:
261                 os << "\\Ovalbox{";
262                 break;
263         case Shadowbox:
264                 os << "\\shadowbox{";
265                 break;
266         case Doublebox:
267                 os << "\\doublebox{";
268                 break;
269         }
270
271         if (params_.inner_box) {
272                 if (params_.use_parbox)
273                         os << "\\parbox";
274                 else
275                         os << "\\begin{minipage}";
276
277                 os << "[" << params_.pos << "]";
278                 if (params_.height_special == "none") {
279                         os << "[" << params_.height.asLatexString() << "]";
280                 } else {
281                         // Special heights
282                         os << "[" << params_.height.value()
283                            << "\\" << params_.height_special << "]";
284                 }
285                 if (params_.inner_pos != params_.pos)
286                         os << "[" << params_.inner_pos << "]";
287
288                 os << "{" << width_string << "}";
289
290                 if (params_.use_parbox)
291                         os << "{";
292                 os << "%\n";
293                 i += 1;
294         }
295
296         i += inset.latex(buf, os, runparams);
297
298         if (params_.inner_box) {
299                 if (params_.use_parbox)
300                         os << "%\n}";
301                 else
302                         os << "%\n\\end{minipage}";
303         }
304
305         switch (btype) {
306         case Frameless:
307                 break;
308         case Boxed:
309                 if (!params_.inner_box)
310                         os << "}"; // for makebox
311                 os << "}";
312                 break;
313         case ovalbox:
314         case Ovalbox:
315         case Doublebox:
316         case Shadowbox:
317                 os << "}";
318                 break;
319         }
320         os << "%\n";
321
322         i += 3;
323
324         return i;
325 }
326
327
328 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os) const
329 {
330         return inset.linuxdoc(buf, os);
331 }
332
333
334 int InsetBox::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
335 {
336         return inset.docbook(buf, os, mixcont);
337 }
338
339
340 int InsetBox::ascii(Buffer const & buf, std::ostream & os, int ll) const
341 {
342         int i = 0;
343         string const pt = params_.type;
344         BoxType btype = boxtranslator().find(params_.type);
345         switch (btype) {
346                 case Frameless:
347                 break;
348                 case Boxed:
349                 os << "[";
350                 break;
351                 case ovalbox:
352                 os << "(";
353                 break;
354                 case Ovalbox:
355                 os << "((";
356                 break;
357                 case Shadowbox:
358                 os << "[";
359                 break;
360                 case Doublebox:
361                 os << "[[";
362                 break;
363         }
364
365         i = inset.ascii(buf, os, ll);
366
367         switch (btype) {
368                 case Frameless:
369                 break;
370                 case Boxed:
371                 os << "]";
372                 break;
373                 case ovalbox:
374                 os << ")";
375                 break;
376                 case Ovalbox:
377                 os << "))";
378                 break;
379                 case Shadowbox:
380                 os << "]/";
381                 break;
382                 case Doublebox:
383                 os << "]]";
384                 break;
385         }
386
387         return i;
388 }
389
390
391 void InsetBox::validate(LaTeXFeatures & features) const
392 {
393         features.require("calc");
394         BoxType btype = boxtranslator().find(params_.type);
395         switch (btype) {
396         case Frameless:
397         case Boxed:
398                 break;
399         case ovalbox:
400         case Ovalbox:
401         case Shadowbox:
402         case Doublebox:
403                 features.require("fancybox");
404                 break;
405         }
406         inset.validate(features);
407 }
408
409
410 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
411         : inset_(inset)
412 {
413 }
414
415
416 string const InsetBoxMailer::name_ = "box";
417
418
419 string const InsetBoxMailer::inset2string(Buffer const &) const
420 {
421         return params2string(inset_.params());
422 }
423
424
425 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
426 {
427         ostringstream data;
428         data << "box" << ' ';
429         params.write(data);
430         return data.str();
431 }
432
433
434 void InsetBoxMailer::string2params(string const & in,
435         InsetBoxParams & params)
436 {
437         params = InsetBoxParams(string());
438
439         if (in.empty())
440                 return;
441
442         istringstream data(in);
443         LyXLex lex(0,0);
444         lex.setStream(data);
445
446         string token;
447         lex.next();
448         token = lex.getString();
449         lex.next();
450
451         params.read(lex);
452 }
453
454
455 InsetBoxParams::InsetBoxParams(string const & label)
456         : type(label),
457           use_parbox(false),
458           inner_box(true),
459           width(LyXLength("100col%")),
460           special("none"),
461           pos('t'),
462           hor_pos('c'),
463           inner_pos('t'),
464           height(LyXLength("1in")),
465           height_special("totalheight") // default is 1\\totalheight
466 {}
467
468
469 void InsetBoxParams::write(ostream & os) const
470 {
471         os << type << "\n";
472         os << "position \"" << pos << "\"\n";
473         os << "hor_pos \"" << hor_pos << "\"\n";
474         os << "has_inner_box " << inner_box << "\n";
475         os << "inner_pos \"" << inner_pos << "\"\n";
476         os << "use_parbox " << use_parbox << "\n";
477         os << "width \"" << width.asString() << "\"\n";
478         os << "special \"" << special << "\"\n";
479         os << "height \"" << height.asString() << "\"\n";
480         os << "height_special \"" << height_special << "\"\n";
481 }
482
483
484 void InsetBoxParams::read(LyXLex & lex)
485 {
486         if (lex.isOK()) {
487                 type = lex.getString();
488         }
489         string token;
490         if (!lex.isOK())
491                 return;
492         lex.next();
493         token = lex.getString();
494         if (token == "position") {
495                 lex.next();
496                 // The [0] is needed. We need the first and only char in
497                 // this string -- MV
498                 pos = lex.getString()[0];
499         } else {
500                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
501                 lex.pushToken(token);
502         }
503         if (!lex.isOK())
504                 return;
505         lex.next();
506         token = lex.getString();
507         if (token == "hor_pos") {
508                 lex.next();
509                 hor_pos = lex.getString()[0];
510         } else {
511                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
512                 lex.pushToken(token);
513         }
514         if (!lex.isOK())
515                 return;
516         lex.next();
517         token = lex.getString();
518         if (token == "has_inner_box") {
519                 lex.next();
520                 inner_box = lex.getInteger();
521         } else {
522                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
523                 lex.pushToken(token);
524         }
525
526         if (!lex.isOK())
527                 return;
528         lex.next();
529         token = lex.getString();
530         if (token == "inner_pos") {
531                 lex.next();
532                 inner_pos = lex.getString()[0];
533         } else {
534                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
535                         << token << endl;
536                 lex.pushToken(token);
537         }
538         if (!lex.isOK())
539                 return;
540         lex.next();
541         token = lex.getString();
542         if (token == "use_parbox") {
543                 lex.next();
544                 use_parbox = lex.getInteger();
545         } else {
546                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
547                 lex.pushToken(token);
548         }
549         if (!lex.isOK())
550                 return;
551         lex.next();
552         token = lex.getString();
553         if (token == "width") {
554                 lex.next();
555                 width = LyXLength(lex.getString());
556         } else {
557                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
558                 lex.pushToken(token);
559         }
560         if (!lex.isOK())
561                 return;
562         lex.next();
563         token = lex.getString();
564         if (token == "special") {
565                 lex.next();
566                 special = lex.getString();
567         } else {
568                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
569                 lex.pushToken(token);
570         }
571         if (!lex.isOK())
572                 return;
573         lex.next();
574         token = lex.getString();
575         if (token == "height") {
576                 lex.next();
577                 height = LyXLength(lex.getString());
578         } else {
579                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
580                 lex.pushToken(token);
581         }
582         if (!lex.isOK())
583                 return;
584         lex.next();
585         token = lex.getString();
586         if (token == "height_special") {
587                 lex.next();
588                 height_special = lex.getString();
589         } else {
590                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
591                 lex.pushToken(token);
592         }
593 }