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/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/Translator.h"
34 #include "frontends/Application.h"
39 using namespace lyx::support;
45 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
46 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
48 BoxTranslator initBoxtranslator()
50 BoxTranslator translator("Boxed", InsetBox::Boxed);
51 translator.addPair("Frameless", InsetBox::Frameless);
52 translator.addPair("Framed", InsetBox::Framed);
53 translator.addPair("ovalbox", InsetBox::ovalbox);
54 translator.addPair("Ovalbox", InsetBox::Ovalbox);
55 translator.addPair("Shadowbox", InsetBox::Shadowbox);
56 translator.addPair("Shaded", InsetBox::Shaded);
57 translator.addPair("Doublebox",InsetBox::Doublebox);
62 BoxTranslatorLoc initBoxtranslatorLoc()
64 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
65 translator.addPair(_("frameless"), InsetBox::Frameless);
66 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
67 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
68 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
69 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
70 translator.addPair(_("shaded background"), InsetBox::Shaded);
71 translator.addPair(_("double frame"), InsetBox::Doublebox);
76 BoxTranslator const & boxtranslator()
78 static BoxTranslator translator = initBoxtranslator();
83 BoxTranslatorLoc const & boxtranslator_loc()
85 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
92 /////////////////////////////////////////////////////////////////////////
96 /////////////////////////////////////////////////////////////////////////
98 InsetBox::InsetBox(Buffer const & buffer, string const & label)
99 : InsetCollapsable(buffer), params_(label)
103 InsetBox::~InsetBox()
105 hideDialogs("box", this);
109 docstring InsetBox::editMessage() const
111 return _("Opened Box Inset");
115 docstring InsetBox::name() const
119 if (boxtranslator().find(params_.type) == Shaded)
121 return from_ascii(name);
125 void InsetBox::write(ostream & os) const
128 InsetCollapsable::write(os);
132 void InsetBox::read(Lexer & lex)
135 InsetCollapsable::read(lex);
139 void InsetBox::setButtonLabel()
141 BoxType const btype = boxtranslator().find(params_.type);
143 docstring const type = _("Box");
146 if (params_.inner_box) {
147 if (params_.use_parbox)
150 inner = _("Minipage");
154 if (btype != Frameless)
155 frame = boxtranslator_loc().find(btype);
158 if (inner.empty() && frame.empty())
160 else if (inner.empty())
161 label = bformat(_("%1$s (%2$s)"),
163 else if (frame.empty())
164 label = bformat(_("%1$s (%2$s)"),
167 label = bformat(_("%1$s (%2$s, %3$s)"),
173 bool InsetBox::hasFixedWidth() const
175 return params_.inner_box || params_.special != "width";
179 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
181 // back up textwidth.
182 int textwidth_backup = m.base.textwidth;
184 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
185 InsetCollapsable::metrics(m, dim);
187 m.base.textwidth = textwidth_backup;
191 bool InsetBox::forcePlainLayout(idx_type) const
193 return !params_.inner_box && params_.type != "Framed";
197 bool InsetBox::showInsetDialog(BufferView * bv) const
199 bv->showDialog("box", params2string(params_),
200 const_cast<InsetBox *>(this));
205 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
207 switch (cmd.action) {
209 case LFUN_INSET_MODIFY: {
210 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
211 if (cmd.getArg(0) == "changetype")
212 params_.type = cmd.getArg(1);
214 string2params(to_utf8(cmd.argument()), params_);
215 setLayout(cur.buffer()->params());
219 case LFUN_INSET_DIALOG_UPDATE:
220 cur.bv().updateDialog("box", params2string(params_));
224 InsetCollapsable::doDispatch(cur, cmd);
230 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
231 FuncStatus & flag) const
233 switch (cmd.action) {
235 case LFUN_INSET_MODIFY:
236 if (cmd.getArg(0) == "changetype")
237 flag.setOnOff(cmd.getArg(1) == params_.type);
238 flag.setEnabled(true);
241 case LFUN_INSET_DIALOG_UPDATE:
242 flag.setEnabled(true);
245 case LFUN_BREAK_PARAGRAPH:
246 if (params_.inner_box || params_.type == "Framed")
247 return InsetCollapsable::getStatus(cur, cmd, flag);
248 flag.setEnabled(false);
252 return InsetCollapsable::getStatus(cur, cmd, flag);
257 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
259 BoxType btype = boxtranslator().find(params_.type);
261 string width_string = params_.width.asLatexString();
262 bool stdwidth = false;
263 if (params_.inner_box &&
264 (width_string.find("1.0\\columnwidth") != string::npos
265 || width_string.find("1.0\\textwidth") != string::npos)) {
273 width_string += " - 2\\fboxsep - 2\\fboxrule";
276 width_string += " - 2\\fboxsep - 0.8pt";
279 width_string += " - 2\\fboxsep - 1.6pt";
282 // Shadow falls outside right margin... opinions?
283 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
286 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
293 // Adapt to column/text width correctly also if paragraphs indented:
301 os << "\\begin{framed}%\n";
306 if (!params_.inner_box) {
308 // Special widths, see usrguide §3.5
310 if (params_.special != "none") {
311 os << "[" << params_.width.value()
312 << '\\' << from_utf8(params_.special)
315 os << '[' << from_ascii(width_string)
317 if (params_.hor_pos != 'c')
318 os << "[" << params_.hor_pos << "]";
330 os << "\\shadowbox{";
336 os << "\\doublebox{";
340 if (params_.inner_box) {
341 if (params_.use_parbox)
344 os << "\\begin{minipage}";
346 os << "[" << params_.pos << "]";
347 if (params_.height_special == "none") {
349 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
352 // set no optional argument when the value is the default "1\height"
353 // (special units like \height are handled as "in")
354 // but when the user has chosen a non-default inner_pos, the height
355 // must be given: \minipage[pos][height][inner-pos]{width}
356 if ((params_.height != Length("1in") ||
357 params_.height_special != "totalheight") ||
358 params_.inner_pos != params_.pos) {
360 os << "[" << params_.height.value()
361 << "\\" << from_utf8(params_.height_special) << "]";
364 if (params_.inner_pos != params_.pos)
365 os << "[" << params_.inner_pos << "]";
368 os << '{' << from_ascii(width_string) << '}';
370 if (params_.use_parbox)
375 if (btype == Shaded) {
376 os << "\\begin{shaded}%\n";
380 i += InsetText::latex(os, runparams);
383 os << "\\end{shaded}";
385 if (params_.inner_box) {
386 if (params_.use_parbox)
389 os << "%\n\\end{minipage}";
396 os << "\\end{framed}";
399 if (!params_.inner_box)
400 os << "}"; // for makebox
420 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
422 BoxType const btype = boxtranslator().find(params_.type);
446 InsetText::plaintext(os, runparams);
477 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
481 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
483 return InsetText::docbook(os, runparams);
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;