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