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_);
207 case LFUN_INSET_DIALOG_UPDATE:
208 cur.bv().updateDialog("box", params2string(params_));
212 InsetCollapsable::doDispatch(cur, cmd);
218 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
219 FuncStatus & flag) const
221 switch (cmd.action) {
223 case LFUN_INSET_MODIFY:
224 if (cmd.getArg(0) == "changetype")
225 flag.setOnOff(cmd.getArg(1) == params_.type);
226 flag.setEnabled(true);
229 case LFUN_INSET_DIALOG_UPDATE:
230 flag.setEnabled(true);
233 case LFUN_BREAK_PARAGRAPH:
234 if (params_.inner_box || params_.type == "Framed")
235 return InsetCollapsable::getStatus(cur, cmd, flag);
236 flag.setEnabled(false);
240 return InsetCollapsable::getStatus(cur, cmd, flag);
245 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
247 BoxType btype = boxtranslator().find(params_.type);
249 string width_string = params_.width.asLatexString();
250 bool stdwidth = false;
251 if (params_.inner_box &&
252 (width_string.find("1.0\\columnwidth") != string::npos
253 || width_string.find("1.0\\textwidth") != string::npos)) {
261 width_string += " - 2\\fboxsep - 2\\fboxrule";
264 width_string += " - 2\\fboxsep - 0.8pt";
267 width_string += " - 2\\fboxsep - 1.6pt";
270 // Shadow falls outside right margin... opinions?
271 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
274 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
281 // Adapt to column/text width correctly also if paragraphs indented:
289 os << "\\begin{framed}%\n";
294 if (!params_.inner_box) {
296 // Special widths, see usrguide §3.5
298 if (params_.special != "none") {
299 os << "[" << params_.width.value()
300 << '\\' << from_utf8(params_.special)
303 os << '[' << from_ascii(width_string)
305 if (params_.hor_pos != 'c')
306 os << "[" << params_.hor_pos << "]";
318 os << "\\shadowbox{";
324 os << "\\doublebox{";
328 if (params_.inner_box) {
329 if (params_.use_parbox)
332 os << "\\begin{minipage}";
334 os << "[" << params_.pos << "]";
335 if (params_.height_special == "none") {
337 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
340 // set no optional argument when the value is the default "1\height"
341 // (special units like \height are handled as "in")
342 // but when the user has chosen a non-default inner_pos, the height
343 // must be given: \minipage[pos][height][inner-pos]{width}
344 if ((params_.height != Length("1in") ||
345 params_.height_special != "totalheight") ||
346 params_.inner_pos != params_.pos) {
348 os << "[" << params_.height.value()
349 << "\\" << from_utf8(params_.height_special) << "]";
352 if (params_.inner_pos != params_.pos)
353 os << "[" << params_.inner_pos << "]";
356 os << '{' << from_ascii(width_string) << '}';
358 if (params_.use_parbox)
363 if (btype == Shaded) {
364 os << "\\begin{shaded}%\n";
368 i += InsetText::latex(os, runparams);
371 os << "\\end{shaded}";
373 if (params_.inner_box) {
374 if (params_.use_parbox)
377 os << "%\n\\end{minipage}";
384 os << "\\end{framed}";
387 if (!params_.inner_box)
388 os << "}"; // for makebox
408 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
410 BoxType const btype = boxtranslator().find(params_.type);
434 InsetText::plaintext(os, runparams);
465 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
469 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
471 return InsetText::docbook(os, runparams);
475 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
477 // construct attributes
478 string attrs = "class='" + params_.type + "'";
480 if (!params_.width.empty())
481 style += ("width: " + params_.width.asHTMLString() + "; ");
482 // The special heights don't really mean anything for us.
483 if (!params_.height.empty() && params_.height_special == "none")
484 style += ("height: " + params_.height.asHTMLString() + "; ");
486 attrs += " style='" + style + "'";
488 xs << html::StartTag("div", attrs);
489 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
490 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
491 xs << html::EndTag("div");
497 void InsetBox::validate(LaTeXFeatures & features) const
499 BoxType btype = boxtranslator().find(params_.type);
504 features.require("framed");
507 features.require("calc");
513 features.require("calc");
514 features.require("fancybox");
517 features.require("color");
518 features.require("framed");
521 InsetCollapsable::validate(features);
525 docstring InsetBox::contextMenu(BufferView const &, int, int) const
527 return from_ascii("context-box");
531 string InsetBox::params2string(InsetBoxParams const & params)
534 data << "box" << ' ';
540 void InsetBox::string2params(string const & in, InsetBoxParams & params)
542 params = InsetBoxParams(string());
546 istringstream data(in);
552 if (!lex || name != "box") {
553 LYXERR0("InsetBox::string2params(" << in << ")\n"
554 "Expected arg 1 to be \"box\"\n");
558 // This is part of the inset proper that is usually swallowed
559 // by Text::readInset
562 if (!lex || id != "Box") {
563 LYXERR0("InsetBox::string2params(" << in << ")\n"
564 "Expected arg 2 to be \"Box\"\n");
571 /////////////////////////////////////////////////////////////////////////
575 /////////////////////////////////////////////////////////////////////////
577 InsetBoxParams::InsetBoxParams(string const & label)
581 width(Length("100col%")),
586 height(Length("1in")),
587 height_special("totalheight") // default is 1\\totalheight
591 void InsetBoxParams::write(ostream & os) const
593 os << "Box " << type << "\n";
594 os << "position \"" << pos << "\"\n";
595 os << "hor_pos \"" << hor_pos << "\"\n";
596 os << "has_inner_box " << inner_box << "\n";
597 os << "inner_pos \"" << inner_pos << "\"\n";
598 os << "use_parbox " << use_parbox << "\n";
599 os << "width \"" << width.asString() << "\"\n";
600 os << "special \"" << special << "\"\n";
601 os << "height \"" << height.asString() << "\"\n";
602 os << "height_special \"" << height_special << "\"\n";
606 void InsetBoxParams::read(Lexer & lex)
608 lex.setContext("InsetBoxParams::read");
610 lex >> "position" >> pos;
611 lex >> "hor_pos" >> hor_pos;
612 lex >> "has_inner_box" >> inner_box;
613 if (type == "Framed")
615 lex >> "inner_pos" >> inner_pos;
616 lex >> "use_parbox" >> use_parbox;
617 lex >> "width" >> width;
618 lex >> "special" >> special;
619 lex >> "height" >> height;
620 lex >> "height_special" >> height_special;