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 "BufferView.h"
20 #include "DispatchResult.h"
21 #include "support/debug.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
24 #include "support/gettext.h"
25 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
29 #include "support/Translator.h"
39 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
40 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
42 BoxTranslator const init_boxtranslator()
44 BoxTranslator translator("Boxed", InsetBox::Boxed);
45 translator.addPair("Frameless", InsetBox::Frameless);
46 translator.addPair("Framed", InsetBox::Framed);
47 translator.addPair("ovalbox", InsetBox::ovalbox);
48 translator.addPair("Ovalbox", InsetBox::Ovalbox);
49 translator.addPair("Shadowbox", InsetBox::Shadowbox);
50 translator.addPair("Shaded", InsetBox::Shaded);
51 translator.addPair("Doublebox",InsetBox::Doublebox);
56 BoxTranslatorLoc const init_boxtranslator_loc()
58 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
59 translator.addPair(_("frameless"), InsetBox::Frameless);
60 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
61 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
62 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
63 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
64 translator.addPair(_("shaded background"), InsetBox::Shaded);
65 translator.addPair(_("double frame"), InsetBox::Doublebox);
70 BoxTranslator const & boxtranslator()
72 static BoxTranslator translator = init_boxtranslator();
77 BoxTranslatorLoc const & boxtranslator_loc()
79 static BoxTranslatorLoc translator = init_boxtranslator_loc();
86 InsetBox::InsetBox(BufferParams const & bp, string const & label)
87 : InsetCollapsable(bp), params_(label)
91 InsetBox::InsetBox(InsetBox const & in)
92 : InsetCollapsable(in), params_(in.params_)
98 InsetBoxMailer(*this).hideDialog();
102 Inset * InsetBox::clone() const
104 return new InsetBox(*this);
108 docstring const InsetBox::editMessage() const
110 return _("Opened Box Inset");
114 docstring InsetBox::name() const
117 string name = string("Box");
118 if (boxtranslator().find(params_.type) == Shaded)
119 name += string(":Shaded");
120 return from_ascii(name);
124 void InsetBox::write(Buffer const & buf, ostream & os) const
127 InsetCollapsable::write(buf, os);
131 void InsetBox::read(Buffer const & buf, Lexer & lex)
134 InsetCollapsable::read(buf, lex);
138 void InsetBox::setButtonLabel()
140 BoxType btype = boxtranslator().find(params_.type);
145 if (btype == Frameless) {
146 if (params_.use_parbox)
147 label += _("Parbox");
149 label += _("Minipage");
151 label += boxtranslator_loc().find(btype);
158 bool InsetBox::hasFixedWidth() const
160 return params_.inner_box || params_.special != "width";
164 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
166 // back up textwidth.
167 int textwidth_backup = m.base.textwidth;
169 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
170 InsetCollapsable::metrics(m, dim);
172 m.base.textwidth = textwidth_backup;
176 bool InsetBox::forceDefaultParagraphs(idx_type) const
178 return !params_.inner_box;
182 bool InsetBox::showInsetDialog(BufferView * bv) const
184 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
189 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
191 switch (cmd.action) {
193 case LFUN_INSET_MODIFY: {
194 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
195 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
196 setLayout(cur.buffer().params());
200 case LFUN_INSET_DIALOG_UPDATE:
201 InsetBoxMailer(*this).updateDialog(&cur.bv());
204 case LFUN_MOUSE_RELEASE:
205 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
206 InsetBoxMailer(*this).showDialog(&cur.bv());
209 InsetCollapsable::doDispatch(cur, cmd);
213 InsetCollapsable::doDispatch(cur, cmd);
219 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
220 FuncStatus & flag) const
222 switch (cmd.action) {
224 case LFUN_INSET_MODIFY:
225 case LFUN_INSET_DIALOG_UPDATE:
228 case LFUN_BREAK_PARAGRAPH:
229 if (params_.inner_box) {
230 return InsetCollapsable::getStatus(cur, cmd, flag);
237 return InsetCollapsable::getStatus(cur, cmd, flag);
242 int InsetBox::latex(Buffer const & buf, odocstream & os,
243 OutputParams const & runparams) const
245 BoxType btype = boxtranslator().find(params_.type);
247 string width_string = params_.width.asLatexString();
248 bool stdwidth(false);
249 if (params_.inner_box &&
250 (width_string.find("1.0\\columnwidth") != string::npos
251 || width_string.find("1.0\\textwidth") != string::npos)) {
259 width_string += " - 2\\fboxsep - 2\\fboxrule";
262 width_string += " - 2\\fboxsep - 0.8pt";
265 width_string += " - 2\\fboxsep - 1.6pt";
268 // Shadow falls outside right margin... opinions?
269 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
272 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
279 // Adapt to column/text width correctly also if paragraphs indented:
287 os << "\\begin{framed}%\n";
292 if (!params_.inner_box) {
294 // Special widths, see usrguide §3.5
296 if (params_.special != "none") {
297 os << "[" << params_.width.value()
298 << '\\' << from_utf8(params_.special)
301 os << '[' << from_ascii(width_string)
303 if (params_.hor_pos != 'c')
304 os << "[" << params_.hor_pos << "]";
316 os << "\\shadowbox{";
322 os << "\\doublebox{";
326 if (params_.inner_box) {
327 if (params_.use_parbox)
330 os << "\\begin{minipage}";
332 os << "[" << params_.pos << "]";
333 if (params_.height_special == "none") {
335 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
338 // set no optional argument when the value is the default "1\height"
339 // (special units like \height are handled as "in")
340 // but when the user has chosen a non-default inner_pos, the height
341 // must be given: \minipage[pos][height][inner-pos]{width}
342 if ((params_.height != Length("1in") ||
343 params_.height_special != "totalheight") ||
344 params_.inner_pos != params_.pos) {
346 os << "[" << params_.height.value()
347 << "\\" << from_utf8(params_.height_special) << "]";
350 if (params_.inner_pos != params_.pos)
351 os << "[" << params_.inner_pos << "]";
354 os << '{' << from_ascii(width_string) << '}';
356 if (params_.use_parbox)
362 os << "\\begin{shaded}%\n";
365 i += InsetText::latex(buf, os, runparams);
368 os << "\\end{shaded}";
370 if (params_.inner_box) {
371 if (params_.use_parbox)
374 os << "%\n\\end{minipage}";
381 os << "\\end{framed}";
384 if (!params_.inner_box)
385 os << "}"; // for makebox
406 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
407 OutputParams const & runparams) const
409 BoxType const btype = boxtranslator().find(params_.type);
433 InsetText::plaintext(buf, os, runparams);
464 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
468 int InsetBox::docbook(Buffer const & buf, odocstream & os,
469 OutputParams const & runparams) const
471 return InsetText::docbook(buf, 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 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
508 string const InsetBoxMailer::name_ = "box";
511 string const InsetBoxMailer::inset2string(Buffer const &) const
513 return params2string(inset_.params());
517 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
520 data << "box" << ' ';
526 void InsetBoxMailer::string2params(string const & in,
527 InsetBoxParams & params)
529 params = InsetBoxParams(string());
533 istringstream data(in);
539 if (!lex || name != name_)
540 return print_mailer_error("InsetBoxMailer", in, 1, name_);
542 // This is part of the inset proper that is usually swallowed
543 // by Text::readInset
546 if (!lex || id != "Box")
547 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
553 InsetBoxParams::InsetBoxParams(string const & label)
557 width(Length("100col%")),
562 height(Length("1in")),
563 height_special("totalheight") // default is 1\\totalheight
567 void InsetBoxParams::write(ostream & os) const
569 os << "Box " << type << "\n";
570 os << "position \"" << pos << "\"\n";
571 os << "hor_pos \"" << hor_pos << "\"\n";
572 os << "has_inner_box " << inner_box << "\n";
573 os << "inner_pos \"" << inner_pos << "\"\n";
574 os << "use_parbox " << use_parbox << "\n";
575 os << "width \"" << width.asString() << "\"\n";
576 os << "special \"" << special << "\"\n";
577 os << "height \"" << height.asString() << "\"\n";
578 os << "height_special \"" << height_special << "\"\n";
582 void InsetBoxParams::read(Lexer & lex)
588 type = lex.getString();
595 token = lex.getString();
598 if (token == "position") {
600 // The [0] is needed. We need the first and only char in
602 pos = lex.getString()[0];
604 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
605 lex.pushToken(token);
609 token = lex.getString();
612 if (token == "hor_pos") {
614 hor_pos = lex.getString()[0];
616 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
617 lex.pushToken(token);
621 token = lex.getString();
624 if (token == "has_inner_box") {
626 inner_box = lex.getInteger();
628 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
629 lex.pushToken(token);
633 token = lex.getString();
636 if (token == "inner_pos") {
638 inner_pos = lex.getString()[0];
640 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
642 lex.pushToken(token);
646 token = lex.getString();
649 if (token == "use_parbox") {
651 use_parbox = lex.getInteger();
653 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
654 lex.pushToken(token);
658 token = lex.getString();
661 if (token == "width") {
663 width = Length(lex.getString());
665 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
666 lex.pushToken(token);
670 token = lex.getString();
673 if (token == "special") {
675 special = lex.getString();
677 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
678 lex.pushToken(token);
682 token = lex.getString();
685 if (token == "height") {
687 height = Length(lex.getString());
689 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
690 lex.pushToken(token);
694 token = lex.getString();
697 if (token == "height_special") {
699 height_special = lex.getString();
701 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
702 lex.pushToken(token);