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