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) {
286 // Special widths, see usrguide §3.5
288 if (params_.special != "none") {
289 os << "[" << params_.width.value()
290 << '\\' << from_utf8(params_.special)
293 os << '[' << from_ascii(width_string)
295 if (params_.hor_pos != 'c')
296 os << "[" << params_.hor_pos << "]";
308 os << "\\shadowbox{";
314 os << "\\doublebox{";
318 if (params_.inner_box) {
319 if (params_.use_parbox)
322 os << "\\begin{minipage}";
324 os << "[" << params_.pos << "]";
325 if (params_.height_special == "none") {
327 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
330 // set no optional argument when the value is the default "1\height"
331 // (special units like \height are handled as "in")
332 // but when the user has chosen a non-default inner_pos, the height
333 // must be given: \minipage[pos][height][inner-pos]{width}
334 if ((params_.height != Length("1in") ||
335 params_.height_special != "totalheight") ||
336 params_.inner_pos != params_.pos) {
338 os << "[" << params_.height.value()
339 << "\\" << from_utf8(params_.height_special) << "]";
342 if (params_.inner_pos != params_.pos)
343 os << "[" << params_.inner_pos << "]";
346 os << '{' << from_ascii(width_string) << '}';
348 if (params_.use_parbox)
353 if (btype == Shaded) {
354 os << "\\begin{shaded}%\n";
358 i += InsetText::latex(os, runparams);
361 os << "\\end{shaded}";
363 if (params_.inner_box) {
364 if (params_.use_parbox)
367 os << "%\n\\end{minipage}";
374 os << "\\end{framed}";
377 if (!params_.inner_box)
378 os << "}"; // for makebox
398 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
400 BoxType const btype = boxtranslator().find(params_.type);
424 InsetText::plaintext(os, runparams);
455 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
459 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
461 return InsetText::docbook(os, runparams);
465 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
467 // construct attributes
468 string attrs = "class='" + params_.type + "'";
470 if (!params_.width.empty())
471 style += ("width: " + params_.width.asHTMLString() + "; ");
472 // The special heights don't really mean anything for us.
473 if (!params_.height.empty() && params_.height_special == "none")
474 style += ("height: " + params_.height.asHTMLString() + "; ");
476 attrs += " style='" + style + "'";
478 xs << html::StartTag("div", attrs);
479 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
480 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
481 xs << html::EndTag("div");
487 void InsetBox::validate(LaTeXFeatures & features) const
489 BoxType btype = boxtranslator().find(params_.type);
494 features.require("framed");
497 features.require("calc");
503 features.require("calc");
504 features.require("fancybox");
507 features.require("color");
508 features.require("framed");
511 InsetCollapsable::validate(features);
515 docstring InsetBox::contextMenu(BufferView const &, int, int) const
517 return from_ascii("context-box");
521 string InsetBox::params2string(InsetBoxParams const & params)
524 data << "box" << ' ';
530 void InsetBox::string2params(string const & in, InsetBoxParams & params)
532 params = InsetBoxParams(string());
536 istringstream data(in);
542 if (!lex || name != "box") {
543 LYXERR0("InsetBox::string2params(" << in << ")\n"
544 "Expected arg 1 to be \"box\"\n");
548 // This is part of the inset proper that is usually swallowed
549 // by Text::readInset
552 if (!lex || id != "Box") {
553 LYXERR0("InsetBox::string2params(" << in << ")\n"
554 "Expected arg 2 to be \"Box\"\n");
561 /////////////////////////////////////////////////////////////////////////
565 /////////////////////////////////////////////////////////////////////////
567 InsetBoxParams::InsetBoxParams(string const & label)
571 width(Length("100col%")),
576 height(Length("1in")),
577 height_special("totalheight") // default is 1\\totalheight
581 void InsetBoxParams::write(ostream & os) const
583 os << "Box " << type << "\n";
584 os << "position \"" << pos << "\"\n";
585 os << "hor_pos \"" << hor_pos << "\"\n";
586 os << "has_inner_box " << inner_box << "\n";
587 os << "inner_pos \"" << inner_pos << "\"\n";
588 os << "use_parbox " << use_parbox << "\n";
589 os << "width \"" << width.asString() << "\"\n";
590 os << "special \"" << special << "\"\n";
591 os << "height \"" << height.asString() << "\"\n";
592 os << "height_special \"" << height_special << "\"\n";
596 void InsetBoxParams::read(Lexer & lex)
598 lex.setContext("InsetBoxParams::read");
600 lex >> "position" >> pos;
601 lex >> "hor_pos" >> hor_pos;
602 lex >> "has_inner_box" >> inner_box;
603 if (type == "Framed")
605 lex >> "inner_pos" >> inner_pos;
606 lex >> "use_parbox" >> use_parbox;
607 lex >> "width" >> width;
608 lex >> "special" >> special;
609 lex >> "height" >> height;
610 lex >> "height_special" >> height_special;