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);
168 InsetBox::priv_dispatch(FuncRequest const & cmd,
169 idx_type & idx, pos_type & pos)
171 BufferView * bv = cmd.view();
173 switch (cmd.action) {
175 case LFUN_INSET_MODIFY: {
176 InsetBoxMailer::string2params(cmd.argument, params_);
178 bv->updateInset(this);
181 case LFUN_INSET_DIALOG_UPDATE:
182 InsetBoxMailer(*this).updateDialog(bv);
185 case LFUN_MOUSE_RELEASE:
186 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
187 InsetBoxMailer(*this).showDialog(bv);
193 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
198 int InsetBox::latex(Buffer const & buf, ostream & os,
199 LatexRunParams const & runparams) const
201 BoxType btype = boxtranslator().find(params_.type);
203 string width_string = params_.width.asLatexString();
204 bool stdwidth(false);
205 if (params_.inner_box &&
206 (width_string.find("1.0\\columnwidth") != string::npos
207 || width_string.find("1.0\\textwidth") != string::npos)) {
213 width_string += " - 2\\fboxsep - 2\\fboxrule";
216 width_string += " - 2\\fboxsep - 0.8pt";
219 width_string += " - 2\\fboxsep - 1.6pt";
222 // Shadow falls outside right margin... opinions?
223 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
226 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
233 // Adapt to column/text width correctly also if paragraphs indented:
242 if (!params_.inner_box) {
244 // Special widths, see usrguide §3.5
245 if (params_.special != "none") {
246 os << "[" << params_.width.value()
247 << "\\" << params_.special << "]";
249 os << "[" << width_string << "]";
250 if (params_.hor_pos != 'c')
251 os << "[" << params_.hor_pos << "]";
263 os << "\\shadowbox{";
266 os << "\\doublebox{";
270 if (params_.inner_box) {
271 if (params_.use_parbox)
274 os << "\\begin{minipage}";
276 os << "[" << params_.pos << "]";
277 if (params_.height_special == "none") {
278 os << "[" << params_.height.asLatexString() << "]";
281 os << "[" << params_.height.value()
282 << "\\" << params_.height_special << "]";
284 if (params_.inner_pos != params_.pos)
285 os << "[" << params_.inner_pos << "]";
287 os << "{" << width_string << "}";
289 if (params_.use_parbox)
295 i += inset.latex(buf, os, runparams);
297 if (params_.inner_box) {
298 if (params_.use_parbox)
301 os << "%\n\\end{minipage}";
308 if (!params_.inner_box)
309 os << "}"; // for makebox
327 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os) const
329 return inset.linuxdoc(buf, os);
333 int InsetBox::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
335 return inset.docbook(buf, os, mixcont);
339 int InsetBox::ascii(Buffer const & buf, std::ostream & os, int ll) const
342 string const pt = params_.type;
343 BoxType btype = boxtranslator().find(params_.type);
364 i = inset.ascii(buf, os, ll);
390 void InsetBox::validate(LaTeXFeatures & features) const
392 features.require("calc");
393 BoxType btype = boxtranslator().find(params_.type);
402 features.require("fancybox");
405 inset.validate(features);
409 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
415 string const InsetBoxMailer::name_ = "box";
418 string const InsetBoxMailer::inset2string(Buffer const &) const
420 return params2string(inset_.params());
424 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
427 data << "box" << ' ';
433 void InsetBoxMailer::string2params(string const & in,
434 InsetBoxParams & params)
436 params = InsetBoxParams(string());
441 istringstream data(in);
447 token = lex.getString();
454 InsetBoxParams::InsetBoxParams(string const & label)
458 width(LyXLength("100col%")),
463 height(LyXLength("1in")),
464 height_special("totalheight") // default is 1\\totalheight
468 void InsetBoxParams::write(ostream & os) const
471 os << "position \"" << pos << "\"\n";
472 os << "hor_pos \"" << hor_pos << "\"\n";
473 os << "has_inner_box " << inner_box << "\n";
474 os << "inner_pos \"" << inner_pos << "\"\n";
475 os << "use_parbox " << use_parbox << "\n";
476 os << "width \"" << width.asString() << "\"\n";
477 os << "special \"" << special << "\"\n";
478 os << "height \"" << height.asString() << "\"\n";
479 os << "height_special \"" << height_special << "\"\n";
483 void InsetBoxParams::read(LyXLex & lex)
486 type = lex.getString();
492 token = lex.getString();
493 if (token == "position") {
495 // The [0] is needed. We need the first and only char in
497 pos = lex.getString()[0];
499 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
500 lex.pushToken(token);
505 token = lex.getString();
506 if (token == "hor_pos") {
508 hor_pos = lex.getString()[0];
510 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
511 lex.pushToken(token);
516 token = lex.getString();
517 if (token == "has_inner_box") {
519 inner_box = lex.getInteger();
521 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
522 lex.pushToken(token);
528 token = lex.getString();
529 if (token == "inner_pos") {
531 inner_pos = lex.getString()[0];
533 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
535 lex.pushToken(token);
540 token = lex.getString();
541 if (token == "use_parbox") {
543 use_parbox = lex.getInteger();
545 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
546 lex.pushToken(token);
551 token = lex.getString();
552 if (token == "width") {
554 width = LyXLength(lex.getString());
556 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
557 lex.pushToken(token);
562 token = lex.getString();
563 if (token == "special") {
565 special = lex.getString();
567 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
568 lex.pushToken(token);
573 token = lex.getString();
574 if (token == "height") {
576 height = LyXLength(lex.getString());
578 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
579 lex.pushToken(token);
584 token = lex.getString();
585 if (token == "height_special") {
587 height_special = lex.getString();
589 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
590 lex.pushToken(token);