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