]> git.lyx.org Git - features.git/blob - src/insets/InsetBox.cpp
* Lazy MathData to avoid unneeded interpretation of macro definitions
[features.git] / src / insets / InsetBox.cpp
1 /**
2  * \file InsetBox.cpp
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 "Buffer.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "support/debug.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
24 #include "support/gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28
29 #include "support/Translator.h"
30
31 #include <sstream>
32
33 using namespace std;
34
35 namespace lyx {
36
37 namespace {
38
39 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
40 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
41
42 BoxTranslator const init_boxtranslator()
43 {
44         BoxTranslator translator("Boxed", InsetBox::Boxed);
45         translator.addPair("Frameless", InsetBox::Frameless);
46         translator.addPair("Framed", InsetBox::Framed);
47         translator.addPair("ovalbox", InsetBox::ovalbox);
48         translator.addPair("Ovalbox", InsetBox::Ovalbox);
49         translator.addPair("Shadowbox", InsetBox::Shadowbox);
50         translator.addPair("Shaded", InsetBox::Shaded);
51         translator.addPair("Doublebox",InsetBox::Doublebox);
52         return translator;
53 }
54
55
56 BoxTranslatorLoc const init_boxtranslator_loc()
57 {
58         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
59         translator.addPair(_("frameless"), InsetBox::Frameless);
60         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
61         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
62         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
63         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
64         translator.addPair(_("shaded background"), InsetBox::Shaded);
65         translator.addPair(_("double frame"), InsetBox::Doublebox);
66         return translator;
67 }
68
69
70 BoxTranslator const & boxtranslator()
71 {
72         static BoxTranslator translator = init_boxtranslator();
73         return translator;
74 }
75
76
77 BoxTranslatorLoc const & boxtranslator_loc()
78 {
79         static BoxTranslatorLoc translator = init_boxtranslator_loc();
80         return translator;
81 }
82
83 } // anon
84
85
86 InsetBox::InsetBox(BufferParams const & bp, string const & label)
87         : InsetCollapsable(bp), params_(label)
88 {}
89
90
91 InsetBox::InsetBox(InsetBox const & in)
92         : InsetCollapsable(in), params_(in.params_)
93 {}
94
95
96 InsetBox::~InsetBox()
97 {
98         InsetBoxMailer(*this).hideDialog();
99 }
100
101
102 Inset * InsetBox::clone() const
103 {
104         return new InsetBox(*this);
105 }
106
107
108 docstring const InsetBox::editMessage() const
109 {
110         return _("Opened Box Inset");
111 }
112
113
114 docstring InsetBox::name() const 
115 {
116         // FIXME: UNICODE
117         string name = string("Box");
118         if (boxtranslator().find(params_.type) == Shaded)
119                 name += string(":Shaded");
120         return from_ascii(name);
121 }
122
123
124 void InsetBox::write(Buffer const & buf, ostream & os) const
125 {
126         params_.write(os);
127         InsetCollapsable::write(buf, os);
128 }
129
130
131 void InsetBox::read(Buffer const & buf, Lexer & lex)
132 {
133         params_.read(lex);
134         InsetCollapsable::read(buf, lex);
135 }
136
137
138 void InsetBox::setButtonLabel()
139 {
140         BoxType btype = boxtranslator().find(params_.type);
141
142         docstring label;
143         label += _("Box");
144         label += " (";
145         if (btype == Frameless) {
146                 if (params_.use_parbox)
147                         label += _("Parbox");
148                 else
149                         label += _("Minipage");
150         } else
151                 label += boxtranslator_loc().find(btype);
152         label += ")";
153
154         setLabel(label);
155 }
156
157
158 bool InsetBox::hasFixedWidth() const
159 {
160       return params_.inner_box || params_.special != "width";
161 }
162
163
164 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
165 {
166         // back up textwidth.
167         int textwidth_backup = m.base.textwidth;
168         if (hasFixedWidth())
169                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
170         InsetCollapsable::metrics(m, dim);
171         // retore textwidth.
172         m.base.textwidth = textwidth_backup;
173 }
174
175
176 bool InsetBox::forceDefaultParagraphs(idx_type) const
177 {
178         return !params_.inner_box;
179 }
180
181
182 bool InsetBox::showInsetDialog(BufferView * bv) const
183 {
184         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
185         return true;
186 }
187
188
189 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
190 {
191         switch (cmd.action) {
192
193         case LFUN_INSET_MODIFY: {
194                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
195                 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
196                 setLayout(cur.buffer().params());
197                 break;
198         }
199
200         case LFUN_INSET_DIALOG_UPDATE:
201                 InsetBoxMailer(*this).updateDialog(&cur.bv());
202                 break;
203
204         case LFUN_MOUSE_RELEASE:
205                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
206                         InsetBoxMailer(*this).showDialog(&cur.bv());
207                         break;
208                 }
209                 InsetCollapsable::doDispatch(cur, cmd);
210                 break;
211
212         default:
213                 InsetCollapsable::doDispatch(cur, cmd);
214                 break;
215         }
216 }
217
218
219 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
220                 FuncStatus & flag) const
221 {
222         switch (cmd.action) {
223
224         case LFUN_INSET_MODIFY:
225         case LFUN_INSET_DIALOG_UPDATE:
226                 flag.enabled(true);
227                 return true;
228         case LFUN_BREAK_PARAGRAPH:
229                 if (params_.inner_box) {
230                         return InsetCollapsable::getStatus(cur, cmd, flag);
231                 } else {
232                         flag.enabled(false);
233                         return true;
234                 }
235
236         default:
237                 return InsetCollapsable::getStatus(cur, cmd, flag);
238         }
239 }
240
241
242 bool InsetBox::isMacroScope(Buffer const & buf) const
243 {
244         BoxType btype = boxtranslator().find(params_.type);
245         return btype != Frameless || params_.inner_box;
246 }
247
248
249 int InsetBox::latex(Buffer const & buf, odocstream & os,
250                     OutputParams const & runparams) const
251 {
252         BoxType btype = boxtranslator().find(params_.type);
253
254         string width_string = params_.width.asLatexString();
255         bool stdwidth(false);
256         if (params_.inner_box &&
257                         (width_string.find("1.0\\columnwidth") != string::npos
258                         || width_string.find("1.0\\textwidth") != string::npos)) {
259                 stdwidth = true;
260                 switch (btype) {
261                 case Frameless:
262                 case Framed:
263                         break;
264                 case Boxed:
265                 case Shaded:
266                         width_string += " - 2\\fboxsep - 2\\fboxrule";
267                         break;
268                 case ovalbox:
269                         width_string += " - 2\\fboxsep - 0.8pt";
270                         break;
271                 case Ovalbox:
272                         width_string += " - 2\\fboxsep - 1.6pt";
273                         break;
274                 case Shadowbox:
275                         // Shadow falls outside right margin... opinions?
276                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
277                         break;
278                 case Doublebox:
279                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
280                         break;
281                 }
282         }
283
284         int i = 0;
285         os << "%\n";
286         // Adapt to column/text width correctly also if paragraphs indented:
287         if (stdwidth)
288                 os << "\\noindent";
289
290         switch (btype) {
291         case Frameless:
292                 break;
293         case Framed:
294                 os << "\\begin{framed}%\n";
295                 i += 1;
296                 break;
297         case Boxed:
298                 os << "\\framebox";
299                 if (!params_.inner_box) {
300                         os << "{\\makebox";
301                         // Special widths, see usrguide §3.5
302                         // FIXME UNICODE
303                         if (params_.special != "none") {
304                                 os << "[" << params_.width.value()
305                                    << '\\' << from_utf8(params_.special)
306                                    << ']';
307                         } else
308                                 os << '[' << from_ascii(width_string)
309                                    << ']';
310                         if (params_.hor_pos != 'c')
311                                 os << "[" << params_.hor_pos << "]";
312                 }
313
314                 os << "{";
315                 break;
316         case ovalbox:
317                 os << "\\ovalbox{";
318                 break;
319         case Ovalbox:
320                 os << "\\Ovalbox{";
321                 break;
322         case Shadowbox:
323                 os << "\\shadowbox{";
324                 break;
325         case Shaded:
326                 // later
327                 break;
328         case Doublebox:
329                 os << "\\doublebox{";
330                 break;
331         }
332
333         if (params_.inner_box) {
334                 if (params_.use_parbox)
335                         os << "\\parbox";
336                 else
337                         os << "\\begin{minipage}";
338
339                 os << "[" << params_.pos << "]";
340                 if (params_.height_special == "none") {
341                         // FIXME UNICODE
342                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
343                 } else {
344                         // Special heights
345                         // set no optional argument when the value is the default "1\height"
346                         // (special units like \height are handled as "in")
347                         // but when the user has chosen a non-default inner_pos, the height
348                         // must be given: \minipage[pos][height][inner-pos]{width}
349                         if ((params_.height != Length("1in") ||
350                                  params_.height_special != "totalheight") ||
351                                 params_.inner_pos != params_.pos) {
352                                 // FIXME UNICODE
353                                 os << "[" << params_.height.value()
354                                         << "\\" << from_utf8(params_.height_special) << "]";
355                         }
356                 }
357                 if (params_.inner_pos != params_.pos)
358                         os << "[" << params_.inner_pos << "]";
359
360                 // FIXME UNICODE
361                 os << '{' << from_ascii(width_string) << '}';
362
363                 if (params_.use_parbox)
364                         os << "{";
365                 os << "%\n";
366                 i += 1;
367         }
368         if (btype == Shaded)
369                 os << "\\begin{shaded}%\n";
370                 i += 1;
371
372         i += InsetText::latex(buf, os, runparams);
373
374         if (btype == Shaded)
375                 os << "\\end{shaded}";
376
377         if (params_.inner_box) {
378                 if (params_.use_parbox)
379                         os << "%\n}";
380                 else
381                         os << "%\n\\end{minipage}";
382         }
383
384         switch (btype) {
385         case Frameless:
386                 break;
387         case Framed:
388                 os << "\\end{framed}";
389                 break;
390         case Boxed:
391                 if (!params_.inner_box)
392                         os << "}"; // for makebox
393                 os << "}";
394                 break;
395         case ovalbox:
396         case Ovalbox:
397         case Doublebox:
398         case Shadowbox:
399                 os << "}";
400                 break;
401         case Shaded:
402                 // already done
403                 break;
404         }
405         os << "%\n";
406
407         i += 3;
408
409         return i;
410 }
411
412
413 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
414                         OutputParams const & runparams) const
415 {
416         BoxType const btype = boxtranslator().find(params_.type);
417
418         switch (btype) {
419                 case Frameless:
420                         break;
421                 case Framed:
422                 case Boxed:
423                         os << "[\n";
424                         break;
425                 case ovalbox:
426                         os << "(\n";
427                         break;
428                 case Ovalbox:
429                         os << "((\n";
430                         break;
431                 case Shadowbox:
432                 case Shaded:
433                         os << "[/\n";
434                         break;
435                 case Doublebox:
436                         os << "[[\n";
437                         break;
438         }
439
440         InsetText::plaintext(buf, os, runparams);
441
442         int len = 0;
443         switch (btype) {
444                 case Frameless:
445                         os << "\n";
446                         break;
447                 case Framed:
448                 case Boxed:
449                         os << "\n]";
450                         len = 1;
451                         break;
452                 case ovalbox:
453                         os << "\n)";
454                         len = 1;
455                         break;
456                 case Ovalbox:
457                         os << "\n))";
458                         len = 2;
459                         break;
460                 case Shadowbox:
461                 case Shaded:
462                         os << "\n/]";
463                         len = 2;
464                         break;
465                 case Doublebox:
466                         os << "\n]]";
467                         len = 2;
468                         break;
469         }
470
471         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
472 }
473
474
475 int InsetBox::docbook(Buffer const & buf, odocstream & os,
476                       OutputParams const & runparams) const
477 {
478         return InsetText::docbook(buf, os, runparams);
479 }
480
481
482 void InsetBox::validate(LaTeXFeatures & features) const
483 {
484         BoxType btype = boxtranslator().find(params_.type);
485         switch (btype) {
486         case Frameless:
487                 break;
488         case Framed:
489                 features.require("framed");
490                 break;
491         case Boxed:
492                 features.require("calc");
493                 break;
494         case ovalbox:
495         case Ovalbox:
496         case Shadowbox:
497         case Doublebox:
498                 features.require("calc");
499                 features.require("fancybox");
500                 break;
501         case Shaded:
502                 features.require("color");
503                 features.require("framed");
504                 break;
505         }
506         InsetText::validate(features);
507 }
508
509
510 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
511         : inset_(inset)
512 {}
513
514
515 string const InsetBoxMailer::name_ = "box";
516
517
518 string const InsetBoxMailer::inset2string(Buffer const &) const
519 {
520         return params2string(inset_.params());
521 }
522
523
524 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
525 {
526         ostringstream data;
527         data << "box" << ' ';
528         params.write(data);
529         return data.str();
530 }
531
532
533 void InsetBoxMailer::string2params(string const & in,
534                                    InsetBoxParams & params)
535 {
536         params = InsetBoxParams(string());
537         if (in.empty())
538                 return;
539
540         istringstream data(in);
541         Lexer lex(0,0);
542         lex.setStream(data);
543
544         string name;
545         lex >> name;
546         if (!lex || name != name_)
547                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
548
549         // This is part of the inset proper that is usually swallowed
550         // by Text::readInset
551         string id;
552         lex >> id;
553         if (!lex || id != "Box")
554                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
555
556         params.read(lex);
557 }
558
559
560 InsetBoxParams::InsetBoxParams(string const & label)
561         : type(label),
562           use_parbox(false),
563           inner_box(true),
564           width(Length("100col%")),
565           special("none"),
566           pos('t'),
567           hor_pos('c'),
568           inner_pos('t'),
569           height(Length("1in")),
570           height_special("totalheight") // default is 1\\totalheight
571 {}
572
573
574 void InsetBoxParams::write(ostream & os) const
575 {
576         os << "Box " << type << "\n";
577         os << "position \"" << pos << "\"\n";
578         os << "hor_pos \"" << hor_pos << "\"\n";
579         os << "has_inner_box " << inner_box << "\n";
580         os << "inner_pos \"" << inner_pos << "\"\n";
581         os << "use_parbox " << use_parbox << "\n";
582         os << "width \"" << width.asString() << "\"\n";
583         os << "special \"" << special << "\"\n";
584         os << "height \"" << height.asString() << "\"\n";
585         os << "height_special \"" << height_special << "\"\n";
586 }
587
588
589 void InsetBoxParams::read(Lexer & lex)
590 {
591         if (!lex.isOK())
592                 return;
593
594         lex.next();
595         type = lex.getString();
596
597         if (!lex)
598                 return;
599
600         lex.next();
601         string token;
602         token = lex.getString();
603         if (!lex)
604                 return;
605         if (token == "position") {
606                 lex.next();
607                 // The [0] is needed. We need the first and only char in
608                 // this string -- MV
609                 pos = lex.getString()[0];
610         } else {
611                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
612                 lex.pushToken(token);
613         }
614
615         lex.next();
616         token = lex.getString();
617         if (!lex)
618                 return;
619         if (token == "hor_pos") {
620                 lex.next();
621                 hor_pos = lex.getString()[0];
622         } else {
623                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
624                 lex.pushToken(token);
625         }
626
627         lex.next();
628         token = lex.getString();
629         if (!lex)
630                 return;
631         if (token == "has_inner_box") {
632                 lex.next();
633                 inner_box = lex.getInteger();
634         } else {
635                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
636                 lex.pushToken(token);
637         }
638
639         lex.next();
640         token = lex.getString();
641         if (!lex)
642                 return;
643         if (token == "inner_pos") {
644                 lex.next();
645                 inner_pos = lex.getString()[0];
646         } else {
647                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
648                         << token << endl;
649                 lex.pushToken(token);
650         }
651
652         lex.next();
653         token = lex.getString();
654         if (!lex)
655                 return;
656         if (token == "use_parbox") {
657                 lex.next();
658                 use_parbox = lex.getInteger();
659         } else {
660                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
661                 lex.pushToken(token);
662         }
663
664         lex.next();
665         token = lex.getString();
666         if (!lex)
667                 return;
668         if (token == "width") {
669                 lex.next();
670                 width = Length(lex.getString());
671         } else {
672                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
673                 lex.pushToken(token);
674         }
675
676         lex.next();
677         token = lex.getString();
678         if (!lex)
679                 return;
680         if (token == "special") {
681                 lex.next();
682                 special = lex.getString();
683         } else {
684                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
685                 lex.pushToken(token);
686         }
687
688         lex.next();
689         token = lex.getString();
690         if (!lex)
691                 return;
692         if (token == "height") {
693                 lex.next();
694                 height = Length(lex.getString());
695         } else {
696                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
697                 lex.pushToken(token);
698         }
699
700         lex.next();
701         token = lex.getString();
702         if (!lex)
703                 return;
704         if (token == "height_special") {
705                 lex.next();
706                 height_special = lex.getString();
707         } else {
708                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
709                 lex.pushToken(token);
710         }
711 }
712
713
714 } // namespace lyx