]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Fix coloured boxes in RTL with [pdf]latex (#8642)
[lyx.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  * \author Uwe Stöhr
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "InsetBox.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "ColorSet.h"
22 #include "Cursor.h"
23 #include "DispatchResult.h"
24 #include "FuncStatus.h"
25 #include "FuncRequest.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "MetricsInfo.h"
29 #include "output_xhtml.h"
30 #include "TexRow.h"
31 #include "texstream.h"
32 #include "TextClass.h"
33
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/Translator.h"
39
40 #include "frontends/Application.h"
41
42 #include <sstream>
43
44 using namespace std;
45 using namespace lyx::support;
46
47 namespace lyx {
48
49 namespace {
50
51 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
52 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
53
54 BoxTranslator initBoxtranslator()
55 {
56         BoxTranslator translator("Boxed", InsetBox::Boxed);
57         translator.addPair("Frameless", InsetBox::Frameless);
58         translator.addPair("Framed", InsetBox::Framed);
59         translator.addPair("ovalbox", InsetBox::ovalbox);
60         translator.addPair("Ovalbox", InsetBox::Ovalbox);
61         translator.addPair("Shadowbox", InsetBox::Shadowbox);
62         translator.addPair("Shaded", InsetBox::Shaded);
63         translator.addPair("Doublebox",InsetBox::Doublebox);
64         return translator;
65 }
66
67
68 BoxTranslatorLoc initBoxtranslatorLoc()
69 {
70         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
71         translator.addPair(_("frameless"), InsetBox::Frameless);
72         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
73         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
74         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
75         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
76         translator.addPair(_("shaded background"), InsetBox::Shaded);
77         translator.addPair(_("double frame"), InsetBox::Doublebox);
78         return translator;
79 }
80
81
82 BoxTranslator const & boxtranslator()
83 {
84         static BoxTranslator const translator = initBoxtranslator();
85         return translator;
86 }
87
88
89 BoxTranslatorLoc const & boxtranslator_loc()
90 {
91         static BoxTranslatorLoc const translator = initBoxtranslatorLoc();
92         return translator;
93 }
94
95 } // namespace
96
97
98 /////////////////////////////////////////////////////////////////////////
99 //
100 // InsetBox
101 //
102 /////////////////////////////////////////////////////////////////////////
103
104 InsetBox::InsetBox(Buffer * buffer, string const & label)
105         : InsetCollapsible(buffer), params_(label)
106 {}
107
108
109 docstring InsetBox::layoutName() const
110 {
111         // FIXME: UNICODE
112         return from_ascii("Box:" + params_.type);
113 }
114
115
116 void InsetBox::write(ostream & os) const
117 {
118         params_.write(os);
119         InsetCollapsible::write(os);
120 }
121
122
123 void InsetBox::read(Lexer & lex)
124 {
125         params_.read(lex);
126         InsetCollapsible::read(lex);
127 }
128
129
130 void InsetBox::setButtonLabel()
131 {
132         BoxType const btype = boxtranslator().find(params_.type);
133
134         docstring const type = _("Box");
135
136         docstring inner;
137         if (params_.inner_box) {
138                 if (params_.use_parbox)
139                         inner = _("Parbox");
140                 else if (params_.use_makebox)
141                         inner = _("Makebox");
142                 else
143                         inner = _("Minipage");
144         }
145
146         docstring frame;
147         if (btype != Frameless)
148                 frame = boxtranslator_loc().find(btype);
149
150         docstring label;
151         if (inner.empty() && frame.empty())
152                 label = type;
153         else if (inner.empty())
154                 label = bformat(_("%1$s (%2$s)"),
155                         type, frame);
156         else if (frame.empty())
157                 label = bformat(_("%1$s (%2$s)"),
158                         type, inner);
159         else
160                 label = bformat(_("%1$s (%2$s, %3$s)"),
161                         type, inner, frame);
162         setLabel(label);
163
164         // set the frame color for the inset if the type is Boxed
165         if (btype == Boxed)
166                 setFrameColor(lcolor.getFromLaTeXName(params_.framecolor));
167         else
168                 setFrameColor(Color_collapsibleframe);
169 }
170
171
172 bool InsetBox::hasFixedWidth() const
173 {
174         return !params_.width.empty() && params_.special == "none";
175 }
176
177
178 bool InsetBox::allowMultiPar() const
179 {
180         return (params_.inner_box && !params_.use_makebox)
181                 || params_.type == "Shaded" || params_.type == "Framed";
182 }
183
184
185 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
186 {
187         // back up textwidth.
188         int textwidth_backup = m.base.textwidth;
189         if (hasFixedWidth())
190                 m.base.textwidth = params_.width.inPixels(m.base);
191         InsetCollapsible::metrics(m, dim);
192         // retore textwidth.
193         m.base.textwidth = textwidth_backup;
194 }
195
196
197 bool InsetBox::forcePlainLayout(idx_type) const
198 {
199         return (!params_.inner_box || params_.use_makebox)
200                 && params_.type != "Shaded" && params_.type != "Framed";
201 }
202
203
204 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
205 {
206         // we only support background color for 3 types
207         if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
208                 return getLayout().bgcolor();
209
210         if (params_.type == "Shaded") {
211                 // FIXME: This hardcoded color is a hack!
212                 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
213                         return getLayout().bgcolor();
214
215                 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
216                 if (c == Color_none)
217                         return getLayout().bgcolor();
218                 return c;
219         }
220
221         if (params_.backgroundcolor != "none")
222                 return lcolor.getFromLaTeXName(params_.backgroundcolor);
223
224         return getLayout().bgcolor();
225 }
226
227
228 LyXAlignment InsetBox::contentAlignment() const
229 {
230         // Custom horizontal alignment is only allowed with a fixed width
231         // and if either makebox or no inner box are used
232         if (params_.width.empty() || !(params_.use_makebox || !params_.inner_box))
233                 return LYX_ALIGN_NONE;
234
235         // The default value below is actually irrelevant
236         LyXAlignment align = LYX_ALIGN_NONE;
237         switch (params_.hor_pos) {
238         case 'l':
239                 align = LYX_ALIGN_LEFT;
240                 break;
241         case 'c':
242                 align = LYX_ALIGN_CENTER;
243                 break;
244         case 'r':
245                 align = LYX_ALIGN_RIGHT;
246                 break;
247         case 's':
248                 align = LYX_ALIGN_BLOCK;
249                 break;
250         }
251         return align;
252 }
253
254
255 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
256 {
257         switch (cmd.action()) {
258
259         case LFUN_INSET_MODIFY: {
260                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
261                 string const first_arg = cmd.getArg(0);
262                 bool const change_type = first_arg == "changetype";
263                 bool const for_box = first_arg == "box";
264                 if (!change_type && !for_box) {
265                         // not for us
266                         // this will not be handled higher up
267                         cur.undispatched();
268                         return;
269                 }
270                 cur.recordUndoInset(this);
271                 if (change_type) {
272                         params_.type = cmd.getArg(1);
273                         // set a makebox if there is no inner box but Frameless was exectued
274                         // otherwise the result would be a non existent box (no inner AND outer box)
275                         // (this was LyX bug 8712)
276                         if (params_.type == "Frameless" && !params_.inner_box) {
277                                 params_.use_makebox = true;
278                                 params_.inner_box = true;
279                         }
280                         // handle the opposite case
281                         if (params_.type == "Boxed" && params_.use_makebox) {
282                                 params_.use_makebox = false;
283                                 params_.inner_box = false;
284                         }
285                 } else
286                         string2params(to_utf8(cmd.argument()), params_);
287                 setButtonLabel();
288                 break;
289         }
290
291         default:
292                 InsetCollapsible::doDispatch(cur, cmd);
293                 break;
294         }
295 }
296
297
298 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
299                 FuncStatus & flag) const
300 {
301         switch (cmd.action()) {
302
303         case LFUN_INSET_MODIFY: {
304                 string const first_arg = cmd.getArg(0);
305                 if (first_arg == "changetype") {
306                         string const type = cmd.getArg(1);
307                         flag.setOnOff(type == params_.type);
308                         flag.setEnabled(!params_.inner_box || type != "Framed");
309                         return true;
310                 }
311                 if (first_arg == "box") {
312                         flag.setEnabled(true);
313                         return true;
314                 }
315                 return InsetCollapsible::getStatus(cur, cmd, flag);
316         }
317
318         case LFUN_INSET_DIALOG_UPDATE:
319                 flag.setEnabled(true);
320                 return true;
321
322         default:
323                 return InsetCollapsible::getStatus(cur, cmd, flag);
324         }
325 }
326
327
328 const string defaultThick = "0.4pt";
329 const string defaultSep = "3pt";
330 const string defaultShadow = "4pt";
331
332 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
333 {
334         BoxType btype = boxtranslator().find(params_.type);
335
336         string width_string = params_.width.asLatexString();
337         string thickness_string = params_.thickness.asLatexString();
338         string separation_string = params_.separation.asLatexString();
339         string shadowsize_string = params_.shadowsize.asLatexString();
340         bool stdwidth = false;
341         // Colored boxes in RTL need to be wrapped into \beginL...\endL
342         string maybeBeginL;
343         string maybeEndL;
344         bool needEndL = false;
345         if (!runparams.isFullUnicode() && runparams.local_font->isRightToLeft()) {
346                 maybeBeginL = "\\beginL";
347                 maybeEndL = "\\endL";
348         }
349         // in general the overall width of some decorated boxes is wider thean the inner box
350         // we could therefore calculate the real width for all sizes so that if the user wants
351         // e.g. 0.1\columnwidth or 2cm he gets exactly this size
352         // however this makes problems when importing TeX code
353         // therefore only recalculate for the most common case that the box should not protrude
354         // the page margins
355         if (params_.inner_box
356                 && ((width_string.find("1\\columnwidth") != string::npos
357                         || width_string.find("1\\textwidth") != string::npos)
358                         || width_string.find("1\\paperwidth") != string::npos
359                         || width_string.find("1\\linewidth") != string::npos)) {
360                 stdwidth = true;
361                 switch (btype) {
362                 case Frameless:
363                         break;
364                 case Framed:
365                         width_string += " - 2\\FrameSep - 2\\FrameRule";
366                         break;
367                 case Boxed:
368                         width_string += " - 2\\fboxsep - 2\\fboxrule";
369                         break;
370                 case Shaded:
371                         break;
372                 case ovalbox:
373                         width_string += " - 2\\fboxsep - 0.8pt";
374                         break;
375                 case Ovalbox:
376                         width_string += " - 2\\fboxsep - 1.6pt";
377                         break;
378                 case Shadowbox:
379                         width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
380                         break;
381                 case Doublebox:
382                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
383                         break;
384                 }
385         }
386
387         os << safebreakln;
388         if (runparams.lastid != -1)
389                 os.texrow().start(runparams.lastid, runparams.lastpos);
390
391         // adapt column/text width correctly also if paragraphs indented
392         if (stdwidth && !(buffer().params().paragraph_separation))
393                 os << "\\noindent";
394
395         switch (btype) {
396         case Frameless:
397                 break;
398         case Framed:
399                 if (thickness_string != defaultThick) {
400                         os << "{\\FrameRule " << from_ascii(thickness_string);
401                         if (separation_string != defaultSep)
402                                 os << "\\FrameSep " << from_ascii(separation_string);
403                 }
404                 if (separation_string != defaultSep && thickness_string == defaultThick)
405                         os << "{\\FrameSep " << from_ascii(separation_string);
406
407                 os << "\\begin{framed}%\n";
408                 break;
409         case Boxed:
410                 if (thickness_string != defaultThick) {
411                         os << "{\\fboxrule " << from_ascii(thickness_string);
412                         if (separation_string != defaultSep)
413                                 os << "\\fboxsep " << from_ascii(separation_string);
414                 }
415                 if (separation_string != defaultSep && thickness_string == defaultThick)
416                         os << "{\\fboxsep " << from_ascii(separation_string);
417                 if (!params_.inner_box && !width_string.empty()) {
418                         if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
419                                 os << maybeBeginL << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
420                                 os << "\\makebox";
421                                 needEndL = !maybeBeginL.empty();
422                         } else
423                                 os << "\\framebox";
424                         // Special widths, see usrguide sec. 3.5
425                         // FIXME UNICODE
426                         if (params_.special != "none") {
427                                 os << "[" << params_.width.value()
428                                    << '\\' << from_utf8(params_.special)
429                                    << ']';
430                         } else
431                                 os << '[' << from_ascii(width_string)
432                                    << ']';
433                         // default horizontal alignment is 'c'
434                         if (params_.hor_pos != 'c')
435                                 os << "[" << params_.hor_pos << "]";
436                 } else {
437                         if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
438                                 os << maybeBeginL << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
439                                 needEndL = !maybeBeginL.empty();
440                         } else 
441                                 os << "\\fbox";
442                 }
443                 os << "{";
444                 break;
445         case ovalbox:
446                 if (!separation_string.empty() && separation_string != defaultSep)
447                         os << "{\\fboxsep " << from_ascii(separation_string);
448                 os << "\\ovalbox{";
449                 break;
450         case Ovalbox:
451                 if (!separation_string.empty() && separation_string != defaultSep)
452                         os << "{\\fboxsep " << from_ascii(separation_string);
453                 os << "\\Ovalbox{";
454                 break;
455         case Shadowbox:
456                 if (thickness_string != defaultThick) {
457                         os << "{\\fboxrule " << from_ascii(thickness_string);
458                         if (separation_string != defaultSep) {
459                                 os << "\\fboxsep " << from_ascii(separation_string);
460                                 if (shadowsize_string != defaultShadow)
461                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
462                         }
463                         if (shadowsize_string != defaultShadow  && separation_string == defaultSep)
464                                 os << "\\shadowsize " << from_ascii(shadowsize_string);
465                 }
466                 if (separation_string != defaultSep && thickness_string == defaultThick) {
467                                 os << "{\\fboxsep " << from_ascii(separation_string);
468                                 if (shadowsize_string != defaultShadow)
469                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
470                 }
471                 if (shadowsize_string != defaultShadow
472                                 && separation_string == defaultSep
473                                 && thickness_string == defaultThick)
474                                 os << "{\\shadowsize " << from_ascii(shadowsize_string);
475                 os << "\\shadowbox{";
476                 break;
477         case Shaded:
478                 // must be set later because e.g. the width settings only work when
479                 // it is inside a minipage or parbox
480                 os << maybeBeginL;
481                 needEndL = !maybeBeginL.empty();
482                 break;
483         case Doublebox:
484                 if (thickness_string != defaultThick) {
485                         os << "{\\fboxrule " << from_ascii(thickness_string);
486                         if (separation_string != defaultSep)
487                                 os << "\\fboxsep " << from_ascii(separation_string);
488                 }
489                 if (separation_string != defaultSep && thickness_string == defaultThick)
490                         os << "{\\fboxsep " << from_ascii(separation_string);
491                 os << "\\doublebox{";
492                 break;
493         }
494
495         if (params_.inner_box) {
496                 if (params_.use_parbox) {
497                         if (params_.backgroundcolor != "none" && btype == Frameless) {
498                                 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
499                                 needEndL = !maybeBeginL.empty();
500                         }
501                         os << "\\parbox";
502                 } else if (params_.use_makebox) {
503                         if (!width_string.empty()) {
504                                 if (params_.backgroundcolor != "none") {
505                                         os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
506                                         needEndL = !maybeBeginL.empty();
507                                 }
508                                 os << "\\makebox";
509                                 // FIXME UNICODE
510                                 // output the width and horizontal position
511                                 if (params_.special != "none") {
512                                         os << "[" << params_.width.value()
513                                            << '\\' << from_utf8(params_.special)
514                                            << ']';
515                                 } else
516                                         os << '[' << from_ascii(width_string)
517                                            << ']';
518                                 if (params_.hor_pos != 'c')
519                                         os << "[" << params_.hor_pos << "]";
520                         } else {
521                                 if (params_.backgroundcolor != "none") {
522                                         os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}";
523                                         needEndL = !maybeBeginL.empty();
524                                 }
525                                 else
526                                         os << "\\mbox";
527                         }
528                         os << "{";
529                 }
530                 else {
531                         if (params_.backgroundcolor != "none" && btype == Frameless) {
532                                 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
533                                 needEndL = !maybeBeginL.empty();
534                         }
535                         os << "\\begin{minipage}";
536                 }
537
538                 // output parameters for parbox and minipage
539                 if (!params_.use_makebox) {
540                         os << "[" << params_.pos << "]";
541                         if (params_.height_special == "none") {
542                                 // FIXME UNICODE
543                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
544                         } else {
545                                 // Special heights
546                                 // set no optional argument when the value is the default "1\height"
547                                 // (special units like \height are handled as "in")
548                                 // but when the user has chosen a non-default inner_pos, the height
549                                 // must be given: \minipage[pos][height][inner-pos]{width}
550                                 if ((params_.height != Length("1in") ||
551                                         params_.height_special != "totalheight") ||
552                                         params_.inner_pos != params_.pos) {
553                                                 // FIXME UNICODE
554                                                 os << "[" << params_.height.value()
555                                                         << "\\" << from_utf8(params_.height_special) << "]";
556                                 }
557                         }
558                         if (params_.inner_pos != params_.pos)
559                                 os << "[" << params_.inner_pos << "]";
560                         // FIXME UNICODE
561                         os << '{' << from_ascii(width_string) << '}';
562                         if (params_.use_parbox)
563                                 os << "{";
564                 }
565
566                 os << "%\n";
567         } // end if inner_box
568
569         if (btype == Shaded) {
570                 os << "\\begin{shaded}%\n";
571         }
572
573         InsetText::latex(os, runparams);
574
575         if (btype == Shaded)
576                 os << "\\end{shaded}";
577
578         if (params_.inner_box) {
579                 if (params_.use_parbox || params_.use_makebox)
580                         os << "%\n}";
581                 else
582                         os << "%\n\\end{minipage}";
583                 if (params_.backgroundcolor != "none" && btype == Frameless
584                         && !(params_.use_makebox && width_string.empty()))
585                         os << "}";
586         }
587
588         switch (btype) {
589         case Frameless:
590                 break;
591         case Framed:
592                 os << "\\end{framed}";
593                 if (separation_string != defaultSep || thickness_string != defaultThick)
594                         os << "}";
595                 break;
596         case Boxed:
597                 os << "}";
598                 if (!params_.inner_box && !width_string.empty()
599                         && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
600                         os << "}";
601                 if (separation_string != defaultSep     || thickness_string != defaultThick)
602                         os << "}";
603                 break;
604         case ovalbox:
605                 os << "}";
606                 if (separation_string != defaultSep)
607                         os << "}";
608                 break;
609         case Ovalbox:
610                 os << "}";
611                 if (separation_string != defaultSep)
612                         os << "}";
613                 break;
614         case Doublebox:
615                 os << "}";
616                 if (separation_string != defaultSep || thickness_string != defaultThick)
617                         os << "}";
618                 break;
619         case Shadowbox:
620                 os << "}";
621                 if (separation_string != defaultSep
622                         || thickness_string != defaultThick
623                         || shadowsize_string != defaultShadow)
624                         os << "}";
625                 break;
626         case Shaded:
627                 // already done
628                 break;
629         }
630         if (needEndL)
631                 os << maybeEndL;
632 }
633
634
635 int InsetBox::plaintext(odocstringstream & os,
636        OutputParams const & runparams, size_t max_length) const
637 {
638         BoxType const btype = boxtranslator().find(params_.type);
639
640         switch (btype) {
641                 case Frameless:
642                         break;
643                 case Framed:
644                 case Boxed:
645                         os << "[\n";
646                         break;
647                 case ovalbox:
648                         os << "(\n";
649                         break;
650                 case Ovalbox:
651                         os << "((\n";
652                         break;
653                 case Shadowbox:
654                 case Shaded:
655                         os << "[/\n";
656                         break;
657                 case Doublebox:
658                         os << "[[\n";
659                         break;
660         }
661
662         InsetText::plaintext(os, runparams, max_length);
663
664         int len = 0;
665         switch (btype) {
666                 case Frameless:
667                         os << "\n";
668                         break;
669                 case Framed:
670                 case Boxed:
671                         os << "\n]";
672                         len = 1;
673                         break;
674                 case ovalbox:
675                         os << "\n)";
676                         len = 1;
677                         break;
678                 case Ovalbox:
679                         os << "\n))";
680                         len = 2;
681                         break;
682                 case Shadowbox:
683                 case Shaded:
684                         os << "\n/]";
685                         len = 2;
686                         break;
687                 case Doublebox:
688                         os << "\n]]";
689                         len = 2;
690                         break;
691         }
692
693         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
694 }
695
696
697 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
698 {
699         return InsetText::docbook(os, runparams);
700 }
701
702
703 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
704 {
705         // construct attributes
706         string attrs = "class='" + params_.type + "'";
707         string style;
708         if (!params_.width.empty()) {
709                 string w = params_.width.asHTMLString();
710                 if (w != "100%")
711                         style += ("width: " + params_.width.asHTMLString() + "; ");
712         }
713         // The special heights don't really mean anything for us.
714         if (!params_.height.empty() && params_.height_special == "none")
715                 style += ("height: " + params_.height.asHTMLString() + "; ");
716         if (!style.empty())
717                 attrs += " style='" + style + "'";
718
719         xs << html::StartTag("div", attrs);
720         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
721         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
722         xs << html::EndTag("div");
723         xs << defer;
724         return docstring();
725 }
726
727
728 void InsetBox::validate(LaTeXFeatures & features) const
729 {
730         BoxType btype = boxtranslator().find(params_.type);
731         switch (btype) {
732         case Frameless:
733                 if (params_.backgroundcolor != "none")
734                         features.require("xcolor");
735                 break;
736         case Framed:
737                 features.require("calc");
738                 features.require("framed");
739                 break;
740         case Boxed:
741                 features.require("calc");
742                 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
743                         features.require("xcolor");
744                 break;
745         case ovalbox:
746         case Ovalbox:
747         case Shadowbox:
748         case Doublebox:
749                 features.require("calc");
750                 features.require("fancybox");
751                 break;
752         case Shaded:
753                 features.require("color");
754                 features.require("framed");
755                 break;
756         }
757         InsetCollapsible::validate(features);
758 }
759
760
761 string InsetBox::contextMenuName() const
762 {
763         return "context-box";
764 }
765
766
767 string InsetBox::params2string(InsetBoxParams const & params)
768 {
769         ostringstream data;
770         data << "box" << ' ';
771         params.write(data);
772         return data.str();
773 }
774
775
776 void InsetBox::string2params(string const & in, InsetBoxParams & params)
777 {
778         if (in.empty())
779                 return;
780
781         istringstream data(in);
782         Lexer lex;
783         lex.setStream(data);
784
785         string name;
786         lex >> name;
787         if (!lex || name != "box") {
788                 LYXERR0("InsetBox::string2params(" << in << ")\n"
789                                           "Expected arg 1 to be \"box\"\n");
790                 return;
791         }
792
793         // This is part of the inset proper that is usually swallowed
794         // by Text::readInset
795         string id;
796         lex >> id;
797         if (!lex || id != "Box") {
798                 LYXERR0("InsetBox::string2params(" << in << ")\n"
799                                           "Expected arg 2 to be \"Box\"\n");
800         }
801
802         params = InsetBoxParams(string());
803         params.read(lex);
804 }
805
806
807 /////////////////////////////////////////////////////////////////////////
808 //
809 // InsetBoxParams
810 //
811 /////////////////////////////////////////////////////////////////////////
812
813 InsetBoxParams::InsetBoxParams(string const & label)
814         : type(label),
815           use_parbox(false),
816           use_makebox(false),
817           inner_box(true),
818           width(Length("100col%")),
819           special("none"),
820           pos('t'),
821           hor_pos('c'),
822           inner_pos('t'),
823           height(Length("1in")),
824           height_special("totalheight"), // default is 1\\totalheight
825           thickness(Length(defaultThick)),
826           separation(Length(defaultSep)),
827           shadowsize(Length(defaultShadow)),
828           framecolor("black"),
829           backgroundcolor("none")
830 {}
831
832
833 void InsetBoxParams::write(ostream & os) const
834 {
835         os << "Box " << type << "\n";
836         os << "position \"" << pos << "\"\n";
837         os << "hor_pos \"" << hor_pos << "\"\n";
838         os << "has_inner_box " << inner_box << "\n";
839         os << "inner_pos \"" << inner_pos << "\"\n";
840         os << "use_parbox " << use_parbox << "\n";
841         os << "use_makebox " << use_makebox << "\n";
842         os << "width \"" << width.asString() << "\"\n";
843         os << "special \"" << special << "\"\n";
844         os << "height \"" << height.asString() << "\"\n";
845         os << "height_special \"" << height_special << "\"\n";
846         os << "thickness \"" << thickness.asString() << "\"\n";
847         os << "separation \"" << separation.asString() << "\"\n";
848         os << "shadowsize \"" << shadowsize.asString() << "\"\n";
849         os << "framecolor \"" << framecolor << "\"\n";
850         os << "backgroundcolor \"" << backgroundcolor << "\"\n";
851 }
852
853
854 void InsetBoxParams::read(Lexer & lex)
855 {
856         lex.setContext("InsetBoxParams::read");
857         lex >> type;
858         lex >> "position" >> pos;
859         lex >> "hor_pos" >> hor_pos;
860         lex >> "has_inner_box" >> inner_box;
861         if (type == "Framed")
862                 inner_box = false;
863         lex >> "inner_pos" >> inner_pos;
864         lex >> "use_parbox" >> use_parbox;
865         lex >> "use_makebox" >> use_makebox;
866         lex >> "width" >> width;
867         lex >> "special" >> special;
868         lex >> "height" >> height;
869         lex >> "height_special" >> height_special;
870         lex >> "thickness" >> thickness;
871         lex >> "separation" >> separation;
872         lex >> "shadowsize" >> shadowsize;
873         lex >> "framecolor" >> framecolor;
874         lex >> "backgroundcolor" >> backgroundcolor;
875 }
876
877
878 } // namespace lyx