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