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