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 // in general the overall width of some decorated boxes is wider thean the inner box
354 // we could therefore calculate the real width for all sizes so that if the user wants
355 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
356 // however this makes problems when importing TeX code
357 // therefore only recalculate for the most common case that the box should not protrude
359 if (params_.inner_box
360 && ((width_string.find("1\\columnwidth") != string::npos
361 || width_string.find("1\\textwidth") != string::npos)
362 || width_string.find("1\\paperwidth") != string::npos
363 || width_string.find("1\\linewidth") != string::npos)) {
369 width_string += " - 2\\FrameSep - 2\\FrameRule";
372 width_string += " - 2\\fboxsep - 2\\fboxrule";
377 width_string += " - 2\\fboxsep - 0.8pt";
380 width_string += " - 2\\fboxsep - 1.6pt";
383 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
386 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
392 if (runparams.lastid != -1)
393 os.texrow().start(runparams.lastid, runparams.lastpos);
395 // adapt column/text width correctly also if paragraphs indented
396 if (stdwidth && !(buffer().params().paragraph_separation))
403 if (thickness_string != defaultThick) {
404 os << "{\\FrameRule " << from_ascii(thickness_string);
405 if (separation_string != defaultSep)
406 os << "\\FrameSep " << from_ascii(separation_string);
408 if (separation_string != defaultSep && thickness_string == defaultThick)
409 os << "{\\FrameSep " << from_ascii(separation_string);
411 os << "\\begin{framed}%\n";
414 if (thickness_string != defaultThick) {
415 os << "{\\fboxrule " << from_ascii(thickness_string);
416 if (separation_string != defaultSep)
417 os << "\\fboxsep " << from_ascii(separation_string);
419 if (separation_string != defaultSep && thickness_string == defaultThick)
420 os << "{\\fboxsep " << from_ascii(separation_string);
421 if (!params_.inner_box && !width_string.empty()) {
422 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
423 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
427 // Special widths, see usrguide sec. 3.5
429 if (params_.special != "none") {
430 os << "[" << params_.width.value()
431 << '\\' << from_utf8(params_.special)
434 os << '[' << from_ascii(width_string)
436 // default horizontal alignment is 'c'
437 if (params_.hor_pos != 'c')
438 os << "[" << params_.hor_pos << "]";
440 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
441 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
443 os << cprotect << "\\fbox";
448 if (!separation_string.empty() && separation_string != defaultSep)
449 os << "{\\fboxsep " << from_ascii(separation_string);
453 if (!separation_string.empty() && separation_string != defaultSep)
454 os << "{\\fboxsep " << from_ascii(separation_string);
458 if (thickness_string != defaultThick) {
459 os << "{\\fboxrule " << from_ascii(thickness_string);
460 if (separation_string != defaultSep) {
461 os << "\\fboxsep " << from_ascii(separation_string);
462 if (shadowsize_string != defaultShadow)
463 os << "\\shadowsize " << from_ascii(shadowsize_string);
465 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
466 os << "\\shadowsize " << from_ascii(shadowsize_string);
468 if (separation_string != defaultSep && thickness_string == defaultThick) {
469 os << "{\\fboxsep " << from_ascii(separation_string);
470 if (shadowsize_string != defaultShadow)
471 os << "\\shadowsize " << from_ascii(shadowsize_string);
473 if (shadowsize_string != defaultShadow
474 && separation_string == defaultSep
475 && thickness_string == defaultThick)
476 os << "{\\shadowsize " << from_ascii(shadowsize_string);
477 os << "\\shadowbox{";
480 // must be set later because e.g. the width settings only work when
481 // it is inside a minipage or parbox
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 << "\\colorbox{" << params_.backgroundcolor << "}{";
500 } else if (params_.use_makebox) {
501 if (!width_string.empty()) {
502 if (params_.backgroundcolor != "none")
503 os << "\\colorbox{" << params_.backgroundcolor << "}{";
506 // output the width and horizontal position
507 if (params_.special != "none") {
508 os << "[" << params_.width.value()
509 << '\\' << from_utf8(params_.special)
512 os << '[' << from_ascii(width_string)
514 if (params_.hor_pos != 'c')
515 os << "[" << params_.hor_pos << "]";
517 if (params_.backgroundcolor != "none")
518 os << "\\colorbox{" << params_.backgroundcolor << "}";
525 if (params_.backgroundcolor != "none" && btype == Frameless)
526 os << "\\colorbox{" << params_.backgroundcolor << "}{";
527 os << "\\begin{minipage}";
530 // output parameters for parbox and minipage
531 if (!params_.use_makebox) {
532 os << "[" << params_.pos << "]";
533 if (params_.height_special == "none") {
535 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
538 // set no optional argument when the value is the default "1\height"
539 // (special units like \height are handled as "in")
540 // but when the user has chosen a non-default inner_pos, the height
541 // must be given: \minipage[pos][height][inner-pos]{width}
542 if ((params_.height != Length("1in") ||
543 params_.height_special != "totalheight") ||
544 params_.inner_pos != params_.pos) {
546 os << "[" << params_.height.value()
547 << "\\" << from_utf8(params_.height_special) << "]";
550 if (params_.inner_pos != params_.pos)
551 os << "[" << params_.inner_pos << "]";
553 os << '{' << from_ascii(width_string) << '}';
554 if (params_.use_parbox)
559 } // end if inner_box
561 if (btype == Shaded) {
562 os << "\\begin{shaded}%\n";
565 InsetText::latex(os, runparams);
568 os << "\\end{shaded}";
570 if (params_.inner_box) {
571 if (params_.use_parbox || params_.use_makebox)
574 os << "%\n\\end{minipage}";
575 if (params_.backgroundcolor != "none" && btype == Frameless
576 && !(params_.use_makebox && width_string.empty()))
584 os << "\\end{framed}";
585 if (separation_string != defaultSep || thickness_string != defaultThick)
590 if (!params_.inner_box && !width_string.empty()
591 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
593 if (separation_string != defaultSep || thickness_string != defaultThick)
598 if (separation_string != defaultSep)
603 if (separation_string != defaultSep)
608 if (separation_string != defaultSep || thickness_string != defaultThick)
613 if (separation_string != defaultSep
614 || thickness_string != defaultThick
615 || shadowsize_string != defaultShadow)
625 int InsetBox::plaintext(odocstringstream & os,
626 OutputParams const & runparams, size_t max_length) const
628 BoxType const btype = boxtranslator().find(params_.type);
652 InsetText::plaintext(os, runparams, max_length);
683 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
687 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
689 return InsetText::docbook(os, runparams);
693 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
695 // construct attributes
696 string attrs = "class='" + params_.type + "'";
698 if (!params_.width.empty()) {
699 string w = params_.width.asHTMLString();
701 style += ("width: " + params_.width.asHTMLString() + "; ");
703 // The special heights don't really mean anything for us.
704 if (!params_.height.empty() && params_.height_special == "none")
705 style += ("height: " + params_.height.asHTMLString() + "; ");
707 attrs += " style='" + style + "'";
709 xs << html::StartTag("div", attrs);
710 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
711 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
712 xs << html::EndTag("div");
718 void InsetBox::validate(LaTeXFeatures & features) const
720 BoxType btype = boxtranslator().find(params_.type);
723 if (params_.backgroundcolor != "none")
724 features.require("xcolor");
727 features.require("calc");
728 features.require("framed");
731 features.require("calc");
732 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
733 features.require("xcolor");
739 features.require("calc");
740 features.require("fancybox");
743 features.require("color");
744 features.require("framed");
747 InsetCollapsible::validate(features);
751 string InsetBox::contextMenuName() const
753 return "context-box";
757 string InsetBox::params2string(InsetBoxParams const & params)
760 data << "box" << ' ';
766 void InsetBox::string2params(string const & in, InsetBoxParams & params)
771 istringstream data(in);
777 if (!lex || name != "box") {
778 LYXERR0("InsetBox::string2params(" << in << ")\n"
779 "Expected arg 1 to be \"box\"\n");
783 // This is part of the inset proper that is usually swallowed
784 // by Text::readInset
787 if (!lex || id != "Box") {
788 LYXERR0("InsetBox::string2params(" << in << ")\n"
789 "Expected arg 2 to be \"Box\"\n");
792 params = InsetBoxParams(string());
797 /////////////////////////////////////////////////////////////////////////
801 /////////////////////////////////////////////////////////////////////////
803 InsetBoxParams::InsetBoxParams(string const & label)
808 width(Length("100col%")),
813 height(Length("1in")),
814 height_special("totalheight"), // default is 1\\totalheight
815 thickness(Length(defaultThick)),
816 separation(Length(defaultSep)),
817 shadowsize(Length(defaultShadow)),
819 backgroundcolor("none")
823 void InsetBoxParams::write(ostream & os) const
825 os << "Box " << type << "\n";
826 os << "position \"" << pos << "\"\n";
827 os << "hor_pos \"" << hor_pos << "\"\n";
828 os << "has_inner_box " << inner_box << "\n";
829 os << "inner_pos \"" << inner_pos << "\"\n";
830 os << "use_parbox " << use_parbox << "\n";
831 os << "use_makebox " << use_makebox << "\n";
832 os << "width \"" << width.asString() << "\"\n";
833 os << "special \"" << special << "\"\n";
834 os << "height \"" << height.asString() << "\"\n";
835 os << "height_special \"" << height_special << "\"\n";
836 os << "thickness \"" << thickness.asString() << "\"\n";
837 os << "separation \"" << separation.asString() << "\"\n";
838 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
839 os << "framecolor \"" << framecolor << "\"\n";
840 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
844 void InsetBoxParams::read(Lexer & lex)
846 lex.setContext("InsetBoxParams::read");
848 lex >> "position" >> pos;
849 lex >> "hor_pos" >> hor_pos;
850 lex >> "has_inner_box" >> inner_box;
851 if (type == "Framed")
853 lex >> "inner_pos" >> inner_pos;
854 lex >> "use_parbox" >> use_parbox;
855 lex >> "use_makebox" >> use_makebox;
856 lex >> "width" >> width;
857 lex >> "special" >> special;
858 lex >> "height" >> height;
859 lex >> "height_special" >> height_special;
860 lex >> "thickness" >> thickness;
861 lex >> "separation" >> separation;
862 lex >> "shadowsize" >> shadowsize;
863 lex >> "framecolor" >> framecolor;
864 lex >> "backgroundcolor" >> backgroundcolor;