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_xhtml.h"
31 #include "texstream.h"
32 #include "TextClass.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/Translator.h"
40 #include "frontends/Application.h"
45 using namespace lyx::support;
51 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
52 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
54 BoxTranslator initBoxtranslator()
56 BoxTranslator translator("Boxed", InsetBox::Boxed);
57 translator.addPair("Frameless", InsetBox::Frameless);
58 translator.addPair("Framed", InsetBox::Framed);
59 translator.addPair("ovalbox", InsetBox::ovalbox);
60 translator.addPair("Ovalbox", InsetBox::Ovalbox);
61 translator.addPair("Shadowbox", InsetBox::Shadowbox);
62 translator.addPair("Shaded", InsetBox::Shaded);
63 translator.addPair("Doublebox",InsetBox::Doublebox);
68 BoxTranslatorLoc initBoxtranslatorLoc()
70 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
71 translator.addPair(_("frameless"), InsetBox::Frameless);
72 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
73 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
74 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
75 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
76 translator.addPair(_("shaded background"), InsetBox::Shaded);
77 translator.addPair(_("double frame"), InsetBox::Doublebox);
82 BoxTranslator const & boxtranslator()
84 static BoxTranslator const translator = initBoxtranslator();
89 BoxTranslatorLoc const & boxtranslator_loc()
91 static BoxTranslatorLoc const translator = initBoxtranslatorLoc();
98 /////////////////////////////////////////////////////////////////////////
102 /////////////////////////////////////////////////////////////////////////
104 InsetBox::InsetBox(Buffer * buffer, string const & label)
105 : InsetCollapsible(buffer), params_(label)
109 docstring InsetBox::layoutName() const
112 return from_ascii("Box:" + params_.type);
116 void InsetBox::write(ostream & os) const
119 InsetCollapsible::write(os);
123 void InsetBox::read(Lexer & lex)
126 InsetCollapsible::read(lex);
130 void InsetBox::setButtonLabel()
132 BoxType const btype = boxtranslator().find(params_.type);
134 docstring const type = _("Box");
137 if (params_.inner_box) {
138 if (params_.use_parbox)
140 else if (params_.use_makebox)
141 inner = _("Makebox");
143 inner = _("Minipage");
147 if (btype != Frameless)
148 frame = boxtranslator_loc().find(btype);
151 if (inner.empty() && frame.empty())
153 else if (inner.empty())
154 label = bformat(_("%1$s (%2$s)"),
156 else if (frame.empty())
157 label = bformat(_("%1$s (%2$s)"),
160 label = bformat(_("%1$s (%2$s, %3$s)"),
164 // set the frame color for the inset if the type is Boxed
166 setFrameColor(lcolor.getFromLaTeXName(params_.framecolor));
168 setFrameColor(Color_collapsibleframe);
172 bool InsetBox::hasFixedWidth() const
174 return !params_.width.empty() && params_.special == "none";
178 bool InsetBox::allowMultiPar() const
180 return (params_.inner_box && !params_.use_makebox)
181 || params_.type == "Shaded" || params_.type == "Framed";
185 void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
187 // back up textwidth.
188 int textwidth_backup = mi.base.textwidth;
190 mi.base.textwidth = mi.base.inPixels(params_.width);
191 InsetCollapsible::metrics(mi, dim);
193 mi.base.textwidth = textwidth_backup;
197 bool InsetBox::forcePlainLayout(idx_type) const
199 return (!params_.inner_box || params_.use_makebox)
200 && params_.type != "Shaded" && params_.type != "Framed";
204 bool InsetBox::needsCProtection(bool const maintext, bool const fragile) const
206 // We need to cprotect boxes that use minipages as inner box
207 // in fragile context
208 if (fragile && params_.inner_box && !params_.use_parbox && !params_.use_makebox)
211 return InsetText::needsCProtection(maintext, fragile);
215 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
217 // we only support background color for 3 types
218 if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
219 return getLayout().bgcolor();
221 if (params_.type == "Shaded") {
222 // FIXME: This hardcoded color is a hack!
223 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
224 return getLayout().bgcolor();
226 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
228 return getLayout().bgcolor();
232 if (params_.backgroundcolor != "none")
233 return lcolor.getFromLaTeXName(params_.backgroundcolor);
235 return getLayout().bgcolor();
239 LyXAlignment InsetBox::contentAlignment() const
241 // Custom horizontal alignment is only allowed with a fixed width
242 // and if either makebox or no inner box are used
243 if (params_.width.empty() || !(params_.use_makebox || !params_.inner_box))
244 return LYX_ALIGN_NONE;
246 // The default value below is actually irrelevant
247 LyXAlignment align = LYX_ALIGN_NONE;
248 switch (params_.hor_pos) {
250 align = LYX_ALIGN_LEFT;
253 align = LYX_ALIGN_CENTER;
256 align = LYX_ALIGN_RIGHT;
259 align = LYX_ALIGN_BLOCK;
266 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
268 switch (cmd.action()) {
270 case LFUN_INSET_MODIFY: {
271 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
272 string const first_arg = cmd.getArg(0);
273 bool const change_type = first_arg == "changetype";
274 bool const for_box = first_arg == "box";
275 if (!change_type && !for_box) {
277 // this will not be handled higher up
281 cur.recordUndoInset(this);
283 params_.type = cmd.getArg(1);
284 // set a makebox if there is no inner box but Frameless was exectued
285 // otherwise the result would be a non existent box (no inner AND outer box)
286 // (this was LyX bug 8712)
287 if (params_.type == "Frameless" && !params_.inner_box) {
288 params_.use_makebox = true;
289 params_.inner_box = true;
291 // handle the opposite case
292 if (params_.type == "Boxed" && params_.use_makebox) {
293 params_.use_makebox = false;
294 params_.inner_box = false;
297 string2params(to_utf8(cmd.argument()), params_);
303 InsetCollapsible::doDispatch(cur, cmd);
309 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
310 FuncStatus & flag) const
312 switch (cmd.action()) {
314 case LFUN_INSET_MODIFY: {
315 string const first_arg = cmd.getArg(0);
316 if (first_arg == "changetype") {
317 string const type = cmd.getArg(1);
318 flag.setOnOff(type == params_.type);
319 flag.setEnabled(!params_.inner_box || type != "Framed");
322 if (first_arg == "box") {
323 flag.setEnabled(true);
326 return InsetCollapsible::getStatus(cur, cmd, flag);
329 case LFUN_INSET_DIALOG_UPDATE:
330 flag.setEnabled(true);
334 return InsetCollapsible::getStatus(cur, cmd, flag);
339 const string defaultThick = "0.4pt";
340 const string defaultSep = "3pt";
341 const string defaultShadow = "4pt";
343 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
345 BoxType btype = boxtranslator().find(params_.type);
347 string width_string = params_.width.asLatexString();
348 string thickness_string = params_.thickness.asLatexString();
349 string separation_string = params_.separation.asLatexString();
350 string shadowsize_string = params_.shadowsize.asLatexString();
351 bool stdwidth = false;
352 string const cprotect = hasCProtectContent(runparams.moving_arg) ? "\\cprotect" : string();
353 // Colored boxes in RTL need to be wrapped into \beginL...\endL
356 bool needEndL = false;
357 if (!runparams.isFullUnicode() && runparams.local_font->isRightToLeft()) {
358 maybeBeginL = "\\beginL";
359 maybeEndL = "\\endL";
361 // in general the overall width of some decorated boxes is wider thean the inner box
362 // we could therefore calculate the real width for all sizes so that if the user wants
363 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
364 // however this makes problems when importing TeX code
365 // therefore only recalculate for the most common case that the box should not protrude
367 if (params_.inner_box
368 && ((width_string.find("1\\columnwidth") != string::npos
369 || width_string.find("1\\textwidth") != string::npos)
370 || width_string.find("1\\paperwidth") != string::npos
371 || width_string.find("1\\linewidth") != string::npos)) {
377 width_string += " - 2\\FrameSep - 2\\FrameRule";
380 width_string += " - 2\\fboxsep - 2\\fboxrule";
385 width_string += " - 2\\fboxsep - 0.8pt";
388 width_string += " - 2\\fboxsep - 1.6pt";
391 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
394 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
400 if (runparams.lastid != -1)
401 os.texrow().start(runparams.lastid, runparams.lastpos);
403 // adapt column/text width correctly also if paragraphs indented
404 if (stdwidth && !(buffer().params().paragraph_separation))
407 bool needendgroup = false;
412 if (thickness_string != defaultThick) {
413 os << "{\\FrameRule " << from_ascii(thickness_string);
414 if (separation_string != defaultSep)
415 os << "\\FrameSep " << from_ascii(separation_string);
417 if (separation_string != defaultSep && thickness_string == defaultThick)
418 os << "{\\FrameSep " << from_ascii(separation_string);
420 os << "\\begin{framed}%\n";
423 if (thickness_string != defaultThick) {
424 os << "{\\fboxrule " << from_ascii(thickness_string);
425 if (separation_string != defaultSep)
426 os << "\\fboxsep " << from_ascii(separation_string);
428 if (separation_string != defaultSep && thickness_string == defaultThick)
429 os << "{\\fboxsep " << from_ascii(separation_string);
430 if (!params_.inner_box && !width_string.empty()) {
431 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
432 os << maybeBeginL << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
434 needEndL = !maybeBeginL.empty();
437 // Special widths, see usrguide sec. 3.5
439 if (params_.special != "none") {
440 os << "[" << params_.width.value()
441 << '\\' << from_utf8(params_.special)
444 os << '[' << from_ascii(width_string)
446 // default horizontal alignment is 'c'
447 if (params_.hor_pos != 'c')
448 os << "[" << params_.hor_pos << "]";
450 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
451 os << maybeBeginL << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
452 needEndL = !maybeBeginL.empty();
454 if (!cprotect.empty() && contains(runparams.active_chars, '^')) {
455 // cprotect relies on ^ being on catcode 7
456 os << "\\begingroup\\catcode`\\^=7";
459 os << cprotect << "\\fbox";
465 if (!separation_string.empty() && separation_string != defaultSep)
466 os << "{\\fboxsep " << from_ascii(separation_string);
470 if (!separation_string.empty() && separation_string != defaultSep)
471 os << "{\\fboxsep " << from_ascii(separation_string);
475 if (thickness_string != defaultThick) {
476 os << "{\\fboxrule " << from_ascii(thickness_string);
477 if (separation_string != defaultSep) {
478 os << "\\fboxsep " << from_ascii(separation_string);
479 if (shadowsize_string != defaultShadow)
480 os << "\\shadowsize " << from_ascii(shadowsize_string);
482 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
483 os << "\\shadowsize " << from_ascii(shadowsize_string);
485 if (separation_string != defaultSep && thickness_string == defaultThick) {
486 os << "{\\fboxsep " << from_ascii(separation_string);
487 if (shadowsize_string != defaultShadow)
488 os << "\\shadowsize " << from_ascii(shadowsize_string);
490 if (shadowsize_string != defaultShadow
491 && separation_string == defaultSep
492 && thickness_string == defaultThick)
493 os << "{\\shadowsize " << from_ascii(shadowsize_string);
494 os << "\\shadowbox{";
497 // must be set later because e.g. the width settings only work when
498 // it is inside a minipage or parbox
500 needEndL = !maybeBeginL.empty();
503 if (thickness_string != defaultThick) {
504 os << "{\\fboxrule " << from_ascii(thickness_string);
505 if (separation_string != defaultSep)
506 os << "\\fboxsep " << from_ascii(separation_string);
508 if (separation_string != defaultSep && thickness_string == defaultThick)
509 os << "{\\fboxsep " << from_ascii(separation_string);
510 os << "\\doublebox{";
514 if (params_.inner_box) {
515 if (params_.use_parbox) {
516 if (params_.backgroundcolor != "none" && btype == Frameless) {
517 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
518 needEndL = !maybeBeginL.empty();
521 } else if (params_.use_makebox) {
522 if (!width_string.empty()) {
523 if (params_.backgroundcolor != "none") {
524 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
525 needEndL = !maybeBeginL.empty();
529 // output the width and horizontal position
530 if (params_.special != "none") {
531 os << "[" << params_.width.value()
532 << '\\' << from_utf8(params_.special)
535 os << '[' << from_ascii(width_string)
537 if (params_.hor_pos != 'c')
538 os << "[" << params_.hor_pos << "]";
540 if (params_.backgroundcolor != "none") {
541 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}";
542 needEndL = !maybeBeginL.empty();
550 if (params_.backgroundcolor != "none" && btype == Frameless) {
551 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
552 needEndL = !maybeBeginL.empty();
554 os << "\\begin{minipage}";
557 // output parameters for parbox and minipage
558 if (!params_.use_makebox) {
559 os << "[" << params_.pos << "]";
560 if (params_.height_special == "none") {
562 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
565 // set no optional argument when the value is the default "1\height"
566 // (special units like \height are handled as "in")
567 // but when the user has chosen a non-default inner_pos, the height
568 // must be given: \minipage[pos][height][inner-pos]{width}
569 if ((params_.height != Length("1in") ||
570 params_.height_special != "totalheight") ||
571 params_.inner_pos != params_.pos) {
573 os << "[" << params_.height.value()
574 << "\\" << from_utf8(params_.height_special) << "]";
577 if (params_.inner_pos != params_.pos)
578 os << "[" << params_.inner_pos << "]";
580 os << '{' << from_ascii(width_string) << '}';
581 if (params_.use_parbox)
586 } // end if inner_box
588 if (btype == Shaded) {
589 os << "\\begin{shaded}%\n";
592 InsetText::latex(os, runparams);
595 os << "\\end{shaded}";
597 if (params_.inner_box) {
598 if (params_.use_parbox || params_.use_makebox)
601 os << "%\n\\end{minipage}";
602 if (params_.backgroundcolor != "none" && btype == Frameless
603 && !(params_.use_makebox && width_string.empty()))
611 os << "\\end{framed}";
612 if (separation_string != defaultSep || thickness_string != defaultThick)
617 if (!params_.inner_box && !width_string.empty()
618 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
620 if (separation_string != defaultSep || thickness_string != defaultThick)
627 if (separation_string != defaultSep)
632 if (separation_string != defaultSep)
637 if (separation_string != defaultSep || thickness_string != defaultThick)
642 if (separation_string != defaultSep
643 || thickness_string != defaultThick
644 || shadowsize_string != defaultShadow)
656 int InsetBox::plaintext(odocstringstream & os,
657 OutputParams const & runparams, size_t max_length) const
659 BoxType const btype = boxtranslator().find(params_.type);
683 InsetText::plaintext(os, runparams, max_length);
714 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
718 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
720 return InsetText::docbook(os, runparams);
724 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
726 // construct attributes
727 string attrs = "class='" + params_.type + "'";
729 if (!params_.width.empty()) {
730 string w = params_.width.asHTMLString();
732 style += ("width: " + params_.width.asHTMLString() + "; ");
734 // The special heights don't really mean anything for us.
735 if (!params_.height.empty() && params_.height_special == "none")
736 style += ("height: " + params_.height.asHTMLString() + "; ");
738 attrs += " style='" + style + "'";
740 xs << html::StartTag("div", attrs);
741 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
742 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
743 xs << html::EndTag("div");
749 void InsetBox::validate(LaTeXFeatures & features) const
751 BoxType btype = boxtranslator().find(params_.type);
754 if (params_.backgroundcolor != "none")
755 features.require("xcolor");
758 features.require("calc");
759 features.require("framed");
762 features.require("calc");
763 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
764 features.require("xcolor");
770 features.require("calc");
771 features.require("fancybox");
774 features.require("color");
775 features.require("framed");
778 InsetCollapsible::validate(features);
782 string InsetBox::contextMenuName() const
784 return "context-box";
788 string InsetBox::params2string(InsetBoxParams const & params)
791 data << "box" << ' ';
797 void InsetBox::string2params(string const & in, InsetBoxParams & params)
802 istringstream data(in);
808 if (!lex || name != "box") {
809 LYXERR0("InsetBox::string2params(" << in << ")\n"
810 "Expected arg 1 to be \"box\"\n");
814 // This is part of the inset proper that is usually swallowed
815 // by Text::readInset
818 if (!lex || id != "Box") {
819 LYXERR0("InsetBox::string2params(" << in << ")\n"
820 "Expected arg 2 to be \"Box\"\n");
823 params = InsetBoxParams(string());
828 /////////////////////////////////////////////////////////////////////////
832 /////////////////////////////////////////////////////////////////////////
834 InsetBoxParams::InsetBoxParams(string const & label)
839 width(Length("100col%")),
844 height(Length("1in")),
845 height_special("totalheight"), // default is 1\\totalheight
846 thickness(Length(defaultThick)),
847 separation(Length(defaultSep)),
848 shadowsize(Length(defaultShadow)),
850 backgroundcolor("none")
854 void InsetBoxParams::write(ostream & os) const
856 os << "Box " << type << "\n";
857 os << "position \"" << pos << "\"\n";
858 os << "hor_pos \"" << hor_pos << "\"\n";
859 os << "has_inner_box " << inner_box << "\n";
860 os << "inner_pos \"" << inner_pos << "\"\n";
861 os << "use_parbox " << use_parbox << "\n";
862 os << "use_makebox " << use_makebox << "\n";
863 os << "width \"" << width.asString() << "\"\n";
864 os << "special \"" << special << "\"\n";
865 os << "height \"" << height.asString() << "\"\n";
866 os << "height_special \"" << height_special << "\"\n";
867 os << "thickness \"" << thickness.asString() << "\"\n";
868 os << "separation \"" << separation.asString() << "\"\n";
869 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
870 os << "framecolor \"" << framecolor << "\"\n";
871 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
875 void InsetBoxParams::read(Lexer & lex)
877 lex.setContext("InsetBoxParams::read");
879 lex >> "position" >> pos;
880 lex >> "hor_pos" >> hor_pos;
881 lex >> "has_inner_box" >> inner_box;
882 if (type == "Framed")
884 lex >> "inner_pos" >> inner_pos;
885 lex >> "use_parbox" >> use_parbox;
886 lex >> "use_makebox" >> use_makebox;
887 lex >> "width" >> width;
888 lex >> "special" >> special;
889 lex >> "height" >> height;
890 lex >> "height_special" >> height_special;
891 lex >> "thickness" >> thickness;
892 lex >> "separation" >> separation;
893 lex >> "shadowsize" >> shadowsize;
894 lex >> "framecolor" >> framecolor;
895 lex >> "backgroundcolor" >> backgroundcolor;