]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Correct ^ catcode for \cprotect
[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 & mi, Dimension & dim) const
186 {
187         // back up textwidth.
188         int textwidth_backup = mi.base.textwidth;
189         if (hasFixedWidth())
190                 mi.base.textwidth = mi.base.inPixels(params_.width);
191         InsetCollapsible::metrics(mi, dim);
192         // retore textwidth.
193         mi.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 bool InsetBox::needsCProtection(bool const maintext, bool const fragile) const
205 {
206         // We need to cprotect boxes that use minipages as inner box
207         // in fragile context
208         if (fragile && params_.inner_box && !params_.use_parbox && !params_.use_makebox)
209                 return true;
210
211         return InsetText::needsCProtection(maintext, fragile);
212 }
213
214
215 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
216 {
217         // we only support background color for 3 types
218         if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
219                 return getLayout().bgcolor();
220
221         if (params_.type == "Shaded") {
222                 // FIXME: This hardcoded color is a hack!
223                 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
224                         return getLayout().bgcolor();
225
226                 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
227                 if (c == Color_none)
228                         return getLayout().bgcolor();
229                 return c;
230         }
231
232         if (params_.backgroundcolor != "none")
233                 return lcolor.getFromLaTeXName(params_.backgroundcolor);
234
235         return getLayout().bgcolor();
236 }
237
238
239 LyXAlignment InsetBox::contentAlignment() const
240 {
241         // Custom horizontal alignment is only allowed with a fixed width
242         // and if either makebox or no inner box are used
243         if (params_.width.empty() || !(params_.use_makebox || !params_.inner_box))
244                 return LYX_ALIGN_NONE;
245
246         // The default value below is actually irrelevant
247         LyXAlignment align = LYX_ALIGN_NONE;
248         switch (params_.hor_pos) {
249         case 'l':
250                 align = LYX_ALIGN_LEFT;
251                 break;
252         case 'c':
253                 align = LYX_ALIGN_CENTER;
254                 break;
255         case 'r':
256                 align = LYX_ALIGN_RIGHT;
257                 break;
258         case 's':
259                 align = LYX_ALIGN_BLOCK;
260                 break;
261         }
262         return align;
263 }
264
265
266 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
267 {
268         switch (cmd.action()) {
269
270         case LFUN_INSET_MODIFY: {
271                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
272                 string const first_arg = cmd.getArg(0);
273                 bool const change_type = first_arg == "changetype";
274                 bool const for_box = first_arg == "box";
275                 if (!change_type && !for_box) {
276                         // not for us
277                         // this will not be handled higher up
278                         cur.undispatched();
279                         return;
280                 }
281                 cur.recordUndoInset(this);
282                 if (change_type) {
283                         params_.type = cmd.getArg(1);
284                         // set a makebox if there is no inner box but Frameless was exectued
285                         // otherwise the result would be a non existent box (no inner AND outer box)
286                         // (this was LyX bug 8712)
287                         if (params_.type == "Frameless" && !params_.inner_box) {
288                                 params_.use_makebox = true;
289                                 params_.inner_box = true;
290                         }
291                         // handle the opposite case
292                         if (params_.type == "Boxed" && params_.use_makebox) {
293                                 params_.use_makebox = false;
294                                 params_.inner_box = false;
295                         }
296                 } else
297                         string2params(to_utf8(cmd.argument()), params_);
298                 setButtonLabel();
299                 break;
300         }
301
302         default:
303                 InsetCollapsible::doDispatch(cur, cmd);
304                 break;
305         }
306 }
307
308
309 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
310                 FuncStatus & flag) const
311 {
312         switch (cmd.action()) {
313
314         case LFUN_INSET_MODIFY: {
315                 string const first_arg = cmd.getArg(0);
316                 if (first_arg == "changetype") {
317                         string const type = cmd.getArg(1);
318                         flag.setOnOff(type == params_.type);
319                         flag.setEnabled(!params_.inner_box || type != "Framed");
320                         return true;
321                 }
322                 if (first_arg == "box") {
323                         flag.setEnabled(true);
324                         return true;
325                 }
326                 return InsetCollapsible::getStatus(cur, cmd, flag);
327         }
328
329         case LFUN_INSET_DIALOG_UPDATE:
330                 flag.setEnabled(true);
331                 return true;
332
333         default:
334                 return InsetCollapsible::getStatus(cur, cmd, flag);
335         }
336 }
337
338
339 const string defaultThick = "0.4pt";
340 const string defaultSep = "3pt";
341 const string defaultShadow = "4pt";
342
343 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
344 {
345         BoxType btype = boxtranslator().find(params_.type);
346
347         string width_string = params_.width.asLatexString();
348         string thickness_string = params_.thickness.asLatexString();
349         string separation_string = params_.separation.asLatexString();
350         string shadowsize_string = params_.shadowsize.asLatexString();
351         bool stdwidth = false;
352         string const cprotect = hasCProtectContent(runparams.moving_arg) ? "\\cprotect" : string();
353         // in general the overall width of some decorated boxes is wider thean the inner box
354         // we could therefore calculate the real width for all sizes so that if the user wants
355         // e.g. 0.1\columnwidth or 2cm he gets exactly this size
356         // however this makes problems when importing TeX code
357         // therefore only recalculate for the most common case that the box should not protrude
358         // the page margins
359         if (params_.inner_box
360                 && ((width_string.find("1\\columnwidth") != string::npos
361                         || width_string.find("1\\textwidth") != string::npos)
362                         || width_string.find("1\\paperwidth") != string::npos
363                         || width_string.find("1\\linewidth") != string::npos)) {
364                 stdwidth = true;
365                 switch (btype) {
366                 case Frameless:
367                         break;
368                 case Framed:
369                         width_string += " - 2\\FrameSep - 2\\FrameRule";
370                         break;
371                 case Boxed:
372                         width_string += " - 2\\fboxsep - 2\\fboxrule";
373                         break;
374                 case Shaded:
375                         break;
376                 case ovalbox:
377                         width_string += " - 2\\fboxsep - 0.8pt";
378                         break;
379                 case Ovalbox:
380                         width_string += " - 2\\fboxsep - 1.6pt";
381                         break;
382                 case Shadowbox:
383                         width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
384                         break;
385                 case Doublebox:
386                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
387                         break;
388                 }
389         }
390
391         os << safebreakln;
392         if (runparams.lastid != -1)
393                 os.texrow().start(runparams.lastid, runparams.lastpos);
394
395         // adapt column/text width correctly also if paragraphs indented
396         if (stdwidth && !(buffer().params().paragraph_separation))
397                 os << "\\noindent";
398
399         bool needendgroup = false;
400         switch (btype) {
401         case Frameless:
402                 break;
403         case Framed:
404                 if (thickness_string != defaultThick) {
405                         os << "{\\FrameRule " << from_ascii(thickness_string);
406                         if (separation_string != defaultSep)
407                                 os << "\\FrameSep " << from_ascii(separation_string);
408                 }
409                 if (separation_string != defaultSep && thickness_string == defaultThick)
410                         os << "{\\FrameSep " << from_ascii(separation_string);
411
412                 os << "\\begin{framed}%\n";
413                 break;
414         case Boxed:
415                 if (thickness_string != defaultThick) {
416                         os << "{\\fboxrule " << from_ascii(thickness_string);
417                         if (separation_string != defaultSep)
418                                 os << "\\fboxsep " << from_ascii(separation_string);
419                 }
420                 if (separation_string != defaultSep && thickness_string == defaultThick)
421                         os << "{\\fboxsep " << from_ascii(separation_string);
422                 if (!params_.inner_box && !width_string.empty()) {
423                         if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
424                                 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
425                                 os << "\\makebox";
426                         } else
427                                 os << "\\framebox";
428                         // Special widths, see usrguide sec. 3.5
429                         // FIXME UNICODE
430                         if (params_.special != "none") {
431                                 os << "[" << params_.width.value()
432                                    << '\\' << from_utf8(params_.special)
433                                    << ']';
434                         } else
435                                 os << '[' << from_ascii(width_string)
436                                    << ']';
437                         // default horizontal alignment is 'c'
438                         if (params_.hor_pos != 'c')
439                                 os << "[" << params_.hor_pos << "]";
440                 } else {
441                         if (params_.framecolor != "black" || params_.backgroundcolor != "none")
442                                 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
443                         else {
444                                 if (!cprotect.empty() && contains(runparams.active_chars, '^')) {
445                                         // cprotect relies on ^ being on catcode 7
446                                         os << "\\begingroup\\catcode`\\^=7";
447                                         needendgroup = true;
448                                 }
449                                 os << cprotect << "\\fbox";
450                         }
451                 }
452                 os << "{";
453                 break;
454         case ovalbox:
455                 if (!separation_string.empty() && separation_string != defaultSep)
456                         os << "{\\fboxsep " << from_ascii(separation_string);
457                 os << "\\ovalbox{";
458                 break;
459         case Ovalbox:
460                 if (!separation_string.empty() && separation_string != defaultSep)
461                         os << "{\\fboxsep " << from_ascii(separation_string);
462                 os << "\\Ovalbox{";
463                 break;
464         case Shadowbox:
465                 if (thickness_string != defaultThick) {
466                         os << "{\\fboxrule " << from_ascii(thickness_string);
467                         if (separation_string != defaultSep) {
468                                 os << "\\fboxsep " << from_ascii(separation_string);
469                                 if (shadowsize_string != defaultShadow)
470                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
471                         }
472                         if (shadowsize_string != defaultShadow  && separation_string == defaultSep)
473                                 os << "\\shadowsize " << from_ascii(shadowsize_string);
474                 }
475                 if (separation_string != defaultSep && thickness_string == defaultThick) {
476                                 os << "{\\fboxsep " << from_ascii(separation_string);
477                                 if (shadowsize_string != defaultShadow)
478                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
479                 }
480                 if (shadowsize_string != defaultShadow
481                                 && separation_string == defaultSep
482                                 && thickness_string == defaultThick)
483                                 os << "{\\shadowsize " << from_ascii(shadowsize_string);
484                 os << "\\shadowbox{";
485                 break;
486         case Shaded:
487                 // must be set later because e.g. the width settings only work when
488                 // it is inside a minipage or parbox
489                 break;
490         case Doublebox:
491                 if (thickness_string != defaultThick) {
492                         os << "{\\fboxrule " << from_ascii(thickness_string);
493                         if (separation_string != defaultSep)
494                                 os << "\\fboxsep " << from_ascii(separation_string);
495                 }
496                 if (separation_string != defaultSep && thickness_string == defaultThick)
497                         os << "{\\fboxsep " << from_ascii(separation_string);
498                 os << "\\doublebox{";
499                 break;
500         }
501
502         if (params_.inner_box) {
503                 if (params_.use_parbox) {
504                         if (params_.backgroundcolor != "none" && btype == Frameless)
505                                 os << "\\colorbox{" << params_.backgroundcolor << "}{";
506                         os << "\\parbox";
507                 } else if (params_.use_makebox) {
508                         if (!width_string.empty()) {
509                                 if (params_.backgroundcolor != "none")
510                                         os << "\\colorbox{" << params_.backgroundcolor << "}{";
511                                 os << "\\makebox";
512                                 // FIXME UNICODE
513                                 // output the width and horizontal position
514                                 if (params_.special != "none") {
515                                         os << "[" << params_.width.value()
516                                            << '\\' << from_utf8(params_.special)
517                                            << ']';
518                                 } else
519                                         os << '[' << from_ascii(width_string)
520                                            << ']';
521                                 if (params_.hor_pos != 'c')
522                                         os << "[" << params_.hor_pos << "]";
523                         } else {
524                                 if (params_.backgroundcolor != "none")
525                                         os << "\\colorbox{" << params_.backgroundcolor << "}";
526                                 else
527                                         os << "\\mbox";
528                         }
529                         os << "{";
530                 }
531                 else {
532                         if (params_.backgroundcolor != "none" && btype == Frameless)
533                                 os << "\\colorbox{" << params_.backgroundcolor << "}{";
534                         os << "\\begin{minipage}";
535                 }
536
537                 // output parameters for parbox and minipage
538                 if (!params_.use_makebox) {
539                         os << "[" << params_.pos << "]";
540                         if (params_.height_special == "none") {
541                                 // FIXME UNICODE
542                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
543                         } else {
544                                 // Special heights
545                                 // set no optional argument when the value is the default "1\height"
546                                 // (special units like \height are handled as "in")
547                                 // but when the user has chosen a non-default inner_pos, the height
548                                 // must be given: \minipage[pos][height][inner-pos]{width}
549                                 if ((params_.height != Length("1in") ||
550                                         params_.height_special != "totalheight") ||
551                                         params_.inner_pos != params_.pos) {
552                                                 // FIXME UNICODE
553                                                 os << "[" << params_.height.value()
554                                                         << "\\" << from_utf8(params_.height_special) << "]";
555                                 }
556                         }
557                         if (params_.inner_pos != params_.pos)
558                                 os << "[" << params_.inner_pos << "]";
559                         // FIXME UNICODE
560                         os << '{' << from_ascii(width_string) << '}';
561                         if (params_.use_parbox)
562                                 os << "{";
563                 }
564
565                 os << "%\n";
566         } // end if inner_box
567
568         if (btype == Shaded) {
569                 os << "\\begin{shaded}%\n";
570         }
571
572         InsetText::latex(os, runparams);
573
574         if (btype == Shaded)
575                 os << "\\end{shaded}";
576
577         if (params_.inner_box) {
578                 if (params_.use_parbox || params_.use_makebox)
579                         os << "%\n}";
580                 else
581                         os << "%\n\\end{minipage}";
582                 if (params_.backgroundcolor != "none" && btype == Frameless
583                         && !(params_.use_makebox && width_string.empty()))
584                         os << "}";
585         }
586
587         switch (btype) {
588         case Frameless:
589                 break;
590         case Framed:
591                 os << "\\end{framed}";
592                 if (separation_string != defaultSep     || thickness_string != defaultThick)
593                         os << "}";
594                 break;
595         case Boxed:
596                 os << "}";
597                 if (!params_.inner_box && !width_string.empty()
598                         && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
599                         os << "}";
600                 if (separation_string != defaultSep || thickness_string != defaultThick)
601                         os << "}";
602                 if (needendgroup)
603                         os << "\\endgroup";
604                 break;
605         case ovalbox:
606                 os << "}";
607                 if (separation_string != defaultSep)
608                         os << "}";
609                 break;
610         case Ovalbox:
611                 os << "}";
612                 if (separation_string != defaultSep)
613                         os << "}";
614                 break;
615         case Doublebox:
616                 os << "}";
617                 if (separation_string != defaultSep || thickness_string != defaultThick)
618                         os << "}";
619                 break;
620         case Shadowbox:
621                 os << "}";
622                 if (separation_string != defaultSep
623                         || thickness_string != defaultThick
624                         || shadowsize_string != defaultShadow)
625                         os << "}";
626                 break;
627         case Shaded:
628                 // already done
629                 break;
630         }
631 }
632
633
634 int InsetBox::plaintext(odocstringstream & os,
635        OutputParams const & runparams, size_t max_length) const
636 {
637         BoxType const btype = boxtranslator().find(params_.type);
638
639         switch (btype) {
640                 case Frameless:
641                         break;
642                 case Framed:
643                 case Boxed:
644                         os << "[\n";
645                         break;
646                 case ovalbox:
647                         os << "(\n";
648                         break;
649                 case Ovalbox:
650                         os << "((\n";
651                         break;
652                 case Shadowbox:
653                 case Shaded:
654                         os << "[/\n";
655                         break;
656                 case Doublebox:
657                         os << "[[\n";
658                         break;
659         }
660
661         InsetText::plaintext(os, runparams, max_length);
662
663         int len = 0;
664         switch (btype) {
665                 case Frameless:
666                         os << "\n";
667                         break;
668                 case Framed:
669                 case Boxed:
670                         os << "\n]";
671                         len = 1;
672                         break;
673                 case ovalbox:
674                         os << "\n)";
675                         len = 1;
676                         break;
677                 case Ovalbox:
678                         os << "\n))";
679                         len = 2;
680                         break;
681                 case Shadowbox:
682                 case Shaded:
683                         os << "\n/]";
684                         len = 2;
685                         break;
686                 case Doublebox:
687                         os << "\n]]";
688                         len = 2;
689                         break;
690         }
691
692         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
693 }
694
695
696 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
697 {
698         return InsetText::docbook(os, runparams);
699 }
700
701
702 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
703 {
704         // construct attributes
705         string attrs = "class='" + params_.type + "'";
706         string style;
707         if (!params_.width.empty()) {
708                 string w = params_.width.asHTMLString();
709                 if (w != "100%")
710                         style += ("width: " + params_.width.asHTMLString() + "; ");
711         }
712         // The special heights don't really mean anything for us.
713         if (!params_.height.empty() && params_.height_special == "none")
714                 style += ("height: " + params_.height.asHTMLString() + "; ");
715         if (!style.empty())
716                 attrs += " style='" + style + "'";
717
718         xs << html::StartTag("div", attrs);
719         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
720         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
721         xs << html::EndTag("div");
722         xs << defer;
723         return docstring();
724 }
725
726
727 void InsetBox::validate(LaTeXFeatures & features) const
728 {
729         BoxType btype = boxtranslator().find(params_.type);
730         switch (btype) {
731         case Frameless:
732                 if (params_.backgroundcolor != "none")
733                         features.require("xcolor");
734                 break;
735         case Framed:
736                 features.require("calc");
737                 features.require("framed");
738                 break;
739         case Boxed:
740                 features.require("calc");
741                 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
742                         features.require("xcolor");
743                 break;
744         case ovalbox:
745         case Ovalbox:
746         case Shadowbox:
747         case Doublebox:
748                 features.require("calc");
749                 features.require("fancybox");
750                 break;
751         case Shaded:
752                 features.require("color");
753                 features.require("framed");
754                 break;
755         }
756         InsetCollapsible::validate(features);
757 }
758
759
760 string InsetBox::contextMenuName() const
761 {
762         return "context-box";
763 }
764
765
766 string InsetBox::params2string(InsetBoxParams const & params)
767 {
768         ostringstream data;
769         data << "box" << ' ';
770         params.write(data);
771         return data.str();
772 }
773
774
775 void InsetBox::string2params(string const & in, InsetBoxParams & params)
776 {
777         if (in.empty())
778                 return;
779
780         istringstream data(in);
781         Lexer lex;
782         lex.setStream(data);
783
784         string name;
785         lex >> name;
786         if (!lex || name != "box") {
787                 LYXERR0("InsetBox::string2params(" << in << ")\n"
788                                           "Expected arg 1 to be \"box\"\n");
789                 return;
790         }
791
792         // This is part of the inset proper that is usually swallowed
793         // by Text::readInset
794         string id;
795         lex >> id;
796         if (!lex || id != "Box") {
797                 LYXERR0("InsetBox::string2params(" << in << ")\n"
798                                           "Expected arg 2 to be \"Box\"\n");
799         }
800
801         params = InsetBoxParams(string());
802         params.read(lex);
803 }
804
805
806 /////////////////////////////////////////////////////////////////////////
807 //
808 // InsetBoxParams
809 //
810 /////////////////////////////////////////////////////////////////////////
811
812 InsetBoxParams::InsetBoxParams(string const & label)
813         : type(label),
814           use_parbox(false),
815           use_makebox(false),
816           inner_box(true),
817           width(Length("100col%")),
818           special("none"),
819           pos('t'),
820           hor_pos('c'),
821           inner_pos('t'),
822           height(Length("1in")),
823           height_special("totalheight"), // default is 1\\totalheight
824           thickness(Length(defaultThick)),
825           separation(Length(defaultSep)),
826           shadowsize(Length(defaultShadow)),
827           framecolor("black"),
828           backgroundcolor("none")
829 {}
830
831
832 void InsetBoxParams::write(ostream & os) const
833 {
834         os << "Box " << type << "\n";
835         os << "position \"" << pos << "\"\n";
836         os << "hor_pos \"" << hor_pos << "\"\n";
837         os << "has_inner_box " << inner_box << "\n";
838         os << "inner_pos \"" << inner_pos << "\"\n";
839         os << "use_parbox " << use_parbox << "\n";
840         os << "use_makebox " << use_makebox << "\n";
841         os << "width \"" << width.asString() << "\"\n";
842         os << "special \"" << special << "\"\n";
843         os << "height \"" << height.asString() << "\"\n";
844         os << "height_special \"" << height_special << "\"\n";
845         os << "thickness \"" << thickness.asString() << "\"\n";
846         os << "separation \"" << separation.asString() << "\"\n";
847         os << "shadowsize \"" << shadowsize.asString() << "\"\n";
848         os << "framecolor \"" << framecolor << "\"\n";
849         os << "backgroundcolor \"" << backgroundcolor << "\"\n";
850 }
851
852
853 void InsetBoxParams::read(Lexer & lex)
854 {
855         lex.setContext("InsetBoxParams::read");
856         lex >> type;
857         lex >> "position" >> pos;
858         lex >> "hor_pos" >> hor_pos;
859         lex >> "has_inner_box" >> inner_box;
860         if (type == "Framed")
861                 inner_box = false;
862         lex >> "inner_pos" >> inner_pos;
863         lex >> "use_parbox" >> use_parbox;
864         lex >> "use_makebox" >> use_makebox;
865         lex >> "width" >> width;
866         lex >> "special" >> special;
867         lex >> "height" >> height;
868         lex >> "height_special" >> height_special;
869         lex >> "thickness" >> thickness;
870         lex >> "separation" >> separation;
871         lex >> "shadowsize" >> shadowsize;
872         lex >> "framecolor" >> framecolor;
873         lex >> "backgroundcolor" >> backgroundcolor;
874 }
875
876
877 } // namespace lyx