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();
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 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
228 switch (cmd.action()) {
230 case LFUN_INSET_MODIFY: {
231 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
232 string const first_arg = cmd.getArg(0);
233 bool const change_type = first_arg == "changetype";
234 bool const for_box = first_arg == "box";
235 if (!change_type && !for_box) {
237 // this will not be handled higher up
241 cur.recordUndoInset(this);
243 params_.type = cmd.getArg(1);
245 string2params(to_utf8(cmd.argument()), params_);
251 InsetCollapsable::doDispatch(cur, cmd);
257 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
258 FuncStatus & flag) const
260 switch (cmd.action()) {
262 case LFUN_INSET_MODIFY: {
263 string const first_arg = cmd.getArg(0);
264 if (first_arg == "changetype") {
265 string const type = cmd.getArg(1);
266 flag.setOnOff(type == params_.type);
267 flag.setEnabled(!params_.inner_box || type != "Framed");
270 if (first_arg == "box") {
271 flag.setEnabled(true);
274 return InsetCollapsable::getStatus(cur, cmd, flag);
277 case LFUN_INSET_DIALOG_UPDATE:
278 flag.setEnabled(true);
282 return InsetCollapsable::getStatus(cur, cmd, flag);
287 const string defaultThick = "0.4pt";
288 const string defaultSep = "3pt";
289 const string defaultShadow = "4pt";
291 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
293 BoxType btype = boxtranslator().find(params_.type);
295 string width_string = params_.width.asLatexString();
296 string thickness_string = params_.thickness.asLatexString();
297 string separation_string = params_.separation.asLatexString();
298 string shadowsize_string = params_.shadowsize.asLatexString();
299 bool stdwidth = false;
300 // in general the overall width of some decorated boxes is wider thean the inner box
301 // we could therefore calculate the real width for all sizes so that if the user wants
302 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
303 // however this makes problems when importing TeX code
304 // therefore only recalculate for the most common case that the box should not protrude
306 if (params_.inner_box
307 && ((width_string.find("1\\columnwidth") != string::npos
308 || width_string.find("1\\textwidth") != string::npos)
309 || width_string.find("1\\paperwidth") != string::npos
310 || width_string.find("1\\linewidth") != string::npos)) {
316 width_string += " - 2\\FrameSep - 2\\FrameRule";
319 width_string += " - 2\\fboxsep - 2\\fboxrule";
324 width_string += " - 2\\fboxsep - 0.8pt";
327 width_string += " - 2\\fboxsep - 1.6pt";
330 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
333 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
339 if (runparams.lastid != -1)
340 os.texrow().start(runparams.lastid, runparams.lastpos);
342 // adapt column/text width correctly also if paragraphs indented
343 if (stdwidth && !(buffer().params().paragraph_separation))
350 if (thickness_string != defaultThick) {
351 os << "{\\FrameRule " << from_ascii(thickness_string);
352 if (separation_string != defaultSep)
353 os << "\\FrameSep " << from_ascii(separation_string);
355 if (separation_string != defaultSep && thickness_string == defaultThick)
356 os << "{\\FrameSep " << from_ascii(separation_string);
358 os << "\\begin{framed}%\n";
361 if (thickness_string != defaultThick) {
362 os << "{\\fboxrule " << from_ascii(thickness_string);
363 if (separation_string != defaultSep)
364 os << "\\fboxsep " << from_ascii(separation_string);
366 if (separation_string != defaultSep && thickness_string == defaultThick)
367 os << "{\\fboxsep " << from_ascii(separation_string);
368 if (!params_.inner_box && !width_string.empty()) {
369 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
370 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
374 // Special widths, see usrguide sec. 3.5
376 if (params_.special != "none") {
377 os << "[" << params_.width.value()
378 << '\\' << from_utf8(params_.special)
381 os << '[' << from_ascii(width_string)
383 if (params_.hor_pos != 'c')
384 os << "[" << params_.hor_pos << "]";
386 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
387 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
394 if (!separation_string.empty() && separation_string != defaultSep)
395 os << "{\\fboxsep " << from_ascii(separation_string);
399 if (!separation_string.empty() && separation_string != defaultSep)
400 os << "{\\fboxsep " << from_ascii(separation_string);
404 if (thickness_string != defaultThick) {
405 os << "{\\fboxrule " << from_ascii(thickness_string);
406 if (separation_string != defaultSep) {
407 os << "\\fboxsep " << from_ascii(separation_string);
408 if (shadowsize_string != defaultShadow)
409 os << "\\shadowsize " << from_ascii(shadowsize_string);
411 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
412 os << "\\shadowsize " << from_ascii(shadowsize_string);
414 if (separation_string != defaultSep && thickness_string == defaultThick) {
415 os << "{\\fboxsep " << from_ascii(separation_string);
416 if (shadowsize_string != defaultShadow)
417 os << "\\shadowsize " << from_ascii(shadowsize_string);
419 if (shadowsize_string != defaultShadow
420 && separation_string == defaultSep
421 && thickness_string == defaultThick)
422 os << "{\\shadowsize " << from_ascii(shadowsize_string);
423 os << "\\shadowbox{";
426 // must be set later because e.g. the width settings only work when
427 // it is inside a minipage or parbox
430 if (thickness_string != defaultThick) {
431 os << "{\\fboxrule " << from_ascii(thickness_string);
432 if (separation_string != defaultSep)
433 os << "\\fboxsep " << from_ascii(separation_string);
435 if (separation_string != defaultSep && thickness_string == defaultThick)
436 os << "{\\fboxsep " << from_ascii(separation_string);
437 os << "\\doublebox{";
441 if (params_.inner_box) {
442 if (params_.use_parbox) {
443 if (params_.backgroundcolor != "none" && btype == Frameless)
444 os << "\\colorbox{" << params_.backgroundcolor << "}{";
446 } else if (params_.use_makebox) {
447 if (!width_string.empty()) {
448 if (params_.backgroundcolor != "none")
449 os << "\\colorbox{" << params_.backgroundcolor << "}{";
452 // output the width and horizontal position
453 if (params_.special != "none") {
454 os << "[" << params_.width.value()
455 << '\\' << from_utf8(params_.special)
458 os << '[' << from_ascii(width_string)
460 if (params_.hor_pos != 'c')
461 os << "[" << params_.hor_pos << "]";
463 if (params_.backgroundcolor != "none")
464 os << "\\colorbox{" << params_.backgroundcolor << "}";
471 if (params_.backgroundcolor != "none" && btype == Frameless)
472 os << "\\colorbox{" << params_.backgroundcolor << "}{";
473 os << "\\begin{minipage}";
476 // output parameters for parbox and minipage
477 if (!params_.use_makebox) {
478 os << "[" << params_.pos << "]";
479 if (params_.height_special == "none") {
481 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
484 // set no optional argument when the value is the default "1\height"
485 // (special units like \height are handled as "in")
486 // but when the user has chosen a non-default inner_pos, the height
487 // must be given: \minipage[pos][height][inner-pos]{width}
488 if ((params_.height != Length("1in") ||
489 params_.height_special != "totalheight") ||
490 params_.inner_pos != params_.pos) {
492 os << "[" << params_.height.value()
493 << "\\" << from_utf8(params_.height_special) << "]";
496 if (params_.inner_pos != params_.pos)
497 os << "[" << params_.inner_pos << "]";
499 os << '{' << from_ascii(width_string) << '}';
500 if (params_.use_parbox)
505 } // end if inner_box
507 if (btype == Shaded) {
508 os << "\\begin{shaded}%\n";
511 // \framebox and \makebox handle hor_pos their own way
512 // hor_pos is senseless for \mbox and \fbox
513 if (!params_.use_makebox
514 && !(btype == Boxed && !params_.inner_box)) {
515 switch (params_.hor_pos) {
517 // do nothing because this is LaTeX's default
520 os << "\\centering ";
523 os << "\\raggedleft ";
528 InsetText::latex(os, runparams);
531 os << "\\end{shaded}";
533 if (params_.inner_box) {
534 if (params_.use_parbox || params_.use_makebox)
537 os << "%\n\\end{minipage}";
538 if (params_.backgroundcolor != "none" && btype == Frameless
539 && !(params_.use_makebox && width_string.empty()))
547 os << "\\end{framed}";
548 if (separation_string != defaultSep || thickness_string != defaultThick)
553 if (!params_.inner_box && !width_string.empty()
554 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
556 if (separation_string != defaultSep || thickness_string != defaultThick)
561 if (separation_string != defaultSep)
566 if (separation_string != defaultSep)
571 if (separation_string != defaultSep || thickness_string != defaultThick)
576 if (separation_string != defaultSep
577 || thickness_string != defaultThick
578 || shadowsize_string != defaultShadow)
588 int InsetBox::plaintext(odocstringstream & os,
589 OutputParams const & runparams, size_t max_length) const
591 BoxType const btype = boxtranslator().find(params_.type);
615 InsetText::plaintext(os, runparams, max_length);
646 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
650 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
652 return InsetText::docbook(os, runparams);
656 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
658 // construct attributes
659 string attrs = "class='" + params_.type + "'";
661 if (!params_.width.empty()) {
662 string w = params_.width.asHTMLString();
664 style += ("width: " + params_.width.asHTMLString() + "; ");
666 // The special heights don't really mean anything for us.
667 if (!params_.height.empty() && params_.height_special == "none")
668 style += ("height: " + params_.height.asHTMLString() + "; ");
670 attrs += " style='" + style + "'";
672 xs << html::StartTag("div", attrs);
673 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
674 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
675 xs << html::EndTag("div");
681 void InsetBox::validate(LaTeXFeatures & features) const
683 BoxType btype = boxtranslator().find(params_.type);
686 if (params_.backgroundcolor != "none")
687 features.require("xcolor");
690 features.require("calc");
691 features.require("framed");
694 features.require("calc");
695 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
696 features.require("xcolor");
702 features.require("calc");
703 features.require("fancybox");
706 features.require("color");
707 features.require("framed");
710 InsetCollapsable::validate(features);
714 string InsetBox::contextMenuName() const
716 return "context-box";
720 string InsetBox::params2string(InsetBoxParams const & params)
723 data << "box" << ' ';
729 void InsetBox::string2params(string const & in, InsetBoxParams & params)
734 istringstream data(in);
740 if (!lex || name != "box") {
741 LYXERR0("InsetBox::string2params(" << in << ")\n"
742 "Expected arg 1 to be \"box\"\n");
746 // This is part of the inset proper that is usually swallowed
747 // by Text::readInset
750 if (!lex || id != "Box") {
751 LYXERR0("InsetBox::string2params(" << in << ")\n"
752 "Expected arg 2 to be \"Box\"\n");
755 params = InsetBoxParams(string());
760 /////////////////////////////////////////////////////////////////////////
764 /////////////////////////////////////////////////////////////////////////
766 InsetBoxParams::InsetBoxParams(string const & label)
771 width(Length("100col%")),
776 height(Length("1in")),
777 height_special("totalheight"), // default is 1\\totalheight
778 thickness(Length(defaultThick)),
779 separation(Length(defaultSep)),
780 shadowsize(Length(defaultShadow)),
782 backgroundcolor("none")
786 void InsetBoxParams::write(ostream & os) const
788 os << "Box " << type << "\n";
789 os << "position \"" << pos << "\"\n";
790 os << "hor_pos \"" << hor_pos << "\"\n";
791 os << "has_inner_box " << inner_box << "\n";
792 os << "inner_pos \"" << inner_pos << "\"\n";
793 os << "use_parbox " << use_parbox << "\n";
794 os << "use_makebox " << use_makebox << "\n";
795 os << "width \"" << width.asString() << "\"\n";
796 os << "special \"" << special << "\"\n";
797 os << "height \"" << height.asString() << "\"\n";
798 os << "height_special \"" << height_special << "\"\n";
799 os << "thickness \"" << thickness.asString() << "\"\n";
800 os << "separation \"" << separation.asString() << "\"\n";
801 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
802 os << "framecolor \"" << framecolor << "\"\n";
803 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
807 void InsetBoxParams::read(Lexer & lex)
809 lex.setContext("InsetBoxParams::read");
811 lex >> "position" >> pos;
812 lex >> "hor_pos" >> hor_pos;
813 lex >> "has_inner_box" >> inner_box;
814 if (type == "Framed")
816 lex >> "inner_pos" >> inner_pos;
817 lex >> "use_parbox" >> use_parbox;
818 lex >> "use_makebox" >> use_makebox;
819 lex >> "width" >> width;
820 lex >> "special" >> special;
821 lex >> "height" >> height;
822 lex >> "height_special" >> height_special;
823 lex >> "thickness" >> thickness;
824 lex >> "separation" >> separation;
825 lex >> "shadowsize" >> shadowsize;
826 lex >> "framecolor" >> framecolor;
827 lex >> "backgroundcolor" >> backgroundcolor;