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 != defaultThick) {
345 os << "{\\FrameRule " << from_ascii(thickness_string);
346 if (separation_string != defaultSep)
347 os << "\\FrameSep " << from_ascii(separation_string);
349 if (separation_string != defaultSep && thickness_string == defaultThick)
350 os << "{\\FrameSep " << from_ascii(separation_string);
352 os << "\\begin{framed}%\n";
355 if (thickness_string != defaultThick) {
356 os << "{\\fboxrule " << from_ascii(thickness_string);
357 if (separation_string != defaultSep)
358 os << "\\fboxsep " << from_ascii(separation_string);
360 if (separation_string != defaultSep && thickness_string == defaultThick)
361 os << "{\\fboxsep " << from_ascii(separation_string);
362 if (!params_.inner_box && !width_string.empty()) {
363 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
364 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
368 // Special widths, see usrguide sec. 3.5
370 if (params_.special != "none") {
371 os << "[" << params_.width.value()
372 << '\\' << from_utf8(params_.special)
375 os << '[' << from_ascii(width_string)
377 if (params_.hor_pos != 'c')
378 os << "[" << params_.hor_pos << "]";
380 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
381 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
388 if (!separation_string.empty() && separation_string != defaultSep)
389 os << "{\\fboxsep " << from_ascii(separation_string);
393 if (!separation_string.empty() && separation_string != defaultSep)
394 os << "{\\fboxsep " << from_ascii(separation_string);
398 if (thickness_string != defaultThick) {
399 os << "{\\fboxrule " << from_ascii(thickness_string);
400 if (separation_string != defaultSep) {
401 os << "\\fboxsep " << from_ascii(separation_string);
402 if (shadowsize_string != defaultShadow)
403 os << "\\shadowsize " << from_ascii(shadowsize_string);
405 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
406 os << "\\shadowsize " << from_ascii(shadowsize_string);
408 if (separation_string != defaultSep && thickness_string == defaultThick) {
409 os << "{\\fboxsep " << from_ascii(separation_string);
410 if (shadowsize_string != defaultShadow)
411 os << "\\shadowsize " << from_ascii(shadowsize_string);
413 if (shadowsize_string != defaultShadow
414 && separation_string == defaultSep
415 && thickness_string == defaultThick)
416 os << "{\\shadowsize " << from_ascii(shadowsize_string);
417 os << "\\shadowbox{";
420 // must be set later because e.g. the width settings only work when
421 // it is inside a minipage or parbox
424 if (thickness_string != defaultThick) {
425 os << "{\\fboxrule " << from_ascii(thickness_string);
426 if (separation_string != defaultSep)
427 os << "\\fboxsep " << from_ascii(separation_string);
429 if (separation_string != defaultSep && thickness_string == defaultThick)
430 os << "{\\fboxsep " << from_ascii(separation_string);
431 os << "\\doublebox{";
435 if (params_.inner_box) {
436 if (params_.use_parbox) {
437 if (params_.backgroundcolor != "none" && btype == Frameless)
438 os << "\\colorbox{" << params_.backgroundcolor << "}{";
440 } else if (params_.use_makebox) {
441 if (!width_string.empty()) {
442 if (params_.backgroundcolor != "none")
443 os << "\\colorbox{" << params_.backgroundcolor << "}{";
446 // output the width and horizontal position
447 if (params_.special != "none") {
448 os << "[" << params_.width.value()
449 << '\\' << from_utf8(params_.special)
452 os << '[' << from_ascii(width_string)
454 if (params_.hor_pos != 'c')
455 os << "[" << params_.hor_pos << "]";
457 if (params_.backgroundcolor != "none")
458 os << "\\colorbox{" << params_.backgroundcolor << "}";
465 if (params_.backgroundcolor != "none" && btype == Frameless)
466 os << "\\colorbox{" << params_.backgroundcolor << "}{";
467 os << "\\begin{minipage}";
470 // output parameters for parbox and minipage
471 if (!params_.use_makebox) {
472 os << "[" << params_.pos << "]";
473 if (params_.height_special == "none") {
475 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
478 // set no optional argument when the value is the default "1\height"
479 // (special units like \height are handled as "in")
480 // but when the user has chosen a non-default inner_pos, the height
481 // must be given: \minipage[pos][height][inner-pos]{width}
482 if ((params_.height != Length("1in") ||
483 params_.height_special != "totalheight") ||
484 params_.inner_pos != params_.pos) {
486 os << "[" << params_.height.value()
487 << "\\" << from_utf8(params_.height_special) << "]";
490 if (params_.inner_pos != params_.pos)
491 os << "[" << params_.inner_pos << "]";
493 os << '{' << from_ascii(width_string) << '}';
494 if (params_.use_parbox)
499 } // end if inner_box
501 if (btype == Shaded) {
502 os << "\\begin{shaded}%\n";
505 // \framebox and \makebox handle hor_pos their own way
506 // hor_pos is senseless for \mbox and \fbox
507 if (!(params_.use_makebox)
508 && !(btype == Boxed && !params_.inner_box)) {
509 switch (params_.hor_pos) {
511 // do nothing because this is LaTeX's default
514 os << "\\centering ";
517 os << "\\raggedleft ";
522 InsetText::latex(os, runparams);
525 os << "\\end{shaded}";
527 if (params_.inner_box) {
528 if (params_.use_parbox || params_.use_makebox)
531 os << "%\n\\end{minipage}";
532 if (params_.backgroundcolor != "none" && btype == Frameless
533 && !(params_.use_makebox && width_string.empty()))
541 os << "\\end{framed}";
542 if (separation_string != defaultSep || thickness_string != defaultThick)
547 if (!params_.inner_box && !width_string.empty()
548 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
550 if (separation_string != defaultSep || thickness_string != defaultThick)
555 if (separation_string != defaultSep)
560 if (separation_string != defaultSep)
565 if (separation_string != defaultSep || thickness_string != defaultThick)
570 if (separation_string != defaultSep
571 || thickness_string != defaultThick
572 || shadowsize_string != defaultShadow)
582 int InsetBox::plaintext(odocstringstream & os,
583 OutputParams const & runparams, size_t max_length) const
585 BoxType const btype = boxtranslator().find(params_.type);
609 InsetText::plaintext(os, runparams, max_length);
640 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
644 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
646 return InsetText::docbook(os, runparams);
650 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
652 // construct attributes
653 string attrs = "class='" + params_.type + "'";
655 if (!params_.width.empty()) {
656 string w = params_.width.asHTMLString();
658 style += ("width: " + params_.width.asHTMLString() + "; ");
660 // The special heights don't really mean anything for us.
661 if (!params_.height.empty() && params_.height_special == "none")
662 style += ("height: " + params_.height.asHTMLString() + "; ");
664 attrs += " style='" + style + "'";
666 xs << html::StartTag("div", attrs);
667 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
668 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
669 xs << html::EndTag("div");
675 void InsetBox::validate(LaTeXFeatures & features) const
677 BoxType btype = boxtranslator().find(params_.type);
680 if (params_.backgroundcolor != "none")
681 features.require("xcolor");
684 features.require("calc");
685 features.require("framed");
688 features.require("calc");
689 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
690 features.require("xcolor");
696 features.require("calc");
697 features.require("fancybox");
700 features.require("color");
701 features.require("framed");
704 InsetCollapsable::validate(features);
708 string InsetBox::contextMenuName() const
710 return "context-box";
714 string InsetBox::params2string(InsetBoxParams const & params)
717 data << "box" << ' ';
723 void InsetBox::string2params(string const & in, InsetBoxParams & params)
728 istringstream data(in);
734 if (!lex || name != "box") {
735 LYXERR0("InsetBox::string2params(" << in << ")\n"
736 "Expected arg 1 to be \"box\"\n");
740 // This is part of the inset proper that is usually swallowed
741 // by Text::readInset
744 if (!lex || id != "Box") {
745 LYXERR0("InsetBox::string2params(" << in << ")\n"
746 "Expected arg 2 to be \"Box\"\n");
749 params = InsetBoxParams(string());
754 /////////////////////////////////////////////////////////////////////////
758 /////////////////////////////////////////////////////////////////////////
760 InsetBoxParams::InsetBoxParams(string const & label)
765 width(Length("100col%")),
770 height(Length("1in")),
771 height_special("totalheight"), // default is 1\\totalheight
772 thickness(Length(defaultThick)),
773 separation(Length(defaultSep)),
774 shadowsize(Length(defaultShadow)),
776 backgroundcolor("none")
780 void InsetBoxParams::write(ostream & os) const
782 os << "Box " << type << "\n";
783 os << "position \"" << pos << "\"\n";
784 os << "hor_pos \"" << hor_pos << "\"\n";
785 os << "has_inner_box " << inner_box << "\n";
786 os << "inner_pos \"" << inner_pos << "\"\n";
787 os << "use_parbox " << use_parbox << "\n";
788 os << "use_makebox " << use_makebox << "\n";
789 os << "width \"" << width.asString() << "\"\n";
790 os << "special \"" << special << "\"\n";
791 os << "height \"" << height.asString() << "\"\n";
792 os << "height_special \"" << height_special << "\"\n";
793 os << "thickness \"" << thickness.asString() << "\"\n";
794 os << "separation \"" << separation.asString() << "\"\n";
795 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
796 os << "framecolor \"" << framecolor << "\"\n";
797 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
801 void InsetBoxParams::read(Lexer & lex)
803 lex.setContext("InsetBoxParams::read");
805 lex >> "position" >> pos;
806 lex >> "hor_pos" >> hor_pos;
807 lex >> "has_inner_box" >> inner_box;
808 if (type == "Framed")
810 lex >> "inner_pos" >> inner_pos;
811 lex >> "use_parbox" >> use_parbox;
812 lex >> "use_makebox" >> use_makebox;
813 lex >> "width" >> width;
814 lex >> "special" >> special;
815 lex >> "height" >> height;
816 lex >> "height_special" >> height_special;
817 lex >> "thickness" >> thickness;
818 lex >> "separation" >> separation;
819 lex >> "shadowsize" >> shadowsize;
820 lex >> "framecolor" >> framecolor;
821 lex >> "backgroundcolor" >> backgroundcolor;