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 // in general the overall width of some decorated boxes is wider thean the inner box
342 // we could therefore calculate the real width for all sizes so that if the user wants
343 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
344 // however this makes problems when importing TeX code
345 // therefore only recalculate for the most common case that the box should not protrude
347 if (params_.inner_box
348 && ((width_string.find("1\\columnwidth") != string::npos
349 || width_string.find("1\\textwidth") != string::npos)
350 || width_string.find("1\\paperwidth") != string::npos
351 || width_string.find("1\\linewidth") != string::npos)) {
357 width_string += " - 2\\FrameSep - 2\\FrameRule";
360 width_string += " - 2\\fboxsep - 2\\fboxrule";
365 width_string += " - 2\\fboxsep - 0.8pt";
368 width_string += " - 2\\fboxsep - 1.6pt";
371 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
374 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
380 if (runparams.lastid != -1)
381 os.texrow().start(runparams.lastid, runparams.lastpos);
383 // adapt column/text width correctly also if paragraphs indented
384 if (stdwidth && !(buffer().params().paragraph_separation))
391 if (thickness_string != defaultThick) {
392 os << "{\\FrameRule " << from_ascii(thickness_string);
393 if (separation_string != defaultSep)
394 os << "\\FrameSep " << from_ascii(separation_string);
396 if (separation_string != defaultSep && thickness_string == defaultThick)
397 os << "{\\FrameSep " << from_ascii(separation_string);
399 os << "\\begin{framed}%\n";
402 if (thickness_string != defaultThick) {
403 os << "{\\fboxrule " << from_ascii(thickness_string);
404 if (separation_string != defaultSep)
405 os << "\\fboxsep " << from_ascii(separation_string);
407 if (separation_string != defaultSep && thickness_string == defaultThick)
408 os << "{\\fboxsep " << from_ascii(separation_string);
409 if (!params_.inner_box && !width_string.empty()) {
410 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
411 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
415 // Special widths, see usrguide sec. 3.5
417 if (params_.special != "none") {
418 os << "[" << params_.width.value()
419 << '\\' << from_utf8(params_.special)
422 os << '[' << from_ascii(width_string)
424 // default horizontal alignment is 'c'
425 if (params_.hor_pos != 'c')
426 os << "[" << params_.hor_pos << "]";
428 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
429 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
436 if (!separation_string.empty() && separation_string != defaultSep)
437 os << "{\\fboxsep " << from_ascii(separation_string);
441 if (!separation_string.empty() && separation_string != defaultSep)
442 os << "{\\fboxsep " << from_ascii(separation_string);
446 if (thickness_string != defaultThick) {
447 os << "{\\fboxrule " << from_ascii(thickness_string);
448 if (separation_string != defaultSep) {
449 os << "\\fboxsep " << from_ascii(separation_string);
450 if (shadowsize_string != defaultShadow)
451 os << "\\shadowsize " << from_ascii(shadowsize_string);
453 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
454 os << "\\shadowsize " << from_ascii(shadowsize_string);
456 if (separation_string != defaultSep && thickness_string == defaultThick) {
457 os << "{\\fboxsep " << from_ascii(separation_string);
458 if (shadowsize_string != defaultShadow)
459 os << "\\shadowsize " << from_ascii(shadowsize_string);
461 if (shadowsize_string != defaultShadow
462 && separation_string == defaultSep
463 && thickness_string == defaultThick)
464 os << "{\\shadowsize " << from_ascii(shadowsize_string);
465 os << "\\shadowbox{";
468 // must be set later because e.g. the width settings only work when
469 // it is inside a minipage or parbox
472 if (thickness_string != defaultThick) {
473 os << "{\\fboxrule " << from_ascii(thickness_string);
474 if (separation_string != defaultSep)
475 os << "\\fboxsep " << from_ascii(separation_string);
477 if (separation_string != defaultSep && thickness_string == defaultThick)
478 os << "{\\fboxsep " << from_ascii(separation_string);
479 os << "\\doublebox{";
483 if (params_.inner_box) {
484 if (params_.use_parbox) {
485 if (params_.backgroundcolor != "none" && btype == Frameless)
486 os << "\\colorbox{" << params_.backgroundcolor << "}{";
488 } else if (params_.use_makebox) {
489 if (!width_string.empty()) {
490 if (params_.backgroundcolor != "none")
491 os << "\\colorbox{" << params_.backgroundcolor << "}{";
494 // output the width and horizontal position
495 if (params_.special != "none") {
496 os << "[" << params_.width.value()
497 << '\\' << from_utf8(params_.special)
500 os << '[' << from_ascii(width_string)
502 if (params_.hor_pos != 'c')
503 os << "[" << params_.hor_pos << "]";
505 if (params_.backgroundcolor != "none")
506 os << "\\colorbox{" << params_.backgroundcolor << "}";
513 if (params_.backgroundcolor != "none" && btype == Frameless)
514 os << "\\colorbox{" << params_.backgroundcolor << "}{";
515 os << "\\begin{minipage}";
518 // output parameters for parbox and minipage
519 if (!params_.use_makebox) {
520 os << "[" << params_.pos << "]";
521 if (params_.height_special == "none") {
523 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
526 // set no optional argument when the value is the default "1\height"
527 // (special units like \height are handled as "in")
528 // but when the user has chosen a non-default inner_pos, the height
529 // must be given: \minipage[pos][height][inner-pos]{width}
530 if ((params_.height != Length("1in") ||
531 params_.height_special != "totalheight") ||
532 params_.inner_pos != params_.pos) {
534 os << "[" << params_.height.value()
535 << "\\" << from_utf8(params_.height_special) << "]";
538 if (params_.inner_pos != params_.pos)
539 os << "[" << params_.inner_pos << "]";
541 os << '{' << from_ascii(width_string) << '}';
542 if (params_.use_parbox)
547 } // end if inner_box
549 if (btype == Shaded) {
550 os << "\\begin{shaded}%\n";
553 InsetText::latex(os, runparams);
556 os << "\\end{shaded}";
558 if (params_.inner_box) {
559 if (params_.use_parbox || params_.use_makebox)
562 os << "%\n\\end{minipage}";
563 if (params_.backgroundcolor != "none" && btype == Frameless
564 && !(params_.use_makebox && width_string.empty()))
572 os << "\\end{framed}";
573 if (separation_string != defaultSep || thickness_string != defaultThick)
578 if (!params_.inner_box && !width_string.empty()
579 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
581 if (separation_string != defaultSep || thickness_string != defaultThick)
586 if (separation_string != defaultSep)
591 if (separation_string != defaultSep)
596 if (separation_string != defaultSep || thickness_string != defaultThick)
601 if (separation_string != defaultSep
602 || thickness_string != defaultThick
603 || shadowsize_string != defaultShadow)
613 int InsetBox::plaintext(odocstringstream & os,
614 OutputParams const & runparams, size_t max_length) const
616 BoxType const btype = boxtranslator().find(params_.type);
640 InsetText::plaintext(os, runparams, max_length);
671 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
675 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
677 return InsetText::docbook(os, runparams);
681 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
683 // construct attributes
684 string attrs = "class='" + params_.type + "'";
686 if (!params_.width.empty()) {
687 string w = params_.width.asHTMLString();
689 style += ("width: " + params_.width.asHTMLString() + "; ");
691 // The special heights don't really mean anything for us.
692 if (!params_.height.empty() && params_.height_special == "none")
693 style += ("height: " + params_.height.asHTMLString() + "; ");
695 attrs += " style='" + style + "'";
697 xs << html::StartTag("div", attrs);
698 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
699 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
700 xs << html::EndTag("div");
706 void InsetBox::validate(LaTeXFeatures & features) const
708 BoxType btype = boxtranslator().find(params_.type);
711 if (params_.backgroundcolor != "none")
712 features.require("xcolor");
715 features.require("calc");
716 features.require("framed");
719 features.require("calc");
720 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
721 features.require("xcolor");
727 features.require("calc");
728 features.require("fancybox");
731 features.require("color");
732 features.require("framed");
735 InsetCollapsible::validate(features);
739 string InsetBox::contextMenuName() const
741 return "context-box";
745 string InsetBox::params2string(InsetBoxParams const & params)
748 data << "box" << ' ';
754 void InsetBox::string2params(string const & in, InsetBoxParams & params)
759 istringstream data(in);
765 if (!lex || name != "box") {
766 LYXERR0("InsetBox::string2params(" << in << ")\n"
767 "Expected arg 1 to be \"box\"\n");
771 // This is part of the inset proper that is usually swallowed
772 // by Text::readInset
775 if (!lex || id != "Box") {
776 LYXERR0("InsetBox::string2params(" << in << ")\n"
777 "Expected arg 2 to be \"Box\"\n");
780 params = InsetBoxParams(string());
785 /////////////////////////////////////////////////////////////////////////
789 /////////////////////////////////////////////////////////////////////////
791 InsetBoxParams::InsetBoxParams(string const & label)
796 width(Length("100col%")),
801 height(Length("1in")),
802 height_special("totalheight"), // default is 1\\totalheight
803 thickness(Length(defaultThick)),
804 separation(Length(defaultSep)),
805 shadowsize(Length(defaultShadow)),
807 backgroundcolor("none")
811 void InsetBoxParams::write(ostream & os) const
813 os << "Box " << type << "\n";
814 os << "position \"" << pos << "\"\n";
815 os << "hor_pos \"" << hor_pos << "\"\n";
816 os << "has_inner_box " << inner_box << "\n";
817 os << "inner_pos \"" << inner_pos << "\"\n";
818 os << "use_parbox " << use_parbox << "\n";
819 os << "use_makebox " << use_makebox << "\n";
820 os << "width \"" << width.asString() << "\"\n";
821 os << "special \"" << special << "\"\n";
822 os << "height \"" << height.asString() << "\"\n";
823 os << "height_special \"" << height_special << "\"\n";
824 os << "thickness \"" << thickness.asString() << "\"\n";
825 os << "separation \"" << separation.asString() << "\"\n";
826 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
827 os << "framecolor \"" << framecolor << "\"\n";
828 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
832 void InsetBoxParams::read(Lexer & lex)
834 lex.setContext("InsetBoxParams::read");
836 lex >> "position" >> pos;
837 lex >> "hor_pos" >> hor_pos;
838 lex >> "has_inner_box" >> inner_box;
839 if (type == "Framed")
841 lex >> "inner_pos" >> inner_pos;
842 lex >> "use_parbox" >> use_parbox;
843 lex >> "use_makebox" >> use_makebox;
844 lex >> "width" >> width;
845 lex >> "special" >> special;
846 lex >> "height" >> height;
847 lex >> "height_special" >> height_special;
848 lex >> "thickness" >> thickness;
849 lex >> "separation" >> separation;
850 lex >> "shadowsize" >> shadowsize;
851 lex >> "framecolor" >> framecolor;
852 lex >> "backgroundcolor" >> backgroundcolor;