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"
37 using std::istringstream;
39 using std::ostringstream;
45 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
46 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
48 BoxTranslator const init_boxtranslator()
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 const init_boxtranslator_loc()
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 = init_boxtranslator();
83 BoxTranslatorLoc const & boxtranslator_loc()
85 static BoxTranslatorLoc translator = init_boxtranslator_loc();
92 InsetBox::InsetBox(BufferParams const & bp, string const & label)
93 : InsetCollapsable(bp), params_(label)
97 InsetBox::InsetBox(InsetBox const & in)
98 : InsetCollapsable(in), params_(in.params_)
102 InsetBox::~InsetBox()
104 InsetBoxMailer(*this).hideDialog();
108 Inset * InsetBox::clone() const
110 return new InsetBox(*this);
114 docstring const InsetBox::editMessage() const
116 return _("Opened Box Inset");
120 docstring InsetBox::name() const
123 string name = string("Box");
124 if (boxtranslator().find(params_.type) == Shaded)
125 name += string(":Shaded");
126 return from_ascii(name);
130 void InsetBox::write(Buffer const & buf, ostream & os) const
133 InsetCollapsable::write(buf, os);
137 void InsetBox::read(Buffer const & buf, Lexer & lex)
140 InsetCollapsable::read(buf, lex);
144 void InsetBox::setButtonLabel()
146 BoxType btype = boxtranslator().find(params_.type);
151 if (btype == Frameless) {
152 if (params_.use_parbox)
153 label += _("Parbox");
155 label += _("Minipage");
157 label += boxtranslator_loc().find(btype);
164 bool InsetBox::hasFixedWidth() const
166 return params_.inner_box || params_.special != "width";
170 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
172 // back up textwidth.
173 int textwidth_backup = m.base.textwidth;
175 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
176 InsetCollapsable::metrics(m, dim);
178 m.base.textwidth = textwidth_backup;
182 bool InsetBox::forceDefaultParagraphs(idx_type) const
184 return !params_.inner_box;
188 bool InsetBox::showInsetDialog(BufferView * bv) const
190 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
195 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
197 switch (cmd.action) {
199 case LFUN_INSET_MODIFY: {
200 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
201 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
202 setLayout(cur.buffer().params());
206 case LFUN_INSET_DIALOG_UPDATE:
207 InsetBoxMailer(*this).updateDialog(&cur.bv());
210 case LFUN_MOUSE_RELEASE:
211 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
212 InsetBoxMailer(*this).showDialog(&cur.bv());
215 InsetCollapsable::doDispatch(cur, cmd);
219 InsetCollapsable::doDispatch(cur, cmd);
225 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
226 FuncStatus & flag) const
228 switch (cmd.action) {
230 case LFUN_INSET_MODIFY:
231 case LFUN_INSET_DIALOG_UPDATE:
234 case LFUN_BREAK_PARAGRAPH:
235 if (params_.inner_box) {
236 return InsetCollapsable::getStatus(cur, cmd, flag);
243 return InsetCollapsable::getStatus(cur, cmd, flag);
248 int InsetBox::latex(Buffer const & buf, odocstream & os,
249 OutputParams const & runparams) const
251 BoxType btype = boxtranslator().find(params_.type);
253 string width_string = params_.width.asLatexString();
254 bool stdwidth(false);
255 if (params_.inner_box &&
256 (width_string.find("1.0\\columnwidth") != string::npos
257 || width_string.find("1.0\\textwidth") != string::npos)) {
265 width_string += " - 2\\fboxsep - 2\\fboxrule";
268 width_string += " - 2\\fboxsep - 0.8pt";
271 width_string += " - 2\\fboxsep - 1.6pt";
274 // Shadow falls outside right margin... opinions?
275 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
278 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
285 // Adapt to column/text width correctly also if paragraphs indented:
293 os << "\\begin{framed}%\n";
298 if (!params_.inner_box) {
300 // Special widths, see usrguide §3.5
302 if (params_.special != "none") {
303 os << "[" << params_.width.value()
304 << '\\' << from_utf8(params_.special)
307 os << '[' << from_ascii(width_string)
309 if (params_.hor_pos != 'c')
310 os << "[" << params_.hor_pos << "]";
322 os << "\\shadowbox{";
328 os << "\\doublebox{";
332 if (params_.inner_box) {
333 if (params_.use_parbox)
336 os << "\\begin{minipage}";
338 os << "[" << params_.pos << "]";
339 if (params_.height_special == "none") {
341 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
344 // set no optional argument when the value is the default "1\height"
345 // (special units like \height are handled as "in")
346 // but when the user has chosen a non-default inner_pos, the height
347 // must be given: \minipage[pos][height][inner-pos]{width}
348 if ((params_.height != Length("1in") ||
349 params_.height_special != "totalheight") ||
350 params_.inner_pos != params_.pos) {
352 os << "[" << params_.height.value()
353 << "\\" << from_utf8(params_.height_special) << "]";
356 if (params_.inner_pos != params_.pos)
357 os << "[" << params_.inner_pos << "]";
360 os << '{' << from_ascii(width_string) << '}';
362 if (params_.use_parbox)
368 os << "\\begin{shaded}%\n";
371 i += InsetText::latex(buf, os, runparams);
374 os << "\\end{shaded}";
376 if (params_.inner_box) {
377 if (params_.use_parbox)
380 os << "%\n\\end{minipage}";
387 os << "\\end{framed}";
390 if (!params_.inner_box)
391 os << "}"; // for makebox
412 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
413 OutputParams const & runparams) const
415 BoxType const btype = boxtranslator().find(params_.type);
439 InsetText::plaintext(buf, os, runparams);
470 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
474 int InsetBox::docbook(Buffer const & buf, odocstream & os,
475 OutputParams const & runparams) const
477 return InsetText::docbook(buf, os, runparams);
481 void InsetBox::validate(LaTeXFeatures & features) const
483 BoxType btype = boxtranslator().find(params_.type);
488 features.require("framed");
491 features.require("calc");
497 features.require("calc");
498 features.require("fancybox");
501 features.require("color");
502 features.require("framed");
505 InsetText::validate(features);
509 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
514 string const InsetBoxMailer::name_ = "box";
517 string const InsetBoxMailer::inset2string(Buffer const &) const
519 return params2string(inset_.params());
523 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
526 data << "box" << ' ';
532 void InsetBoxMailer::string2params(string const & in,
533 InsetBoxParams & params)
535 params = InsetBoxParams(string());
539 istringstream data(in);
545 if (!lex || name != name_)
546 return print_mailer_error("InsetBoxMailer", in, 1, name_);
548 // This is part of the inset proper that is usually swallowed
549 // by Text::readInset
552 if (!lex || id != "Box")
553 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
559 InsetBoxParams::InsetBoxParams(string const & label)
563 width(Length("100col%")),
568 height(Length("1in")),
569 height_special("totalheight") // default is 1\\totalheight
573 void InsetBoxParams::write(ostream & os) const
575 os << "Box " << type << "\n";
576 os << "position \"" << pos << "\"\n";
577 os << "hor_pos \"" << hor_pos << "\"\n";
578 os << "has_inner_box " << inner_box << "\n";
579 os << "inner_pos \"" << inner_pos << "\"\n";
580 os << "use_parbox " << use_parbox << "\n";
581 os << "width \"" << width.asString() << "\"\n";
582 os << "special \"" << special << "\"\n";
583 os << "height \"" << height.asString() << "\"\n";
584 os << "height_special \"" << height_special << "\"\n";
588 void InsetBoxParams::read(Lexer & lex)
594 type = lex.getString();
601 token = lex.getString();
604 if (token == "position") {
606 // The [0] is needed. We need the first and only char in
608 pos = lex.getString()[0];
610 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
611 lex.pushToken(token);
615 token = lex.getString();
618 if (token == "hor_pos") {
620 hor_pos = lex.getString()[0];
622 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
623 lex.pushToken(token);
627 token = lex.getString();
630 if (token == "has_inner_box") {
632 inner_box = lex.getInteger();
634 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
635 lex.pushToken(token);
639 token = lex.getString();
642 if (token == "inner_pos") {
644 inner_pos = lex.getString()[0];
646 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
648 lex.pushToken(token);
652 token = lex.getString();
655 if (token == "use_parbox") {
657 use_parbox = lex.getInteger();
659 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
660 lex.pushToken(token);
664 token = lex.getString();
667 if (token == "width") {
669 width = Length(lex.getString());
671 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
672 lex.pushToken(token);
676 token = lex.getString();
679 if (token == "special") {
681 special = lex.getString();
683 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
684 lex.pushToken(token);
688 token = lex.getString();
691 if (token == "height") {
693 height = Length(lex.getString());
695 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
696 lex.pushToken(token);
700 token = lex.getString();
703 if (token == "height_special") {
705 height_special = lex.getString();
707 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
708 lex.pushToken(token);