3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
19 #include "BufferParams.h"
20 #include "BufferView.h"
23 #include "DispatchResult.h"
24 #include "FuncStatus.h"
25 #include "FuncRequest.h"
26 #include "LaTeXFeatures.h"
28 #include "MetricsInfo.h"
29 #include "output_docbook.h"
30 #include "output_xhtml.h"
32 #include "texstream.h"
33 #include "TextClass.h"
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"
42 #include "frontends/Application.h"
47 using namespace lyx::support;
53 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
54 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
56 BoxTranslator initBoxtranslator()
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);
70 BoxTranslatorLoc initBoxtranslatorLoc()
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);
84 BoxTranslator const & boxtranslator()
86 static BoxTranslator const translator = initBoxtranslator();
91 BoxTranslatorLoc const & boxtranslator_loc()
93 static BoxTranslatorLoc const translator = initBoxtranslatorLoc();
100 /////////////////////////////////////////////////////////////////////////
104 /////////////////////////////////////////////////////////////////////////
106 InsetBox::InsetBox(Buffer * buffer, string const & label)
107 : InsetCollapsible(buffer), params_(label)
111 docstring InsetBox::layoutName() const
114 return from_ascii("Box:" + params_.type);
118 void InsetBox::write(ostream & os) const
121 InsetCollapsible::write(os);
125 void InsetBox::read(Lexer & lex)
128 InsetCollapsible::read(lex);
132 void InsetBox::setButtonLabel()
134 BoxType const btype = boxtranslator().find(params_.type);
136 docstring const type = _("Box");
139 if (params_.inner_box) {
140 if (params_.use_parbox)
142 else if (params_.use_makebox)
143 inner = _("Makebox");
145 inner = _("Minipage");
149 if (btype != Frameless)
150 frame = boxtranslator_loc().find(btype);
153 if (inner.empty() && frame.empty())
155 else if (inner.empty())
156 label = bformat(_("%1$s (%2$s)"),
158 else if (frame.empty())
159 label = bformat(_("%1$s (%2$s)"),
162 label = bformat(_("%1$s (%2$s, %3$s)"),
166 // set the frame color for the inset if the type is Boxed
168 setFrameColor(lcolor.getFromLaTeXName(params_.framecolor));
170 setFrameColor(Color_collapsibleframe);
174 bool InsetBox::hasFixedWidth() const
176 return !params_.width.empty() && params_.special == "none";
180 bool InsetBox::allowMultiPar() const
182 return (params_.inner_box && !params_.use_makebox)
183 || params_.type == "Shaded" || params_.type == "Framed";
187 void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
189 // back up textwidth.
190 int textwidth_backup = mi.base.textwidth;
192 mi.base.textwidth = mi.base.inPixels(params_.width);
193 InsetCollapsible::metrics(mi, dim);
194 // restore textwidth.
195 mi.base.textwidth = textwidth_backup;
199 bool InsetBox::forcePlainLayout(idx_type) const
201 return (!params_.inner_box || params_.use_makebox)
202 && params_.type != "Shaded" && params_.type != "Framed";
206 bool InsetBox::needsCProtection(bool const maintext, bool const fragile) const
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)
213 return InsetText::needsCProtection(maintext, fragile);
217 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
219 // we only support background color for 3 types
220 if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
221 return getLayout().bgcolor();
223 if (params_.type == "Shaded") {
224 if (!buffer().params().isboxbgcolor)
225 return getLayout().bgcolor();
227 ColorCode c = lcolor.getFromLyXName("boxbgcolor@" + buffer().fileName().absFileName());
229 return getLayout().bgcolor();
233 if (params_.backgroundcolor != "none")
234 return lcolor.getFromLaTeXName(params_.backgroundcolor);
236 return getLayout().bgcolor();
240 LyXAlignment InsetBox::contentAlignment() const
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;
247 // The default value below is actually irrelevant
248 LyXAlignment align = LYX_ALIGN_NONE;
249 switch (params_.hor_pos) {
251 align = LYX_ALIGN_LEFT;
254 align = LYX_ALIGN_CENTER;
257 align = LYX_ALIGN_RIGHT;
260 align = LYX_ALIGN_BLOCK;
267 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
269 switch (cmd.action()) {
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) {
278 // this will not be handled higher up
282 cur.recordUndoInset(this);
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;
292 // handle the opposite case
293 if (params_.type == "Boxed" && params_.use_makebox) {
294 params_.use_makebox = false;
295 params_.inner_box = false;
298 string2params(to_utf8(cmd.argument()), params_);
304 InsetCollapsible::doDispatch(cur, cmd);
310 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
311 FuncStatus & flag) const
313 switch (cmd.action()) {
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");
323 if (first_arg == "box") {
324 flag.setEnabled(true);
327 return InsetCollapsible::getStatus(cur, cmd, flag);
330 case LFUN_INSET_DIALOG_UPDATE:
331 flag.setEnabled(true);
335 return InsetCollapsible::getStatus(cur, cmd, flag);
340 const string defaultThick = "0.4pt";
341 const string defaultSep = "3pt";
342 const string defaultShadow = "4pt";
344 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
346 BoxType btype = boxtranslator().find(params_.type);
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
357 bool needEndL = false;
358 if (!runparams.isFullUnicode() && runparams.local_font->isRightToLeft()) {
359 maybeBeginL = "\\beginL";
360 maybeEndL = "\\endL";
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
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)) {
378 width_string += " - 2\\FrameSep - 2\\FrameRule";
381 width_string += " - 2\\fboxsep - 2\\fboxrule";
386 width_string += " - 2\\fboxsep - 0.8pt";
389 width_string += " - 2\\fboxsep - 1.6pt";
392 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
395 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
401 if (runparams.lastid != -1)
402 os.texrow().start(runparams.lastid, runparams.lastpos);
404 // adapt column/text width correctly also if paragraphs indented
405 if (stdwidth && !(buffer().params().paragraph_separation))
408 bool needendgroup = false;
413 if (thickness_string != defaultThick) {
414 os << "{\\FrameRule " << from_ascii(thickness_string);
415 if (separation_string != defaultSep)
416 os << "\\FrameSep " << from_ascii(separation_string);
418 if (separation_string != defaultSep && thickness_string == defaultThick)
419 os << "{\\FrameSep " << from_ascii(separation_string);
421 os << "\\begin{framed}%\n";
424 if (thickness_string != defaultThick) {
425 os << "{\\fboxrule " << from_ascii(thickness_string);
426 if (separation_string != defaultSep)
427 os << "\\fboxsep " << from_ascii(separation_string);
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 << "}{";
435 needEndL = !maybeBeginL.empty();
438 // Special widths, see usrguide sec. 3.5
440 if (params_.special != "none") {
441 os << "[" << params_.width.value()
442 << '\\' << from_utf8(params_.special)
445 os << '[' << from_ascii(width_string)
447 // default horizontal alignment is 'c'
448 if (params_.hor_pos != 'c')
449 os << "[" << params_.hor_pos << "]";
451 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
452 os << maybeBeginL << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
453 needEndL = !maybeBeginL.empty();
455 if (!cprotect.empty() && contains(runparams.active_chars, '^')) {
456 // cprotect relies on ^ being on catcode 7
457 os << "\\begingroup\\catcode`\\^=7";
460 os << cprotect << "\\fbox";
466 if (!separation_string.empty() && separation_string != defaultSep)
467 os << "{\\fboxsep " << from_ascii(separation_string);
471 if (!separation_string.empty() && separation_string != defaultSep)
472 os << "{\\fboxsep " << from_ascii(separation_string);
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);
483 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
484 os << "\\shadowsize " << from_ascii(shadowsize_string);
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);
491 if (shadowsize_string != defaultShadow
492 && separation_string == defaultSep
493 && thickness_string == defaultThick)
494 os << "{\\shadowsize " << from_ascii(shadowsize_string);
495 os << "\\shadowbox{";
498 // must be set later because e.g. the width settings only work when
499 // it is inside a minipage or parbox
501 needEndL = !maybeBeginL.empty();
504 if (thickness_string != defaultThick) {
505 os << "{\\fboxrule " << from_ascii(thickness_string);
506 if (separation_string != defaultSep)
507 os << "\\fboxsep " << from_ascii(separation_string);
509 if (separation_string != defaultSep && thickness_string == defaultThick)
510 os << "{\\fboxsep " << from_ascii(separation_string);
511 os << "\\doublebox{";
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();
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();
530 // output the width and horizontal position
531 if (params_.special != "none") {
532 os << "[" << params_.width.value()
533 << '\\' << from_utf8(params_.special)
536 os << '[' << from_ascii(width_string)
538 if (params_.hor_pos != 'c')
539 os << "[" << params_.hor_pos << "]";
541 if (params_.backgroundcolor != "none") {
542 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}";
543 needEndL = !maybeBeginL.empty();
551 if (params_.backgroundcolor != "none" && btype == Frameless) {
552 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
553 needEndL = !maybeBeginL.empty();
555 os << "\\begin{minipage}";
558 // output parameters for parbox and minipage
559 if (!params_.use_makebox) {
560 os << "[" << params_.pos << "]";
561 if (params_.height_special == "none") {
563 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
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) {
574 os << "[" << params_.height.value()
575 << "\\" << from_utf8(params_.height_special) << "]";
578 if (params_.inner_pos != params_.pos)
579 os << "[" << params_.inner_pos << "]";
581 os << '{' << from_ascii(width_string) << '}';
582 if (params_.use_parbox)
587 } // end if inner_box
589 if (btype == Shaded) {
590 os << "\\begin{shaded}%\n";
593 InsetText::latex(os, runparams);
596 os << "\\end{shaded}";
598 if (params_.inner_box) {
599 if (params_.use_parbox || params_.use_makebox)
602 os << "%\n\\end{minipage}";
603 if (params_.backgroundcolor != "none" && btype == Frameless
604 && !(params_.use_makebox && width_string.empty()))
612 os << "\\end{framed}";
613 if (separation_string != defaultSep || thickness_string != defaultThick)
618 if (!params_.inner_box && !width_string.empty()
619 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
621 if (separation_string != defaultSep || thickness_string != defaultThick)
628 if (separation_string != defaultSep)
633 if (separation_string != defaultSep)
638 if (separation_string != defaultSep || thickness_string != defaultThick)
643 if (separation_string != defaultSep
644 || thickness_string != defaultThick
645 || shadowsize_string != defaultShadow)
657 int InsetBox::plaintext(odocstringstream & os,
658 OutputParams const & runparams, size_t max_length) const
660 BoxType const btype = boxtranslator().find(params_.type);
684 InsetText::plaintext(os, runparams, max_length);
715 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
719 void InsetBox::docbook(XMLStream & xs, OutputParams const & runparams) const
721 // There really should be a wrapper tag for this layout.
722 bool hasBoxTag = !getLayout().docbookwrappertag().empty();
724 LYXERR0("Assertion failed: box layout " + getLayout().name() + " missing DocBookWrapperTag.");
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()));
730 bool outputBoxTag = hasBoxTag && !isAlreadyInBox;
732 // Generate the box tag (typically, <sidebar>).
734 if (!xs.isLastTagCR())
737 xs << xml::StartTag(getLayout().docbookwrappertag(), getLayout().docbookwrapperattr());
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());
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.
754 rp.docbook_in_par = false;
755 rp.docbook_force_pars = true;
757 xs.startDivision(false);
758 while (current_par != paragraphs().end())
759 current_par = makeAny(text(), buffer(), xs, rp, current_par);
764 if (!xs.isLastTagCR())
767 xs << xml::EndTag(getLayout().docbookwrappertag());
773 docstring InsetBox::xhtml(XMLStream & xs, OutputParams const & runparams) const
775 // construct attributes
776 string attrs = "class='" + params_.type + "'";
778 if (!params_.width.empty()) {
779 string w = params_.width.asHTMLString();
781 style += ("width: " + params_.width.asHTMLString() + "; ");
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() + "; ");
787 attrs += " style='" + style + "'";
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");
798 void InsetBox::validate(LaTeXFeatures & features) const
800 BoxType btype = boxtranslator().find(params_.type);
803 if (params_.backgroundcolor != "none")
804 features.require("xcolor");
807 features.require("calc");
808 features.require("framed");
811 features.require("calc");
812 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
813 features.require("xcolor");
819 features.require("calc");
820 features.require("fancybox");
823 features.require("color");
824 features.require("framed");
827 InsetCollapsible::validate(features);
831 string InsetBox::contextMenuName() const
833 return "context-box";
837 string InsetBox::params2string(InsetBoxParams const & params)
840 data << "box" << ' ';
846 void InsetBox::string2params(string const & in, InsetBoxParams & params)
851 istringstream data(in);
857 if (!lex || name != "box") {
858 LYXERR0("InsetBox::string2params(" << in << ")\n"
859 "Expected arg 1 to be \"box\"\n");
863 // This is part of the inset proper that is usually swallowed
864 // by Text::readInset
867 if (!lex || id != "Box") {
868 LYXERR0("InsetBox::string2params(" << in << ")\n"
869 "Expected arg 2 to be \"Box\"\n");
872 params = InsetBoxParams(string());
877 /////////////////////////////////////////////////////////////////////////
881 /////////////////////////////////////////////////////////////////////////
883 InsetBoxParams::InsetBoxParams(string const & label)
888 width(Length("100col%")),
893 height(Length("1in")),
894 height_special("totalheight"), // default is 1\\totalheight
895 thickness(Length(defaultThick)),
896 separation(Length(defaultSep)),
897 shadowsize(Length(defaultShadow)),
899 backgroundcolor("none")
903 void InsetBoxParams::write(ostream & os) const
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";
924 void InsetBoxParams::read(Lexer & lex)
926 lex.setContext("InsetBoxParams::read");
928 lex >> "position" >> pos;
929 lex >> "hor_pos" >> hor_pos;
930 lex >> "has_inner_box" >> inner_box;
931 if (type == "Framed")
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;