]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
more dispatchresult changes
[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(true);
181         }
182         case LFUN_INSET_DIALOG_UPDATE:
183                 InsetBoxMailer(*this).updateDialog(bv);
184                 return DispatchResult(true);
185
186         case LFUN_MOUSE_RELEASE:
187                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
188                         InsetBoxMailer(*this).showDialog(bv);
189                         return DispatchResult(true);
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,
329                        LatexRunParams const & runparams) const
330 {
331         return inset.linuxdoc(buf, os, runparams);
332 }
333
334
335 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
336                       LatexRunParams const & runparams) const
337 {
338         return inset.docbook(buf, os, runparams);
339 }
340
341
342 int InsetBox::ascii(Buffer const & buf, std::ostream & os,
343                     LatexRunParams const & runparams) const
344 {
345         int i = 0;
346         string const pt = params_.type;
347         BoxType btype = boxtranslator().find(params_.type);
348         switch (btype) {
349                 case Frameless:
350                 break;
351                 case Boxed:
352                 os << "[";
353                 break;
354                 case ovalbox:
355                 os << "(";
356                 break;
357                 case Ovalbox:
358                 os << "((";
359                 break;
360                 case Shadowbox:
361                 os << "[";
362                 break;
363                 case Doublebox:
364                 os << "[[";
365                 break;
366         }
367
368         i = inset.ascii(buf, os, runparams);
369
370         switch (btype) {
371                 case Frameless:
372                 break;
373                 case Boxed:
374                 os << "]";
375                 break;
376                 case ovalbox:
377                 os << ")";
378                 break;
379                 case Ovalbox:
380                 os << "))";
381                 break;
382                 case Shadowbox:
383                 os << "]/";
384                 break;
385                 case Doublebox:
386                 os << "]]";
387                 break;
388         }
389
390         return i;
391 }
392
393
394 void InsetBox::validate(LaTeXFeatures & features) const
395 {
396         features.require("calc");
397         BoxType btype = boxtranslator().find(params_.type);
398         switch (btype) {
399         case Frameless:
400         case Boxed:
401                 break;
402         case ovalbox:
403         case Ovalbox:
404         case Shadowbox:
405         case Doublebox:
406                 features.require("fancybox");
407                 break;
408         }
409         inset.validate(features);
410 }
411
412
413 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
414         : inset_(inset)
415 {
416 }
417
418
419 string const InsetBoxMailer::name_ = "box";
420
421
422 string const InsetBoxMailer::inset2string(Buffer const &) const
423 {
424         return params2string(inset_.params());
425 }
426
427
428 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
429 {
430         ostringstream data;
431         data << "box" << ' ';
432         params.write(data);
433         return data.str();
434 }
435
436
437 void InsetBoxMailer::string2params(string const & in,
438         InsetBoxParams & params)
439 {
440         params = InsetBoxParams(string());
441
442         if (in.empty())
443                 return;
444
445         istringstream data(in);
446         LyXLex lex(0,0);
447         lex.setStream(data);
448
449         string token;
450         lex.next();
451         token = lex.getString();
452         lex.next();
453
454         params.read(lex);
455 }
456
457
458 InsetBoxParams::InsetBoxParams(string const & label)
459         : type(label),
460           use_parbox(false),
461           inner_box(true),
462           width(LyXLength("100col%")),
463           special("none"),
464           pos('t'),
465           hor_pos('c'),
466           inner_pos('t'),
467           height(LyXLength("1in")),
468           height_special("totalheight") // default is 1\\totalheight
469 {}
470
471
472 void InsetBoxParams::write(ostream & os) const
473 {
474         os << type << "\n";
475         os << "position \"" << pos << "\"\n";
476         os << "hor_pos \"" << hor_pos << "\"\n";
477         os << "has_inner_box " << inner_box << "\n";
478         os << "inner_pos \"" << inner_pos << "\"\n";
479         os << "use_parbox " << use_parbox << "\n";
480         os << "width \"" << width.asString() << "\"\n";
481         os << "special \"" << special << "\"\n";
482         os << "height \"" << height.asString() << "\"\n";
483         os << "height_special \"" << height_special << "\"\n";
484 }
485
486
487 void InsetBoxParams::read(LyXLex & lex)
488 {
489         if (lex.isOK()) {
490                 type = lex.getString();
491         }
492         string token;
493         if (!lex.isOK())
494                 return;
495         lex.next();
496         token = lex.getString();
497         if (token == "position") {
498                 lex.next();
499                 // The [0] is needed. We need the first and only char in
500                 // this string -- MV
501                 pos = lex.getString()[0];
502         } else {
503                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
504                 lex.pushToken(token);
505         }
506         if (!lex.isOK())
507                 return;
508         lex.next();
509         token = lex.getString();
510         if (token == "hor_pos") {
511                 lex.next();
512                 hor_pos = lex.getString()[0];
513         } else {
514                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
515                 lex.pushToken(token);
516         }
517         if (!lex.isOK())
518                 return;
519         lex.next();
520         token = lex.getString();
521         if (token == "has_inner_box") {
522                 lex.next();
523                 inner_box = lex.getInteger();
524         } else {
525                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
526                 lex.pushToken(token);
527         }
528
529         if (!lex.isOK())
530                 return;
531         lex.next();
532         token = lex.getString();
533         if (token == "inner_pos") {
534                 lex.next();
535                 inner_pos = lex.getString()[0];
536         } else {
537                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
538                         << token << endl;
539                 lex.pushToken(token);
540         }
541         if (!lex.isOK())
542                 return;
543         lex.next();
544         token = lex.getString();
545         if (token == "use_parbox") {
546                 lex.next();
547                 use_parbox = lex.getInteger();
548         } else {
549                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
550                 lex.pushToken(token);
551         }
552         if (!lex.isOK())
553                 return;
554         lex.next();
555         token = lex.getString();
556         if (token == "width") {
557                 lex.next();
558                 width = LyXLength(lex.getString());
559         } else {
560                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
561                 lex.pushToken(token);
562         }
563         if (!lex.isOK())
564                 return;
565         lex.next();
566         token = lex.getString();
567         if (token == "special") {
568                 lex.next();
569                 special = lex.getString();
570         } else {
571                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
572                 lex.pushToken(token);
573         }
574         if (!lex.isOK())
575                 return;
576         lex.next();
577         token = lex.getString();
578         if (token == "height") {
579                 lex.next();
580                 height = LyXLength(lex.getString());
581         } else {
582                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
583                 lex.pushToken(token);
584         }
585         if (!lex.isOK())
586                 return;
587         lex.next();
588         token = lex.getString();
589         if (token == "height_special") {
590                 lex.next();
591                 height_special = lex.getString();
592         } else {
593                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
594                 lex.pushToken(token);
595         }
596 }