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.
20 #include "BufferParams.h"
21 #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"
44 using namespace lyx::support;
50 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
51 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
53 BoxTranslator initBoxtranslator()
55 BoxTranslator translator("Boxed", InsetBox::Boxed);
56 translator.addPair("Frameless", InsetBox::Frameless);
57 translator.addPair("Framed", InsetBox::Framed);
58 translator.addPair("ovalbox", InsetBox::ovalbox);
59 translator.addPair("Ovalbox", InsetBox::Ovalbox);
60 translator.addPair("Shadowbox", InsetBox::Shadowbox);
61 translator.addPair("Shaded", InsetBox::Shaded);
62 translator.addPair("Doublebox",InsetBox::Doublebox);
67 BoxTranslatorLoc initBoxtranslatorLoc()
69 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
70 translator.addPair(_("frameless"), InsetBox::Frameless);
71 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
72 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
73 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
74 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
75 translator.addPair(_("shaded background"), InsetBox::Shaded);
76 translator.addPair(_("double frame"), InsetBox::Doublebox);
81 BoxTranslator const & boxtranslator()
83 static BoxTranslator translator = initBoxtranslator();
88 BoxTranslatorLoc const & boxtranslator_loc()
90 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
97 /////////////////////////////////////////////////////////////////////////
101 /////////////////////////////////////////////////////////////////////////
103 InsetBox::InsetBox(Buffer * buffer, string const & label)
104 : InsetCollapsable(buffer), params_(label)
108 docstring InsetBox::name() const
112 if (boxtranslator().find(params_.type) == Shaded)
114 return from_ascii(name);
118 void InsetBox::write(ostream & os) const
121 InsetCollapsable::write(os);
125 void InsetBox::read(Lexer & lex)
128 InsetCollapsable::read(lex);
132 void InsetBox::setButtonLabel()
134 BoxType const btype = boxtranslator().find(params_.type);
136 docstring const type = _("Box");
139 if (params_.inner_box) {
140 if (params_.use_parbox)
143 inner = _("Minipage");
147 if (btype != Frameless)
148 frame = boxtranslator_loc().find(btype);
151 if (inner.empty() && frame.empty())
153 else if (inner.empty())
154 label = bformat(_("%1$s (%2$s)"),
156 else if (frame.empty())
157 label = bformat(_("%1$s (%2$s)"),
160 label = bformat(_("%1$s (%2$s, %3$s)"),
166 bool InsetBox::hasFixedWidth() const
168 return params_.inner_box || params_.special != "width";
172 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
174 // back up textwidth.
175 int textwidth_backup = m.base.textwidth;
177 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
178 InsetCollapsable::metrics(m, dim);
180 m.base.textwidth = textwidth_backup;
184 bool InsetBox::forcePlainLayout(idx_type) const
186 return !params_.inner_box && params_.type != "Framed";
190 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
192 switch (cmd.action()) {
194 case LFUN_INSET_MODIFY: {
195 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
196 if (cmd.getArg(0) == "changetype")
197 params_.type = cmd.getArg(1);
199 string2params(to_utf8(cmd.argument()), params_);
205 InsetCollapsable::doDispatch(cur, cmd);
211 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
212 FuncStatus & flag) const
214 switch (cmd.action()) {
216 case LFUN_INSET_MODIFY:
217 if (cmd.getArg(0) == "changetype")
218 flag.setOnOff(cmd.getArg(1) == params_.type);
219 flag.setEnabled(true);
222 case LFUN_INSET_DIALOG_UPDATE:
223 flag.setEnabled(true);
226 case LFUN_BREAK_PARAGRAPH:
227 if (params_.inner_box || params_.type == "Framed")
228 return InsetCollapsable::getStatus(cur, cmd, flag);
229 flag.setEnabled(false);
233 return InsetCollapsable::getStatus(cur, cmd, flag);
238 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
240 BoxType btype = boxtranslator().find(params_.type);
242 string width_string = params_.width.asLatexString();
243 bool stdwidth = false;
244 if (params_.inner_box &&
245 (width_string.find("1.0\\columnwidth") != string::npos
246 || width_string.find("1.0\\textwidth") != string::npos)) {
254 width_string += " - 2\\fboxsep - 2\\fboxrule";
257 width_string += " - 2\\fboxsep - 0.8pt";
260 width_string += " - 2\\fboxsep - 1.6pt";
263 // Shadow falls outside right margin... opinions?
264 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
267 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
274 // Adapt to column/text width correctly also if paragraphs indented:
283 os << "\\begin{framed}%\n";
288 if (!params_.inner_box) {
290 // Special widths, see usrguide §3.5
292 if (params_.special != "none") {
293 os << "["/* << params_.width.value()*/ << (double) 1
294 << '\\' << from_utf8(params_.special)
297 os << '[' << from_ascii(width_string)
299 if (params_.hor_pos != 'c')
300 os << "[" << params_.hor_pos << "]";*/
312 os << "\\shadowbox{";
318 os << "\\doublebox{";
322 if (params_.inner_box) {
323 if (params_.use_parbox)
326 os << "\\begin{minipage}";
328 os << "[" << params_.pos << "]";
329 if (params_.height_special == "none") {
331 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
334 // set no optional argument when the value is the default "1\height"
335 // (special units like \height are handled as "in")
336 // but when the user has chosen a non-default inner_pos, the height
337 // must be given: \minipage[pos][height][inner-pos]{width}
338 if ((params_.height != Length("1in") ||
339 params_.height_special != "totalheight") ||
340 params_.inner_pos != params_.pos) {
342 os << "[" << params_.height.value()
343 << "\\" << from_utf8(params_.height_special) << "]";
346 if (params_.inner_pos != params_.pos)
347 os << "[" << params_.inner_pos << "]";
350 os << '{' << from_ascii(width_string) << '}';
352 if (params_.use_parbox)
357 if (btype == Shaded) {
358 os << "\\begin{shaded}%\n";
362 i += InsetText::latex(os, runparams);
365 os << "\\end{shaded}";
367 if (params_.inner_box) {
368 if (params_.use_parbox)
371 os << "%\n\\end{minipage}";
378 os << "\\end{framed}";
381 if (!params_.inner_box)
382 os << "}"; // for makebox
404 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
406 BoxType const btype = boxtranslator().find(params_.type);
430 InsetText::plaintext(os, runparams);
461 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
465 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
467 return InsetText::docbook(os, runparams);
471 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
473 // construct attributes
474 string attrs = "class='" + params_.type + "'";
476 if (!params_.width.empty())
477 style += ("width: " + params_.width.asHTMLString() + "; ");
478 // The special heights don't really mean anything for us.
479 if (!params_.height.empty() && params_.height_special == "none")
480 style += ("height: " + params_.height.asHTMLString() + "; ");
482 attrs += " style='" + style + "'";
484 xs << html::StartTag("div", attrs);
485 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
486 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
487 xs << html::EndTag("div");
493 void InsetBox::validate(LaTeXFeatures & features) const
495 BoxType btype = boxtranslator().find(params_.type);
500 features.require("framed");
503 features.require("calc");
509 features.require("calc");
510 features.require("fancybox");
513 features.require("color");
514 features.require("framed");
517 InsetCollapsable::validate(features);
521 docstring InsetBox::contextMenu(BufferView const &, int, int) const
523 return from_ascii("context-box");
527 string InsetBox::params2string(InsetBoxParams const & params)
530 data << "box" << ' ';
536 void InsetBox::string2params(string const & in, InsetBoxParams & params)
538 params = InsetBoxParams(string());
542 istringstream data(in);
548 if (!lex || name != "box") {
549 LYXERR0("InsetBox::string2params(" << in << ")\n"
550 "Expected arg 1 to be \"box\"\n");
554 // This is part of the inset proper that is usually swallowed
555 // by Text::readInset
558 if (!lex || id != "Box") {
559 LYXERR0("InsetBox::string2params(" << in << ")\n"
560 "Expected arg 2 to be \"Box\"\n");
567 /////////////////////////////////////////////////////////////////////////
571 /////////////////////////////////////////////////////////////////////////
573 InsetBoxParams::InsetBoxParams(string const & label)
577 width(Length("100col%")),
582 height(Length("1in")),
583 height_special("totalheight") // default is 1\\totalheight
587 void InsetBoxParams::write(ostream & os) const
589 os << "Box " << type << "\n";
590 os << "position \"" << pos << "\"\n";
591 os << "hor_pos \"" << hor_pos << "\"\n";
592 os << "has_inner_box " << inner_box << "\n";
593 os << "inner_pos \"" << inner_pos << "\"\n";
594 os << "use_parbox " << use_parbox << "\n";
595 os << "width \"" << width.asString() << "\"\n";
596 os << "special \"" << special << "\"\n";
597 os << "height \"" << height.asString() << "\"\n";
598 os << "height_special \"" << height_special << "\"\n";
602 void InsetBoxParams::read(Lexer & lex)
604 lex.setContext("InsetBoxParams::read");
606 lex >> "position" >> pos;
607 lex >> "hor_pos" >> hor_pos;
608 lex >> "has_inner_box" >> inner_box;
609 if (type == "Framed")
611 lex >> "inner_pos" >> inner_pos;
612 lex >> "use_parbox" >> use_parbox;
613 lex >> "width" >> width;
614 lex >> "special" >> special;
615 lex >> "height" >> height;
616 lex >> "height_special" >> height_special;