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"
19 #include "funcrequest.h"
21 #include "LaTeXFeatures.h"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
27 #include "support/std_sstream.h"
31 using std::istringstream;
33 using std::ostringstream;
39 BoxTranslator const init_boxtranslator() {
40 BoxTranslator translator("Boxed", InsetBox::Boxed);
41 translator.addPair("Frameless", InsetBox::Frameless);
42 translator.addPair("ovalbox", InsetBox::ovalbox);
43 translator.addPair("Ovalbox", InsetBox::Ovalbox);
44 translator.addPair("Shadowbox", InsetBox::Shadowbox);
45 translator.addPair("Doublebox",InsetBox::Doublebox);
50 BoxTranslator const init_boxtranslator_loc() {
51 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
52 translator.addPair(_("Frameless"), InsetBox::Frameless);
53 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
54 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
55 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
56 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
61 BoxTranslator const & boxtranslator() {
62 static BoxTranslator translator = init_boxtranslator();
67 BoxTranslator const & boxtranslator_loc() {
68 static BoxTranslator translator = init_boxtranslator_loc();
82 InsetBox::InsetBox(BufferParams const & bp, string const & label)
83 : InsetCollapsable(bp), params_(label)
89 InsetBox::InsetBox(InsetBox const & in)
90 : InsetCollapsable(in), params_(in.params_)
98 InsetBoxMailer mailer(*this);
103 auto_ptr<InsetBase> InsetBox::clone() const
105 return auto_ptr<InsetBase>(new InsetBox(*this));
109 string const InsetBox::editMessage() const
111 return _("Opened Box Inset");
115 void InsetBox::write(Buffer const & buf, ostream & os) const
118 InsetCollapsable::write(buf, os);
122 void InsetBox::read(Buffer const & buf, LyXLex & lex)
125 InsetCollapsable::read(buf, lex);
130 void InsetBox::setButtonLabel()
132 LyXFont font(LyXFont::ALL_SANE);
136 BoxType btype = boxtranslator().find(params_.type);
137 if (btype == Frameless) {
138 if (params_.use_parbox)
139 setLabel(_("Parbox"));
141 setLabel(_("Minipage"));
143 setLabel(boxtranslator_loc().find(btype));
145 font.setColor(LColor::foreground);
146 setBackgroundColor(LColor::background);
151 void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
153 InsetCollapsable::metrics(mi, dim);
154 if (params_.inner_box && isOpen())
155 dim.wid = mi.base.textwidth;
160 bool InsetBox::showInsetDialog(BufferView * bv) const
162 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
167 dispatch_result InsetBox::localDispatch(FuncRequest const & cmd)
169 BufferView * bv = cmd.view();
171 switch (cmd.action) {
173 case LFUN_INSET_MODIFY: {
174 InsetBoxMailer::string2params(cmd.argument, params_);
176 bv->updateInset(this);
179 case LFUN_INSET_DIALOG_UPDATE:
180 InsetBoxMailer(*this).updateDialog(bv);
183 case LFUN_MOUSE_RELEASE:
184 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
185 InsetBoxMailer(*this).showDialog(bv);
191 return InsetCollapsable::localDispatch(cmd);
196 int InsetBox::latex(Buffer const & buf, ostream & os,
197 LatexRunParams const & runparams) const
199 BoxType btype = boxtranslator().find(params_.type);
201 string width_string = params_.width.asLatexString();
202 bool stdwidth(false);
203 if (params_.inner_box &&
204 (width_string.find("1.0\\columnwidth") != string::npos
205 || width_string.find("1.0\\textwidth") != string::npos)) {
211 width_string += " - 2\\fboxsep - 2\\fboxrule";
214 width_string += " - 2\\fboxsep - 0.8pt";
217 width_string += " - 2\\fboxsep - 1.6pt";
220 // Shadow falls outside right margin... opinions?
221 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
224 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
231 // Adapt to column/text width correctly also if paragraphs indented:
240 if (!params_.inner_box) {
242 // Special widths, see usrguide §3.5
243 if (params_.special != "none") {
244 os << "[" << params_.width.value()
245 << "\\" << params_.special << "]";
247 os << "[" << width_string << "]";
248 if (params_.hor_pos != 'c')
249 os << "[" << params_.hor_pos << "]";
261 os << "\\shadowbox{";
264 os << "\\doublebox{";
268 if (params_.inner_box) {
269 if (params_.use_parbox)
272 os << "\\begin{minipage}";
274 os << "[" << params_.pos << "]";
275 if (params_.height_special == "none") {
276 os << "[" << params_.height.asLatexString() << "]";
279 os << "[" << params_.height.value()
280 << "\\" << params_.height_special << "]";
282 if (params_.inner_pos != params_.pos)
283 os << "[" << params_.inner_pos << "]";
285 os << "{" << width_string << "}";
287 if (params_.use_parbox)
293 i += inset.latex(buf, os, runparams);
295 if (params_.inner_box) {
296 if (params_.use_parbox)
299 os << "%\n\\end{minipage}";
306 if (!params_.inner_box)
307 os << "}"; // for makebox
325 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os) const
327 return inset.linuxdoc(buf, os);
331 int InsetBox::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
333 return inset.docbook(buf, os, mixcont);
337 int InsetBox::ascii(Buffer const & buf, std::ostream & os, int ll) const
340 string const pt = params_.type;
341 BoxType btype = boxtranslator().find(params_.type);
362 i = inset.ascii(buf, os, ll);
388 void InsetBox::validate(LaTeXFeatures & features) const
390 features.require("calc");
391 BoxType btype = boxtranslator().find(params_.type);
400 features.require("fancybox");
403 inset.validate(features);
407 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
413 string const InsetBoxMailer::name_ = "box";
416 string const InsetBoxMailer::inset2string(Buffer const &) const
418 return params2string(inset_.params());
422 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
425 data << "box" << ' ';
431 void InsetBoxMailer::string2params(string const & in,
432 InsetBoxParams & params)
434 params = InsetBoxParams(string());
439 istringstream data(in);
445 token = lex.getString();
452 InsetBoxParams::InsetBoxParams(string const & label)
456 width(LyXLength("100col%")),
461 height(LyXLength("1in")),
462 height_special("totalheight") // default is 1\\totalheight
466 void InsetBoxParams::write(ostream & os) const
469 os << "position \"" << pos << "\"\n";
470 os << "hor_pos \"" << hor_pos << "\"\n";
471 os << "has_inner_box " << inner_box << "\n";
472 os << "inner_pos \"" << inner_pos << "\"\n";
473 os << "use_parbox " << use_parbox << "\n";
474 os << "width \"" << width.asString() << "\"\n";
475 os << "special \"" << special << "\"\n";
476 os << "height \"" << height.asString() << "\"\n";
477 os << "height_special \"" << height_special << "\"\n";
481 void InsetBoxParams::read(LyXLex & lex)
484 type = lex.getString();
490 token = lex.getString();
491 if (token == "position") {
493 // The [0] is needed. We need the first and only char in
495 pos = lex.getString()[0];
497 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
498 lex.pushToken(token);
503 token = lex.getString();
504 if (token == "hor_pos") {
506 hor_pos = lex.getString()[0];
508 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
509 lex.pushToken(token);
514 token = lex.getString();
515 if (token == "has_inner_box") {
517 inner_box = lex.getInteger();
519 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
520 lex.pushToken(token);
526 token = lex.getString();
527 if (token == "inner_pos") {
529 inner_pos = lex.getString()[0];
531 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
533 lex.pushToken(token);
538 token = lex.getString();
539 if (token == "use_parbox") {
541 use_parbox = lex.getInteger();
543 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
544 lex.pushToken(token);
549 token = lex.getString();
550 if (token == "width") {
552 width = LyXLength(lex.getString());
554 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
555 lex.pushToken(token);
560 token = lex.getString();
561 if (token == "special") {
563 special = lex.getString();
565 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
566 lex.pushToken(token);
571 token = lex.getString();
572 if (token == "height") {
574 height = LyXLength(lex.getString());
576 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
577 lex.pushToken(token);
582 token = lex.getString();
583 if (token == "height_special") {
585 height_special = lex.getString();
587 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
588 lex.pushToken(token);