]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Amend 6c3447c8: FindAdv: sometimes a space is added on some math symbols
[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 "MetricsInfo.h"
28 #include "output_docbook.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/FileName.h"
37 #include "support/gettext.h"
38 #include "support/Lexer.h"
39 #include "support/lstrings.h"
40 #include "support/Translator.h"
41
42 #include "frontends/Application.h"
43
44 #include <sstream>
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace lyx {
50
51 namespace {
52
53 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
54 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
55
56 BoxTranslator initBoxtranslator()
57 {
58         BoxTranslator translator("Boxed", InsetBox::Boxed);
59         translator.addPair("Frameless", InsetBox::Frameless);
60         translator.addPair("Framed", InsetBox::Framed);
61         translator.addPair("ovalbox", InsetBox::ovalbox);
62         translator.addPair("Ovalbox", InsetBox::Ovalbox);
63         translator.addPair("Shadowbox", InsetBox::Shadowbox);
64         translator.addPair("Shaded", InsetBox::Shaded);
65         translator.addPair("Doublebox",InsetBox::Doublebox);
66         return translator;
67 }
68
69
70 BoxTranslatorLoc initBoxtranslatorLoc()
71 {
72         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
73         translator.addPair(_("frameless"), InsetBox::Frameless);
74         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
75         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
76         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
77         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
78         translator.addPair(_("shaded background"), InsetBox::Shaded);
79         translator.addPair(_("double frame"), InsetBox::Doublebox);
80         return translator;
81 }
82
83
84 BoxTranslator const & boxtranslator()
85 {
86         static BoxTranslator const translator = initBoxtranslator();
87         return translator;
88 }
89
90
91 BoxTranslatorLoc const & boxtranslator_loc()
92 {
93         static BoxTranslatorLoc const translator = initBoxtranslatorLoc();
94         return translator;
95 }
96
97 } // namespace
98
99
100 /////////////////////////////////////////////////////////////////////////
101 //
102 // InsetBox
103 //
104 /////////////////////////////////////////////////////////////////////////
105
106 InsetBox::InsetBox(Buffer * buffer, string const & label)
107         : InsetCollapsible(buffer), params_(label)
108 {}
109
110
111 docstring InsetBox::layoutName() const
112 {
113         // FIXME: UNICODE
114         return from_ascii("Box:" + params_.type);
115 }
116
117
118 void InsetBox::write(ostream & os) const
119 {
120         params_.write(os);
121         InsetCollapsible::write(os);
122 }
123
124
125 void InsetBox::read(Lexer & lex)
126 {
127         params_.read(lex);
128         InsetCollapsible::read(lex);
129 }
130
131
132 void InsetBox::setButtonLabel()
133 {
134         BoxType const btype = boxtranslator().find(params_.type);
135
136         docstring const type = _("Box");
137
138         docstring inner;
139         if (params_.inner_box) {
140                 if (params_.use_parbox)
141                         inner = _("Parbox");
142                 else if (params_.use_makebox)
143                         inner = _("Makebox");
144                 else
145                         inner = _("Minipage");
146         }
147
148         docstring frame;
149         if (btype != Frameless)
150                 frame = boxtranslator_loc().find(btype);
151
152         docstring label;
153         if (inner.empty() && frame.empty())
154                 label = type;
155         else if (inner.empty())
156                 label = bformat(_("%1$s (%2$s)"),
157                         type, frame);
158         else if (frame.empty())
159                 label = bformat(_("%1$s (%2$s)"),
160                         type, inner);
161         else
162                 label = bformat(_("%1$s (%2$s, %3$s)"),
163                         type, inner, frame);
164         setLabel(label);
165
166         // set the frame color for the inset if the type is Boxed
167         if (btype == Boxed)
168                 setFrameColor(lcolor.getFromLaTeXName(getFrameColor(true)));
169         else
170                 setFrameColor(Color_collapsibleframe);
171 }
172
173
174 bool InsetBox::hasFixedWidth() const
175 {
176         return !params_.width.empty() && params_.special == "none";
177 }
178
179
180 bool InsetBox::allowMultiPar() const
181 {
182         return (params_.inner_box && !params_.use_makebox)
183                 || params_.type == "Shaded" || params_.type == "Framed";
184 }
185
186
187 void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
188 {
189         // back up textwidth.
190         int textwidth_backup = mi.base.textwidth;
191         if (hasFixedWidth())
192                 mi.base.textwidth = mi.base.inPixels(params_.width);
193         InsetCollapsible::metrics(mi, dim);
194         // restore textwidth.
195         mi.base.textwidth = textwidth_backup;
196 }
197
198
199 bool InsetBox::forcePlainLayout(idx_type) const
200 {
201         return (!params_.inner_box || params_.use_makebox)
202                 && params_.type != "Shaded" && params_.type != "Framed";
203 }
204
205
206 bool InsetBox::needsCProtection(bool const maintext, bool const fragile) const
207 {
208         // We need to cprotect boxes that use minipages as inner box
209         // in fragile context
210         if (fragile && params_.inner_box && !params_.use_parbox && !params_.use_makebox)
211                 return true;
212
213         return InsetText::needsCProtection(maintext, fragile);
214 }
215
216
217 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
218 {
219         // we only support background color for 3 types
220         if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
221                 return getLayout().bgcolor();
222
223         if (params_.type == "Shaded") {
224                 if (!buffer().params().isboxbgcolor)
225                         return getLayout().bgcolor();
226
227                 ColorCode c = lcolor.getFromLyXName("boxbgcolor@" + buffer().fileName().absFileName());
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()
359             && runparams.local_font && runparams.local_font->isRightToLeft()) {
360                 maybeBeginL = "\\beginL";
361                 maybeEndL = "\\endL";
362         }
363         // in general the overall width of some decorated boxes is wider thean the inner box
364         // we could therefore calculate the real width for all sizes so that if the user wants
365         // e.g. 0.1\columnwidth or 2cm he gets exactly this size
366         // however this makes problems when importing TeX code
367         // therefore only recalculate for the most common case that the box should not protrude
368         // the page margins
369         if (params_.inner_box
370                 && ((width_string.find("1\\columnwidth") != string::npos
371                         || width_string.find("1\\textwidth") != string::npos)
372                         || width_string.find("1\\paperwidth") != string::npos
373                         || width_string.find("1\\linewidth") != string::npos)) {
374                 stdwidth = true;
375                 switch (btype) {
376                 case Frameless:
377                         break;
378                 case Framed:
379                         width_string += " - 2\\FrameSep - 2\\FrameRule";
380                         break;
381                 case Boxed:
382                         width_string += " - 2\\fboxsep - 2\\fboxrule";
383                         break;
384                 case Shaded:
385                         break;
386                 case ovalbox:
387                         width_string += " - 2\\fboxsep - 0.8pt";
388                         break;
389                 case Ovalbox:
390                         width_string += " - 2\\fboxsep - 1.6pt";
391                         break;
392                 case Shadowbox:
393                         width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
394                         break;
395                 case Doublebox:
396                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
397                         break;
398                 }
399         }
400
401         os << safebreakln;
402         if (runparams.lastid != -1)
403                 os.texrow().start(runparams.lastid, runparams.lastpos);
404
405         // adapt column/text width correctly also if paragraphs indented
406         if (stdwidth && !(buffer().params().paragraph_separation))
407                 os << "\\noindent";
408
409         bool needendgroup = false;
410         switch (btype) {
411         case Frameless:
412                 break;
413         case Framed:
414                 if (thickness_string != defaultThick) {
415                         os << "{\\FrameRule " << from_ascii(thickness_string);
416                         if (separation_string != defaultSep)
417                                 os << "\\FrameSep " << from_ascii(separation_string);
418                 }
419                 if (separation_string != defaultSep && thickness_string == defaultThick)
420                         os << "{\\FrameSep " << from_ascii(separation_string);
421
422                 os << "\\begin{framed}%\n";
423                 break;
424         case Boxed:
425                 if (thickness_string != defaultThick) {
426                         os << "{\\fboxrule " << from_ascii(thickness_string);
427                         if (separation_string != defaultSep)
428                                 os << "\\fboxsep " << from_ascii(separation_string);
429                 }
430                 if (separation_string != defaultSep && thickness_string == defaultThick)
431                         os << "{\\fboxsep " << from_ascii(separation_string);
432                 if (!params_.inner_box && !width_string.empty()) {
433                         if (useFColorBox()) {
434                                 os << maybeBeginL
435                                    << "\\fcolorbox{" << getFrameColor()
436                                    << "}{" << getBackgroundColor()
437                                    << "}{" << "\\makebox";
438                                 needEndL = !maybeBeginL.empty();
439                         } else
440                                 os << "\\framebox";
441                         // Special widths, see usrguide sec. 3.5
442                         // FIXME UNICODE
443                         if (params_.special != "none") {
444                                 os << "[" << params_.width.value()
445                                    << '\\' << from_utf8(params_.special)
446                                    << ']';
447                         } else
448                                 os << '[' << from_ascii(width_string)
449                                    << ']';
450                         // default horizontal alignment is 'c'
451                         if (params_.hor_pos != 'c')
452                                 os << "[" << params_.hor_pos << "]";
453                 } else {
454                         if (useFColorBox()) {
455                                 os << maybeBeginL
456                                    << "\\fcolorbox{" << getFrameColor()
457                                    << "}{" << getBackgroundColor() << "}";
458                                 needEndL = !maybeBeginL.empty();
459                         } else {
460                                 if (!cprotect.empty() && contains(runparams.active_chars, '^')) {
461                                         // cprotect relies on ^ being on catcode 7
462                                         os << "\\begingroup\\catcode`\\^=7";
463                                         needendgroup = true;
464                                 }
465                                 os << cprotect << "\\fbox";
466                         }
467                 }
468                 os << "{";
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 Ovalbox:
476                 if (!separation_string.empty() && separation_string != defaultSep)
477                         os << "{\\fboxsep " << from_ascii(separation_string);
478                 os << "\\Ovalbox{";
479                 break;
480         case Shadowbox:
481                 if (thickness_string != defaultThick) {
482                         os << "{\\fboxrule " << from_ascii(thickness_string);
483                         if (separation_string != defaultSep) {
484                                 os << "\\fboxsep " << from_ascii(separation_string);
485                                 if (shadowsize_string != defaultShadow)
486                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
487                         }
488                         if (shadowsize_string != defaultShadow  && separation_string == defaultSep)
489                                 os << "\\shadowsize " << from_ascii(shadowsize_string);
490                 }
491                 if (separation_string != defaultSep && thickness_string == defaultThick) {
492                                 os << "{\\fboxsep " << from_ascii(separation_string);
493                                 if (shadowsize_string != defaultShadow)
494                                         os << "\\shadowsize " << from_ascii(shadowsize_string);
495                 }
496                 if (shadowsize_string != defaultShadow
497                                 && separation_string == defaultSep
498                                 && thickness_string == defaultThick)
499                                 os << "{\\shadowsize " << from_ascii(shadowsize_string);
500                 os << "\\shadowbox{";
501                 break;
502         case Shaded:
503                 // must be set later because e.g. the width settings only work when
504                 // it is inside a minipage or parbox
505                 os << maybeBeginL;
506                 needEndL = !maybeBeginL.empty();
507                 break;
508         case Doublebox:
509                 if (thickness_string != defaultThick) {
510                         os << "{\\fboxrule " << from_ascii(thickness_string);
511                         if (separation_string != defaultSep)
512                                 os << "\\fboxsep " << from_ascii(separation_string);
513                 }
514                 if (separation_string != defaultSep && thickness_string == defaultThick)
515                         os << "{\\fboxsep " << from_ascii(separation_string);
516                 os << "\\doublebox{";
517                 break;
518         }
519
520         if (params_.inner_box) {
521                 if (params_.use_parbox) {
522                         if (params_.backgroundcolor != "none" && btype == Frameless) {
523                                 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
524                                 needEndL = !maybeBeginL.empty();
525                         }
526                         os << "\\parbox";
527                 } else if (params_.use_makebox) {
528                         if (!width_string.empty()) {
529                                 if (params_.backgroundcolor != "none") {
530                                         os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
531                                         needEndL = !maybeBeginL.empty();
532                                 }
533                                 os << "\\makebox";
534                                 // FIXME UNICODE
535                                 // output the width and horizontal position
536                                 if (params_.special != "none") {
537                                         os << "[" << params_.width.value()
538                                            << '\\' << from_utf8(params_.special)
539                                            << ']';
540                                 } else
541                                         os << '[' << from_ascii(width_string)
542                                            << ']';
543                                 if (params_.hor_pos != 'c')
544                                         os << "[" << params_.hor_pos << "]";
545                         } else {
546                                 if (params_.backgroundcolor != "none") {
547                                         os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}";
548                                         needEndL = !maybeBeginL.empty();
549                                 }
550                                 else
551                                         os << "\\mbox";
552                         }
553                         os << "{";
554                 }
555                 else {
556                         if (params_.backgroundcolor != "none" && btype == Frameless) {
557                                 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
558                                 needEndL = !maybeBeginL.empty();
559                         }
560                         os << "\\begin{minipage}";
561                 }
562
563                 // output parameters for parbox and minipage
564                 if (!params_.use_makebox) {
565                         os << "[" << params_.pos << "]";
566                         if (params_.height_special == "none") {
567                                 // FIXME UNICODE
568                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
569                         } else {
570                                 // Special heights
571                                 // set no optional argument when the value is the default "1\height"
572                                 // (special units like \height are handled as "in")
573                                 // but when the user has chosen a non-default inner_pos, the height
574                                 // must be given: \minipage[pos][height][inner-pos]{width}
575                                 if ((params_.height != Length("1in") ||
576                                         params_.height_special != "totalheight") ||
577                                         params_.inner_pos != params_.pos) {
578                                                 // FIXME UNICODE
579                                                 os << "[" << params_.height.value()
580                                                         << "\\" << from_utf8(params_.height_special) << "]";
581                                 }
582                         }
583                         if (params_.inner_pos != params_.pos)
584                                 os << "[" << params_.inner_pos << "]";
585                         // FIXME UNICODE
586                         os << '{' << from_ascii(width_string) << '}';
587                         if (params_.use_parbox)
588                                 os << "{";
589                 }
590
591                 os << "%\n";
592         } // end if inner_box
593
594         if (btype == Shaded) {
595                 os << "\\begin{shaded}%\n";
596         }
597
598         InsetText::latex(os, runparams);
599
600         if (btype == Shaded)
601                 os << "\\end{shaded}";
602
603         if (params_.inner_box) {
604                 if (params_.use_parbox || params_.use_makebox)
605                         os << "%\n}";
606                 else
607                         os << "%\n\\end{minipage}";
608                 if (params_.backgroundcolor != "none" && btype == Frameless
609                         && !(params_.use_makebox && width_string.empty()))
610                         os << "}";
611         }
612
613         switch (btype) {
614         case Frameless:
615                 break;
616         case Framed:
617                 os << "\\end{framed}";
618                 if (separation_string != defaultSep || thickness_string != defaultThick)
619                         os << "}";
620                 break;
621         case Boxed:
622                 os << "}";
623                 if (!params_.inner_box && !width_string.empty() && useFColorBox())
624                         os << "}";
625                 if (separation_string != defaultSep || thickness_string != defaultThick)
626                         os << "}";
627                 if (needendgroup)
628                         os << "\\endgroup";
629                 break;
630         case ovalbox:
631                 os << "}";
632                 if (separation_string != defaultSep)
633                         os << "}";
634                 break;
635         case Ovalbox:
636                 os << "}";
637                 if (separation_string != defaultSep)
638                         os << "}";
639                 break;
640         case Doublebox:
641                 os << "}";
642                 if (separation_string != defaultSep || thickness_string != defaultThick)
643                         os << "}";
644                 break;
645         case Shadowbox:
646                 os << "}";
647                 if (separation_string != defaultSep
648                         || thickness_string != defaultThick
649                         || shadowsize_string != defaultShadow)
650                         os << "}";
651                 break;
652         case Shaded:
653                 // already done
654                 break;
655         }
656         if (needEndL)
657                 os << maybeEndL;
658 }
659
660
661 int InsetBox::plaintext(odocstringstream & os,
662        OutputParams const & runparams, size_t max_length) const
663 {
664         BoxType const btype = boxtranslator().find(params_.type);
665
666         switch (btype) {
667                 case Frameless:
668                         break;
669                 case Framed:
670                 case Boxed:
671                         os << "[\n";
672                         break;
673                 case ovalbox:
674                         os << "(\n";
675                         break;
676                 case Ovalbox:
677                         os << "((\n";
678                         break;
679                 case Shadowbox:
680                 case Shaded:
681                         os << "[/\n";
682                         break;
683                 case Doublebox:
684                         os << "[[\n";
685                         break;
686         }
687
688         InsetText::plaintext(os, runparams, max_length);
689
690         int len = 0;
691         switch (btype) {
692                 case Frameless:
693                         os << "\n";
694                         break;
695                 case Framed:
696                 case Boxed:
697                         os << "\n]";
698                         len = 1;
699                         break;
700                 case ovalbox:
701                         os << "\n)";
702                         len = 1;
703                         break;
704                 case Ovalbox:
705                         os << "\n))";
706                         len = 2;
707                         break;
708                 case Shadowbox:
709                 case Shaded:
710                         os << "\n/]";
711                         len = 2;
712                         break;
713                 case Doublebox:
714                         os << "\n]]";
715                         len = 2;
716                         break;
717         }
718
719         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
720 }
721
722
723 void InsetBox::docbook(XMLStream & xs, OutputParams const & runparams) const
724 {
725         // There really should be a wrapper tag for this layout.
726         bool hasBoxTag = !getLayout().docbookwrappertag().empty();
727         if (!hasBoxTag)
728                 LYXERR0("Assertion failed: box layout " + getLayout().name() + " missing DocBookWrapperTag.");
729
730         // Avoid nesting boxes in DocBook, it's not allowed. Only make the check for <sidebar> to avoid destroying
731         // tags if this is not the wrapper tag for this layout (unlikely).
732         bool isAlreadyInBox = hasBoxTag && xs.isTagOpen(xml::StartTag(getLayout().docbookwrappertag()));
733
734         bool outputBoxTag = hasBoxTag && !isAlreadyInBox;
735
736         // Generate the box tag (typically, <sidebar>).
737         if (outputBoxTag) {
738                 if (!xs.isLastTagCR())
739                         xs << xml::CR();
740
741                 xs << xml::StartTag(getLayout().docbookwrappertag(), getLayout().docbookwrapperattr());
742                 xs << xml::CR();
743         }
744
745         // If the box starts with a sectioning item, use as box title.
746         auto current_par = paragraphs().begin();
747         if (current_par->layout().category() == from_utf8("Sectioning")) {
748                 // Only generate the first paragraph.
749                 current_par = makeAny(text(), buffer(), xs, runparams, paragraphs().begin());
750         }
751
752         // Don't call InsetText::docbook, as this would generate all paragraphs in the inset, not the ones we are
753         // interested in. The best solution would be to call docbookParagraphs with an updated OutputParams object to only
754         // generate paragraphs after the title, but it leads to strange crashes, as if text().paragraphs() then returns
755         // a smaller set of paragrphs.
756         // Elements in the box must keep their paragraphs.
757         auto rp = runparams;
758         rp.docbook_in_par = false;
759         rp.docbook_force_pars = true;
760
761         xs.startDivision(false);
762         while (current_par != paragraphs().end())
763                 current_par = makeAny(text(), buffer(), xs, rp, current_par);
764         xs.endDivision();
765
766         // Close the box.
767         if (outputBoxTag) {
768                 if (!xs.isLastTagCR())
769                         xs << xml::CR();
770
771                 xs << xml::EndTag(getLayout().docbookwrappertag());
772                 xs << xml::CR();
773         }
774 }
775
776
777 docstring InsetBox::xhtml(XMLStream & xs, OutputParams const & runparams) const
778 {
779         // construct attributes
780         string attrs = "class='" + params_.type + "'";
781         string style;
782         if (!params_.width.empty()) {
783                 string w = params_.width.asHTMLString();
784                 if (w != "100%")
785                         style += ("width: " + params_.width.asHTMLString() + "; ");
786         }
787         // The special heights don't really mean anything for us.
788         if (!params_.height.empty() && params_.height_special == "none")
789                 style += ("height: " + params_.height.asHTMLString() + "; ");
790         if (!style.empty())
791                 attrs += " style='" + style + "'";
792
793         xs << xml::StartTag("div", attrs);
794         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
795         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
796         xs << xml::EndTag("div");
797         xs << defer;
798         return docstring();
799 }
800
801
802 void InsetBox::validate(LaTeXFeatures & features) const
803 {
804         BoxType btype = boxtranslator().find(params_.type);
805         switch (btype) {
806         case Frameless:
807                 if (params_.backgroundcolor != "none")
808                         features.require("xcolor");
809                 break;
810         case Framed:
811                 features.require("calc");
812                 features.require("framed");
813                 break;
814         case Boxed:
815                 features.require("calc");
816                 if (useFColorBox())
817                         // \fcolorbox is provided by [x]color
818                         features.require("xcolor");
819                 break;
820         case ovalbox:
821         case Ovalbox:
822         case Shadowbox:
823         case Doublebox:
824                 features.require("calc");
825                 features.require("fancybox");
826                 break;
827         case Shaded:
828                 features.require("color");
829                 features.require("framed");
830                 break;
831         }
832         InsetCollapsible::validate(features);
833 }
834
835
836 string InsetBox::contextMenuName() const
837 {
838         return "context-box";
839 }
840
841
842 string InsetBox::params2string(InsetBoxParams const & params)
843 {
844         ostringstream data;
845         data << "box" << ' ';
846         params.write(data);
847         return data.str();
848 }
849
850
851 void InsetBox::string2params(string const & in, InsetBoxParams & params)
852 {
853         if (in.empty())
854                 return;
855
856         istringstream data(in);
857         Lexer lex;
858         lex.setStream(data);
859
860         string name;
861         lex >> name;
862         if (!lex || name != "box") {
863                 LYXERR0("InsetBox::string2params(" << in << ")\n"
864                                           "Expected arg 1 to be \"box\"\n");
865                 return;
866         }
867
868         // This is part of the inset proper that is usually swallowed
869         // by Text::readInset
870         string id;
871         lex >> id;
872         if (!lex || id != "Box") {
873                 LYXERR0("InsetBox::string2params(" << in << ")\n"
874                                           "Expected arg 2 to be \"Box\"\n");
875         }
876
877         params = InsetBoxParams(string());
878         params.read(lex);
879 }
880
881
882 string const InsetBox::getFrameColor(bool const gui) const
883 {
884         if (params_.framecolor == "default")
885                 return gui ? "foreground" : "black";
886         return params_.framecolor;
887 }
888
889
890 string const InsetBox::getBackgroundColor() const
891 {
892         if (params_.backgroundcolor == "none")
893                 return buffer().params().isbackgroundcolor
894                                 ? "page_backgroundcolor"
895                                 : "white";
896         return params_.backgroundcolor;
897 }
898
899
900 bool InsetBox::useFColorBox() const
901 {
902         // we need an \fcolorbox if the framecolor or the backgroundcolor
903         // is non-default. We also do it with black and white for consistency.
904         return params_.framecolor != "default" || params_.backgroundcolor != "none";
905 }
906
907
908 /////////////////////////////////////////////////////////////////////////
909 //
910 // InsetBoxParams
911 //
912 /////////////////////////////////////////////////////////////////////////
913
914 InsetBoxParams::InsetBoxParams(string const & label)
915         : type(label),
916           use_parbox(false),
917           use_makebox(false),
918           inner_box(true),
919           width(Length("100col%")),
920           special("none"),
921           pos('t'),
922           hor_pos('c'),
923           inner_pos('t'),
924           height(Length("1in")),
925           height_special("totalheight"), // default is 1\\totalheight
926           thickness(Length(defaultThick)),
927           separation(Length(defaultSep)),
928           shadowsize(Length(defaultShadow)),
929           framecolor("default"),
930           backgroundcolor("none")
931 {}
932
933
934 void InsetBoxParams::write(ostream & os) const
935 {
936         os << "Box " << type << "\n";
937         os << "position \"" << pos << "\"\n";
938         os << "hor_pos \"" << hor_pos << "\"\n";
939         os << "has_inner_box " << inner_box << "\n";
940         os << "inner_pos \"" << inner_pos << "\"\n";
941         os << "use_parbox " << use_parbox << "\n";
942         os << "use_makebox " << use_makebox << "\n";
943         os << "width \"" << width.asString() << "\"\n";
944         os << "special \"" << special << "\"\n";
945         os << "height \"" << height.asString() << "\"\n";
946         os << "height_special \"" << height_special << "\"\n";
947         os << "thickness \"" << thickness.asString() << "\"\n";
948         os << "separation \"" << separation.asString() << "\"\n";
949         os << "shadowsize \"" << shadowsize.asString() << "\"\n";
950         os << "framecolor \"" << framecolor << "\"\n";
951         os << "backgroundcolor \"" << backgroundcolor << "\"\n";
952 }
953
954
955 void InsetBoxParams::read(Lexer & lex)
956 {
957         lex.setContext("InsetBoxParams::read");
958         lex >> type;
959         lex >> "position" >> pos;
960         lex >> "hor_pos" >> hor_pos;
961         lex >> "has_inner_box" >> inner_box;
962         if (type == "Framed")
963                 inner_box = false;
964         lex >> "inner_pos" >> inner_pos;
965         lex >> "use_parbox" >> use_parbox;
966         lex >> "use_makebox" >> use_makebox;
967         lex >> "width" >> width;
968         lex >> "special" >> special;
969         lex >> "height" >> height;
970         lex >> "height_special" >> height_special;
971         lex >> "thickness" >> thickness;
972         lex >> "separation" >> separation;
973         lex >> "shadowsize" >> shadowsize;
974         lex >> "framecolor" >> framecolor;
975         lex >> "backgroundcolor" >> backgroundcolor;
976 }
977
978
979 } // namespace lyx