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 "TextClass.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33 #include "support/Translator.h"
35 #include "frontends/Application.h"
40 using namespace lyx::support;
46 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
47 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
49 BoxTranslator initBoxtranslator()
51 BoxTranslator translator("Boxed", InsetBox::Boxed);
52 translator.addPair("Frameless", InsetBox::Frameless);
53 translator.addPair("Framed", InsetBox::Framed);
54 translator.addPair("ovalbox", InsetBox::ovalbox);
55 translator.addPair("Ovalbox", InsetBox::Ovalbox);
56 translator.addPair("Shadowbox", InsetBox::Shadowbox);
57 translator.addPair("Shaded", InsetBox::Shaded);
58 translator.addPair("Doublebox",InsetBox::Doublebox);
63 BoxTranslatorLoc initBoxtranslatorLoc()
65 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
66 translator.addPair(_("frameless"), InsetBox::Frameless);
67 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
68 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
69 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
70 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
71 translator.addPair(_("shaded background"), InsetBox::Shaded);
72 translator.addPair(_("double frame"), InsetBox::Doublebox);
77 BoxTranslator const & boxtranslator()
79 static BoxTranslator translator = initBoxtranslator();
84 BoxTranslatorLoc const & boxtranslator_loc()
86 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
93 /////////////////////////////////////////////////////////////////////////
97 /////////////////////////////////////////////////////////////////////////
99 InsetBox::InsetBox(Buffer const & buffer, string const & label)
100 : InsetCollapsable(buffer), params_(label)
104 InsetBox::~InsetBox()
106 hideDialogs("box", this);
110 docstring InsetBox::editMessage() const
112 return _("Opened Box Inset");
116 docstring InsetBox::name() const
120 if (boxtranslator().find(params_.type) == Shaded)
122 return from_ascii(name);
126 void InsetBox::write(ostream & os) const
129 InsetCollapsable::write(os);
133 void InsetBox::read(Lexer & lex)
136 InsetCollapsable::read(lex);
140 void InsetBox::setButtonLabel()
142 BoxType const btype = boxtranslator().find(params_.type);
144 docstring const type = _("Box");
147 if (params_.inner_box) {
148 if (params_.use_parbox)
151 inner = _("Minipage");
155 if (btype != Frameless)
156 frame = boxtranslator_loc().find(btype);
159 if (inner.empty() && frame.empty())
161 else if (inner.empty())
162 label = bformat(_("%1$s (%2$s)"),
164 else if (frame.empty())
165 label = bformat(_("%1$s (%2$s)"),
168 label = bformat(_("%1$s (%2$s, %3$s)"),
174 bool InsetBox::hasFixedWidth() const
176 return params_.inner_box || params_.special != "width";
180 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
182 // back up textwidth.
183 int textwidth_backup = m.base.textwidth;
185 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
186 InsetCollapsable::metrics(m, dim);
188 m.base.textwidth = textwidth_backup;
192 bool InsetBox::forcePlainLayout(idx_type) const
194 return !params_.inner_box && params_.type != "Framed";
198 bool InsetBox::showInsetDialog(BufferView * bv) const
200 bv->showDialog("box", params2string(params_),
201 const_cast<InsetBox *>(this));
206 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
208 switch (cmd.action) {
210 case LFUN_INSET_MODIFY: {
211 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
212 if (cmd.getArg(0) == "changetype")
213 params_.type = cmd.getArg(1);
215 string2params(to_utf8(cmd.argument()), params_);
216 setLayout(cur.buffer()->params());
220 case LFUN_INSET_DIALOG_UPDATE:
221 cur.bv().updateDialog("box", params2string(params_));
225 InsetCollapsable::doDispatch(cur, cmd);
231 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
232 FuncStatus & flag) const
234 switch (cmd.action) {
236 case LFUN_INSET_MODIFY:
237 if (cmd.getArg(0) == "changetype")
238 flag.setOnOff(cmd.getArg(1) == params_.type);
239 flag.setEnabled(true);
242 case LFUN_INSET_DIALOG_UPDATE:
243 flag.setEnabled(true);
246 case LFUN_BREAK_PARAGRAPH:
247 if (params_.inner_box || params_.type == "Framed")
248 return InsetCollapsable::getStatus(cur, cmd, flag);
249 flag.setEnabled(false);
253 return InsetCollapsable::getStatus(cur, cmd, flag);
258 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
260 BoxType btype = boxtranslator().find(params_.type);
262 string width_string = params_.width.asLatexString();
263 bool stdwidth = false;
264 if (params_.inner_box &&
265 (width_string.find("1.0\\columnwidth") != string::npos
266 || width_string.find("1.0\\textwidth") != string::npos)) {
274 width_string += " - 2\\fboxsep - 2\\fboxrule";
277 width_string += " - 2\\fboxsep - 0.8pt";
280 width_string += " - 2\\fboxsep - 1.6pt";
283 // Shadow falls outside right margin... opinions?
284 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
287 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
294 // Adapt to column/text width correctly also if paragraphs indented:
302 os << "\\begin{framed}%\n";
307 if (!params_.inner_box) {
309 // Special widths, see usrguide §3.5
311 if (params_.special != "none") {
312 os << "[" << params_.width.value()
313 << '\\' << from_utf8(params_.special)
316 os << '[' << from_ascii(width_string)
318 if (params_.hor_pos != 'c')
319 os << "[" << params_.hor_pos << "]";
331 os << "\\shadowbox{";
337 os << "\\doublebox{";
341 if (params_.inner_box) {
342 if (params_.use_parbox)
345 os << "\\begin{minipage}";
347 os << "[" << params_.pos << "]";
348 if (params_.height_special == "none") {
350 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
353 // set no optional argument when the value is the default "1\height"
354 // (special units like \height are handled as "in")
355 // but when the user has chosen a non-default inner_pos, the height
356 // must be given: \minipage[pos][height][inner-pos]{width}
357 if ((params_.height != Length("1in") ||
358 params_.height_special != "totalheight") ||
359 params_.inner_pos != params_.pos) {
361 os << "[" << params_.height.value()
362 << "\\" << from_utf8(params_.height_special) << "]";
365 if (params_.inner_pos != params_.pos)
366 os << "[" << params_.inner_pos << "]";
369 os << '{' << from_ascii(width_string) << '}';
371 if (params_.use_parbox)
376 if (btype == Shaded) {
377 os << "\\begin{shaded}%\n";
381 i += InsetText::latex(os, runparams);
384 os << "\\end{shaded}";
386 if (params_.inner_box) {
387 if (params_.use_parbox)
390 os << "%\n\\end{minipage}";
397 os << "\\end{framed}";
400 if (!params_.inner_box)
401 os << "}"; // for makebox
421 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
423 BoxType const btype = boxtranslator().find(params_.type);
447 InsetText::plaintext(os, runparams);
478 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
482 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
484 return InsetText::docbook(os, runparams);
488 docstring InsetBox::xhtml(odocstream &, OutputParams const & runparams) const
491 if (!params_.width.empty())
492 style += ("width: " + params_.width.asHTMLString() + ";");
493 if (!params_.height.empty())
494 style += ("height: " + params_.height.asHTMLString() + ";");
496 docstring retval = from_ascii("<div class='" + params_.type + "'");
498 retval += from_ascii(" style='" + style + "'");
501 docstring defer = InsetText::xhtml(os, runparams);
503 retval += "</div>\n";
504 retval += defer + "\n";
509 void InsetBox::validate(LaTeXFeatures & features) const
511 BoxType btype = boxtranslator().find(params_.type);
516 features.require("framed");
519 features.require("calc");
525 features.require("calc");
526 features.require("fancybox");
529 features.require("color");
530 features.require("framed");
533 InsetCollapsable::validate(features);
537 docstring InsetBox::contextMenu(BufferView const &, int, int) const
539 return from_ascii("context-box");
543 string InsetBox::params2string(InsetBoxParams const & params)
546 data << "box" << ' ';
552 void InsetBox::string2params(string const & in, InsetBoxParams & params)
554 params = InsetBoxParams(string());
558 istringstream data(in);
564 if (!lex || name != "box") {
565 LYXERR0("InsetBox::string2params(" << in << ")\n"
566 "Expected arg 1 to be \"box\"\n");
570 // This is part of the inset proper that is usually swallowed
571 // by Text::readInset
574 if (!lex || id != "Box") {
575 LYXERR0("InsetBox::string2params(" << in << ")\n"
576 "Expected arg 2 to be \"Box\"\n");
583 /////////////////////////////////////////////////////////////////////////
587 /////////////////////////////////////////////////////////////////////////
589 InsetBoxParams::InsetBoxParams(string const & label)
593 width(Length("100col%")),
598 height(Length("1in")),
599 height_special("totalheight") // default is 1\\totalheight
603 void InsetBoxParams::write(ostream & os) const
605 os << "Box " << type << "\n";
606 os << "position \"" << pos << "\"\n";
607 os << "hor_pos \"" << hor_pos << "\"\n";
608 os << "has_inner_box " << inner_box << "\n";
609 os << "inner_pos \"" << inner_pos << "\"\n";
610 os << "use_parbox " << use_parbox << "\n";
611 os << "width \"" << width.asString() << "\"\n";
612 os << "special \"" << special << "\"\n";
613 os << "height \"" << height.asString() << "\"\n";
614 os << "height_special \"" << height_special << "\"\n";
618 void InsetBoxParams::read(Lexer & lex)
620 lex.setContext("InsetBoxParams::read");
622 lex >> "position" >> pos;
623 lex >> "hor_pos" >> hor_pos;
624 lex >> "has_inner_box" >> inner_box;
625 if (type == "Framed")
627 lex >> "inner_pos" >> inner_pos;
628 lex >> "use_parbox" >> use_parbox;
629 lex >> "width" >> width;
630 lex >> "special" >> special;
631 lex >> "height" >> height;
632 lex >> "height_special" >> height_special;