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 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
189 switch (cmd.action()) {
191 case LFUN_INSET_MODIFY: {
192 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
193 if (cmd.getArg(0) == "changetype")
194 params_.type = cmd.getArg(1);
196 string2params(to_utf8(cmd.argument()), params_);
202 InsetCollapsable::doDispatch(cur, cmd);
208 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
209 FuncStatus & flag) const
211 switch (cmd.action()) {
213 case LFUN_INSET_MODIFY:
214 if (cmd.getArg(0) == "changetype")
215 flag.setOnOff(cmd.getArg(1) == params_.type);
216 flag.setEnabled(true);
219 case LFUN_INSET_DIALOG_UPDATE:
220 flag.setEnabled(true);
223 case LFUN_BREAK_PARAGRAPH:
224 if (params_.inner_box || params_.type == "Framed")
225 return InsetCollapsable::getStatus(cur, cmd, flag);
226 flag.setEnabled(false);
230 return InsetCollapsable::getStatus(cur, cmd, flag);
235 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
237 BoxType btype = boxtranslator().find(params_.type);
239 string width_string = params_.width.asLatexString();
240 bool stdwidth = false;
241 if (params_.inner_box &&
242 (width_string.find("1.0\\columnwidth") != string::npos
243 || width_string.find("1.0\\textwidth") != string::npos)) {
251 width_string += " - 2\\fboxsep - 2\\fboxrule";
254 width_string += " - 2\\fboxsep - 0.8pt";
257 width_string += " - 2\\fboxsep - 1.6pt";
260 // Shadow falls outside right margin... opinions?
261 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
264 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
271 // Adapt to column/text width correctly also if paragraphs indented:
279 os << "\\begin{framed}%\n";
284 if (!params_.inner_box) {
285 // Special widths, see usrguide §3.5
287 if (params_.special != "none") {
288 os << "[" << params_.width.value()
289 << '\\' << from_utf8(params_.special)
292 os << '[' << from_ascii(width_string)
294 if (params_.hor_pos != 'c')
295 os << "[" << params_.hor_pos << "]";
307 os << "\\shadowbox{";
313 os << "\\doublebox{";
317 if (params_.inner_box) {
318 if (params_.use_parbox)
321 os << "\\begin{minipage}";
323 os << "[" << params_.pos << "]";
324 if (params_.height_special == "none") {
326 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
329 // set no optional argument when the value is the default "1\height"
330 // (special units like \height are handled as "in")
331 // but when the user has chosen a non-default inner_pos, the height
332 // must be given: \minipage[pos][height][inner-pos]{width}
333 if ((params_.height != Length("1in") ||
334 params_.height_special != "totalheight") ||
335 params_.inner_pos != params_.pos) {
337 os << "[" << params_.height.value()
338 << "\\" << from_utf8(params_.height_special) << "]";
341 if (params_.inner_pos != params_.pos)
342 os << "[" << params_.inner_pos << "]";
345 os << '{' << from_ascii(width_string) << '}';
347 if (params_.use_parbox)
352 if (btype == Shaded) {
353 os << "\\begin{shaded}%\n";
357 i += InsetText::latex(os, runparams);
360 os << "\\end{shaded}";
362 if (params_.inner_box) {
363 if (params_.use_parbox)
366 os << "%\n\\end{minipage}";
373 os << "\\end{framed}";
395 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
397 BoxType const btype = boxtranslator().find(params_.type);
421 InsetText::plaintext(os, runparams);
452 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
456 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
458 return InsetText::docbook(os, runparams);
462 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
464 // construct attributes
465 string attrs = "class='" + params_.type + "'";
467 if (!params_.width.empty())
468 style += ("width: " + params_.width.asHTMLString() + "; ");
469 // The special heights don't really mean anything for us.
470 if (!params_.height.empty() && params_.height_special == "none")
471 style += ("height: " + params_.height.asHTMLString() + "; ");
473 attrs += " style='" + style + "'";
475 xs << html::StartTag("div", attrs);
476 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
477 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
478 xs << html::EndTag("div");
484 void InsetBox::validate(LaTeXFeatures & features) const
486 BoxType btype = boxtranslator().find(params_.type);
491 features.require("framed");
494 features.require("calc");
500 features.require("calc");
501 features.require("fancybox");
504 features.require("color");
505 features.require("framed");
508 InsetCollapsable::validate(features);
512 docstring InsetBox::contextMenu(BufferView const &, int, int) const
514 return from_ascii("context-box");
518 string InsetBox::params2string(InsetBoxParams const & params)
521 data << "box" << ' ';
527 void InsetBox::string2params(string const & in, InsetBoxParams & params)
529 params = InsetBoxParams(string());
533 istringstream data(in);
539 if (!lex || name != "box") {
540 LYXERR0("InsetBox::string2params(" << in << ")\n"
541 "Expected arg 1 to be \"box\"\n");
545 // This is part of the inset proper that is usually swallowed
546 // by Text::readInset
549 if (!lex || id != "Box") {
550 LYXERR0("InsetBox::string2params(" << in << ")\n"
551 "Expected arg 2 to be \"Box\"\n");
558 /////////////////////////////////////////////////////////////////////////
562 /////////////////////////////////////////////////////////////////////////
564 InsetBoxParams::InsetBoxParams(string const & label)
568 width(Length("100col%")),
573 height(Length("1in")),
574 height_special("totalheight") // default is 1\\totalheight
578 void InsetBoxParams::write(ostream & os) const
580 os << "Box " << type << "\n";
581 os << "position \"" << pos << "\"\n";
582 os << "hor_pos \"" << hor_pos << "\"\n";
583 os << "has_inner_box " << inner_box << "\n";
584 os << "inner_pos \"" << inner_pos << "\"\n";
585 os << "use_parbox " << use_parbox << "\n";
586 os << "width \"" << width.asString() << "\"\n";
587 os << "special \"" << special << "\"\n";
588 os << "height \"" << height.asString() << "\"\n";
589 os << "height_special \"" << height_special << "\"\n";
593 void InsetBoxParams::read(Lexer & lex)
595 lex.setContext("InsetBoxParams::read");
597 lex >> "position" >> pos;
598 lex >> "hor_pos" >> hor_pos;
599 lex >> "has_inner_box" >> inner_box;
600 if (type == "Framed")
602 lex >> "inner_pos" >> inner_pos;
603 lex >> "use_parbox" >> use_parbox;
604 lex >> "width" >> width;
605 lex >> "special" >> special;
606 lex >> "height" >> height;
607 lex >> "height_special" >> height_special;