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 if (!params_.use_makebox)
242 return LYX_ALIGN_NONE;
244 // The default value below is actually irrelevant
245 LyXAlignment align = LYX_ALIGN_NONE;
246 switch (params_.hor_pos) {
248 align = LYX_ALIGN_LEFT;
251 align = LYX_ALIGN_CENTER;
254 align = LYX_ALIGN_RIGHT;
257 align = LYX_ALIGN_BLOCK;
264 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
266 switch (cmd.action()) {
268 case LFUN_INSET_MODIFY: {
269 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
270 string const first_arg = cmd.getArg(0);
271 bool const change_type = first_arg == "changetype";
272 bool const for_box = first_arg == "box";
273 if (!change_type && !for_box) {
275 // this will not be handled higher up
279 cur.recordUndoInset(this);
281 params_.type = cmd.getArg(1);
282 // set a makebox if there is no inner box but Frameless was exectued
283 // otherwise the result would be a non existent box (no inner AND outer box)
284 // (this was LyX bug 8712)
285 if (params_.type == "Frameless" && !params_.inner_box) {
286 params_.use_makebox = true;
287 params_.inner_box = true;
289 // handle the opposite case
290 if (params_.type == "Boxed" && params_.use_makebox) {
291 params_.use_makebox = false;
292 params_.inner_box = false;
295 string2params(to_utf8(cmd.argument()), params_);
301 InsetCollapsible::doDispatch(cur, cmd);
307 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
308 FuncStatus & flag) const
310 switch (cmd.action()) {
312 case LFUN_INSET_MODIFY: {
313 string const first_arg = cmd.getArg(0);
314 if (first_arg == "changetype") {
315 string const type = cmd.getArg(1);
316 flag.setOnOff(type == params_.type);
317 flag.setEnabled(!params_.inner_box || type != "Framed");
320 if (first_arg == "box") {
321 flag.setEnabled(true);
324 return InsetCollapsible::getStatus(cur, cmd, flag);
327 case LFUN_INSET_DIALOG_UPDATE:
328 flag.setEnabled(true);
332 return InsetCollapsible::getStatus(cur, cmd, flag);
337 const string defaultThick = "0.4pt";
338 const string defaultSep = "3pt";
339 const string defaultShadow = "4pt";
341 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
343 BoxType btype = boxtranslator().find(params_.type);
345 string width_string = params_.width.asLatexString();
346 string thickness_string = params_.thickness.asLatexString();
347 string separation_string = params_.separation.asLatexString();
348 string shadowsize_string = params_.shadowsize.asLatexString();
349 bool stdwidth = false;
350 string const cprotect = hasCProtectContent(runparams.moving_arg) ? "\\cprotect" : string();
351 // in general the overall width of some decorated boxes is wider thean the inner box
352 // we could therefore calculate the real width for all sizes so that if the user wants
353 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
354 // however this makes problems when importing TeX code
355 // therefore only recalculate for the most common case that the box should not protrude
357 if (params_.inner_box
358 && ((width_string.find("1\\columnwidth") != string::npos
359 || width_string.find("1\\textwidth") != string::npos)
360 || width_string.find("1\\paperwidth") != string::npos
361 || width_string.find("1\\linewidth") != string::npos)) {
367 width_string += " - 2\\FrameSep - 2\\FrameRule";
370 width_string += " - 2\\fboxsep - 2\\fboxrule";
375 width_string += " - 2\\fboxsep - 0.8pt";
378 width_string += " - 2\\fboxsep - 1.6pt";
381 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
384 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
390 if (runparams.lastid != -1)
391 os.texrow().start(runparams.lastid, runparams.lastpos);
393 // adapt column/text width correctly also if paragraphs indented
394 if (stdwidth && !(buffer().params().paragraph_separation))
401 if (thickness_string != defaultThick) {
402 os << "{\\FrameRule " << from_ascii(thickness_string);
403 if (separation_string != defaultSep)
404 os << "\\FrameSep " << from_ascii(separation_string);
406 if (separation_string != defaultSep && thickness_string == defaultThick)
407 os << "{\\FrameSep " << from_ascii(separation_string);
409 os << "\\begin{framed}%\n";
412 if (thickness_string != defaultThick) {
413 os << "{\\fboxrule " << from_ascii(thickness_string);
414 if (separation_string != defaultSep)
415 os << "\\fboxsep " << from_ascii(separation_string);
417 if (separation_string != defaultSep && thickness_string == defaultThick)
418 os << "{\\fboxsep " << from_ascii(separation_string);
419 if (!params_.inner_box && !width_string.empty()) {
420 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
421 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
425 // Special widths, see usrguide sec. 3.5
427 if (params_.special != "none") {
428 os << "[" << params_.width.value()
429 << '\\' << from_utf8(params_.special)
432 os << '[' << from_ascii(width_string)
434 // default horizontal alignment is 'c'
435 if (params_.hor_pos != 'c')
436 os << "[" << params_.hor_pos << "]";
438 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
439 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
441 os << cprotect << "\\fbox";
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
482 if (thickness_string != defaultThick) {
483 os << "{\\fboxrule " << from_ascii(thickness_string);
484 if (separation_string != defaultSep)
485 os << "\\fboxsep " << from_ascii(separation_string);
487 if (separation_string != defaultSep && thickness_string == defaultThick)
488 os << "{\\fboxsep " << from_ascii(separation_string);
489 os << "\\doublebox{";
493 if (params_.inner_box) {
494 if (params_.use_parbox) {
495 if (params_.backgroundcolor != "none" && btype == Frameless)
496 os << "\\colorbox{" << params_.backgroundcolor << "}{";
498 } else if (params_.use_makebox) {
499 if (!width_string.empty()) {
500 if (params_.backgroundcolor != "none")
501 os << "\\colorbox{" << params_.backgroundcolor << "}{";
504 // output the width and horizontal position
505 if (params_.special != "none") {
506 os << "[" << params_.width.value()
507 << '\\' << from_utf8(params_.special)
510 os << '[' << from_ascii(width_string)
512 if (params_.hor_pos != 'c')
513 os << "[" << params_.hor_pos << "]";
515 if (params_.backgroundcolor != "none")
516 os << "\\colorbox{" << params_.backgroundcolor << "}";
523 if (params_.backgroundcolor != "none" && btype == Frameless)
524 os << "\\colorbox{" << params_.backgroundcolor << "}{";
525 os << "\\begin{minipage}";
528 // output parameters for parbox and minipage
529 if (!params_.use_makebox) {
530 os << "[" << params_.pos << "]";
531 if (params_.height_special == "none") {
533 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
536 // set no optional argument when the value is the default "1\height"
537 // (special units like \height are handled as "in")
538 // but when the user has chosen a non-default inner_pos, the height
539 // must be given: \minipage[pos][height][inner-pos]{width}
540 if ((params_.height != Length("1in") ||
541 params_.height_special != "totalheight") ||
542 params_.inner_pos != params_.pos) {
544 os << "[" << params_.height.value()
545 << "\\" << from_utf8(params_.height_special) << "]";
548 if (params_.inner_pos != params_.pos)
549 os << "[" << params_.inner_pos << "]";
551 os << '{' << from_ascii(width_string) << '}';
552 if (params_.use_parbox)
557 } // end if inner_box
559 if (btype == Shaded) {
560 os << "\\begin{shaded}%\n";
563 InsetText::latex(os, runparams);
566 os << "\\end{shaded}";
568 if (params_.inner_box) {
569 if (params_.use_parbox || params_.use_makebox)
572 os << "%\n\\end{minipage}";
573 if (params_.backgroundcolor != "none" && btype == Frameless
574 && !(params_.use_makebox && width_string.empty()))
582 os << "\\end{framed}";
583 if (separation_string != defaultSep || thickness_string != defaultThick)
588 if (!params_.inner_box && !width_string.empty()
589 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
591 if (separation_string != defaultSep || thickness_string != defaultThick)
596 if (separation_string != defaultSep)
601 if (separation_string != defaultSep)
606 if (separation_string != defaultSep || thickness_string != defaultThick)
611 if (separation_string != defaultSep
612 || thickness_string != defaultThick
613 || shadowsize_string != defaultShadow)
623 int InsetBox::plaintext(odocstringstream & os,
624 OutputParams const & runparams, size_t max_length) const
626 BoxType const btype = boxtranslator().find(params_.type);
650 InsetText::plaintext(os, runparams, max_length);
681 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
685 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
687 return InsetText::docbook(os, runparams);
691 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
693 // construct attributes
694 string attrs = "class='" + params_.type + "'";
696 if (!params_.width.empty()) {
697 string w = params_.width.asHTMLString();
699 style += ("width: " + params_.width.asHTMLString() + "; ");
701 // The special heights don't really mean anything for us.
702 if (!params_.height.empty() && params_.height_special == "none")
703 style += ("height: " + params_.height.asHTMLString() + "; ");
705 attrs += " style='" + style + "'";
707 xs << html::StartTag("div", attrs);
708 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
709 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
710 xs << html::EndTag("div");
716 void InsetBox::validate(LaTeXFeatures & features) const
718 BoxType btype = boxtranslator().find(params_.type);
721 if (params_.backgroundcolor != "none")
722 features.require("xcolor");
725 features.require("calc");
726 features.require("framed");
729 features.require("calc");
730 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
731 features.require("xcolor");
737 features.require("calc");
738 features.require("fancybox");
741 features.require("color");
742 features.require("framed");
745 InsetCollapsible::validate(features);
749 string InsetBox::contextMenuName() const
751 return "context-box";
755 string InsetBox::params2string(InsetBoxParams const & params)
758 data << "box" << ' ';
764 void InsetBox::string2params(string const & in, InsetBoxParams & params)
769 istringstream data(in);
775 if (!lex || name != "box") {
776 LYXERR0("InsetBox::string2params(" << in << ")\n"
777 "Expected arg 1 to be \"box\"\n");
781 // This is part of the inset proper that is usually swallowed
782 // by Text::readInset
785 if (!lex || id != "Box") {
786 LYXERR0("InsetBox::string2params(" << in << ")\n"
787 "Expected arg 2 to be \"Box\"\n");
790 params = InsetBoxParams(string());
795 /////////////////////////////////////////////////////////////////////////
799 /////////////////////////////////////////////////////////////////////////
801 InsetBoxParams::InsetBoxParams(string const & label)
806 width(Length("100col%")),
811 height(Length("1in")),
812 height_special("totalheight"), // default is 1\\totalheight
813 thickness(Length(defaultThick)),
814 separation(Length(defaultSep)),
815 shadowsize(Length(defaultShadow)),
817 backgroundcolor("none")
821 void InsetBoxParams::write(ostream & os) const
823 os << "Box " << type << "\n";
824 os << "position \"" << pos << "\"\n";
825 os << "hor_pos \"" << hor_pos << "\"\n";
826 os << "has_inner_box " << inner_box << "\n";
827 os << "inner_pos \"" << inner_pos << "\"\n";
828 os << "use_parbox " << use_parbox << "\n";
829 os << "use_makebox " << use_makebox << "\n";
830 os << "width \"" << width.asString() << "\"\n";
831 os << "special \"" << special << "\"\n";
832 os << "height \"" << height.asString() << "\"\n";
833 os << "height_special \"" << height_special << "\"\n";
834 os << "thickness \"" << thickness.asString() << "\"\n";
835 os << "separation \"" << separation.asString() << "\"\n";
836 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
837 os << "framecolor \"" << framecolor << "\"\n";
838 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
842 void InsetBoxParams::read(Lexer & lex)
844 lex.setContext("InsetBoxParams::read");
846 lex >> "position" >> pos;
847 lex >> "hor_pos" >> hor_pos;
848 lex >> "has_inner_box" >> inner_box;
849 if (type == "Framed")
851 lex >> "inner_pos" >> inner_pos;
852 lex >> "use_parbox" >> use_parbox;
853 lex >> "use_makebox" >> use_makebox;
854 lex >> "width" >> width;
855 lex >> "special" >> special;
856 lex >> "height" >> height;
857 lex >> "height_special" >> height_special;
858 lex >> "thickness" >> thickness;
859 lex >> "separation" >> separation;
860 lex >> "shadowsize" >> shadowsize;
861 lex >> "framecolor" >> framecolor;
862 lex >> "backgroundcolor" >> backgroundcolor;