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
10 * Full author contact details are available in file CREDITS.
18 #include "BufferParams.h"
19 #include "BufferView.h"
22 #include "DispatchResult.h"
23 #include "FuncStatus.h"
24 #include "FuncRequest.h"
25 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/Translator.h"
37 #include "frontends/Application.h"
42 using namespace lyx::support;
48 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
49 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
51 BoxTranslator initBoxtranslator()
53 BoxTranslator translator("Boxed", InsetBox::Boxed);
54 translator.addPair("Frameless", InsetBox::Frameless);
55 translator.addPair("Framed", InsetBox::Framed);
56 translator.addPair("ovalbox", InsetBox::ovalbox);
57 translator.addPair("Ovalbox", InsetBox::Ovalbox);
58 translator.addPair("Shadowbox", InsetBox::Shadowbox);
59 translator.addPair("Shaded", InsetBox::Shaded);
60 translator.addPair("Doublebox",InsetBox::Doublebox);
65 BoxTranslatorLoc initBoxtranslatorLoc()
67 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
68 translator.addPair(_("frameless"), InsetBox::Frameless);
69 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
70 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
71 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
72 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
73 translator.addPair(_("shaded background"), InsetBox::Shaded);
74 translator.addPair(_("double frame"), InsetBox::Doublebox);
79 BoxTranslator const & boxtranslator()
81 static BoxTranslator translator = initBoxtranslator();
86 BoxTranslatorLoc const & boxtranslator_loc()
88 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
95 /////////////////////////////////////////////////////////////////////////
99 /////////////////////////////////////////////////////////////////////////
101 InsetBox::InsetBox(Buffer * buffer, string const & label)
102 : InsetCollapsable(buffer), params_(label)
106 docstring InsetBox::layoutName() const
109 return from_ascii("Box:" + params_.type);
113 void InsetBox::write(ostream & os) const
116 InsetCollapsable::write(os);
120 void InsetBox::read(Lexer & lex)
123 InsetCollapsable::read(lex);
127 void InsetBox::setButtonLabel()
129 BoxType const btype = boxtranslator().find(params_.type);
131 docstring const type = _("Box");
134 if (params_.inner_box) {
135 if (params_.use_parbox)
137 else if (params_.use_makebox)
138 inner = _("Makebox");
140 inner = _("Minipage");
144 if (btype != Frameless)
145 frame = boxtranslator_loc().find(btype);
148 if (inner.empty() && frame.empty())
150 else if (inner.empty())
151 label = bformat(_("%1$s (%2$s)"),
153 else if (frame.empty())
154 label = bformat(_("%1$s (%2$s)"),
157 label = bformat(_("%1$s (%2$s, %3$s)"),
163 bool InsetBox::hasFixedWidth() const
165 return from_ascii(params_.width.asLatexString()) != "-9.99\\columnwidth";
169 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
171 // back up textwidth.
172 int textwidth_backup = m.base.textwidth;
174 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
175 InsetCollapsable::metrics(m, dim);
177 m.base.textwidth = textwidth_backup;
181 bool InsetBox::forcePlainLayout(idx_type) const
183 return (!params_.inner_box || params_.use_makebox)
184 && params_.type != "Shaded" && params_.type != "Framed";
188 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
190 if (params_.type != "Shaded")
191 return getLayout().bgcolor();
192 // FIXME: This hardcoded color is a hack!
193 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
194 return getLayout().bgcolor();
195 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
197 return getLayout().bgcolor();
202 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
204 switch (cmd.action()) {
206 case LFUN_INSET_MODIFY: {
207 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
208 string const first_arg = cmd.getArg(0);
209 bool const change_type = first_arg == "changetype";
210 bool const for_box = first_arg == "box";
211 if (!change_type && !for_box) {
213 // this will not be handled higher up
217 cur.recordUndoInset(ATOMIC_UNDO, this);
219 params_.type = cmd.getArg(1);
221 string2params(to_utf8(cmd.argument()), params_);
227 InsetCollapsable::doDispatch(cur, cmd);
233 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
234 FuncStatus & flag) const
236 switch (cmd.action()) {
238 case LFUN_INSET_MODIFY: {
239 string const first_arg = cmd.getArg(0);
240 if (first_arg == "changetype") {
241 string const type = cmd.getArg(1);
242 flag.setOnOff(type == params_.type);
243 flag.setEnabled(!params_.inner_box || type != "Framed");
246 if (first_arg == "box") {
247 flag.setEnabled(true);
250 return InsetCollapsable::getStatus(cur, cmd, flag);
253 case LFUN_INSET_DIALOG_UPDATE:
254 flag.setEnabled(true);
257 case LFUN_PARAGRAPH_BREAK:
258 if ((params_.inner_box && !params_.use_makebox)
259 || params_.type == "Shaded" || params_.type == "Framed")
260 return InsetCollapsable::getStatus(cur, cmd, flag);
261 flag.setEnabled(false);
265 return InsetCollapsable::getStatus(cur, cmd, flag);
270 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
272 BoxType btype = boxtranslator().find(params_.type);
274 string width_string = params_.width.asLatexString();
275 bool stdwidth = false;
276 if (params_.inner_box &&
277 (width_string.find("1.0\\columnwidth") != string::npos
278 || width_string.find("1.0\\textwidth") != string::npos)) {
286 width_string += " - 2\\fboxsep - 2\\fboxrule";
289 width_string += " - 2\\fboxsep - 0.8pt";
292 width_string += " - 2\\fboxsep - 1.6pt";
295 // Shadow falls outside right margin... opinions?
296 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
299 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
305 if (runparams.lastid != -1)
306 os.texrow().start(runparams.lastid, runparams.lastpos);
308 // Adapt to column/text width correctly also if paragraphs indented:
316 os << "\\begin{framed}%\n";
319 // "-999col%" is the code for no width
320 if (from_ascii(width_string) != "-9.99\\columnwidth") {
322 if (!params_.inner_box) {
323 // Special widths, see usrguide §3.5
325 if (params_.special != "none") {
326 os << "[" << params_.width.value()
327 << '\\' << from_utf8(params_.special)
330 os << '[' << from_ascii(width_string)
332 if (params_.hor_pos != 'c')
333 os << "[" << params_.hor_pos << "]";
346 os << "\\shadowbox{";
349 // must be set later because e.g. the width settings only work when
350 // it is inside a minipage or parbox
353 os << "\\doublebox{";
357 if (params_.inner_box) {
358 if (params_.use_parbox)
360 else if (params_.use_makebox) {
361 // "-999col%" is the code for no width
362 if (from_ascii(width_string) != "-9.99\\columnwidth") {
365 // output the width and horizontal position
366 if (params_.special != "none") {
367 os << "[" << params_.width.value()
368 << '\\' << from_utf8(params_.special)
371 os << '[' << from_ascii(width_string)
373 if (params_.hor_pos != 'c')
374 os << "[" << params_.hor_pos << "]";
380 os << "\\begin{minipage}";
382 // output parameters for parbox and minipage
383 if (!params_.use_makebox) {
384 os << "[" << params_.pos << "]";
385 if (params_.height_special == "none") {
387 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
390 // set no optional argument when the value is the default "1\height"
391 // (special units like \height are handled as "in")
392 // but when the user has chosen a non-default inner_pos, the height
393 // must be given: \minipage[pos][height][inner-pos]{width}
394 if ((params_.height != Length("1in") ||
395 params_.height_special != "totalheight") ||
396 params_.inner_pos != params_.pos) {
398 os << "[" << params_.height.value()
399 << "\\" << from_utf8(params_.height_special) << "]";
402 if (params_.inner_pos != params_.pos)
403 os << "[" << params_.inner_pos << "]";
405 os << '{' << from_ascii(width_string) << '}';
406 if (params_.use_parbox)
411 } // end if inner_box
413 if (btype == Shaded) {
414 os << "\\begin{shaded}%\n";
417 InsetText::latex(os, runparams);
420 os << "\\end{shaded}";
422 if (params_.inner_box) {
423 if (params_.use_parbox || params_.use_makebox)
426 os << "%\n\\end{minipage}";
433 os << "\\end{framed}";
451 int InsetBox::plaintext(odocstringstream & os,
452 OutputParams const & runparams, size_t max_length) const
454 BoxType const btype = boxtranslator().find(params_.type);
478 InsetText::plaintext(os, runparams, max_length);
509 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
513 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
515 return InsetText::docbook(os, runparams);
519 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
521 // construct attributes
522 string attrs = "class='" + params_.type + "'";
524 if (!params_.width.empty()) {
525 string w = params_.width.asHTMLString();
527 style += ("width: " + params_.width.asHTMLString() + "; ");
529 // The special heights don't really mean anything for us.
530 if (!params_.height.empty() && params_.height_special == "none")
531 style += ("height: " + params_.height.asHTMLString() + "; ");
533 attrs += " style='" + style + "'";
535 xs << html::StartTag("div", attrs);
536 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
537 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
538 xs << html::EndTag("div");
544 void InsetBox::validate(LaTeXFeatures & features) const
546 BoxType btype = boxtranslator().find(params_.type);
551 features.require("framed");
554 features.require("calc");
560 features.require("calc");
561 features.require("fancybox");
564 features.require("color");
565 features.require("framed");
568 InsetCollapsable::validate(features);
572 string InsetBox::contextMenuName() const
574 return "context-box";
578 string InsetBox::params2string(InsetBoxParams const & params)
581 data << "box" << ' ';
587 void InsetBox::string2params(string const & in, InsetBoxParams & params)
592 istringstream data(in);
598 if (!lex || name != "box") {
599 LYXERR0("InsetBox::string2params(" << in << ")\n"
600 "Expected arg 1 to be \"box\"\n");
604 // This is part of the inset proper that is usually swallowed
605 // by Text::readInset
608 if (!lex || id != "Box") {
609 LYXERR0("InsetBox::string2params(" << in << ")\n"
610 "Expected arg 2 to be \"Box\"\n");
613 params = InsetBoxParams(string());
618 /////////////////////////////////////////////////////////////////////////
622 /////////////////////////////////////////////////////////////////////////
624 InsetBoxParams::InsetBoxParams(string const & label)
629 width(Length("100col%")),
634 height(Length("1in")),
635 height_special("totalheight") // default is 1\\totalheight
639 void InsetBoxParams::write(ostream & os) const
641 os << "Box " << type << "\n";
642 os << "position \"" << pos << "\"\n";
643 os << "hor_pos \"" << hor_pos << "\"\n";
644 os << "has_inner_box " << inner_box << "\n";
645 os << "inner_pos \"" << inner_pos << "\"\n";
646 os << "use_parbox " << use_parbox << "\n";
647 os << "use_makebox " << use_makebox << "\n";
648 os << "width \"" << width.asString() << "\"\n";
649 os << "special \"" << special << "\"\n";
650 os << "height \"" << height.asString() << "\"\n";
651 os << "height_special \"" << height_special << "\"\n";
655 void InsetBoxParams::read(Lexer & lex)
657 lex.setContext("InsetBoxParams::read");
659 lex >> "position" >> pos;
660 lex >> "hor_pos" >> hor_pos;
661 lex >> "has_inner_box" >> inner_box;
662 if (type == "Framed")
664 lex >> "inner_pos" >> inner_pos;
665 lex >> "use_parbox" >> use_parbox;
666 lex >> "use_makebox" >> use_makebox;
667 lex >> "width" >> width;
668 lex >> "special" >> special;
669 lex >> "height" >> height;
670 lex >> "height_special" >> height_special;