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 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
273 BoxType btype = boxtranslator().find(params_.type);
275 string width_string = params_.width.asLatexString();
276 string thickness_string = params_.thickness.asLatexString();
277 string defaultThick = "0.4pt";
278 string separation_string = params_.separation.asLatexString();
279 string defaultSep = "3pt";
280 string shadowsize_string = params_.shadowsize.asLatexString();
281 string defaultShadow = "4pt";
282 bool stdwidth = false;
283 // in general the overall width of some decorated boxes is wider thean the inner box
284 // we could therefore calculate the real width for all sizes so that if the user wants
285 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
286 // however this makes problems when importing TeX code
287 // therefore only recalculate for the most common case that the box should not protrude
289 if (params_.inner_box
290 && ((width_string.find("1\\columnwidth") != string::npos
291 || width_string.find("1\\textwidth") != string::npos)
292 || width_string.find("1\\paperwidth") != string::npos
293 || width_string.find("1\\linewidth") != string::npos)) {
299 width_string += " - 2\\FrameSep - 2\\FrameRule";
302 width_string += " - 2\\fboxsep - 2\\fboxrule";
307 width_string += " - 2\\fboxsep - 0.8pt";
310 width_string += " - 2\\fboxsep - 1.6pt";
313 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
316 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
322 if (runparams.lastid != -1)
323 os.texrow().start(runparams.lastid, runparams.lastpos);
325 // adapt column/text width correctly also if paragraphs indented
326 if (stdwidth && !(buffer().params().paragraph_separation))
333 if (!(thickness_string.find(defaultThick) != string::npos)) {
334 os << "{\\FrameRule " << from_ascii(thickness_string);
335 if (!(separation_string.find(defaultSep) != string::npos))
336 os << "\\FrameSep " << from_ascii(separation_string);
338 if (!(separation_string.find(defaultSep) != string::npos)
339 && (thickness_string.find(defaultThick) != string::npos))
340 os << "{\\FrameSep " << from_ascii(separation_string);
342 os << "\\begin{framed}%\n";
345 if (!(thickness_string.find(defaultThick) != string::npos)) {
346 os << "{\\fboxrule " << from_ascii(thickness_string);
347 if (!(separation_string.find(defaultSep) != string::npos))
348 os << "\\fboxsep " << from_ascii(separation_string);
350 if (!(separation_string.find(defaultSep) != string::npos)
351 && (thickness_string.find(defaultThick) != string::npos))
352 os << "{\\fboxsep " << from_ascii(separation_string);
353 if (!width_string.empty()) {
354 if (!params_.inner_box) {
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 << "]";
374 if (!separation_string.empty() && separation_string.find(defaultSep) == string::npos)
375 os << "{\\fboxsep " << from_ascii(separation_string);
379 if (!separation_string.empty() && separation_string.find(defaultSep) == string::npos)
380 os << "{\\fboxsep " << from_ascii(separation_string);
384 if (!(thickness_string.find(defaultThick) != string::npos)) {
385 os << "{\\fboxrule " << from_ascii(thickness_string);
386 if (!(separation_string.find(defaultSep) != string::npos)) {
387 os << "\\fboxsep " << from_ascii(separation_string);
388 if (!(shadowsize_string.find(defaultShadow) != string::npos))
389 os << "\\shadowsize " << from_ascii(shadowsize_string);
391 if (!(shadowsize_string.find(defaultShadow) != string::npos)
392 && (separation_string.find(defaultSep) != string::npos))
393 os << "\\shadowsize " << from_ascii(shadowsize_string);
395 if (!(separation_string.find(defaultSep) != string::npos)
396 && (thickness_string.find(defaultThick) != string::npos)) {
397 os << "{\\fboxsep " << from_ascii(separation_string);
398 if (!(shadowsize_string.find(defaultShadow) != string::npos))
399 os << "\\shadowsize " << from_ascii(shadowsize_string);
401 if (!(shadowsize_string.find(defaultShadow) != string::npos)
402 && (separation_string.find(defaultSep) != string::npos)
403 && (thickness_string.find(defaultThick) != string::npos))
404 os << "{\\shadowsize " << from_ascii(shadowsize_string);
405 os << "\\shadowbox{";
408 // must be set later because e.g. the width settings only work when
409 // it is inside a minipage or parbox
412 if (!(thickness_string.find(defaultThick) != string::npos)) {
413 os << "{\\fboxrule " << from_ascii(thickness_string);
414 if (!(separation_string.find(defaultSep) != string::npos))
415 os << "\\fboxsep " << from_ascii(separation_string);
417 if (!(separation_string.find(defaultSep) != string::npos)
418 && (thickness_string.find(defaultThick) != string::npos))
419 os << "{\\fboxsep " << from_ascii(separation_string);
420 os << "\\doublebox{";
424 if (params_.inner_box) {
425 if (params_.use_parbox)
427 else if (params_.use_makebox) {
428 if (!width_string.empty()) {
431 // output the width and horizontal position
432 if (params_.special != "none") {
433 os << "[" << params_.width.value()
434 << '\\' << from_utf8(params_.special)
437 os << '[' << from_ascii(width_string)
439 if (params_.hor_pos != 'c')
440 os << "[" << params_.hor_pos << "]";
446 os << "\\begin{minipage}";
448 // output parameters for parbox and minipage
449 if (!params_.use_makebox) {
450 os << "[" << params_.pos << "]";
451 if (params_.height_special == "none") {
453 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
456 // set no optional argument when the value is the default "1\height"
457 // (special units like \height are handled as "in")
458 // but when the user has chosen a non-default inner_pos, the height
459 // must be given: \minipage[pos][height][inner-pos]{width}
460 if ((params_.height != Length("1in") ||
461 params_.height_special != "totalheight") ||
462 params_.inner_pos != params_.pos) {
464 os << "[" << params_.height.value()
465 << "\\" << from_utf8(params_.height_special) << "]";
468 if (params_.inner_pos != params_.pos)
469 os << "[" << params_.inner_pos << "]";
471 os << '{' << from_ascii(width_string) << '}';
472 if (params_.use_parbox)
477 } // end if inner_box
479 if (btype == Shaded) {
480 os << "\\begin{shaded}%\n";
483 // \framebox and \makebox handle hor_pos their own way
484 // hor_pos is senseless for \mbox and \fbox
485 if (!(params_.use_makebox)
486 && !(btype == Boxed && !params_.inner_box)) {
487 switch (params_.hor_pos) {
489 // do nothing because this is LaTeX's default
492 os << "\\centering ";
495 os << "\\raggedleft ";
500 InsetText::latex(os, runparams);
503 os << "\\end{shaded}";
505 if (params_.inner_box) {
506 if (params_.use_parbox || params_.use_makebox)
509 os << "%\n\\end{minipage}";
516 os << "\\end{framed}";
517 if (!(separation_string.find(defaultSep) != string::npos)
518 || !(thickness_string.find(defaultThick) != string::npos))
523 if (!(separation_string.find(defaultSep) != string::npos)
524 || !(thickness_string.find(defaultThick) != string::npos))
529 if (!(separation_string.find(defaultSep) != string::npos))
534 if (!(separation_string.find(defaultSep) != string::npos))
539 if (!(separation_string.find(defaultSep) != string::npos)
540 || !(thickness_string.find(defaultThick) != string::npos))
545 if (!(separation_string.find(defaultSep) != string::npos)
546 || !(thickness_string.find(defaultThick) != string::npos)
547 || !(shadowsize_string.find(defaultShadow) != string::npos))
557 int InsetBox::plaintext(odocstringstream & os,
558 OutputParams const & runparams, size_t max_length) const
560 BoxType const btype = boxtranslator().find(params_.type);
584 InsetText::plaintext(os, runparams, max_length);
615 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
619 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
621 return InsetText::docbook(os, runparams);
625 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
627 // construct attributes
628 string attrs = "class='" + params_.type + "'";
630 if (!params_.width.empty()) {
631 string w = params_.width.asHTMLString();
633 style += ("width: " + params_.width.asHTMLString() + "; ");
635 // The special heights don't really mean anything for us.
636 if (!params_.height.empty() && params_.height_special == "none")
637 style += ("height: " + params_.height.asHTMLString() + "; ");
639 attrs += " style='" + style + "'";
641 xs << html::StartTag("div", attrs);
642 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
643 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
644 xs << html::EndTag("div");
650 void InsetBox::validate(LaTeXFeatures & features) const
652 BoxType btype = boxtranslator().find(params_.type);
657 features.require("calc");
658 features.require("framed");
661 features.require("calc");
667 features.require("calc");
668 features.require("fancybox");
671 features.require("color");
672 features.require("framed");
675 InsetCollapsable::validate(features);
679 string InsetBox::contextMenuName() const
681 return "context-box";
685 string InsetBox::params2string(InsetBoxParams const & params)
688 data << "box" << ' ';
694 void InsetBox::string2params(string const & in, InsetBoxParams & params)
699 istringstream data(in);
705 if (!lex || name != "box") {
706 LYXERR0("InsetBox::string2params(" << in << ")\n"
707 "Expected arg 1 to be \"box\"\n");
711 // This is part of the inset proper that is usually swallowed
712 // by Text::readInset
715 if (!lex || id != "Box") {
716 LYXERR0("InsetBox::string2params(" << in << ")\n"
717 "Expected arg 2 to be \"Box\"\n");
720 params = InsetBoxParams(string());
725 /////////////////////////////////////////////////////////////////////////
729 /////////////////////////////////////////////////////////////////////////
731 InsetBoxParams::InsetBoxParams(string const & label)
736 width(Length("100col%")),
741 height(Length("1in")),
742 height_special("totalheight"), // default is 1\\totalheight
743 thickness(Length("0.4pt")),
744 separation(Length("3pt")),
745 shadowsize(Length("4pt"))
749 void InsetBoxParams::write(ostream & os) const
751 os << "Box " << type << "\n";
752 os << "position \"" << pos << "\"\n";
753 os << "hor_pos \"" << hor_pos << "\"\n";
754 os << "has_inner_box " << inner_box << "\n";
755 os << "inner_pos \"" << inner_pos << "\"\n";
756 os << "use_parbox " << use_parbox << "\n";
757 os << "use_makebox " << use_makebox << "\n";
758 os << "width \"" << width.asString() << "\"\n";
759 os << "special \"" << special << "\"\n";
760 os << "height \"" << height.asString() << "\"\n";
761 os << "height_special \"" << height_special << "\"\n";
762 os << "thickness \"" << thickness.asString() << "\"\n";
763 os << "separation \"" << separation.asString() << "\"\n";
764 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
768 void InsetBoxParams::read(Lexer & lex)
770 lex.setContext("InsetBoxParams::read");
772 lex >> "position" >> pos;
773 lex >> "hor_pos" >> hor_pos;
774 lex >> "has_inner_box" >> inner_box;
775 if (type == "Framed")
777 lex >> "inner_pos" >> inner_pos;
778 lex >> "use_parbox" >> use_parbox;
779 lex >> "use_makebox" >> use_makebox;
780 lex >> "width" >> width;
781 lex >> "special" >> special;
782 lex >> "height" >> height;
783 lex >> "height_special" >> height_special;
784 lex >> "thickness" >> thickness;
785 lex >> "separation" >> separation;
786 lex >> "shadowsize" >> shadowsize;