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<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 bool InsetBox::isMacroScope(Buffer const &) const
244 BoxType btype = boxtranslator().find(params_.type);
245 return btype != Frameless || params_.inner_box;
249 int InsetBox::latex(Buffer const & buf, odocstream & os,
250 OutputParams const & runparams) const
252 BoxType btype = boxtranslator().find(params_.type);
254 string width_string = params_.width.asLatexString();
255 bool stdwidth(false);
256 if (params_.inner_box &&
257 (width_string.find("1.0\\columnwidth") != string::npos
258 || width_string.find("1.0\\textwidth") != string::npos)) {
266 width_string += " - 2\\fboxsep - 2\\fboxrule";
269 width_string += " - 2\\fboxsep - 0.8pt";
272 width_string += " - 2\\fboxsep - 1.6pt";
275 // Shadow falls outside right margin... opinions?
276 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
279 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
286 // Adapt to column/text width correctly also if paragraphs indented:
294 os << "\\begin{framed}%\n";
299 if (!params_.inner_box) {
301 // Special widths, see usrguide §3.5
303 if (params_.special != "none") {
304 os << "[" << params_.width.value()
305 << '\\' << from_utf8(params_.special)
308 os << '[' << from_ascii(width_string)
310 if (params_.hor_pos != 'c')
311 os << "[" << params_.hor_pos << "]";
323 os << "\\shadowbox{";
329 os << "\\doublebox{";
333 if (params_.inner_box) {
334 if (params_.use_parbox)
337 os << "\\begin{minipage}";
339 os << "[" << params_.pos << "]";
340 if (params_.height_special == "none") {
342 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
345 // set no optional argument when the value is the default "1\height"
346 // (special units like \height are handled as "in")
347 // but when the user has chosen a non-default inner_pos, the height
348 // must be given: \minipage[pos][height][inner-pos]{width}
349 if ((params_.height != Length("1in") ||
350 params_.height_special != "totalheight") ||
351 params_.inner_pos != params_.pos) {
353 os << "[" << params_.height.value()
354 << "\\" << from_utf8(params_.height_special) << "]";
357 if (params_.inner_pos != params_.pos)
358 os << "[" << params_.inner_pos << "]";
361 os << '{' << from_ascii(width_string) << '}';
363 if (params_.use_parbox)
369 os << "\\begin{shaded}%\n";
372 i += InsetText::latex(buf, os, runparams);
375 os << "\\end{shaded}";
377 if (params_.inner_box) {
378 if (params_.use_parbox)
381 os << "%\n\\end{minipage}";
388 os << "\\end{framed}";
391 if (!params_.inner_box)
392 os << "}"; // for makebox
413 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
414 OutputParams const & runparams) const
416 BoxType const btype = boxtranslator().find(params_.type);
440 InsetText::plaintext(buf, os, runparams);
471 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
475 int InsetBox::docbook(Buffer const & buf, odocstream & os,
476 OutputParams const & runparams) const
478 return InsetText::docbook(buf, os, runparams);
482 void InsetBox::validate(LaTeXFeatures & features) const
484 BoxType btype = boxtranslator().find(params_.type);
489 features.require("framed");
492 features.require("calc");
498 features.require("calc");
499 features.require("fancybox");
502 features.require("color");
503 features.require("framed");
506 InsetText::validate(features);
510 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
515 string const InsetBoxMailer::name_ = "box";
518 string const InsetBoxMailer::inset2string(Buffer const &) const
520 return params2string(inset_.params());
524 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
527 data << "box" << ' ';
533 void InsetBoxMailer::string2params(string const & in,
534 InsetBoxParams & params)
536 params = InsetBoxParams(string());
540 istringstream data(in);
546 if (!lex || name != name_)
547 return print_mailer_error("InsetBoxMailer", in, 1, name_);
549 // This is part of the inset proper that is usually swallowed
550 // by Text::readInset
553 if (!lex || id != "Box")
554 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
560 InsetBoxParams::InsetBoxParams(string const & label)
564 width(Length("100col%")),
569 height(Length("1in")),
570 height_special("totalheight") // default is 1\\totalheight
574 void InsetBoxParams::write(ostream & os) const
576 os << "Box " << type << "\n";
577 os << "position \"" << pos << "\"\n";
578 os << "hor_pos \"" << hor_pos << "\"\n";
579 os << "has_inner_box " << inner_box << "\n";
580 os << "inner_pos \"" << inner_pos << "\"\n";
581 os << "use_parbox " << use_parbox << "\n";
582 os << "width \"" << width.asString() << "\"\n";
583 os << "special \"" << special << "\"\n";
584 os << "height \"" << height.asString() << "\"\n";
585 os << "height_special \"" << height_special << "\"\n";
589 void InsetBoxParams::read(Lexer & lex)
595 type = lex.getString();
602 token = lex.getString();
605 if (token == "position") {
607 // The [0] is needed. We need the first and only char in
609 pos = lex.getString()[0];
611 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
612 lex.pushToken(token);
616 token = lex.getString();
619 if (token == "hor_pos") {
621 hor_pos = lex.getString()[0];
623 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
624 lex.pushToken(token);
628 token = lex.getString();
631 if (token == "has_inner_box") {
633 inner_box = lex.getInteger();
635 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
636 lex.pushToken(token);
640 token = lex.getString();
643 if (token == "inner_pos") {
645 inner_pos = lex.getString()[0];
647 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
649 lex.pushToken(token);
653 token = lex.getString();
656 if (token == "use_parbox") {
658 use_parbox = lex.getInteger();
660 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
661 lex.pushToken(token);
665 token = lex.getString();
668 if (token == "width") {
670 width = Length(lex.getString());
672 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
673 lex.pushToken(token);
677 token = lex.getString();
680 if (token == "special") {
682 special = lex.getString();
684 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
685 lex.pushToken(token);
689 token = lex.getString();
692 if (token == "height") {
694 height = Length(lex.getString());
696 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
697 lex.pushToken(token);
701 token = lex.getString();
704 if (token == "height_special") {
706 height_special = lex.getString();
708 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
709 lex.pushToken(token);