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 if (params_.type != "Shaded")
199 return getLayout().bgcolor();
200 // FIXME: This hardcoded color is a hack!
201 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
202 return getLayout().bgcolor();
203 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
205 return getLayout().bgcolor();
210 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
212 switch (cmd.action()) {
214 case LFUN_INSET_MODIFY: {
215 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
216 string const first_arg = cmd.getArg(0);
217 bool const change_type = first_arg == "changetype";
218 bool const for_box = first_arg == "box";
219 if (!change_type && !for_box) {
221 // this will not be handled higher up
225 cur.recordUndoInset(this);
227 params_.type = cmd.getArg(1);
229 string2params(to_utf8(cmd.argument()), params_);
235 InsetCollapsable::doDispatch(cur, cmd);
241 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
242 FuncStatus & flag) const
244 switch (cmd.action()) {
246 case LFUN_INSET_MODIFY: {
247 string const first_arg = cmd.getArg(0);
248 if (first_arg == "changetype") {
249 string const type = cmd.getArg(1);
250 flag.setOnOff(type == params_.type);
251 flag.setEnabled(!params_.inner_box || type != "Framed");
254 if (first_arg == "box") {
255 flag.setEnabled(true);
258 return InsetCollapsable::getStatus(cur, cmd, flag);
261 case LFUN_INSET_DIALOG_UPDATE:
262 flag.setEnabled(true);
266 return InsetCollapsable::getStatus(cur, cmd, flag);
271 const string defaultThick = "0.4pt";
272 const string defaultSep = "3pt";
273 const string defaultShadow = "4pt";
275 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
277 BoxType btype = boxtranslator().find(params_.type);
279 string width_string = params_.width.asLatexString();
280 string thickness_string = params_.thickness.asLatexString();
281 string separation_string = params_.separation.asLatexString();
282 string shadowsize_string = params_.shadowsize.asLatexString();
283 bool stdwidth = false;
284 // in general the overall width of some decorated boxes is wider thean the inner box
285 // we could therefore calculate the real width for all sizes so that if the user wants
286 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
287 // however this makes problems when importing TeX code
288 // therefore only recalculate for the most common case that the box should not protrude
290 if (params_.inner_box
291 && ((width_string.find("1\\columnwidth") != string::npos
292 || width_string.find("1\\textwidth") != string::npos)
293 || width_string.find("1\\paperwidth") != string::npos
294 || width_string.find("1\\linewidth") != string::npos)) {
300 width_string += " - 2\\FrameSep - 2\\FrameRule";
303 width_string += " - 2\\fboxsep - 2\\fboxrule";
308 width_string += " - 2\\fboxsep - 0.8pt";
311 width_string += " - 2\\fboxsep - 1.6pt";
314 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
317 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
323 if (runparams.lastid != -1)
324 os.texrow().start(runparams.lastid, runparams.lastpos);
326 // adapt column/text width correctly also if paragraphs indented
327 if (stdwidth && !(buffer().params().paragraph_separation))
334 if (!(thickness_string.find(defaultThick) != string::npos)) {
335 os << "{\\FrameRule " << from_ascii(thickness_string);
336 if (!(separation_string.find(defaultSep) != string::npos))
337 os << "\\FrameSep " << from_ascii(separation_string);
339 if (!(separation_string.find(defaultSep) != string::npos)
340 && (thickness_string.find(defaultThick) != string::npos))
341 os << "{\\FrameSep " << from_ascii(separation_string);
343 os << "\\begin{framed}%\n";
346 if (!(thickness_string.find(defaultThick) != string::npos)) {
347 os << "{\\fboxrule " << from_ascii(thickness_string);
348 if (!(separation_string.find(defaultSep) != string::npos))
349 os << "\\fboxsep " << from_ascii(separation_string);
351 if (!(separation_string.find(defaultSep) != string::npos)
352 && (thickness_string.find(defaultThick) != string::npos))
353 os << "{\\fboxsep " << from_ascii(separation_string);
354 if (!params_.inner_box && !width_string.empty()) {
356 // Special widths, see usrguide sec. 3.5
358 if (params_.special != "none") {
359 os << "[" << params_.width.value()
360 << '\\' << from_utf8(params_.special)
363 os << '[' << from_ascii(width_string)
365 if (params_.hor_pos != 'c')
366 os << "[" << params_.hor_pos << "]";
372 if (!separation_string.empty() && separation_string.find(defaultSep) == string::npos)
373 os << "{\\fboxsep " << from_ascii(separation_string);
377 if (!separation_string.empty() && separation_string.find(defaultSep) == string::npos)
378 os << "{\\fboxsep " << from_ascii(separation_string);
382 if (!(thickness_string.find(defaultThick) != string::npos)) {
383 os << "{\\fboxrule " << from_ascii(thickness_string);
384 if (!(separation_string.find(defaultSep) != string::npos)) {
385 os << "\\fboxsep " << from_ascii(separation_string);
386 if (!(shadowsize_string.find(defaultShadow) != string::npos))
387 os << "\\shadowsize " << from_ascii(shadowsize_string);
389 if (!(shadowsize_string.find(defaultShadow) != string::npos)
390 && (separation_string.find(defaultSep) != string::npos))
391 os << "\\shadowsize " << from_ascii(shadowsize_string);
393 if (!(separation_string.find(defaultSep) != string::npos)
394 && (thickness_string.find(defaultThick) != string::npos)) {
395 os << "{\\fboxsep " << from_ascii(separation_string);
396 if (!(shadowsize_string.find(defaultShadow) != string::npos))
397 os << "\\shadowsize " << from_ascii(shadowsize_string);
399 if (!(shadowsize_string.find(defaultShadow) != string::npos)
400 && (separation_string.find(defaultSep) != string::npos)
401 && (thickness_string.find(defaultThick) != string::npos))
402 os << "{\\shadowsize " << from_ascii(shadowsize_string);
403 os << "\\shadowbox{";
406 // must be set later because e.g. the width settings only work when
407 // it is inside a minipage or parbox
410 if (!(thickness_string.find(defaultThick) != string::npos)) {
411 os << "{\\fboxrule " << from_ascii(thickness_string);
412 if (!(separation_string.find(defaultSep) != string::npos))
413 os << "\\fboxsep " << from_ascii(separation_string);
415 if (!(separation_string.find(defaultSep) != string::npos)
416 && (thickness_string.find(defaultThick) != string::npos))
417 os << "{\\fboxsep " << from_ascii(separation_string);
418 os << "\\doublebox{";
422 if (params_.inner_box) {
423 if (params_.use_parbox)
425 else if (params_.use_makebox) {
426 if (!width_string.empty()) {
429 // output the width and horizontal position
430 if (params_.special != "none") {
431 os << "[" << params_.width.value()
432 << '\\' << from_utf8(params_.special)
435 os << '[' << from_ascii(width_string)
437 if (params_.hor_pos != 'c')
438 os << "[" << params_.hor_pos << "]";
444 os << "\\begin{minipage}";
446 // output parameters for parbox and minipage
447 if (!params_.use_makebox) {
448 os << "[" << params_.pos << "]";
449 if (params_.height_special == "none") {
451 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
454 // set no optional argument when the value is the default "1\height"
455 // (special units like \height are handled as "in")
456 // but when the user has chosen a non-default inner_pos, the height
457 // must be given: \minipage[pos][height][inner-pos]{width}
458 if ((params_.height != Length("1in") ||
459 params_.height_special != "totalheight") ||
460 params_.inner_pos != params_.pos) {
462 os << "[" << params_.height.value()
463 << "\\" << from_utf8(params_.height_special) << "]";
466 if (params_.inner_pos != params_.pos)
467 os << "[" << params_.inner_pos << "]";
469 os << '{' << from_ascii(width_string) << '}';
470 if (params_.use_parbox)
475 } // end if inner_box
477 if (btype == Shaded) {
478 os << "\\begin{shaded}%\n";
481 // \framebox and \makebox handle hor_pos their own way
482 // hor_pos is senseless for \mbox and \fbox
483 if (!(params_.use_makebox)
484 && !(btype == Boxed && !params_.inner_box)) {
485 switch (params_.hor_pos) {
487 // do nothing because this is LaTeX's default
490 os << "\\centering ";
493 os << "\\raggedleft ";
498 InsetText::latex(os, runparams);
501 os << "\\end{shaded}";
503 if (params_.inner_box) {
504 if (params_.use_parbox || params_.use_makebox)
507 os << "%\n\\end{minipage}";
514 os << "\\end{framed}";
515 if (!(separation_string.find(defaultSep) != string::npos)
516 || !(thickness_string.find(defaultThick) != string::npos))
521 if (!(separation_string.find(defaultSep) != string::npos)
522 || !(thickness_string.find(defaultThick) != string::npos))
527 if (!(separation_string.find(defaultSep) != string::npos))
532 if (!(separation_string.find(defaultSep) != string::npos))
537 if (!(separation_string.find(defaultSep) != string::npos)
538 || !(thickness_string.find(defaultThick) != string::npos))
543 if (!(separation_string.find(defaultSep) != string::npos)
544 || !(thickness_string.find(defaultThick) != string::npos)
545 || !(shadowsize_string.find(defaultShadow) != string::npos))
555 int InsetBox::plaintext(odocstringstream & os,
556 OutputParams const & runparams, size_t max_length) const
558 BoxType const btype = boxtranslator().find(params_.type);
582 InsetText::plaintext(os, runparams, max_length);
613 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
617 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
619 return InsetText::docbook(os, runparams);
623 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
625 // construct attributes
626 string attrs = "class='" + params_.type + "'";
628 if (!params_.width.empty()) {
629 string w = params_.width.asHTMLString();
631 style += ("width: " + params_.width.asHTMLString() + "; ");
633 // The special heights don't really mean anything for us.
634 if (!params_.height.empty() && params_.height_special == "none")
635 style += ("height: " + params_.height.asHTMLString() + "; ");
637 attrs += " style='" + style + "'";
639 xs << html::StartTag("div", attrs);
640 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
641 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
642 xs << html::EndTag("div");
648 void InsetBox::validate(LaTeXFeatures & features) const
650 BoxType btype = boxtranslator().find(params_.type);
655 features.require("calc");
656 features.require("framed");
659 features.require("calc");
665 features.require("calc");
666 features.require("fancybox");
669 features.require("color");
670 features.require("framed");
673 InsetCollapsable::validate(features);
677 string InsetBox::contextMenuName() const
679 return "context-box";
683 string InsetBox::params2string(InsetBoxParams const & params)
686 data << "box" << ' ';
692 void InsetBox::string2params(string const & in, InsetBoxParams & params)
697 istringstream data(in);
703 if (!lex || name != "box") {
704 LYXERR0("InsetBox::string2params(" << in << ")\n"
705 "Expected arg 1 to be \"box\"\n");
709 // This is part of the inset proper that is usually swallowed
710 // by Text::readInset
713 if (!lex || id != "Box") {
714 LYXERR0("InsetBox::string2params(" << in << ")\n"
715 "Expected arg 2 to be \"Box\"\n");
718 params = InsetBoxParams(string());
723 /////////////////////////////////////////////////////////////////////////
727 /////////////////////////////////////////////////////////////////////////
729 InsetBoxParams::InsetBoxParams(string const & label)
734 width(Length("100col%")),
739 height(Length("1in")),
740 height_special("totalheight"), // default is 1\\totalheight
741 thickness(Length(defaultThick)),
742 separation(Length(defaultSep)),
743 shadowsize(Length(defaultShadow))
747 void InsetBoxParams::write(ostream & os) const
749 os << "Box " << type << "\n";
750 os << "position \"" << pos << "\"\n";
751 os << "hor_pos \"" << hor_pos << "\"\n";
752 os << "has_inner_box " << inner_box << "\n";
753 os << "inner_pos \"" << inner_pos << "\"\n";
754 os << "use_parbox " << use_parbox << "\n";
755 os << "use_makebox " << use_makebox << "\n";
756 os << "width \"" << width.asString() << "\"\n";
757 os << "special \"" << special << "\"\n";
758 os << "height \"" << height.asString() << "\"\n";
759 os << "height_special \"" << height_special << "\"\n";
760 os << "thickness \"" << thickness.asString() << "\"\n";
761 os << "separation \"" << separation.asString() << "\"\n";
762 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
766 void InsetBoxParams::read(Lexer & lex)
768 lex.setContext("InsetBoxParams::read");
770 lex >> "position" >> pos;
771 lex >> "hor_pos" >> hor_pos;
772 lex >> "has_inner_box" >> inner_box;
773 if (type == "Framed")
775 lex >> "inner_pos" >> inner_pos;
776 lex >> "use_parbox" >> use_parbox;
777 lex >> "use_makebox" >> use_makebox;
778 lex >> "width" >> width;
779 lex >> "special" >> special;
780 lex >> "height" >> height;
781 lex >> "height_special" >> height_special;
782 lex >> "thickness" >> thickness;
783 lex >> "separation" >> separation;
784 lex >> "shadowsize" >> shadowsize;