]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Strip et al. for citation search
[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/FileName.h"
38 #include "support/gettext.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(params_.framecolor));
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() && 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         // There really should be a wrapper tag for this layout.
722         bool hasBoxTag = !getLayout().docbookwrappertag().empty();
723         if (!hasBoxTag)
724                 LYXERR0("Assertion failed: box layout " + getLayout().name() + " missing DocBookWrapperTag.");
725
726         // Avoid nesting boxes in DocBook, it's not allowed. Only make the check for <sidebar> to avoid destroying
727         // tags if this is not the wrapper tag for this layout (unlikely).
728         bool isAlreadyInBox = hasBoxTag && xs.isTagOpen(xml::StartTag(getLayout().docbookwrappertag()));
729
730         bool outputBoxTag = hasBoxTag && !isAlreadyInBox;
731
732         // Generate the box tag (typically, <sidebar>).
733         if (outputBoxTag) {
734                 if (!xs.isLastTagCR())
735                         xs << xml::CR();
736
737                 xs << xml::StartTag(getLayout().docbookwrappertag(), getLayout().docbookwrapperattr());
738                 xs << xml::CR();
739         }
740
741         // If the box starts with a sectioning item, use as box title.
742         auto current_par = paragraphs().begin();
743         if (current_par->layout().category() == from_utf8("Sectioning")) {
744                 // Only generate the first paragraph.
745                 current_par = makeAny(text(), buffer(), xs, runparams, paragraphs().begin());
746         }
747
748         // Don't call InsetText::docbook, as this would generate all paragraphs in the inset, not the ones we are
749         // interested in. The best solution would be to call docbookParagraphs with an updated OutputParams object to only
750         // generate paragraphs after the title, but it leads to strange crashes, as if text().paragraphs() then returns
751         // a smaller set of paragrphs.
752         // Elements in the box must keep their paragraphs.
753         auto rp = runparams;
754         rp.docbook_in_par = false;
755         rp.docbook_force_pars = true;
756
757         xs.startDivision(false);
758         while (current_par != paragraphs().end())
759                 current_par = makeAny(text(), buffer(), xs, rp, current_par);
760         xs.endDivision();
761
762         // Close the box.
763         if (outputBoxTag) {
764                 if (!xs.isLastTagCR())
765                         xs << xml::CR();
766
767                 xs << xml::EndTag(getLayout().docbookwrappertag());
768                 xs << xml::CR();
769         }
770 }
771
772
773 docstring InsetBox::xhtml(XMLStream & xs, OutputParams const & runparams) const
774 {
775         // construct attributes
776         string attrs = "class='" + params_.type + "'";
777         string style;
778         if (!params_.width.empty()) {
779                 string w = params_.width.asHTMLString();
780                 if (w != "100%")
781                         style += ("width: " + params_.width.asHTMLString() + "; ");
782         }
783         // The special heights don't really mean anything for us.
784         if (!params_.height.empty() && params_.height_special == "none")
785                 style += ("height: " + params_.height.asHTMLString() + "; ");
786         if (!style.empty())
787                 attrs += " style='" + style + "'";
788
789         xs << xml::StartTag("div", attrs);
790         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
791         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
792         xs << xml::EndTag("div");
793         xs << defer;
794         return docstring();
795 }
796
797
798 void InsetBox::validate(LaTeXFeatures & features) const
799 {
800         BoxType btype = boxtranslator().find(params_.type);
801         switch (btype) {
802         case Frameless:
803                 if (params_.backgroundcolor != "none")
804                         features.require("xcolor");
805                 break;
806         case Framed:
807                 features.require("calc");
808                 features.require("framed");
809                 break;
810         case Boxed:
811                 features.require("calc");
812                 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
813                         features.require("xcolor");
814                 break;
815         case ovalbox:
816         case Ovalbox:
817         case Shadowbox:
818         case Doublebox:
819                 features.require("calc");
820                 features.require("fancybox");
821                 break;
822         case Shaded:
823                 features.require("color");
824                 features.require("framed");
825                 break;
826         }
827         InsetCollapsible::validate(features);
828 }
829
830
831 string InsetBox::contextMenuName() const
832 {
833         return "context-box";
834 }
835
836
837 string InsetBox::params2string(InsetBoxParams const & params)
838 {
839         ostringstream data;
840         data << "box" << ' ';
841         params.write(data);
842         return data.str();
843 }
844
845
846 void InsetBox::string2params(string const & in, InsetBoxParams & params)
847 {
848         if (in.empty())
849                 return;
850
851         istringstream data(in);
852         Lexer lex;
853         lex.setStream(data);
854
855         string name;
856         lex >> name;
857         if (!lex || name != "box") {
858                 LYXERR0("InsetBox::string2params(" << in << ")\n"
859                                           "Expected arg 1 to be \"box\"\n");
860                 return;
861         }
862
863         // This is part of the inset proper that is usually swallowed
864         // by Text::readInset
865         string id;
866         lex >> id;
867         if (!lex || id != "Box") {
868                 LYXERR0("InsetBox::string2params(" << in << ")\n"
869                                           "Expected arg 2 to be \"Box\"\n");
870         }
871
872         params = InsetBoxParams(string());
873         params.read(lex);
874 }
875
876
877 /////////////////////////////////////////////////////////////////////////
878 //
879 // InsetBoxParams
880 //
881 /////////////////////////////////////////////////////////////////////////
882
883 InsetBoxParams::InsetBoxParams(string const & label)
884         : type(label),
885           use_parbox(false),
886           use_makebox(false),
887           inner_box(true),
888           width(Length("100col%")),
889           special("none"),
890           pos('t'),
891           hor_pos('c'),
892           inner_pos('t'),
893           height(Length("1in")),
894           height_special("totalheight"), // default is 1\\totalheight
895           thickness(Length(defaultThick)),
896           separation(Length(defaultSep)),
897           shadowsize(Length(defaultShadow)),
898           framecolor("black"),
899           backgroundcolor("none")
900 {}
901
902
903 void InsetBoxParams::write(ostream & os) const
904 {
905         os << "Box " << type << "\n";
906         os << "position \"" << pos << "\"\n";
907         os << "hor_pos \"" << hor_pos << "\"\n";
908         os << "has_inner_box " << inner_box << "\n";
909         os << "inner_pos \"" << inner_pos << "\"\n";
910         os << "use_parbox " << use_parbox << "\n";
911         os << "use_makebox " << use_makebox << "\n";
912         os << "width \"" << width.asString() << "\"\n";
913         os << "special \"" << special << "\"\n";
914         os << "height \"" << height.asString() << "\"\n";
915         os << "height_special \"" << height_special << "\"\n";
916         os << "thickness \"" << thickness.asString() << "\"\n";
917         os << "separation \"" << separation.asString() << "\"\n";
918         os << "shadowsize \"" << shadowsize.asString() << "\"\n";
919         os << "framecolor \"" << framecolor << "\"\n";
920         os << "backgroundcolor \"" << backgroundcolor << "\"\n";
921 }
922
923
924 void InsetBoxParams::read(Lexer & lex)
925 {
926         lex.setContext("InsetBoxParams::read");
927         lex >> type;
928         lex >> "position" >> pos;
929         lex >> "hor_pos" >> hor_pos;
930         lex >> "has_inner_box" >> inner_box;
931         if (type == "Framed")
932                 inner_box = false;
933         lex >> "inner_pos" >> inner_pos;
934         lex >> "use_parbox" >> use_parbox;
935         lex >> "use_makebox" >> use_makebox;
936         lex >> "width" >> width;
937         lex >> "special" >> special;
938         lex >> "height" >> height;
939         lex >> "height_special" >> height_special;
940         lex >> "thickness" >> thickness;
941         lex >> "separation" >> separation;
942         lex >> "shadowsize" >> shadowsize;
943         lex >> "framecolor" >> framecolor;
944         lex >> "backgroundcolor" >> backgroundcolor;
945 }
946
947
948 } // namespace lyx