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