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"
27 #include "MetricsInfo.h"
28 #include "output_docbook.h"
29 #include "output_xhtml.h"
31 #include "texstream.h"
32 #include "TextClass.h"
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"
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(getFrameColor(true)));
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()
359 && runparams.local_font && runparams.local_font->isRightToLeft()) {
360 maybeBeginL = "\\beginL";
361 maybeEndL = "\\endL";
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
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)) {
379 width_string += " - 2\\FrameSep - 2\\FrameRule";
382 width_string += " - 2\\fboxsep - 2\\fboxrule";
387 width_string += " - 2\\fboxsep - 0.8pt";
390 width_string += " - 2\\fboxsep - 1.6pt";
393 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
396 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
402 if (runparams.lastid != -1)
403 os.texrow().start(runparams.lastid, runparams.lastpos);
405 // adapt column/text width correctly also if paragraphs indented
406 if (stdwidth && !(buffer().params().paragraph_separation))
409 bool needendgroup = false;
414 if (thickness_string != defaultThick) {
415 os << "{\\FrameRule " << from_ascii(thickness_string);
416 if (separation_string != defaultSep)
417 os << "\\FrameSep " << from_ascii(separation_string);
419 if (separation_string != defaultSep && thickness_string == defaultThick)
420 os << "{\\FrameSep " << from_ascii(separation_string);
422 os << "\\begin{framed}%\n";
425 if (thickness_string != defaultThick) {
426 os << "{\\fboxrule " << from_ascii(thickness_string);
427 if (separation_string != defaultSep)
428 os << "\\fboxsep " << from_ascii(separation_string);
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()) {
435 << "\\fcolorbox{" << getFrameColor()
436 << "}{" << getBackgroundColor()
437 << "}{" << "\\makebox";
438 needEndL = !maybeBeginL.empty();
441 // Special widths, see usrguide sec. 3.5
443 if (params_.special != "none") {
444 os << "[" << params_.width.value()
445 << '\\' << from_utf8(params_.special)
448 os << '[' << from_ascii(width_string)
450 // default horizontal alignment is 'c'
451 if (params_.hor_pos != 'c')
452 os << "[" << params_.hor_pos << "]";
454 if (useFColorBox()) {
456 << "\\fcolorbox{" << getFrameColor()
457 << "}{" << getBackgroundColor() << "}";
458 needEndL = !maybeBeginL.empty();
460 if (!cprotect.empty() && contains(runparams.active_chars, '^')) {
461 // cprotect relies on ^ being on catcode 7
462 os << "\\begingroup\\catcode`\\^=7";
465 os << cprotect << "\\fbox";
471 if (!separation_string.empty() && separation_string != defaultSep)
472 os << "{\\fboxsep " << from_ascii(separation_string);
476 if (!separation_string.empty() && separation_string != defaultSep)
477 os << "{\\fboxsep " << from_ascii(separation_string);
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);
488 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
489 os << "\\shadowsize " << from_ascii(shadowsize_string);
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);
496 if (shadowsize_string != defaultShadow
497 && separation_string == defaultSep
498 && thickness_string == defaultThick)
499 os << "{\\shadowsize " << from_ascii(shadowsize_string);
500 os << "\\shadowbox{";
503 // must be set later because e.g. the width settings only work when
504 // it is inside a minipage or parbox
506 needEndL = !maybeBeginL.empty();
509 if (thickness_string != defaultThick) {
510 os << "{\\fboxrule " << from_ascii(thickness_string);
511 if (separation_string != defaultSep)
512 os << "\\fboxsep " << from_ascii(separation_string);
514 if (separation_string != defaultSep && thickness_string == defaultThick)
515 os << "{\\fboxsep " << from_ascii(separation_string);
516 os << "\\doublebox{";
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();
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();
535 // output the width and horizontal position
536 if (params_.special != "none") {
537 os << "[" << params_.width.value()
538 << '\\' << from_utf8(params_.special)
541 os << '[' << from_ascii(width_string)
543 if (params_.hor_pos != 'c')
544 os << "[" << params_.hor_pos << "]";
546 if (params_.backgroundcolor != "none") {
547 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}";
548 needEndL = !maybeBeginL.empty();
556 if (params_.backgroundcolor != "none" && btype == Frameless) {
557 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
558 needEndL = !maybeBeginL.empty();
560 os << "\\begin{minipage}";
563 // output parameters for parbox and minipage
564 if (!params_.use_makebox) {
565 os << "[" << params_.pos << "]";
566 if (params_.height_special == "none") {
568 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
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) {
579 os << "[" << params_.height.value()
580 << "\\" << from_utf8(params_.height_special) << "]";
583 if (params_.inner_pos != params_.pos)
584 os << "[" << params_.inner_pos << "]";
586 os << '{' << from_ascii(width_string) << '}';
587 if (params_.use_parbox)
592 } // end if inner_box
594 if (btype == Shaded) {
595 os << "\\begin{shaded}%\n";
598 InsetText::latex(os, runparams);
601 os << "\\end{shaded}";
603 if (params_.inner_box) {
604 if (params_.use_parbox || params_.use_makebox)
607 os << "%\n\\end{minipage}";
608 if (params_.backgroundcolor != "none" && btype == Frameless
609 && !(params_.use_makebox && width_string.empty()))
617 os << "\\end{framed}";
618 if (separation_string != defaultSep || thickness_string != defaultThick)
623 if (!params_.inner_box && !width_string.empty() && useFColorBox())
625 if (separation_string != defaultSep || thickness_string != defaultThick)
632 if (separation_string != defaultSep)
637 if (separation_string != defaultSep)
642 if (separation_string != defaultSep || thickness_string != defaultThick)
647 if (separation_string != defaultSep
648 || thickness_string != defaultThick
649 || shadowsize_string != defaultShadow)
661 int InsetBox::plaintext(odocstringstream & os,
662 OutputParams const & runparams, size_t max_length) const
664 BoxType const btype = boxtranslator().find(params_.type);
688 InsetText::plaintext(os, runparams, max_length);
719 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
723 void InsetBox::docbook(XMLStream & xs, OutputParams const & runparams) const
725 // There really should be a wrapper tag for this layout.
726 bool hasBoxTag = !getLayout().docbookwrappertag().empty();
728 LYXERR0("Assertion failed: box layout " + getLayout().name() + " missing DocBookWrapperTag.");
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()));
734 bool outputBoxTag = hasBoxTag && !isAlreadyInBox;
736 // Generate the box tag (typically, <sidebar>).
738 if (!xs.isLastTagCR())
741 xs << xml::StartTag(getLayout().docbookwrappertag(), getLayout().docbookwrapperattr());
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());
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.
758 rp.docbook_in_par = false;
759 rp.docbook_force_pars = true;
761 xs.startDivision(false);
762 while (current_par != paragraphs().end())
763 current_par = makeAny(text(), buffer(), xs, rp, current_par);
768 if (!xs.isLastTagCR())
771 xs << xml::EndTag(getLayout().docbookwrappertag());
777 docstring InsetBox::xhtml(XMLStream & xs, OutputParams const & runparams) const
779 // construct attributes
780 string attrs = "class='" + params_.type + "'";
782 if (!params_.width.empty()) {
783 string w = params_.width.asHTMLString();
785 style += ("width: " + params_.width.asHTMLString() + "; ");
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() + "; ");
791 attrs += " style='" + style + "'";
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");
802 void InsetBox::validate(LaTeXFeatures & features) const
804 BoxType btype = boxtranslator().find(params_.type);
807 if (params_.backgroundcolor != "none")
808 features.require("xcolor");
811 features.require("calc");
812 features.require("framed");
815 features.require("calc");
817 // \fcolorbox is provided by [x]color
818 features.require("xcolor");
824 features.require("calc");
825 features.require("fancybox");
828 features.require("color");
829 features.require("framed");
832 InsetCollapsible::validate(features);
836 string InsetBox::contextMenuName() const
838 return "context-box";
842 string InsetBox::params2string(InsetBoxParams const & params)
845 data << "box" << ' ';
851 void InsetBox::string2params(string const & in, InsetBoxParams & params)
856 istringstream data(in);
862 if (!lex || name != "box") {
863 LYXERR0("InsetBox::string2params(" << in << ")\n"
864 "Expected arg 1 to be \"box\"\n");
868 // This is part of the inset proper that is usually swallowed
869 // by Text::readInset
872 if (!lex || id != "Box") {
873 LYXERR0("InsetBox::string2params(" << in << ")\n"
874 "Expected arg 2 to be \"Box\"\n");
877 params = InsetBoxParams(string());
882 string const InsetBox::getFrameColor(bool const gui) const
884 if (params_.framecolor == "default")
885 return gui ? "foreground" : "black";
886 return params_.framecolor;
890 string const InsetBox::getBackgroundColor() const
892 if (params_.backgroundcolor == "none")
893 return buffer().params().isbackgroundcolor
894 ? "page_backgroundcolor"
896 return params_.backgroundcolor;
900 bool InsetBox::useFColorBox() const
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";
908 /////////////////////////////////////////////////////////////////////////
912 /////////////////////////////////////////////////////////////////////////
914 InsetBoxParams::InsetBoxParams(string const & label)
919 width(Length("100col%")),
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")
934 void InsetBoxParams::write(ostream & os) const
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";
955 void InsetBoxParams::read(Lexer & lex)
957 lex.setContext("InsetBoxParams::read");
959 lex >> "position" >> pos;
960 lex >> "hor_pos" >> hor_pos;
961 lex >> "has_inner_box" >> inner_box;
962 if (type == "Framed")
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;