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 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
206 // we only support background color for 3 types
207 if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
208 return getLayout().bgcolor();
210 if (params_.type == "Shaded") {
211 // FIXME: This hardcoded color is a hack!
212 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
213 return getLayout().bgcolor();
215 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
217 return getLayout().bgcolor();
221 if (params_.backgroundcolor != "none")
222 return lcolor.getFromLaTeXName(params_.backgroundcolor);
224 return getLayout().bgcolor();
228 LyXAlignment InsetBox::contentAlignment() const
230 if (!params_.use_makebox)
231 return LYX_ALIGN_NONE;
233 // The default value below is actually irrelevant
234 LyXAlignment align = LYX_ALIGN_NONE;
235 switch (params_.hor_pos) {
237 align = LYX_ALIGN_LEFT;
240 align = LYX_ALIGN_CENTER;
243 align = LYX_ALIGN_RIGHT;
246 align = LYX_ALIGN_BLOCK;
253 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
255 switch (cmd.action()) {
257 case LFUN_INSET_MODIFY: {
258 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
259 string const first_arg = cmd.getArg(0);
260 bool const change_type = first_arg == "changetype";
261 bool const for_box = first_arg == "box";
262 if (!change_type && !for_box) {
264 // this will not be handled higher up
268 cur.recordUndoInset(this);
270 params_.type = cmd.getArg(1);
271 // set a makebox if there is no inner box but Frameless was exectued
272 // otherwise the result would be a non existent box (no inner AND outer box)
273 // (this was LyX bug 8712)
274 if (params_.type == "Frameless" && !params_.inner_box) {
275 params_.use_makebox = true;
276 params_.inner_box = true;
278 // handle the opposite case
279 if (params_.type == "Boxed" && params_.use_makebox) {
280 params_.use_makebox = false;
281 params_.inner_box = false;
284 string2params(to_utf8(cmd.argument()), params_);
290 InsetCollapsible::doDispatch(cur, cmd);
296 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
297 FuncStatus & flag) const
299 switch (cmd.action()) {
301 case LFUN_INSET_MODIFY: {
302 string const first_arg = cmd.getArg(0);
303 if (first_arg == "changetype") {
304 string const type = cmd.getArg(1);
305 flag.setOnOff(type == params_.type);
306 flag.setEnabled(!params_.inner_box || type != "Framed");
309 if (first_arg == "box") {
310 flag.setEnabled(true);
313 return InsetCollapsible::getStatus(cur, cmd, flag);
316 case LFUN_INSET_DIALOG_UPDATE:
317 flag.setEnabled(true);
321 return InsetCollapsible::getStatus(cur, cmd, flag);
326 const string defaultThick = "0.4pt";
327 const string defaultSep = "3pt";
328 const string defaultShadow = "4pt";
330 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
332 BoxType btype = boxtranslator().find(params_.type);
334 string width_string = params_.width.asLatexString();
335 string thickness_string = params_.thickness.asLatexString();
336 string separation_string = params_.separation.asLatexString();
337 string shadowsize_string = params_.shadowsize.asLatexString();
338 bool stdwidth = false;
339 string const cprotect = hasCProtectContent() ? "\\cprotect" : string();
340 // in general the overall width of some decorated boxes is wider thean the inner box
341 // we could therefore calculate the real width for all sizes so that if the user wants
342 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
343 // however this makes problems when importing TeX code
344 // therefore only recalculate for the most common case that the box should not protrude
346 if (params_.inner_box
347 && ((width_string.find("1\\columnwidth") != string::npos
348 || width_string.find("1\\textwidth") != string::npos)
349 || width_string.find("1\\paperwidth") != string::npos
350 || width_string.find("1\\linewidth") != string::npos)) {
356 width_string += " - 2\\FrameSep - 2\\FrameRule";
359 width_string += " - 2\\fboxsep - 2\\fboxrule";
364 width_string += " - 2\\fboxsep - 0.8pt";
367 width_string += " - 2\\fboxsep - 1.6pt";
370 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
373 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
379 if (runparams.lastid != -1)
380 os.texrow().start(runparams.lastid, runparams.lastpos);
382 // adapt column/text width correctly also if paragraphs indented
383 if (stdwidth && !(buffer().params().paragraph_separation))
390 if (thickness_string != defaultThick) {
391 os << "{\\FrameRule " << from_ascii(thickness_string);
392 if (separation_string != defaultSep)
393 os << "\\FrameSep " << from_ascii(separation_string);
395 if (separation_string != defaultSep && thickness_string == defaultThick)
396 os << "{\\FrameSep " << from_ascii(separation_string);
398 os << "\\begin{framed}%\n";
401 if (thickness_string != defaultThick) {
402 os << "{\\fboxrule " << from_ascii(thickness_string);
403 if (separation_string != defaultSep)
404 os << "\\fboxsep " << from_ascii(separation_string);
406 if (separation_string != defaultSep && thickness_string == defaultThick)
407 os << "{\\fboxsep " << from_ascii(separation_string);
408 if (!params_.inner_box && !width_string.empty()) {
409 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
410 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
414 // Special widths, see usrguide sec. 3.5
416 if (params_.special != "none") {
417 os << "[" << params_.width.value()
418 << '\\' << from_utf8(params_.special)
421 os << '[' << from_ascii(width_string)
423 // default horizontal alignment is 'c'
424 if (params_.hor_pos != 'c')
425 os << "[" << params_.hor_pos << "]";
427 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
428 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
430 os << cprotect << "\\fbox";
435 if (!separation_string.empty() && separation_string != defaultSep)
436 os << "{\\fboxsep " << from_ascii(separation_string);
440 if (!separation_string.empty() && separation_string != defaultSep)
441 os << "{\\fboxsep " << from_ascii(separation_string);
445 if (thickness_string != defaultThick) {
446 os << "{\\fboxrule " << from_ascii(thickness_string);
447 if (separation_string != defaultSep) {
448 os << "\\fboxsep " << from_ascii(separation_string);
449 if (shadowsize_string != defaultShadow)
450 os << "\\shadowsize " << from_ascii(shadowsize_string);
452 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
453 os << "\\shadowsize " << from_ascii(shadowsize_string);
455 if (separation_string != defaultSep && thickness_string == defaultThick) {
456 os << "{\\fboxsep " << from_ascii(separation_string);
457 if (shadowsize_string != defaultShadow)
458 os << "\\shadowsize " << from_ascii(shadowsize_string);
460 if (shadowsize_string != defaultShadow
461 && separation_string == defaultSep
462 && thickness_string == defaultThick)
463 os << "{\\shadowsize " << from_ascii(shadowsize_string);
464 os << "\\shadowbox{";
467 // must be set later because e.g. the width settings only work when
468 // it is inside a minipage or parbox
471 if (thickness_string != defaultThick) {
472 os << "{\\fboxrule " << from_ascii(thickness_string);
473 if (separation_string != defaultSep)
474 os << "\\fboxsep " << from_ascii(separation_string);
476 if (separation_string != defaultSep && thickness_string == defaultThick)
477 os << "{\\fboxsep " << from_ascii(separation_string);
478 os << "\\doublebox{";
482 if (params_.inner_box) {
483 if (params_.use_parbox) {
484 if (params_.backgroundcolor != "none" && btype == Frameless)
485 os << "\\colorbox{" << params_.backgroundcolor << "}{";
487 } else if (params_.use_makebox) {
488 if (!width_string.empty()) {
489 if (params_.backgroundcolor != "none")
490 os << "\\colorbox{" << params_.backgroundcolor << "}{";
493 // output the width and horizontal position
494 if (params_.special != "none") {
495 os << "[" << params_.width.value()
496 << '\\' << from_utf8(params_.special)
499 os << '[' << from_ascii(width_string)
501 if (params_.hor_pos != 'c')
502 os << "[" << params_.hor_pos << "]";
504 if (params_.backgroundcolor != "none")
505 os << "\\colorbox{" << params_.backgroundcolor << "}";
512 if (params_.backgroundcolor != "none" && btype == Frameless)
513 os << "\\colorbox{" << params_.backgroundcolor << "}{";
514 os << "\\begin{minipage}";
517 // output parameters for parbox and minipage
518 if (!params_.use_makebox) {
519 os << "[" << params_.pos << "]";
520 if (params_.height_special == "none") {
522 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
525 // set no optional argument when the value is the default "1\height"
526 // (special units like \height are handled as "in")
527 // but when the user has chosen a non-default inner_pos, the height
528 // must be given: \minipage[pos][height][inner-pos]{width}
529 if ((params_.height != Length("1in") ||
530 params_.height_special != "totalheight") ||
531 params_.inner_pos != params_.pos) {
533 os << "[" << params_.height.value()
534 << "\\" << from_utf8(params_.height_special) << "]";
537 if (params_.inner_pos != params_.pos)
538 os << "[" << params_.inner_pos << "]";
540 os << '{' << from_ascii(width_string) << '}';
541 if (params_.use_parbox)
546 } // end if inner_box
548 if (btype == Shaded) {
549 os << "\\begin{shaded}%\n";
552 InsetText::latex(os, runparams);
555 os << "\\end{shaded}";
557 if (params_.inner_box) {
558 if (params_.use_parbox || params_.use_makebox)
561 os << "%\n\\end{minipage}";
562 if (params_.backgroundcolor != "none" && btype == Frameless
563 && !(params_.use_makebox && width_string.empty()))
571 os << "\\end{framed}";
572 if (separation_string != defaultSep || thickness_string != defaultThick)
577 if (!params_.inner_box && !width_string.empty()
578 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
580 if (separation_string != defaultSep || thickness_string != defaultThick)
585 if (separation_string != defaultSep)
590 if (separation_string != defaultSep)
595 if (separation_string != defaultSep || thickness_string != defaultThick)
600 if (separation_string != defaultSep
601 || thickness_string != defaultThick
602 || shadowsize_string != defaultShadow)
612 int InsetBox::plaintext(odocstringstream & os,
613 OutputParams const & runparams, size_t max_length) const
615 BoxType const btype = boxtranslator().find(params_.type);
639 InsetText::plaintext(os, runparams, max_length);
670 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
674 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
676 return InsetText::docbook(os, runparams);
680 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
682 // construct attributes
683 string attrs = "class='" + params_.type + "'";
685 if (!params_.width.empty()) {
686 string w = params_.width.asHTMLString();
688 style += ("width: " + params_.width.asHTMLString() + "; ");
690 // The special heights don't really mean anything for us.
691 if (!params_.height.empty() && params_.height_special == "none")
692 style += ("height: " + params_.height.asHTMLString() + "; ");
694 attrs += " style='" + style + "'";
696 xs << html::StartTag("div", attrs);
697 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
698 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
699 xs << html::EndTag("div");
705 void InsetBox::validate(LaTeXFeatures & features) const
707 BoxType btype = boxtranslator().find(params_.type);
710 if (params_.backgroundcolor != "none")
711 features.require("xcolor");
714 features.require("calc");
715 features.require("framed");
718 features.require("calc");
719 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
720 features.require("xcolor");
726 features.require("calc");
727 features.require("fancybox");
730 features.require("color");
731 features.require("framed");
734 InsetCollapsible::validate(features);
738 string InsetBox::contextMenuName() const
740 return "context-box";
744 string InsetBox::params2string(InsetBoxParams const & params)
747 data << "box" << ' ';
753 void InsetBox::string2params(string const & in, InsetBoxParams & params)
758 istringstream data(in);
764 if (!lex || name != "box") {
765 LYXERR0("InsetBox::string2params(" << in << ")\n"
766 "Expected arg 1 to be \"box\"\n");
770 // This is part of the inset proper that is usually swallowed
771 // by Text::readInset
774 if (!lex || id != "Box") {
775 LYXERR0("InsetBox::string2params(" << in << ")\n"
776 "Expected arg 2 to be \"Box\"\n");
779 params = InsetBoxParams(string());
784 /////////////////////////////////////////////////////////////////////////
788 /////////////////////////////////////////////////////////////////////////
790 InsetBoxParams::InsetBoxParams(string const & label)
795 width(Length("100col%")),
800 height(Length("1in")),
801 height_special("totalheight"), // default is 1\\totalheight
802 thickness(Length(defaultThick)),
803 separation(Length(defaultSep)),
804 shadowsize(Length(defaultShadow)),
806 backgroundcolor("none")
810 void InsetBoxParams::write(ostream & os) const
812 os << "Box " << type << "\n";
813 os << "position \"" << pos << "\"\n";
814 os << "hor_pos \"" << hor_pos << "\"\n";
815 os << "has_inner_box " << inner_box << "\n";
816 os << "inner_pos \"" << inner_pos << "\"\n";
817 os << "use_parbox " << use_parbox << "\n";
818 os << "use_makebox " << use_makebox << "\n";
819 os << "width \"" << width.asString() << "\"\n";
820 os << "special \"" << special << "\"\n";
821 os << "height \"" << height.asString() << "\"\n";
822 os << "height_special \"" << height_special << "\"\n";
823 os << "thickness \"" << thickness.asString() << "\"\n";
824 os << "separation \"" << separation.asString() << "\"\n";
825 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
826 os << "framecolor \"" << framecolor << "\"\n";
827 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
831 void InsetBoxParams::read(Lexer & lex)
833 lex.setContext("InsetBoxParams::read");
835 lex >> "position" >> pos;
836 lex >> "hor_pos" >> hor_pos;
837 lex >> "has_inner_box" >> inner_box;
838 if (type == "Framed")
840 lex >> "inner_pos" >> inner_pos;
841 lex >> "use_parbox" >> use_parbox;
842 lex >> "use_makebox" >> use_makebox;
843 lex >> "width" >> width;
844 lex >> "special" >> special;
845 lex >> "height" >> height;
846 lex >> "height_special" >> height_special;
847 lex >> "thickness" >> thickness;
848 lex >> "separation" >> separation;
849 lex >> "shadowsize" >> shadowsize;
850 lex >> "framecolor" >> framecolor;
851 lex >> "backgroundcolor" >> backgroundcolor;