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"
30 #include "TextClass.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/Translator.h"
38 #include "frontends/Application.h"
43 using namespace lyx::support;
49 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
50 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
52 BoxTranslator initBoxtranslator()
54 BoxTranslator translator("Boxed", InsetBox::Boxed);
55 translator.addPair("Frameless", InsetBox::Frameless);
56 translator.addPair("Framed", InsetBox::Framed);
57 translator.addPair("ovalbox", InsetBox::ovalbox);
58 translator.addPair("Ovalbox", InsetBox::Ovalbox);
59 translator.addPair("Shadowbox", InsetBox::Shadowbox);
60 translator.addPair("Shaded", InsetBox::Shaded);
61 translator.addPair("Doublebox",InsetBox::Doublebox);
66 BoxTranslatorLoc initBoxtranslatorLoc()
68 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
69 translator.addPair(_("frameless"), InsetBox::Frameless);
70 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
71 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
72 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
73 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
74 translator.addPair(_("shaded background"), InsetBox::Shaded);
75 translator.addPair(_("double frame"), InsetBox::Doublebox);
80 BoxTranslator const & boxtranslator()
82 static BoxTranslator const translator = initBoxtranslator();
87 BoxTranslatorLoc const & boxtranslator_loc()
89 static BoxTranslatorLoc const translator = initBoxtranslatorLoc();
96 /////////////////////////////////////////////////////////////////////////
100 /////////////////////////////////////////////////////////////////////////
102 InsetBox::InsetBox(Buffer * buffer, string const & label)
103 : InsetCollapsable(buffer), params_(label)
107 docstring InsetBox::layoutName() const
110 return from_ascii("Box:" + params_.type);
114 void InsetBox::write(ostream & os) const
117 InsetCollapsable::write(os);
121 void InsetBox::read(Lexer & lex)
124 InsetCollapsable::read(lex);
128 void InsetBox::setButtonLabel()
130 BoxType const btype = boxtranslator().find(params_.type);
132 docstring const type = _("Box");
135 if (params_.inner_box) {
136 if (params_.use_parbox)
138 else if (params_.use_makebox)
139 inner = _("Makebox");
141 inner = _("Minipage");
145 if (btype != Frameless)
146 frame = boxtranslator_loc().find(btype);
149 if (inner.empty() && frame.empty())
151 else if (inner.empty())
152 label = bformat(_("%1$s (%2$s)"),
154 else if (frame.empty())
155 label = bformat(_("%1$s (%2$s)"),
158 label = bformat(_("%1$s (%2$s, %3$s)"),
162 // set the frame color for the inset if the type is Boxed
164 setFrameColor(lcolor.getFromLaTeXName(params_.framecolor));
166 setFrameColor(Color_collapsableframe);
170 bool InsetBox::hasFixedWidth() const
172 return !params_.width.empty() && params_.special == "none";
176 bool InsetBox::allowMultiPar() const
178 return (params_.inner_box && !params_.use_makebox)
179 || params_.type == "Shaded" || params_.type == "Framed";
183 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
185 // back up textwidth.
186 int textwidth_backup = m.base.textwidth;
188 m.base.textwidth = params_.width.inPixels(m.base);
189 InsetCollapsable::metrics(m, dim);
191 m.base.textwidth = textwidth_backup;
195 bool InsetBox::forcePlainLayout(idx_type) const
197 return (!params_.inner_box || params_.use_makebox)
198 && params_.type != "Shaded" && params_.type != "Framed";
202 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
204 // we only support background color for 3 types
205 if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
206 return getLayout().bgcolor();
208 if (params_.type == "Shaded") {
209 // FIXME: This hardcoded color is a hack!
210 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
211 return getLayout().bgcolor();
213 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
215 return getLayout().bgcolor();
219 if (params_.backgroundcolor != "none")
220 return lcolor.getFromLaTeXName(params_.backgroundcolor);
222 return getLayout().bgcolor();
226 LyXAlignment InsetBox::contentAlignment() const
228 if (!params_.use_makebox)
229 return LYX_ALIGN_NONE;
231 // The default value below is actually irrelevant
232 LyXAlignment align = LYX_ALIGN_NONE;
233 switch (params_.hor_pos) {
235 align = LYX_ALIGN_LEFT;
238 align = LYX_ALIGN_CENTER;
241 align = LYX_ALIGN_RIGHT;
244 align = LYX_ALIGN_BLOCK;
251 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
253 switch (cmd.action()) {
255 case LFUN_INSET_MODIFY: {
256 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
257 string const first_arg = cmd.getArg(0);
258 bool const change_type = first_arg == "changetype";
259 bool const for_box = first_arg == "box";
260 if (!change_type && !for_box) {
262 // this will not be handled higher up
266 cur.recordUndoInset(this);
268 params_.type = cmd.getArg(1);
269 // set a makebox if there is no inner box but Frameless was exectued
270 // otherwise the result would be a non existent box (no inner AND outer box)
271 // (this was LyX bug 8712)
272 if (params_.type == "Frameless" && !params_.inner_box) {
273 params_.use_makebox = true;
274 params_.inner_box = true;
276 // handle the opposite case
277 if (params_.type == "Boxed" && params_.use_makebox) {
278 params_.use_makebox = false;
279 params_.inner_box = false;
282 string2params(to_utf8(cmd.argument()), params_);
288 InsetCollapsable::doDispatch(cur, cmd);
294 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
295 FuncStatus & flag) const
297 switch (cmd.action()) {
299 case LFUN_INSET_MODIFY: {
300 string const first_arg = cmd.getArg(0);
301 if (first_arg == "changetype") {
302 string const type = cmd.getArg(1);
303 flag.setOnOff(type == params_.type);
304 flag.setEnabled(!params_.inner_box || type != "Framed");
307 if (first_arg == "box") {
308 flag.setEnabled(true);
311 return InsetCollapsable::getStatus(cur, cmd, flag);
314 case LFUN_INSET_DIALOG_UPDATE:
315 flag.setEnabled(true);
319 return InsetCollapsable::getStatus(cur, cmd, flag);
324 const string defaultThick = "0.4pt";
325 const string defaultSep = "3pt";
326 const string defaultShadow = "4pt";
328 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
330 BoxType btype = boxtranslator().find(params_.type);
332 string width_string = params_.width.asLatexString();
333 string thickness_string = params_.thickness.asLatexString();
334 string separation_string = params_.separation.asLatexString();
335 string shadowsize_string = params_.shadowsize.asLatexString();
336 bool stdwidth = false;
337 // in general the overall width of some decorated boxes is wider thean the inner box
338 // we could therefore calculate the real width for all sizes so that if the user wants
339 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
340 // however this makes problems when importing TeX code
341 // therefore only recalculate for the most common case that the box should not protrude
343 if (params_.inner_box
344 && ((width_string.find("1\\columnwidth") != string::npos
345 || width_string.find("1\\textwidth") != string::npos)
346 || width_string.find("1\\paperwidth") != string::npos
347 || width_string.find("1\\linewidth") != string::npos)) {
353 width_string += " - 2\\FrameSep - 2\\FrameRule";
356 width_string += " - 2\\fboxsep - 2\\fboxrule";
361 width_string += " - 2\\fboxsep - 0.8pt";
364 width_string += " - 2\\fboxsep - 1.6pt";
367 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
370 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
376 if (runparams.lastid != -1)
377 os.texrow().start(runparams.lastid, runparams.lastpos);
379 // adapt column/text width correctly also if paragraphs indented
380 if (stdwidth && !(buffer().params().paragraph_separation))
387 if (thickness_string != defaultThick) {
388 os << "{\\FrameRule " << from_ascii(thickness_string);
389 if (separation_string != defaultSep)
390 os << "\\FrameSep " << from_ascii(separation_string);
392 if (separation_string != defaultSep && thickness_string == defaultThick)
393 os << "{\\FrameSep " << from_ascii(separation_string);
395 os << "\\begin{framed}%\n";
398 if (thickness_string != defaultThick) {
399 os << "{\\fboxrule " << from_ascii(thickness_string);
400 if (separation_string != defaultSep)
401 os << "\\fboxsep " << from_ascii(separation_string);
403 if (separation_string != defaultSep && thickness_string == defaultThick)
404 os << "{\\fboxsep " << from_ascii(separation_string);
405 if (!params_.inner_box && !width_string.empty()) {
406 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
407 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
411 // Special widths, see usrguide sec. 3.5
413 if (params_.special != "none") {
414 os << "[" << params_.width.value()
415 << '\\' << from_utf8(params_.special)
418 os << '[' << from_ascii(width_string)
420 // default horizontal alignment is 'c'
421 if (params_.hor_pos != 'c')
422 os << "[" << params_.hor_pos << "]";
424 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
425 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
432 if (!separation_string.empty() && separation_string != defaultSep)
433 os << "{\\fboxsep " << from_ascii(separation_string);
437 if (!separation_string.empty() && separation_string != defaultSep)
438 os << "{\\fboxsep " << from_ascii(separation_string);
442 if (thickness_string != defaultThick) {
443 os << "{\\fboxrule " << from_ascii(thickness_string);
444 if (separation_string != defaultSep) {
445 os << "\\fboxsep " << from_ascii(separation_string);
446 if (shadowsize_string != defaultShadow)
447 os << "\\shadowsize " << from_ascii(shadowsize_string);
449 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
450 os << "\\shadowsize " << from_ascii(shadowsize_string);
452 if (separation_string != defaultSep && thickness_string == defaultThick) {
453 os << "{\\fboxsep " << from_ascii(separation_string);
454 if (shadowsize_string != defaultShadow)
455 os << "\\shadowsize " << from_ascii(shadowsize_string);
457 if (shadowsize_string != defaultShadow
458 && separation_string == defaultSep
459 && thickness_string == defaultThick)
460 os << "{\\shadowsize " << from_ascii(shadowsize_string);
461 os << "\\shadowbox{";
464 // must be set later because e.g. the width settings only work when
465 // it is inside a minipage or parbox
468 if (thickness_string != defaultThick) {
469 os << "{\\fboxrule " << from_ascii(thickness_string);
470 if (separation_string != defaultSep)
471 os << "\\fboxsep " << from_ascii(separation_string);
473 if (separation_string != defaultSep && thickness_string == defaultThick)
474 os << "{\\fboxsep " << from_ascii(separation_string);
475 os << "\\doublebox{";
479 if (params_.inner_box) {
480 if (params_.use_parbox) {
481 if (params_.backgroundcolor != "none" && btype == Frameless)
482 os << "\\colorbox{" << params_.backgroundcolor << "}{";
484 } else if (params_.use_makebox) {
485 if (!width_string.empty()) {
486 if (params_.backgroundcolor != "none")
487 os << "\\colorbox{" << params_.backgroundcolor << "}{";
490 // output the width and horizontal position
491 if (params_.special != "none") {
492 os << "[" << params_.width.value()
493 << '\\' << from_utf8(params_.special)
496 os << '[' << from_ascii(width_string)
498 if (params_.hor_pos != 'c')
499 os << "[" << params_.hor_pos << "]";
501 if (params_.backgroundcolor != "none")
502 os << "\\colorbox{" << params_.backgroundcolor << "}";
509 if (params_.backgroundcolor != "none" && btype == Frameless)
510 os << "\\colorbox{" << params_.backgroundcolor << "}{";
511 os << "\\begin{minipage}";
514 // output parameters for parbox and minipage
515 if (!params_.use_makebox) {
516 os << "[" << params_.pos << "]";
517 if (params_.height_special == "none") {
519 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
522 // set no optional argument when the value is the default "1\height"
523 // (special units like \height are handled as "in")
524 // but when the user has chosen a non-default inner_pos, the height
525 // must be given: \minipage[pos][height][inner-pos]{width}
526 if ((params_.height != Length("1in") ||
527 params_.height_special != "totalheight") ||
528 params_.inner_pos != params_.pos) {
530 os << "[" << params_.height.value()
531 << "\\" << from_utf8(params_.height_special) << "]";
534 if (params_.inner_pos != params_.pos)
535 os << "[" << params_.inner_pos << "]";
537 os << '{' << from_ascii(width_string) << '}';
538 if (params_.use_parbox)
543 } // end if inner_box
545 if (btype == Shaded) {
546 os << "\\begin{shaded}%\n";
549 InsetText::latex(os, runparams);
552 os << "\\end{shaded}";
554 if (params_.inner_box) {
555 if (params_.use_parbox || params_.use_makebox)
558 os << "%\n\\end{minipage}";
559 if (params_.backgroundcolor != "none" && btype == Frameless
560 && !(params_.use_makebox && width_string.empty()))
568 os << "\\end{framed}";
569 if (separation_string != defaultSep || thickness_string != defaultThick)
574 if (!params_.inner_box && !width_string.empty()
575 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
577 if (separation_string != defaultSep || thickness_string != defaultThick)
582 if (separation_string != defaultSep)
587 if (separation_string != defaultSep)
592 if (separation_string != defaultSep || thickness_string != defaultThick)
597 if (separation_string != defaultSep
598 || thickness_string != defaultThick
599 || shadowsize_string != defaultShadow)
609 int InsetBox::plaintext(odocstringstream & os,
610 OutputParams const & runparams, size_t max_length) const
612 BoxType const btype = boxtranslator().find(params_.type);
636 InsetText::plaintext(os, runparams, max_length);
667 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
671 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
673 return InsetText::docbook(os, runparams);
677 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
679 // construct attributes
680 string attrs = "class='" + params_.type + "'";
682 if (!params_.width.empty()) {
683 string w = params_.width.asHTMLString();
685 style += ("width: " + params_.width.asHTMLString() + "; ");
687 // The special heights don't really mean anything for us.
688 if (!params_.height.empty() && params_.height_special == "none")
689 style += ("height: " + params_.height.asHTMLString() + "; ");
691 attrs += " style='" + style + "'";
693 xs << html::StartTag("div", attrs);
694 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
695 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
696 xs << html::EndTag("div");
702 void InsetBox::validate(LaTeXFeatures & features) const
704 BoxType btype = boxtranslator().find(params_.type);
707 if (params_.backgroundcolor != "none")
708 features.require("xcolor");
711 features.require("calc");
712 features.require("framed");
715 features.require("calc");
716 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
717 features.require("xcolor");
723 features.require("calc");
724 features.require("fancybox");
727 features.require("color");
728 features.require("framed");
731 InsetCollapsable::validate(features);
735 string InsetBox::contextMenuName() const
737 return "context-box";
741 string InsetBox::params2string(InsetBoxParams const & params)
744 data << "box" << ' ';
750 void InsetBox::string2params(string const & in, InsetBoxParams & params)
755 istringstream data(in);
761 if (!lex || name != "box") {
762 LYXERR0("InsetBox::string2params(" << in << ")\n"
763 "Expected arg 1 to be \"box\"\n");
767 // This is part of the inset proper that is usually swallowed
768 // by Text::readInset
771 if (!lex || id != "Box") {
772 LYXERR0("InsetBox::string2params(" << in << ")\n"
773 "Expected arg 2 to be \"Box\"\n");
776 params = InsetBoxParams(string());
781 /////////////////////////////////////////////////////////////////////////
785 /////////////////////////////////////////////////////////////////////////
787 InsetBoxParams::InsetBoxParams(string const & label)
792 width(Length("100col%")),
797 height(Length("1in")),
798 height_special("totalheight"), // default is 1\\totalheight
799 thickness(Length(defaultThick)),
800 separation(Length(defaultSep)),
801 shadowsize(Length(defaultShadow)),
803 backgroundcolor("none")
807 void InsetBoxParams::write(ostream & os) const
809 os << "Box " << type << "\n";
810 os << "position \"" << pos << "\"\n";
811 os << "hor_pos \"" << hor_pos << "\"\n";
812 os << "has_inner_box " << inner_box << "\n";
813 os << "inner_pos \"" << inner_pos << "\"\n";
814 os << "use_parbox " << use_parbox << "\n";
815 os << "use_makebox " << use_makebox << "\n";
816 os << "width \"" << width.asString() << "\"\n";
817 os << "special \"" << special << "\"\n";
818 os << "height \"" << height.asString() << "\"\n";
819 os << "height_special \"" << height_special << "\"\n";
820 os << "thickness \"" << thickness.asString() << "\"\n";
821 os << "separation \"" << separation.asString() << "\"\n";
822 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
823 os << "framecolor \"" << framecolor << "\"\n";
824 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
828 void InsetBoxParams::read(Lexer & lex)
830 lex.setContext("InsetBoxParams::read");
832 lex >> "position" >> pos;
833 lex >> "hor_pos" >> hor_pos;
834 lex >> "has_inner_box" >> inner_box;
835 if (type == "Framed")
837 lex >> "inner_pos" >> inner_pos;
838 lex >> "use_parbox" >> use_parbox;
839 lex >> "use_makebox" >> use_makebox;
840 lex >> "width" >> width;
841 lex >> "special" >> special;
842 lex >> "height" >> height;
843 lex >> "height_special" >> height_special;
844 lex >> "thickness" >> thickness;
845 lex >> "separation" >> separation;
846 lex >> "shadowsize" >> shadowsize;
847 lex >> "framecolor" >> framecolor;
848 lex >> "backgroundcolor" >> backgroundcolor;