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 LyXAlignment InsetBox::contentAlignment() const
228 if (!params_.use_makebox)
229 return LYX_ALIGN_NONE;
231 // The default value below is actually irrelevant
232 LyXAlignment align = LYX_ALIGN_NONE;
233 switch (params_.hor_pos) {
235 align = LYX_ALIGN_LEFT;
238 align = LYX_ALIGN_CENTER;
241 align = LYX_ALIGN_RIGHT;
244 align = LYX_ALIGN_BLOCK;
251 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
253 switch (cmd.action()) {
255 case LFUN_INSET_MODIFY: {
256 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
257 string const first_arg = cmd.getArg(0);
258 bool const change_type = first_arg == "changetype";
259 bool const for_box = first_arg == "box";
260 if (!change_type && !for_box) {
262 // this will not be handled higher up
266 cur.recordUndoInset(this);
268 params_.type = cmd.getArg(1);
270 string2params(to_utf8(cmd.argument()), params_);
276 InsetCollapsable::doDispatch(cur, cmd);
282 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
283 FuncStatus & flag) const
285 switch (cmd.action()) {
287 case LFUN_INSET_MODIFY: {
288 string const first_arg = cmd.getArg(0);
289 if (first_arg == "changetype") {
290 string const type = cmd.getArg(1);
291 flag.setOnOff(type == params_.type);
292 flag.setEnabled(!params_.inner_box || type != "Framed");
295 if (first_arg == "box") {
296 flag.setEnabled(true);
299 return InsetCollapsable::getStatus(cur, cmd, flag);
302 case LFUN_INSET_DIALOG_UPDATE:
303 flag.setEnabled(true);
307 return InsetCollapsable::getStatus(cur, cmd, flag);
312 const string defaultThick = "0.4pt";
313 const string defaultSep = "3pt";
314 const string defaultShadow = "4pt";
316 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
318 BoxType btype = boxtranslator().find(params_.type);
320 string width_string = params_.width.asLatexString();
321 string thickness_string = params_.thickness.asLatexString();
322 string separation_string = params_.separation.asLatexString();
323 string shadowsize_string = params_.shadowsize.asLatexString();
324 bool stdwidth = false;
325 // in general the overall width of some decorated boxes is wider thean the inner box
326 // we could therefore calculate the real width for all sizes so that if the user wants
327 // e.g. 0.1\columnwidth or 2cm he gets exactly this size
328 // however this makes problems when importing TeX code
329 // therefore only recalculate for the most common case that the box should not protrude
331 if (params_.inner_box
332 && ((width_string.find("1\\columnwidth") != string::npos
333 || width_string.find("1\\textwidth") != string::npos)
334 || width_string.find("1\\paperwidth") != string::npos
335 || width_string.find("1\\linewidth") != string::npos)) {
341 width_string += " - 2\\FrameSep - 2\\FrameRule";
344 width_string += " - 2\\fboxsep - 2\\fboxrule";
349 width_string += " - 2\\fboxsep - 0.8pt";
352 width_string += " - 2\\fboxsep - 1.6pt";
355 width_string += " - 2\\fboxsep - 2\\fboxrule - \\shadowsize";
358 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
364 if (runparams.lastid != -1)
365 os.texrow().start(runparams.lastid, runparams.lastpos);
367 // adapt column/text width correctly also if paragraphs indented
368 if (stdwidth && !(buffer().params().paragraph_separation))
375 if (thickness_string != defaultThick) {
376 os << "{\\FrameRule " << from_ascii(thickness_string);
377 if (separation_string != defaultSep)
378 os << "\\FrameSep " << from_ascii(separation_string);
380 if (separation_string != defaultSep && thickness_string == defaultThick)
381 os << "{\\FrameSep " << from_ascii(separation_string);
383 os << "\\begin{framed}%\n";
386 if (thickness_string != defaultThick) {
387 os << "{\\fboxrule " << from_ascii(thickness_string);
388 if (separation_string != defaultSep)
389 os << "\\fboxsep " << from_ascii(separation_string);
391 if (separation_string != defaultSep && thickness_string == defaultThick)
392 os << "{\\fboxsep " << from_ascii(separation_string);
393 if (!params_.inner_box && !width_string.empty()) {
394 if (params_.framecolor != "black" || params_.backgroundcolor != "none") {
395 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}{";
399 // Special widths, see usrguide sec. 3.5
401 if (params_.special != "none") {
402 os << "[" << params_.width.value()
403 << '\\' << from_utf8(params_.special)
406 os << '[' << from_ascii(width_string)
408 // default horizontal alignment is 'c'
409 if (params_.hor_pos != 'c')
410 os << "[" << params_.hor_pos << "]";
412 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
413 os << "\\fcolorbox{" << params_.framecolor << "}{" << params_.backgroundcolor << "}";
420 if (!separation_string.empty() && separation_string != defaultSep)
421 os << "{\\fboxsep " << from_ascii(separation_string);
425 if (!separation_string.empty() && separation_string != defaultSep)
426 os << "{\\fboxsep " << from_ascii(separation_string);
430 if (thickness_string != defaultThick) {
431 os << "{\\fboxrule " << from_ascii(thickness_string);
432 if (separation_string != defaultSep) {
433 os << "\\fboxsep " << from_ascii(separation_string);
434 if (shadowsize_string != defaultShadow)
435 os << "\\shadowsize " << from_ascii(shadowsize_string);
437 if (shadowsize_string != defaultShadow && separation_string == defaultSep)
438 os << "\\shadowsize " << from_ascii(shadowsize_string);
440 if (separation_string != defaultSep && thickness_string == defaultThick) {
441 os << "{\\fboxsep " << from_ascii(separation_string);
442 if (shadowsize_string != defaultShadow)
443 os << "\\shadowsize " << from_ascii(shadowsize_string);
445 if (shadowsize_string != defaultShadow
446 && separation_string == defaultSep
447 && thickness_string == defaultThick)
448 os << "{\\shadowsize " << from_ascii(shadowsize_string);
449 os << "\\shadowbox{";
452 // must be set later because e.g. the width settings only work when
453 // it is inside a minipage or parbox
456 if (thickness_string != defaultThick) {
457 os << "{\\fboxrule " << from_ascii(thickness_string);
458 if (separation_string != defaultSep)
459 os << "\\fboxsep " << from_ascii(separation_string);
461 if (separation_string != defaultSep && thickness_string == defaultThick)
462 os << "{\\fboxsep " << from_ascii(separation_string);
463 os << "\\doublebox{";
467 if (params_.inner_box) {
468 if (params_.use_parbox) {
469 if (params_.backgroundcolor != "none" && btype == Frameless)
470 os << "\\colorbox{" << params_.backgroundcolor << "}{";
472 } else if (params_.use_makebox) {
473 if (!width_string.empty()) {
474 if (params_.backgroundcolor != "none")
475 os << "\\colorbox{" << params_.backgroundcolor << "}{";
478 // output the width and horizontal position
479 if (params_.special != "none") {
480 os << "[" << params_.width.value()
481 << '\\' << from_utf8(params_.special)
484 os << '[' << from_ascii(width_string)
486 if (params_.hor_pos != 'c')
487 os << "[" << params_.hor_pos << "]";
489 if (params_.backgroundcolor != "none")
490 os << "\\colorbox{" << params_.backgroundcolor << "}";
497 if (params_.backgroundcolor != "none" && btype == Frameless)
498 os << "\\colorbox{" << params_.backgroundcolor << "}{";
499 os << "\\begin{minipage}";
502 // output parameters for parbox and minipage
503 if (!params_.use_makebox) {
504 os << "[" << params_.pos << "]";
505 if (params_.height_special == "none") {
507 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
510 // set no optional argument when the value is the default "1\height"
511 // (special units like \height are handled as "in")
512 // but when the user has chosen a non-default inner_pos, the height
513 // must be given: \minipage[pos][height][inner-pos]{width}
514 if ((params_.height != Length("1in") ||
515 params_.height_special != "totalheight") ||
516 params_.inner_pos != params_.pos) {
518 os << "[" << params_.height.value()
519 << "\\" << from_utf8(params_.height_special) << "]";
522 if (params_.inner_pos != params_.pos)
523 os << "[" << params_.inner_pos << "]";
525 os << '{' << from_ascii(width_string) << '}';
526 if (params_.use_parbox)
531 } // end if inner_box
533 if (btype == Shaded) {
534 os << "\\begin{shaded}%\n";
537 InsetText::latex(os, runparams);
540 os << "\\end{shaded}";
542 if (params_.inner_box) {
543 if (params_.use_parbox || params_.use_makebox)
546 os << "%\n\\end{minipage}";
547 if (params_.backgroundcolor != "none" && btype == Frameless
548 && !(params_.use_makebox && width_string.empty()))
556 os << "\\end{framed}";
557 if (separation_string != defaultSep || thickness_string != defaultThick)
562 if (!params_.inner_box && !width_string.empty()
563 && (params_.framecolor != "black" || params_.backgroundcolor != "none"))
565 if (separation_string != defaultSep || thickness_string != defaultThick)
570 if (separation_string != defaultSep)
575 if (separation_string != defaultSep)
580 if (separation_string != defaultSep || thickness_string != defaultThick)
585 if (separation_string != defaultSep
586 || thickness_string != defaultThick
587 || shadowsize_string != defaultShadow)
597 int InsetBox::plaintext(odocstringstream & os,
598 OutputParams const & runparams, size_t max_length) const
600 BoxType const btype = boxtranslator().find(params_.type);
624 InsetText::plaintext(os, runparams, max_length);
655 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
659 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
661 return InsetText::docbook(os, runparams);
665 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
667 // construct attributes
668 string attrs = "class='" + params_.type + "'";
670 if (!params_.width.empty()) {
671 string w = params_.width.asHTMLString();
673 style += ("width: " + params_.width.asHTMLString() + "; ");
675 // The special heights don't really mean anything for us.
676 if (!params_.height.empty() && params_.height_special == "none")
677 style += ("height: " + params_.height.asHTMLString() + "; ");
679 attrs += " style='" + style + "'";
681 xs << html::StartTag("div", attrs);
682 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
683 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
684 xs << html::EndTag("div");
690 void InsetBox::validate(LaTeXFeatures & features) const
692 BoxType btype = boxtranslator().find(params_.type);
695 if (params_.backgroundcolor != "none")
696 features.require("xcolor");
699 features.require("calc");
700 features.require("framed");
703 features.require("calc");
704 if (params_.framecolor != "black" || params_.backgroundcolor != "none")
705 features.require("xcolor");
711 features.require("calc");
712 features.require("fancybox");
715 features.require("color");
716 features.require("framed");
719 InsetCollapsable::validate(features);
723 string InsetBox::contextMenuName() const
725 return "context-box";
729 string InsetBox::params2string(InsetBoxParams const & params)
732 data << "box" << ' ';
738 void InsetBox::string2params(string const & in, InsetBoxParams & params)
743 istringstream data(in);
749 if (!lex || name != "box") {
750 LYXERR0("InsetBox::string2params(" << in << ")\n"
751 "Expected arg 1 to be \"box\"\n");
755 // This is part of the inset proper that is usually swallowed
756 // by Text::readInset
759 if (!lex || id != "Box") {
760 LYXERR0("InsetBox::string2params(" << in << ")\n"
761 "Expected arg 2 to be \"Box\"\n");
764 params = InsetBoxParams(string());
769 /////////////////////////////////////////////////////////////////////////
773 /////////////////////////////////////////////////////////////////////////
775 InsetBoxParams::InsetBoxParams(string const & label)
780 width(Length("100col%")),
785 height(Length("1in")),
786 height_special("totalheight"), // default is 1\\totalheight
787 thickness(Length(defaultThick)),
788 separation(Length(defaultSep)),
789 shadowsize(Length(defaultShadow)),
791 backgroundcolor("none")
795 void InsetBoxParams::write(ostream & os) const
797 os << "Box " << type << "\n";
798 os << "position \"" << pos << "\"\n";
799 os << "hor_pos \"" << hor_pos << "\"\n";
800 os << "has_inner_box " << inner_box << "\n";
801 os << "inner_pos \"" << inner_pos << "\"\n";
802 os << "use_parbox " << use_parbox << "\n";
803 os << "use_makebox " << use_makebox << "\n";
804 os << "width \"" << width.asString() << "\"\n";
805 os << "special \"" << special << "\"\n";
806 os << "height \"" << height.asString() << "\"\n";
807 os << "height_special \"" << height_special << "\"\n";
808 os << "thickness \"" << thickness.asString() << "\"\n";
809 os << "separation \"" << separation.asString() << "\"\n";
810 os << "shadowsize \"" << shadowsize.asString() << "\"\n";
811 os << "framecolor \"" << framecolor << "\"\n";
812 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
816 void InsetBoxParams::read(Lexer & lex)
818 lex.setContext("InsetBoxParams::read");
820 lex >> "position" >> pos;
821 lex >> "hor_pos" >> hor_pos;
822 lex >> "has_inner_box" >> inner_box;
823 if (type == "Framed")
825 lex >> "inner_pos" >> inner_pos;
826 lex >> "use_parbox" >> use_parbox;
827 lex >> "use_makebox" >> use_makebox;
828 lex >> "width" >> width;
829 lex >> "special" >> special;
830 lex >> "height" >> height;
831 lex >> "height_special" >> height_special;
832 lex >> "thickness" >> thickness;
833 lex >> "separation" >> separation;
834 lex >> "shadowsize" >> shadowsize;
835 lex >> "framecolor" >> framecolor;
836 lex >> "backgroundcolor" >> backgroundcolor;