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 & m, Dimension & dim) const
187 // back up textwidth.
188 int textwidth_backup = m.base.textwidth;
190 m.base.textwidth = params_.width.inPixels(m.base);
191 InsetCollapsible::metrics(m, dim);
193 m.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 // Custom horizontal alignment is only allowed with a fixed width
231 // and if either makebox or no inner box are used
232 if (params_.width.empty() || !(params_.use_makebox || !params_.inner_box))
233 return LYX_ALIGN_NONE;
235 // The default value below is actually irrelevant
236 LyXAlignment align = LYX_ALIGN_NONE;
237 switch (params_.hor_pos) {
239 align = LYX_ALIGN_LEFT;
242 align = LYX_ALIGN_CENTER;
245 align = LYX_ALIGN_RIGHT;
248 align = LYX_ALIGN_BLOCK;
255 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
257 switch (cmd.action()) {
259 case LFUN_INSET_MODIFY: {
260 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
261 string const first_arg = cmd.getArg(0);
262 bool const change_type = first_arg == "changetype";
263 bool const for_box = first_arg == "box";
264 if (!change_type && !for_box) {
266 // this will not be handled higher up
270 cur.recordUndoInset(this);
272 params_.type = cmd.getArg(1);
273 // set a makebox if there is no inner box but Frameless was exectued
274 // otherwise the result would be a non existent box (no inner AND outer box)
275 // (this was LyX bug 8712)
276 if (params_.type == "Frameless" && !params_.inner_box) {
277 params_.use_makebox = true;
278 params_.inner_box = true;
280 // handle the opposite case
281 if (params_.type == "Boxed" && params_.use_makebox) {
282 params_.use_makebox = false;
283 params_.inner_box = false;
286 string2params(to_utf8(cmd.argument()), params_);
292 InsetCollapsible::doDispatch(cur, cmd);
298 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
299 FuncStatus & flag) const
301 switch (cmd.action()) {
303 case LFUN_INSET_MODIFY: {
304 string const first_arg = cmd.getArg(0);
305 if (first_arg == "changetype") {
306 string const type = cmd.getArg(1);
307 flag.setOnOff(type == params_.type);
308 flag.setEnabled(!params_.inner_box || type != "Framed");
311 if (first_arg == "box") {
312 flag.setEnabled(true);
315 return InsetCollapsible::getStatus(cur, cmd, flag);
318 case LFUN_INSET_DIALOG_UPDATE:
319 flag.setEnabled(true);
323 return InsetCollapsible::getStatus(cur, cmd, flag);
328 const string defaultThick = "0.4pt";
329 const string defaultSep = "3pt";
330 const string defaultShadow = "4pt";
332 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
334 BoxType btype = boxtranslator().find(params_.type);
336 string width_string = params_.width.asLatexString();
337 string thickness_string = params_.thickness.asLatexString();
338 string separation_string = params_.separation.asLatexString();
339 string shadowsize_string = params_.shadowsize.asLatexString();
340 bool stdwidth = false;
341 // Colored boxes in RTL need to be wrapped into \beginL...\endL
344 bool needEndL = false;
345 if (!runparams.isFullUnicode() && runparams.local_font->isRightToLeft()) {
346 maybeBeginL = "\\beginL";
347 maybeEndL = "\\endL";
349 // in general the overall width of some decorated boxes is wider thean the inner box
350 // we could therefore calculate the real width for all sizes so that if the user wants
351 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
352 // however this makes problems when importing TeX code
353 // therefore only recalculate for the most common case that the box should not protrude
355 if (params_.inner_box
356 && ((width_string.find("1\\columnwidth") != string::npos
357 || width_string.find("1\\textwidth") != string::npos)
358 || width_string.find("1\\paperwidth") != string::npos
359 || width_string.find("1\\linewidth") != string::npos)) {
365 width_string += " - 2\\FrameSep - 2\\FrameRule";
368 width_string += " - 2\\fboxsep - 2\\fboxrule";
373 width_string += " - 2\\fboxsep - 0.8pt";
376 width_string += " - 2\\fboxsep - 1.6pt";
379 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
382 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
388 if (runparams.lastid != -1)
389 os.texrow().start(runparams.lastid, runparams.lastpos);
391 // adapt column/text width correctly also if paragraphs indented
392 if (stdwidth && !(buffer().params().paragraph_separation))
399 if (thickness_string != defaultThick) {
400 os << "{\\FrameRule " << from_ascii(thickness_string);
401 if (separation_string != defaultSep)
402 os << "\\FrameSep " << from_ascii(separation_string);
404 if (separation_string != defaultSep && thickness_string == defaultThick)
405 os << "{\\FrameSep " << from_ascii(separation_string);
407 os << "\\begin{framed}%\n";
410 if (thickness_string != defaultThick) {
411 os << "{\\fboxrule " << from_ascii(thickness_string);
412 if (separation_string != defaultSep)
413 os << "\\fboxsep " << from_ascii(separation_string);
415 if (separation_string != defaultSep && thickness_string == defaultThick)
416 os << "{\\fboxsep " << from_ascii(separation_string);
417 if (!params_.inner_box && !width_string.empty()) {
418 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
419 os << maybeBeginL << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
421 needEndL = !maybeBeginL.empty();
424 // Special widths, see usrguide sec. 3.5
426 if (params_.special != "none") {
427 os << "[" << params_.width.value()
428 << '\\' << from_utf8(params_.special)
431 os << '[' << from_ascii(width_string)
433 // default horizontal alignment is 'c'
434 if (params_.hor_pos != 'c')
435 os << "[" << params_.hor_pos << "]";
437 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
438 os << maybeBeginL << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
439 needEndL = !maybeBeginL.empty();
446 if (!separation_string.empty() && separation_string != defaultSep)
447 os << "{\\fboxsep " << from_ascii(separation_string);
451 if (!separation_string.empty() && separation_string != defaultSep)
452 os << "{\\fboxsep " << from_ascii(separation_string);
456 if (thickness_string != defaultThick) {
457 os << "{\\fboxrule " << from_ascii(thickness_string);
458 if (separation_string != defaultSep) {
459 os << "\\fboxsep " << from_ascii(separation_string);
460 if (shadowsize_string != defaultShadow)
461 os << "\\shadowsize " << from_ascii(shadowsize_string);
463 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
464 os << "\\shadowsize " << from_ascii(shadowsize_string);
466 if (separation_string != defaultSep && thickness_string == defaultThick) {
467 os << "{\\fboxsep " << from_ascii(separation_string);
468 if (shadowsize_string != defaultShadow)
469 os << "\\shadowsize " << from_ascii(shadowsize_string);
471 if (shadowsize_string != defaultShadow
472 && separation_string == defaultSep
473 && thickness_string == defaultThick)
474 os << "{\\shadowsize " << from_ascii(shadowsize_string);
475 os << "\\shadowbox{";
478 // must be set later because e.g. the width settings only work when
479 // it is inside a minipage or parbox
481 needEndL = !maybeBeginL.empty();
484 if (thickness_string != defaultThick) {
485 os << "{\\fboxrule " << from_ascii(thickness_string);
486 if (separation_string != defaultSep)
487 os << "\\fboxsep " << from_ascii(separation_string);
489 if (separation_string != defaultSep && thickness_string == defaultThick)
490 os << "{\\fboxsep " << from_ascii(separation_string);
491 os << "\\doublebox{";
495 if (params_.inner_box) {
496 if (params_.use_parbox) {
497 if (params_.backgroundcolor != "none" && btype == Frameless) {
498 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
499 needEndL = !maybeBeginL.empty();
502 } else if (params_.use_makebox) {
503 if (!width_string.empty()) {
504 if (params_.backgroundcolor != "none") {
505 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
506 needEndL = !maybeBeginL.empty();
510 // output the width and horizontal position
511 if (params_.special != "none") {
512 os << "[" << params_.width.value()
513 << '\\' << from_utf8(params_.special)
516 os << '[' << from_ascii(width_string)
518 if (params_.hor_pos != 'c')
519 os << "[" << params_.hor_pos << "]";
521 if (params_.backgroundcolor != "none") {
522 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}";
523 needEndL = !maybeBeginL.empty();
531 if (params_.backgroundcolor != "none" && btype == Frameless) {
532 os << maybeBeginL << "\\colorbox{" << params_.backgroundcolor << "}{";
533 needEndL = !maybeBeginL.empty();
535 os << "\\begin{minipage}";
538 // output parameters for parbox and minipage
539 if (!params_.use_makebox) {
540 os << "[" << params_.pos << "]";
541 if (params_.height_special == "none") {
543 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
546 // set no optional argument when the value is the default "1\height"
547 // (special units like \height are handled as "in")
548 // but when the user has chosen a non-default inner_pos, the height
549 // must be given: \minipage[pos][height][inner-pos]{width}
550 if ((params_.height != Length("1in") ||
551 params_.height_special != "totalheight") ||
552 params_.inner_pos != params_.pos) {
554 os << "[" << params_.height.value()
555 << "\\" << from_utf8(params_.height_special) << "]";
558 if (params_.inner_pos != params_.pos)
559 os << "[" << params_.inner_pos << "]";
561 os << '{' << from_ascii(width_string) << '}';
562 if (params_.use_parbox)
567 } // end if inner_box
569 if (btype == Shaded) {
570 os << "\\begin{shaded}%\n";
573 InsetText::latex(os, runparams);
576 os << "\\end{shaded}";
578 if (params_.inner_box) {
579 if (params_.use_parbox || params_.use_makebox)
582 os << "%\n\\end{minipage}";
583 if (params_.backgroundcolor != "none" && btype == Frameless
584 && !(params_.use_makebox && width_string.empty()))
592 os << "\\end{framed}";
593 if (separation_string != defaultSep || thickness_string != defaultThick)
598 if (!params_.inner_box && !width_string.empty()
599 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
601 if (separation_string != defaultSep || thickness_string != defaultThick)
606 if (separation_string != defaultSep)
611 if (separation_string != defaultSep)
616 if (separation_string != defaultSep || thickness_string != defaultThick)
621 if (separation_string != defaultSep
622 || thickness_string != defaultThick
623 || shadowsize_string != defaultShadow)
635 int InsetBox::plaintext(odocstringstream & os,
636 OutputParams const & runparams, size_t max_length) const
638 BoxType const btype = boxtranslator().find(params_.type);
662 InsetText::plaintext(os, runparams, max_length);
693 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
697 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
699 return InsetText::docbook(os, runparams);
703 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
705 // construct attributes
706 string attrs = "class='" + params_.type + "'";
708 if (!params_.width.empty()) {
709 string w = params_.width.asHTMLString();
711 style += ("width: " + params_.width.asHTMLString() + "; ");
713 // The special heights don't really mean anything for us.
714 if (!params_.height.empty() && params_.height_special == "none")
715 style += ("height: " + params_.height.asHTMLString() + "; ");
717 attrs += " style='" + style + "'";
719 xs << html::StartTag("div", attrs);
720 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
721 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
722 xs << html::EndTag("div");
728 void InsetBox::validate(LaTeXFeatures & features) const
730 BoxType btype = boxtranslator().find(params_.type);
733 if (params_.backgroundcolor != "none")
734 features.require("xcolor");
737 features.require("calc");
738 features.require("framed");
741 features.require("calc");
742 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
743 features.require("xcolor");
749 features.require("calc");
750 features.require("fancybox");
753 features.require("color");
754 features.require("framed");
757 InsetCollapsible::validate(features);
761 string InsetBox::contextMenuName() const
763 return "context-box";
767 string InsetBox::params2string(InsetBoxParams const & params)
770 data << "box" << ' ';
776 void InsetBox::string2params(string const & in, InsetBoxParams & params)
781 istringstream data(in);
787 if (!lex || name != "box") {
788 LYXERR0("InsetBox::string2params(" << in << ")\n"
789 "Expected arg 1 to be \"box\"\n");
793 // This is part of the inset proper that is usually swallowed
794 // by Text::readInset
797 if (!lex || id != "Box") {
798 LYXERR0("InsetBox::string2params(" << in << ")\n"
799 "Expected arg 2 to be \"Box\"\n");
802 params = InsetBoxParams(string());
807 /////////////////////////////////////////////////////////////////////////
811 /////////////////////////////////////////////////////////////////////////
813 InsetBoxParams::InsetBoxParams(string const & label)
818 width(Length("100col%")),
823 height(Length("1in")),
824 height_special("totalheight"), // default is 1\\totalheight
825 thickness(Length(defaultThick)),
826 separation(Length(defaultSep)),
827 shadowsize(Length(defaultShadow)),
829 backgroundcolor("none")
833 void InsetBoxParams::write(ostream & os) const
835 os << "Box " << type << "\n";
836 os << "position \"" << pos << "\"\n";
837 os << "hor_pos \"" << hor_pos << "\"\n";
838 os << "has_inner_box " << inner_box << "\n";
839 os << "inner_pos \"" << inner_pos << "\"\n";
840 os << "use_parbox " << use_parbox << "\n";
841 os << "use_makebox " << use_makebox << "\n";
842 os << "width \"" << width.asString() << "\"\n";
843 os << "special \"" << special << "\"\n";
844 os << "height \"" << height.asString() << "\"\n";
845 os << "height_special \"" << height_special << "\"\n";
846 os << "thickness \"" << thickness.asString() << "\"\n";
847 os << "separation \"" << separation.asString() << "\"\n";
848 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
849 os << "framecolor \"" << framecolor << "\"\n";
850 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
854 void InsetBoxParams::read(Lexer & lex)
856 lex.setContext("InsetBoxParams::read");
858 lex >> "position" >> pos;
859 lex >> "hor_pos" >> hor_pos;
860 lex >> "has_inner_box" >> inner_box;
861 if (type == "Framed")
863 lex >> "inner_pos" >> inner_pos;
864 lex >> "use_parbox" >> use_parbox;
865 lex >> "use_makebox" >> use_makebox;
866 lex >> "width" >> width;
867 lex >> "special" >> special;
868 lex >> "height" >> height;
869 lex >> "height_special" >> height_special;
870 lex >> "thickness" >> thickness;
871 lex >> "separation" >> separation;
872 lex >> "shadowsize" >> shadowsize;
873 lex >> "framecolor" >> framecolor;
874 lex >> "backgroundcolor" >> backgroundcolor;