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)"),
164 bool InsetBox::hasFixedWidth() const
166 return !params_.width.empty();
170 bool InsetBox::allowMultiPar() const
172 return (params_.inner_box && !params_.use_makebox)
173 || params_.type == "Shaded" || params_.type == "Framed";
177 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
179 // back up textwidth.
180 int textwidth_backup = m.base.textwidth;
182 m.base.textwidth = params_.width.inPixels(m.base);
183 InsetCollapsable::metrics(m, dim);
185 m.base.textwidth = textwidth_backup;
189 bool InsetBox::forcePlainLayout(idx_type) const
191 return (!params_.inner_box || params_.use_makebox)
192 && params_.type != "Shaded" && params_.type != "Framed";
196 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
198 // we only support background color for 3 types
199 if (params_.type != "Shaded" && params_.type != "Frameless" && params_.type != "Boxed")
200 return getLayout().bgcolor();
201 if (params_.type == "Shaded") {
202 // FIXME: This hardcoded color is a hack!
203 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
204 return getLayout().bgcolor();
206 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
208 return getLayout().bgcolor();
211 if (params_.backgroundcolor == "none")
212 return getLayout().bgcolor();
213 ColorCode boxbackground = lcolor.getFromLyXName(params_.backgroundcolor);
214 return boxbackground;
216 return getLayout().bgcolor();
220 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
222 switch (cmd.action()) {
224 case LFUN_INSET_MODIFY: {
225 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
226 string const first_arg = cmd.getArg(0);
227 bool const change_type = first_arg == "changetype";
228 bool const for_box = first_arg == "box";
229 if (!change_type && !for_box) {
231 // this will not be handled higher up
235 cur.recordUndoInset(this);
237 params_.type = cmd.getArg(1);
239 string2params(to_utf8(cmd.argument()), params_);
245 InsetCollapsable::doDispatch(cur, cmd);
251 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
252 FuncStatus & flag) const
254 switch (cmd.action()) {
256 case LFUN_INSET_MODIFY: {
257 string const first_arg = cmd.getArg(0);
258 if (first_arg == "changetype") {
259 string const type = cmd.getArg(1);
260 flag.setOnOff(type == params_.type);
261 flag.setEnabled(!params_.inner_box || type != "Framed");
264 if (first_arg == "box") {
265 flag.setEnabled(true);
268 return InsetCollapsable::getStatus(cur, cmd, flag);
271 case LFUN_INSET_DIALOG_UPDATE:
272 flag.setEnabled(true);
276 return InsetCollapsable::getStatus(cur, cmd, flag);
281 const string defaultThick = "0.4pt";
282 const string defaultSep = "3pt";
283 const string defaultShadow = "4pt";
285 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
287 BoxType btype = boxtranslator().find(params_.type);
289 string width_string = params_.width.asLatexString();
290 string thickness_string = params_.thickness.asLatexString();
291 string separation_string = params_.separation.asLatexString();
292 string shadowsize_string = params_.shadowsize.asLatexString();
293 bool stdwidth = false;
294 // in general the overall width of some decorated boxes is wider thean the inner box
295 // we could therefore calculate the real width for all sizes so that if the user wants
296 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
297 // however this makes problems when importing TeX code
298 // therefore only recalculate for the most common case that the box should not protrude
300 if (params_.inner_box
301 && ((width_string.find("1\\columnwidth") != string::npos
302 || width_string.find("1\\textwidth") != string::npos)
303 || width_string.find("1\\paperwidth") != string::npos
304 || width_string.find("1\\linewidth") != string::npos)) {
310 width_string += " - 2\\FrameSep - 2\\FrameRule";
313 width_string += " - 2\\fboxsep - 2\\fboxrule";
318 width_string += " - 2\\fboxsep - 0.8pt";
321 width_string += " - 2\\fboxsep - 1.6pt";
324 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
327 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
333 if (runparams.lastid != -1)
334 os.texrow().start(runparams.lastid, runparams.lastpos);
336 // adapt column/text width correctly also if paragraphs indented
337 if (stdwidth && !(buffer().params().paragraph_separation))
344 if (!(thickness_string.find(defaultThick) != string::npos)) {
345 os << "{\\FrameRule " << from_ascii(thickness_string);
346 if (!(separation_string.find(defaultSep) != string::npos))
347 os << "\\FrameSep " << from_ascii(separation_string);
349 if (!(separation_string.find(defaultSep) != string::npos)
350 && (thickness_string.find(defaultThick) != string::npos))
351 os << "{\\FrameSep " << from_ascii(separation_string);
353 os << "\\begin{framed}%\n";
356 if (!(thickness_string.find(defaultThick) != string::npos)) {
357 os << "{\\fboxrule " << from_ascii(thickness_string);
358 if (!(separation_string.find(defaultSep) != string::npos))
359 os << "\\fboxsep " << from_ascii(separation_string);
361 if (!(separation_string.find(defaultSep) != string::npos)
362 && (thickness_string.find(defaultThick) != string::npos))
363 os << "{\\fboxsep " << from_ascii(separation_string);
364 if (!params_.inner_box && !width_string.empty()) {
365 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
366 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
370 // Special widths, see usrguide sec. 3.5
372 if (params_.special != "none") {
373 os << "[" << params_.width.value()
374 << '\\' << from_utf8(params_.special)
377 os << '[' << from_ascii(width_string)
379 if (params_.hor_pos != 'c')
380 os << "[" << params_.hor_pos << "]";
382 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
383 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
390 if (!separation_string.empty() && separation_string.find(defaultSep) == string::npos)
391 os << "{\\fboxsep " << from_ascii(separation_string);
395 if (!separation_string.empty() && separation_string.find(defaultSep) == string::npos)
396 os << "{\\fboxsep " << from_ascii(separation_string);
400 if (!(thickness_string.find(defaultThick) != string::npos)) {
401 os << "{\\fboxrule " << from_ascii(thickness_string);
402 if (!(separation_string.find(defaultSep) != string::npos)) {
403 os << "\\fboxsep " << from_ascii(separation_string);
404 if (!(shadowsize_string.find(defaultShadow) != string::npos))
405 os << "\\shadowsize " << from_ascii(shadowsize_string);
407 if (!(shadowsize_string.find(defaultShadow) != string::npos)
408 && (separation_string.find(defaultSep) != string::npos))
409 os << "\\shadowsize " << from_ascii(shadowsize_string);
411 if (!(separation_string.find(defaultSep) != string::npos)
412 && (thickness_string.find(defaultThick) != string::npos)) {
413 os << "{\\fboxsep " << from_ascii(separation_string);
414 if (!(shadowsize_string.find(defaultShadow) != string::npos))
415 os << "\\shadowsize " << from_ascii(shadowsize_string);
417 if (!(shadowsize_string.find(defaultShadow) != string::npos)
418 && (separation_string.find(defaultSep) != string::npos)
419 && (thickness_string.find(defaultThick) != string::npos))
420 os << "{\\shadowsize " << from_ascii(shadowsize_string);
421 os << "\\shadowbox{";
424 // must be set later because e.g. the width settings only work when
425 // it is inside a minipage or parbox
428 if (!(thickness_string.find(defaultThick) != string::npos)) {
429 os << "{\\fboxrule " << from_ascii(thickness_string);
430 if (!(separation_string.find(defaultSep) != string::npos))
431 os << "\\fboxsep " << from_ascii(separation_string);
433 if (!(separation_string.find(defaultSep) != string::npos)
434 && (thickness_string.find(defaultThick) != string::npos))
435 os << "{\\fboxsep " << from_ascii(separation_string);
436 os << "\\doublebox{";
440 if (params_.inner_box) {
441 if (params_.use_parbox) {
442 if (params_.backgroundcolor != "none" && btype == Frameless)
443 os << "\\colorbox{" << params_.backgroundcolor << "}{";
445 } else if (params_.use_makebox) {
446 if (!width_string.empty()) {
447 if (params_.backgroundcolor != "none")
448 os << "\\colorbox{" << params_.backgroundcolor << "}{";
451 // output the width and horizontal position
452 if (params_.special != "none") {
453 os << "[" << params_.width.value()
454 << '\\' << from_utf8(params_.special)
457 os << '[' << from_ascii(width_string)
459 if (params_.hor_pos != 'c')
460 os << "[" << params_.hor_pos << "]";
462 if (params_.backgroundcolor != "none")
463 os << "\\colorbox{" << params_.backgroundcolor << "}{";
469 if (params_.backgroundcolor != "none" && btype == Frameless)
470 os << "\\colorbox{" << params_.backgroundcolor << "}{";
471 os << "\\begin{minipage}";
474 // output parameters for parbox and minipage
475 if (!params_.use_makebox) {
476 os << "[" << params_.pos << "]";
477 if (params_.height_special == "none") {
479 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
482 // set no optional argument when the value is the default "1\height"
483 // (special units like \height are handled as "in")
484 // but when the user has chosen a non-default inner_pos, the height
485 // must be given: \minipage[pos][height][inner-pos]{width}
486 if ((params_.height != Length("1in") ||
487 params_.height_special != "totalheight") ||
488 params_.inner_pos != params_.pos) {
490 os << "[" << params_.height.value()
491 << "\\" << from_utf8(params_.height_special) << "]";
494 if (params_.inner_pos != params_.pos)
495 os << "[" << params_.inner_pos << "]";
497 os << '{' << from_ascii(width_string) << '}';
498 if (params_.use_parbox)
503 } // end if inner_box
505 if (btype == Shaded) {
506 os << "\\begin{shaded}%\n";
509 // \framebox and \makebox handle hor_pos their own way
510 // hor_pos is senseless for \mbox and \fbox
511 if (!(params_.use_makebox)
512 && !(btype == Boxed && !params_.inner_box)) {
513 switch (params_.hor_pos) {
515 // do nothing because this is LaTeX's default
518 os << "\\centering ";
521 os << "\\raggedleft ";
526 InsetText::latex(os, runparams);
529 os << "\\end{shaded}";
531 if (params_.inner_box) {
532 if (params_.use_parbox || params_.use_makebox)
535 os << "%\n\\end{minipage}";
536 if (params_.backgroundcolor != "none" && btype == Frameless
537 && !(params_.use_makebox && width_string.empty()))
545 os << "\\end{framed}";
546 if (!(separation_string.find(defaultSep) != string::npos)
547 || !(thickness_string.find(defaultThick) != string::npos))
552 if (!params_.inner_box && !width_string.empty()
553 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
555 if (!(separation_string.find(defaultSep) != string::npos)
556 || !(thickness_string.find(defaultThick) != string::npos))
561 if (!(separation_string.find(defaultSep) != string::npos))
566 if (!(separation_string.find(defaultSep) != string::npos))
571 if (!(separation_string.find(defaultSep) != string::npos)
572 || !(thickness_string.find(defaultThick) != string::npos))
577 if (!(separation_string.find(defaultSep) != string::npos)
578 || !(thickness_string.find(defaultThick) != string::npos)
579 || !(shadowsize_string.find(defaultShadow) != string::npos))
589 int InsetBox::plaintext(odocstringstream & os,
590 OutputParams const & runparams, size_t max_length) const
592 BoxType const btype = boxtranslator().find(params_.type);
616 InsetText::plaintext(os, runparams, max_length);
647 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
651 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
653 return InsetText::docbook(os, runparams);
657 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
659 // construct attributes
660 string attrs = "class='" + params_.type + "'";
662 if (!params_.width.empty()) {
663 string w = params_.width.asHTMLString();
665 style += ("width: " + params_.width.asHTMLString() + "; ");
667 // The special heights don't really mean anything for us.
668 if (!params_.height.empty() && params_.height_special == "none")
669 style += ("height: " + params_.height.asHTMLString() + "; ");
671 attrs += " style='" + style + "'";
673 xs << html::StartTag("div", attrs);
674 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
675 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
676 xs << html::EndTag("div");
682 void InsetBox::validate(LaTeXFeatures & features) const
684 BoxType btype = boxtranslator().find(params_.type);
687 if (params_.backgroundcolor != "none")
688 features.require("xcolor");
691 features.require("calc");
692 features.require("framed");
695 features.require("calc");
696 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
697 features.require("xcolor");
703 features.require("calc");
704 features.require("fancybox");
707 features.require("color");
708 features.require("framed");
711 InsetCollapsable::validate(features);
715 string InsetBox::contextMenuName() const
717 return "context-box";
721 string InsetBox::params2string(InsetBoxParams const & params)
724 data << "box" << ' ';
730 void InsetBox::string2params(string const & in, InsetBoxParams & params)
735 istringstream data(in);
741 if (!lex || name != "box") {
742 LYXERR0("InsetBox::string2params(" << in << ")\n"
743 "Expected arg 1 to be \"box\"\n");
747 // This is part of the inset proper that is usually swallowed
748 // by Text::readInset
751 if (!lex || id != "Box") {
752 LYXERR0("InsetBox::string2params(" << in << ")\n"
753 "Expected arg 2 to be \"Box\"\n");
756 params = InsetBoxParams(string());
761 /////////////////////////////////////////////////////////////////////////
765 /////////////////////////////////////////////////////////////////////////
767 InsetBoxParams::InsetBoxParams(string const & label)
772 width(Length("100col%")),
777 height(Length("1in")),
778 height_special("totalheight"), // default is 1\\totalheight
779 thickness(Length(defaultThick)),
780 separation(Length(defaultSep)),
781 shadowsize(Length(defaultShadow)),
783 backgroundcolor("none")
787 void InsetBoxParams::write(ostream & os) const
789 os << "Box " << type << "\n";
790 os << "position \"" << pos << "\"\n";
791 os << "hor_pos \"" << hor_pos << "\"\n";
792 os << "has_inner_box " << inner_box << "\n";
793 os << "inner_pos \"" << inner_pos << "\"\n";
794 os << "use_parbox " << use_parbox << "\n";
795 os << "use_makebox " << use_makebox << "\n";
796 os << "width \"" << width.asString() << "\"\n";
797 os << "special \"" << special << "\"\n";
798 os << "height \"" << height.asString() << "\"\n";
799 os << "height_special \"" << height_special << "\"\n";
800 os << "thickness \"" << thickness.asString() << "\"\n";
801 os << "separation \"" << separation.asString() << "\"\n";
802 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
803 os << "framecolor \"" << framecolor << "\"\n";
804 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
808 void InsetBoxParams::read(Lexer & lex)
810 lex.setContext("InsetBoxParams::read");
812 lex >> "position" >> pos;
813 lex >> "hor_pos" >> hor_pos;
814 lex >> "has_inner_box" >> inner_box;
815 if (type == "Framed")
817 lex >> "inner_pos" >> inner_pos;
818 lex >> "use_parbox" >> use_parbox;
819 lex >> "use_makebox" >> use_makebox;
820 lex >> "width" >> width;
821 lex >> "special" >> special;
822 lex >> "height" >> height;
823 lex >> "height_special" >> height_special;
824 lex >> "thickness" >> thickness;
825 lex >> "separation" >> separation;
826 lex >> "shadowsize" >> shadowsize;
827 lex >> "framecolor" >> framecolor;
828 lex >> "backgroundcolor" >> backgroundcolor;