]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Wrap rotated cells with linebreaks in varwidth
[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         switch (btype) {
400         case Frameless:
401                 break;
402         case Framed:
403                 if (thickness_string != defaultThick) {
404                         os << "{\\FrameRule " << from_ascii(thickness_string);
405                         if (separation_string != defaultSep)
406                                 os << "\\FrameSep " << from_ascii(separation_string);
407                 }
408                 if (separation_string != defaultSep && thickness_string == defaultThick)
409                         os << "{\\FrameSep " << from_ascii(separation_string);
410
411                 os << "\\begin{framed}%\n";
412                 break;
413         case Boxed:
414                 if (thickness_string != defaultThick) {
415                         os << "{\\fboxrule " << from_ascii(thickness_string);
416                         if (separation_string != defaultSep)
417                                 os << "\\fboxsep " << from_ascii(separation_string);
418                 }
419                 if (separation_string != defaultSep && thickness_string == defaultThick)
420                         os << "{\\fboxsep " << from_ascii(separation_string);
421                 if (!params_.inner_box && !width_string.empty()) {
422                         if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
423                                 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
424                                 os << "\\makebox";
425                         } else
426                                 os << "\\framebox";
427                         // Special widths, see usrguide sec. 3.5
428                         // FIXME UNICODE
429                         if (params_.special != "none") {
430                                 os << "[" << params_.width.value()
431                                    << '\\' << from_utf8(params_.special)
432                                    << ']';
433                         } else
434                                 os << '[' << from_ascii(width_string)
435                                    << ']';
436                         // default horizontal alignment is 'c'
437                         if (params_.hor_pos != 'c')
438                                 os << "[" << params_.hor_pos << "]";
439                 } else {
440                         if (params_.framecolor != "black" || params_.backgroundcolor != "none")
441                                 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
442                         else
443                                 os << cprotect << "\\fbox";
444                 }
445                 os << "{";
446                 break;
447         case ovalbox:
448                 if (!separation_string.empty() && separation_string != defaultSep)
449                         os << "{\\fboxsep " << from_ascii(separation_string);
450                 os << "\\ovalbox{";
451                 break;
452         case Ovalbox:
453                 if (!separation_string.empty() && separation_string != defaultSep)
454                         os << "{\\fboxsep " << from_ascii(separation_string);
455                 os << "\\Ovalbox{";
456                 break;
457         case Shadowbox:
458                 if (thickness_string != defaultThick) {
459                         os << "{\\fboxrule " << from_ascii(thickness_string);
460                         if (separation_string != defaultSep) {
461                                 os << "\\fboxsep " << from_ascii(separation_string);
462                                 if (shadowsize_string != defaultShadow)
463                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
464                         }
465                         if (shadowsize_string != defaultShadow  && separation_string == defaultSep)
466                                 os << "\\shadowsize " << from_ascii(shadowsize_string);
467                 }
468                 if (separation_string != defaultSep && thickness_string == defaultThick) {
469                                 os << "{\\fboxsep " << from_ascii(separation_string);
470                                 if (shadowsize_string != defaultShadow)
471                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
472                 }
473                 if (shadowsize_string != defaultShadow
474                                 && separation_string == defaultSep
475                                 && thickness_string == defaultThick)
476                                 os << "{\\shadowsize " << from_ascii(shadowsize_string);
477                 os << "\\shadowbox{";
478                 break;
479         case Shaded:
480                 // must be set later because e.g. the width settings only work when
481                 // it is inside a minipage or parbox
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 << "\\colorbox{" << params_.backgroundcolor << "}{";
499                         os << "\\parbox";
500                 } else if (params_.use_makebox) {
501                         if (!width_string.empty()) {
502                                 if (params_.backgroundcolor != "none")
503                                         os << "\\colorbox{" << params_.backgroundcolor << "}{";
504                                 os << "\\makebox";
505                                 // FIXME UNICODE
506                                 // output the width and horizontal position
507                                 if (params_.special != "none") {
508                                         os << "[" << params_.width.value()
509                                            << '\\' << from_utf8(params_.special)
510                                            << ']';
511                                 } else
512                                         os << '[' << from_ascii(width_string)
513                                            << ']';
514                                 if (params_.hor_pos != 'c')
515                                         os << "[" << params_.hor_pos << "]";
516                         } else {
517                                 if (params_.backgroundcolor != "none")
518                                         os << "\\colorbox{" << params_.backgroundcolor << "}";
519                                 else
520                                         os << "\\mbox";
521                         }
522                         os << "{";
523                 }
524                 else {
525                         if (params_.backgroundcolor != "none" && btype == Frameless)
526                                 os << "\\colorbox{" << params_.backgroundcolor << "}{";
527                         os << "\\begin{minipage}";
528                 }
529
530                 // output parameters for parbox and minipage
531                 if (!params_.use_makebox) {
532                         os << "[" << params_.pos << "]";
533                         if (params_.height_special == "none") {
534                                 // FIXME UNICODE
535                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
536                         } else {
537                                 // Special heights
538                                 // set no optional argument when the value is the default "1\height"
539                                 // (special units like \height are handled as "in")
540                                 // but when the user has chosen a non-default inner_pos, the height
541                                 // must be given: \minipage[pos][height][inner-pos]{width}
542                                 if ((params_.height != Length("1in") ||
543                                         params_.height_special != "totalheight") ||
544                                         params_.inner_pos != params_.pos) {
545                                                 // FIXME UNICODE
546                                                 os << "[" << params_.height.value()
547                                                         << "\\" << from_utf8(params_.height_special) << "]";
548                                 }
549                         }
550                         if (params_.inner_pos != params_.pos)
551                                 os << "[" << params_.inner_pos << "]";
552                         // FIXME UNICODE
553                         os << '{' << from_ascii(width_string) << '}';
554                         if (params_.use_parbox)
555                                 os << "{";
556                 }
557
558                 os << "%\n";
559         } // end if inner_box
560
561         if (btype == Shaded) {
562                 os << "\\begin{shaded}%\n";
563         }
564
565         InsetText::latex(os, runparams);
566
567         if (btype == Shaded)
568                 os << "\\end{shaded}";
569
570         if (params_.inner_box) {
571                 if (params_.use_parbox || params_.use_makebox)
572                         os << "%\n}";
573                 else
574                         os << "%\n\\end{minipage}";
575                 if (params_.backgroundcolor != "none" && btype == Frameless
576                         && !(params_.use_makebox && width_string.empty()))
577                         os << "}";
578         }
579
580         switch (btype) {
581         case Frameless:
582                 break;
583         case Framed:
584                 os << "\\end{framed}";
585                 if (separation_string != defaultSep     || thickness_string != defaultThick)
586                         os << "}";
587                 break;
588         case Boxed:
589                 os << "}";
590                 if (!params_.inner_box && !width_string.empty()
591                         && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
592                         os << "}";
593                 if (separation_string != defaultSep     || thickness_string != defaultThick)
594                         os << "}";
595                 break;
596         case ovalbox:
597                 os << "}";
598                 if (separation_string != defaultSep)
599                         os << "}";
600                 break;
601         case Ovalbox:
602                 os << "}";
603                 if (separation_string != defaultSep)
604                         os << "}";
605                 break;
606         case Doublebox:
607                 os << "}";
608                 if (separation_string != defaultSep || thickness_string != defaultThick)
609                         os << "}";
610                 break;
611         case Shadowbox:
612                 os << "}";
613                 if (separation_string != defaultSep
614                         || thickness_string != defaultThick
615                         || shadowsize_string != defaultShadow)
616                         os << "}";
617                 break;
618         case Shaded:
619                 // already done
620                 break;
621         }
622 }
623
624
625 int InsetBox::plaintext(odocstringstream & os,
626        OutputParams const & runparams, size_t max_length) const
627 {
628         BoxType const btype = boxtranslator().find(params_.type);
629
630         switch (btype) {
631                 case Frameless:
632                         break;
633                 case Framed:
634                 case Boxed:
635                         os << "[\n";
636                         break;
637                 case ovalbox:
638                         os << "(\n";
639                         break;
640                 case Ovalbox:
641                         os << "((\n";
642                         break;
643                 case Shadowbox:
644                 case Shaded:
645                         os << "[/\n";
646                         break;
647                 case Doublebox:
648                         os << "[[\n";
649                         break;
650         }
651
652         InsetText::plaintext(os, runparams, max_length);
653
654         int len = 0;
655         switch (btype) {
656                 case Frameless:
657                         os << "\n";
658                         break;
659                 case Framed:
660                 case Boxed:
661                         os << "\n]";
662                         len = 1;
663                         break;
664                 case ovalbox:
665                         os << "\n)";
666                         len = 1;
667                         break;
668                 case Ovalbox:
669                         os << "\n))";
670                         len = 2;
671                         break;
672                 case Shadowbox:
673                 case Shaded:
674                         os << "\n/]";
675                         len = 2;
676                         break;
677                 case Doublebox:
678                         os << "\n]]";
679                         len = 2;
680                         break;
681         }
682
683         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
684 }
685
686
687 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
688 {
689         return InsetText::docbook(os, runparams);
690 }
691
692
693 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
694 {
695         // construct attributes
696         string attrs = "class='" + params_.type + "'";
697         string style;
698         if (!params_.width.empty()) {
699                 string w = params_.width.asHTMLString();
700                 if (w != "100%")
701                         style += ("width: " + params_.width.asHTMLString() + "; ");
702         }
703         // The special heights don't really mean anything for us.
704         if (!params_.height.empty() && params_.height_special == "none")
705                 style += ("height: " + params_.height.asHTMLString() + "; ");
706         if (!style.empty())
707                 attrs += " style='" + style + "'";
708
709         xs << html::StartTag("div", attrs);
710         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
711         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
712         xs << html::EndTag("div");
713         xs << defer;
714         return docstring();
715 }
716
717
718 void InsetBox::validate(LaTeXFeatures & features) const
719 {
720         BoxType btype = boxtranslator().find(params_.type);
721         switch (btype) {
722         case Frameless:
723                 if (params_.backgroundcolor != "none")
724                         features.require("xcolor");
725                 break;
726         case Framed:
727                 features.require("calc");
728                 features.require("framed");
729                 break;
730         case Boxed:
731                 features.require("calc");
732                 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
733                         features.require("xcolor");
734                 break;
735         case ovalbox:
736         case Ovalbox:
737         case Shadowbox:
738         case Doublebox:
739                 features.require("calc");
740                 features.require("fancybox");
741                 break;
742         case Shaded:
743                 features.require("color");
744                 features.require("framed");
745                 break;
746         }
747         InsetCollapsible::validate(features);
748 }
749
750
751 string InsetBox::contextMenuName() const
752 {
753         return "context-box";
754 }
755
756
757 string InsetBox::params2string(InsetBoxParams const & params)
758 {
759         ostringstream data;
760         data << "box" << ' ';
761         params.write(data);
762         return data.str();
763 }
764
765
766 void InsetBox::string2params(string const & in, InsetBoxParams & params)
767 {
768         if (in.empty())
769                 return;
770
771         istringstream data(in);
772         Lexer lex;
773         lex.setStream(data);
774
775         string name;
776         lex >> name;
777         if (!lex || name != "box") {
778                 LYXERR0("InsetBox::string2params(" << in << ")\n"
779                                           "Expected arg 1 to be \"box\"\n");
780                 return;
781         }
782
783         // This is part of the inset proper that is usually swallowed
784         // by Text::readInset
785         string id;
786         lex >> id;
787         if (!lex || id != "Box") {
788                 LYXERR0("InsetBox::string2params(" << in << ")\n"
789                                           "Expected arg 2 to be \"Box\"\n");
790         }
791
792         params = InsetBoxParams(string());
793         params.read(lex);
794 }
795
796
797 /////////////////////////////////////////////////////////////////////////
798 //
799 // InsetBoxParams
800 //
801 /////////////////////////////////////////////////////////////////////////
802
803 InsetBoxParams::InsetBoxParams(string const & label)
804         : type(label),
805           use_parbox(false),
806           use_makebox(false),
807           inner_box(true),
808           width(Length("100col%")),
809           special("none"),
810           pos('t'),
811           hor_pos('c'),
812           inner_pos('t'),
813           height(Length("1in")),
814           height_special("totalheight"), // default is 1\\totalheight
815           thickness(Length(defaultThick)),
816           separation(Length(defaultSep)),
817           shadowsize(Length(defaultShadow)),
818           framecolor("black"),
819           backgroundcolor("none")
820 {}
821
822
823 void InsetBoxParams::write(ostream & os) const
824 {
825         os << "Box " << type << "\n";
826         os << "position \"" << pos << "\"\n";
827         os << "hor_pos \"" << hor_pos << "\"\n";
828         os << "has_inner_box " << inner_box << "\n";
829         os << "inner_pos \"" << inner_pos << "\"\n";
830         os << "use_parbox " << use_parbox << "\n";
831         os << "use_makebox " << use_makebox << "\n";
832         os << "width \"" << width.asString() << "\"\n";
833         os << "special \"" << special << "\"\n";
834         os << "height \"" << height.asString() << "\"\n";
835         os << "height_special \"" << height_special << "\"\n";
836         os << "thickness \"" << thickness.asString() << "\"\n";
837         os << "separation \"" << separation.asString() << "\"\n";
838         os << "shadowsize \"" << shadowsize.asString() << "\"\n";
839         os << "framecolor \"" << framecolor << "\"\n";
840         os << "backgroundcolor \"" << backgroundcolor << "\"\n";
841 }
842
843
844 void InsetBoxParams::read(Lexer & lex)
845 {
846         lex.setContext("InsetBoxParams::read");
847         lex >> type;
848         lex >> "position" >> pos;
849         lex >> "hor_pos" >> hor_pos;
850         lex >> "has_inner_box" >> inner_box;
851         if (type == "Framed")
852                 inner_box = false;
853         lex >> "inner_pos" >> inner_pos;
854         lex >> "use_parbox" >> use_parbox;
855         lex >> "use_makebox" >> use_makebox;
856         lex >> "width" >> width;
857         lex >> "special" >> special;
858         lex >> "height" >> height;
859         lex >> "height_special" >> height_special;
860         lex >> "thickness" >> thickness;
861         lex >> "separation" >> separation;
862         lex >> "shadowsize" >> shadowsize;
863         lex >> "framecolor" >> framecolor;
864         lex >> "backgroundcolor" >> backgroundcolor;
865 }
866
867
868 } // namespace lyx