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"
21 #include "DispatchResult.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
26 #include "MetricsInfo.h"
27 #include "output_xhtml.h"
28 #include "TextClass.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/Translator.h"
36 #include "frontends/Application.h"
41 using namespace lyx::support;
47 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
48 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
50 BoxTranslator initBoxtranslator()
52 BoxTranslator translator("Boxed", InsetBox::Boxed);
53 translator.addPair("Frameless", InsetBox::Frameless);
54 translator.addPair("Framed", InsetBox::Framed);
55 translator.addPair("ovalbox", InsetBox::ovalbox);
56 translator.addPair("Ovalbox", InsetBox::Ovalbox);
57 translator.addPair("Shadowbox", InsetBox::Shadowbox);
58 translator.addPair("Shaded", InsetBox::Shaded);
59 translator.addPair("Doublebox",InsetBox::Doublebox);
64 BoxTranslatorLoc initBoxtranslatorLoc()
66 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
67 translator.addPair(_("frameless"), InsetBox::Frameless);
68 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
69 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
70 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
71 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
72 translator.addPair(_("shaded background"), InsetBox::Shaded);
73 translator.addPair(_("double frame"), InsetBox::Doublebox);
78 BoxTranslator const & boxtranslator()
80 static BoxTranslator translator = initBoxtranslator();
85 BoxTranslatorLoc const & boxtranslator_loc()
87 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
94 /////////////////////////////////////////////////////////////////////////
98 /////////////////////////////////////////////////////////////////////////
100 InsetBox::InsetBox(Buffer * buffer, string const & label)
101 : InsetCollapsable(buffer), params_(label)
105 docstring InsetBox::name() const
109 if (boxtranslator().find(params_.type) == Shaded)
111 return from_ascii(name);
115 void InsetBox::write(ostream & os) const
118 InsetCollapsable::write(os);
122 void InsetBox::read(Lexer & lex)
125 InsetCollapsable::read(lex);
129 void InsetBox::setButtonLabel()
131 BoxType const btype = boxtranslator().find(params_.type);
133 docstring const type = _("Box");
136 if (params_.inner_box) {
137 if (params_.use_parbox)
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 params_.inner_box || params_.special != "width";
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_.type != "Framed";
187 bool InsetBox::showInsetDialog(BufferView * bv) const
189 bv->showDialog("box");
194 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
196 switch (cmd.action) {
198 case LFUN_INSET_MODIFY: {
199 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
200 if (cmd.getArg(0) == "changetype")
201 params_.type = cmd.getArg(1);
203 string2params(to_utf8(cmd.argument()), params_);
209 InsetCollapsable::doDispatch(cur, cmd);
215 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
216 FuncStatus & flag) const
218 switch (cmd.action) {
220 case LFUN_INSET_MODIFY:
221 if (cmd.getArg(0) == "changetype")
222 flag.setOnOff(cmd.getArg(1) == params_.type);
223 flag.setEnabled(true);
226 case LFUN_INSET_DIALOG_UPDATE:
227 flag.setEnabled(true);
230 case LFUN_BREAK_PARAGRAPH:
231 if (params_.inner_box || params_.type == "Framed")
232 return InsetCollapsable::getStatus(cur, cmd, flag);
233 flag.setEnabled(false);
237 return InsetCollapsable::getStatus(cur, cmd, flag);
242 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
244 BoxType btype = boxtranslator().find(params_.type);
246 string width_string = params_.width.asLatexString();
247 bool stdwidth = false;
248 if (params_.inner_box &&
249 (width_string.find("1.0\\columnwidth") != string::npos
250 || width_string.find("1.0\\textwidth") != string::npos)) {
258 width_string += " - 2\\fboxsep - 2\\fboxrule";
261 width_string += " - 2\\fboxsep - 0.8pt";
264 width_string += " - 2\\fboxsep - 1.6pt";
267 // Shadow falls outside right margin... opinions?
268 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
271 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
278 // Adapt to column/text width correctly also if paragraphs indented:
286 os << "\\begin{framed}%\n";
291 if (!params_.inner_box) {
293 // Special widths, see usrguide §3.5
295 if (params_.special != "none") {
296 os << "[" << params_.width.value()
297 << '\\' << from_utf8(params_.special)
300 os << '[' << from_ascii(width_string)
302 if (params_.hor_pos != 'c')
303 os << "[" << params_.hor_pos << "]";
315 os << "\\shadowbox{";
321 os << "\\doublebox{";
325 if (params_.inner_box) {
326 if (params_.use_parbox)
329 os << "\\begin{minipage}";
331 os << "[" << params_.pos << "]";
332 if (params_.height_special == "none") {
334 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
337 // set no optional argument when the value is the default "1\height"
338 // (special units like \height are handled as "in")
339 // but when the user has chosen a non-default inner_pos, the height
340 // must be given: \minipage[pos][height][inner-pos]{width}
341 if ((params_.height != Length("1in") ||
342 params_.height_special != "totalheight") ||
343 params_.inner_pos != params_.pos) {
345 os << "[" << params_.height.value()
346 << "\\" << from_utf8(params_.height_special) << "]";
349 if (params_.inner_pos != params_.pos)
350 os << "[" << params_.inner_pos << "]";
353 os << '{' << from_ascii(width_string) << '}';
355 if (params_.use_parbox)
360 if (btype == Shaded) {
361 os << "\\begin{shaded}%\n";
365 i += InsetText::latex(os, runparams);
368 os << "\\end{shaded}";
370 if (params_.inner_box) {
371 if (params_.use_parbox)
374 os << "%\n\\end{minipage}";
381 os << "\\end{framed}";
384 if (!params_.inner_box)
385 os << "}"; // for makebox
405 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
407 BoxType const btype = boxtranslator().find(params_.type);
431 InsetText::plaintext(os, runparams);
462 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
466 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
468 return InsetText::docbook(os, runparams);
472 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
474 // construct attributes
475 string attrs = "class='" + params_.type + "'";
477 if (!params_.width.empty())
478 style += ("width: " + params_.width.asHTMLString() + "; ");
479 // The special heights don't really mean anything for us.
480 if (!params_.height.empty() && params_.height_special == "none")
481 style += ("height: " + params_.height.asHTMLString() + "; ");
483 attrs += " style='" + style + "'";
485 xs << html::StartTag("div", attrs);
486 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
487 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
488 xs << html::EndTag("div");
494 void InsetBox::validate(LaTeXFeatures & features) const
496 BoxType btype = boxtranslator().find(params_.type);
501 features.require("framed");
504 features.require("calc");
510 features.require("calc");
511 features.require("fancybox");
514 features.require("color");
515 features.require("framed");
518 InsetCollapsable::validate(features);
522 docstring InsetBox::contextMenu(BufferView const &, int, int) const
524 return from_ascii("context-box");
528 string InsetBox::params2string(InsetBoxParams const & params)
531 data << "box" << ' ';
537 void InsetBox::string2params(string const & in, InsetBoxParams & params)
539 params = InsetBoxParams(string());
543 istringstream data(in);
549 if (!lex || name != "box") {
550 LYXERR0("InsetBox::string2params(" << in << ")\n"
551 "Expected arg 1 to be \"box\"\n");
555 // This is part of the inset proper that is usually swallowed
556 // by Text::readInset
559 if (!lex || id != "Box") {
560 LYXERR0("InsetBox::string2params(" << in << ")\n"
561 "Expected arg 2 to be \"Box\"\n");
568 /////////////////////////////////////////////////////////////////////////
572 /////////////////////////////////////////////////////////////////////////
574 InsetBoxParams::InsetBoxParams(string const & label)
578 width(Length("100col%")),
583 height(Length("1in")),
584 height_special("totalheight") // default is 1\\totalheight
588 void InsetBoxParams::write(ostream & os) const
590 os << "Box " << type << "\n";
591 os << "position \"" << pos << "\"\n";
592 os << "hor_pos \"" << hor_pos << "\"\n";
593 os << "has_inner_box " << inner_box << "\n";
594 os << "inner_pos \"" << inner_pos << "\"\n";
595 os << "use_parbox " << use_parbox << "\n";
596 os << "width \"" << width.asString() << "\"\n";
597 os << "special \"" << special << "\"\n";
598 os << "height \"" << height.asString() << "\"\n";
599 os << "height_special \"" << height_special << "\"\n";
603 void InsetBoxParams::read(Lexer & lex)
605 lex.setContext("InsetBoxParams::read");
607 lex >> "position" >> pos;
608 lex >> "hor_pos" >> hor_pos;
609 lex >> "has_inner_box" >> inner_box;
610 if (type == "Framed")
612 lex >> "inner_pos" >> inner_pos;
613 lex >> "use_parbox" >> use_parbox;
614 lex >> "width" >> width;
615 lex >> "special" >> special;
616 lex >> "height" >> height;
617 lex >> "height_special" >> height_special;