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/Translator.h"
33 #include "frontends/Application.h"
43 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
44 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
46 BoxTranslator initBoxtranslator()
48 BoxTranslator translator("Boxed", InsetBox::Boxed);
49 translator.addPair("Frameless", InsetBox::Frameless);
50 translator.addPair("Framed", InsetBox::Framed);
51 translator.addPair("ovalbox", InsetBox::ovalbox);
52 translator.addPair("Ovalbox", InsetBox::Ovalbox);
53 translator.addPair("Shadowbox", InsetBox::Shadowbox);
54 translator.addPair("Shaded", InsetBox::Shaded);
55 translator.addPair("Doublebox",InsetBox::Doublebox);
60 BoxTranslatorLoc initBoxtranslatorLoc()
62 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
63 translator.addPair(_("frameless"), InsetBox::Frameless);
64 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
65 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
66 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
67 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
68 translator.addPair(_("shaded background"), InsetBox::Shaded);
69 translator.addPair(_("double frame"), InsetBox::Doublebox);
74 BoxTranslator const & boxtranslator()
76 static BoxTranslator translator = initBoxtranslator();
81 BoxTranslatorLoc const & boxtranslator_loc()
83 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
90 /////////////////////////////////////////////////////////////////////////
94 /////////////////////////////////////////////////////////////////////////
96 InsetBox::InsetBox(Buffer const & buffer, string const & label)
97 : InsetCollapsable(buffer), params_(label)
99 if (forceEmptyLayout())
100 paragraphs().back().setLayout(buffer.params().documentClass().emptyLayout());
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 btype = boxtranslator().find(params_.type);
147 if (btype == Frameless) {
148 if (params_.use_parbox)
149 label += _("Parbox");
151 label += _("Minipage");
153 label += boxtranslator_loc().find(btype);
161 bool InsetBox::hasFixedWidth() const
163 return params_.inner_box || params_.special != "width";
167 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
169 // back up textwidth.
170 int textwidth_backup = m.base.textwidth;
172 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
173 InsetCollapsable::metrics(m, dim);
175 m.base.textwidth = textwidth_backup;
179 bool InsetBox::forceEmptyLayout(idx_type) const
181 return !params_.inner_box && params_.type != "Framed";
185 bool InsetBox::showInsetDialog(BufferView * bv) const
187 bv->showDialog("box", params2string(params_),
188 const_cast<InsetBox *>(this));
193 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
195 switch (cmd.action) {
197 case LFUN_INSET_MODIFY: {
198 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
199 if (cmd.getArg(0) == "changetype")
200 params_.type = cmd.getArg(1);
202 string2params(to_utf8(cmd.argument()), params_);
203 setLayout(cur.buffer().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 void InsetBox::validate(LaTeXFeatures & features) const
477 BoxType btype = boxtranslator().find(params_.type);
482 features.require("framed");
485 features.require("calc");
491 features.require("calc");
492 features.require("fancybox");
495 features.require("color");
496 features.require("framed");
499 InsetText::validate(features);
503 docstring InsetBox::contextMenu(BufferView const &, int, int) const
505 return from_ascii("context-box");
509 string InsetBox::params2string(InsetBoxParams const & params)
512 data << "box" << ' ';
518 void InsetBox::string2params(string const & in, InsetBoxParams & params)
520 params = InsetBoxParams(string());
524 istringstream data(in);
530 if (!lex || name != "box") {
531 LYXERR0("InsetBox::string2params(" << in << ")\n"
532 "Expected arg 1 to be \"box\"\n");
536 // This is part of the inset proper that is usually swallowed
537 // by Text::readInset
540 if (!lex || id != "Box") {
541 LYXERR0("InsetBox::string2params(" << in << ")\n"
542 "Expected arg 2 to be \"Box\"\n");
549 /////////////////////////////////////////////////////////////////////////
553 /////////////////////////////////////////////////////////////////////////
555 InsetBoxParams::InsetBoxParams(string const & label)
559 width(Length("100col%")),
564 height(Length("1in")),
565 height_special("totalheight") // default is 1\\totalheight
569 void InsetBoxParams::write(ostream & os) const
571 os << "Box " << type << "\n";
572 os << "position \"" << pos << "\"\n";
573 os << "hor_pos \"" << hor_pos << "\"\n";
574 os << "has_inner_box " << inner_box << "\n";
575 os << "inner_pos \"" << inner_pos << "\"\n";
576 os << "use_parbox " << use_parbox << "\n";
577 os << "width \"" << width.asString() << "\"\n";
578 os << "special \"" << special << "\"\n";
579 os << "height \"" << height.asString() << "\"\n";
580 os << "height_special \"" << height_special << "\"\n";
584 void InsetBoxParams::read(Lexer & lex)
586 lex.setContext("InsetBoxParams::read");
588 lex >> "position" >> pos;
589 lex >> "hor_pos" >> hor_pos;
590 lex >> "has_inner_box" >> inner_box;
591 if (type == "Framed")
593 lex >> "inner_pos" >> inner_pos;
594 lex >> "use_parbox" >> use_parbox;
595 lex >> "width" >> width;
596 lex >> "special" >> special;
597 lex >> "height" >> height;
598 lex >> "height_special" >> height_special;