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.
17 #include "BufferView.h"
18 #include "dispatchresult.h"
20 #include "funcrequest.h"
22 #include "LaTeXFeatures.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
28 #include "support/std_sstream.h"
32 using std::istringstream;
34 using std::ostringstream;
40 BoxTranslator const init_boxtranslator() {
41 BoxTranslator translator("Boxed", InsetBox::Boxed);
42 translator.addPair("Frameless", InsetBox::Frameless);
43 translator.addPair("ovalbox", InsetBox::ovalbox);
44 translator.addPair("Ovalbox", InsetBox::Ovalbox);
45 translator.addPair("Shadowbox", InsetBox::Shadowbox);
46 translator.addPair("Doublebox",InsetBox::Doublebox);
51 BoxTranslator const init_boxtranslator_loc() {
52 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
53 translator.addPair(_("Frameless"), InsetBox::Frameless);
54 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
55 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
56 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
57 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
62 BoxTranslator const & boxtranslator() {
63 static BoxTranslator translator = init_boxtranslator();
68 BoxTranslator const & boxtranslator_loc() {
69 static BoxTranslator translator = init_boxtranslator_loc();
83 InsetBox::InsetBox(BufferParams const & bp, string const & label)
84 : InsetCollapsable(bp), params_(label)
90 InsetBox::InsetBox(InsetBox const & in)
91 : InsetCollapsable(in), params_(in.params_)
99 InsetBoxMailer mailer(*this);
104 auto_ptr<InsetBase> InsetBox::clone() const
106 return auto_ptr<InsetBase>(new InsetBox(*this));
110 string const InsetBox::editMessage() const
112 return _("Opened Box Inset");
116 void InsetBox::write(Buffer const & buf, ostream & os) const
119 InsetCollapsable::write(buf, os);
123 void InsetBox::read(Buffer const & buf, LyXLex & lex)
126 InsetCollapsable::read(buf, lex);
131 void InsetBox::setButtonLabel()
133 LyXFont font(LyXFont::ALL_SANE);
137 BoxType btype = boxtranslator().find(params_.type);
138 if (btype == Frameless) {
139 if (params_.use_parbox)
140 setLabel(_("Parbox"));
142 setLabel(_("Minipage"));
144 setLabel(boxtranslator_loc().find(btype));
146 font.setColor(LColor::foreground);
147 setBackgroundColor(LColor::background);
152 void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
154 InsetCollapsable::metrics(mi, dim);
155 if (params_.inner_box && isOpen())
156 dim.wid = mi.base.textwidth;
161 bool InsetBox::showInsetDialog(BufferView * bv) const
163 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
169 InsetBox::priv_dispatch(FuncRequest const & cmd,
170 idx_type & idx, pos_type & pos)
172 BufferView * bv = cmd.view();
174 switch (cmd.action) {
176 case LFUN_INSET_MODIFY: {
177 InsetBoxMailer::string2params(cmd.argument, params_);
179 bv->updateInset(this);
182 case LFUN_INSET_DIALOG_UPDATE:
183 InsetBoxMailer(*this).updateDialog(bv);
186 case LFUN_MOUSE_RELEASE:
187 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
188 InsetBoxMailer(*this).showDialog(bv);
194 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
199 int InsetBox::latex(Buffer const & buf, ostream & os,
200 LatexRunParams const & runparams) const
202 BoxType btype = boxtranslator().find(params_.type);
204 string width_string = params_.width.asLatexString();
205 bool stdwidth(false);
206 if (params_.inner_box &&
207 (width_string.find("1.0\\columnwidth") != string::npos
208 || width_string.find("1.0\\textwidth") != string::npos)) {
214 width_string += " - 2\\fboxsep - 2\\fboxrule";
217 width_string += " - 2\\fboxsep - 0.8pt";
220 width_string += " - 2\\fboxsep - 1.6pt";
223 // Shadow falls outside right margin... opinions?
224 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
227 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
234 // Adapt to column/text width correctly also if paragraphs indented:
243 if (!params_.inner_box) {
245 // Special widths, see usrguide §3.5
246 if (params_.special != "none") {
247 os << "[" << params_.width.value()
248 << "\\" << params_.special << "]";
250 os << "[" << width_string << "]";
251 if (params_.hor_pos != 'c')
252 os << "[" << params_.hor_pos << "]";
264 os << "\\shadowbox{";
267 os << "\\doublebox{";
271 if (params_.inner_box) {
272 if (params_.use_parbox)
275 os << "\\begin{minipage}";
277 os << "[" << params_.pos << "]";
278 if (params_.height_special == "none") {
279 os << "[" << params_.height.asLatexString() << "]";
282 os << "[" << params_.height.value()
283 << "\\" << params_.height_special << "]";
285 if (params_.inner_pos != params_.pos)
286 os << "[" << params_.inner_pos << "]";
288 os << "{" << width_string << "}";
290 if (params_.use_parbox)
296 i += inset.latex(buf, os, runparams);
298 if (params_.inner_box) {
299 if (params_.use_parbox)
302 os << "%\n\\end{minipage}";
309 if (!params_.inner_box)
310 os << "}"; // for makebox
328 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os) const
330 return inset.linuxdoc(buf, os);
334 int InsetBox::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
336 return inset.docbook(buf, os, mixcont);
340 int InsetBox::ascii(Buffer const & buf, std::ostream & os, int ll) const
343 string const pt = params_.type;
344 BoxType btype = boxtranslator().find(params_.type);
365 i = inset.ascii(buf, os, ll);
391 void InsetBox::validate(LaTeXFeatures & features) const
393 features.require("calc");
394 BoxType btype = boxtranslator().find(params_.type);
403 features.require("fancybox");
406 inset.validate(features);
410 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
416 string const InsetBoxMailer::name_ = "box";
419 string const InsetBoxMailer::inset2string(Buffer const &) const
421 return params2string(inset_.params());
425 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
428 data << "box" << ' ';
434 void InsetBoxMailer::string2params(string const & in,
435 InsetBoxParams & params)
437 params = InsetBoxParams(string());
442 istringstream data(in);
448 token = lex.getString();
455 InsetBoxParams::InsetBoxParams(string const & label)
459 width(LyXLength("100col%")),
464 height(LyXLength("1in")),
465 height_special("totalheight") // default is 1\\totalheight
469 void InsetBoxParams::write(ostream & os) const
472 os << "position \"" << pos << "\"\n";
473 os << "hor_pos \"" << hor_pos << "\"\n";
474 os << "has_inner_box " << inner_box << "\n";
475 os << "inner_pos \"" << inner_pos << "\"\n";
476 os << "use_parbox " << use_parbox << "\n";
477 os << "width \"" << width.asString() << "\"\n";
478 os << "special \"" << special << "\"\n";
479 os << "height \"" << height.asString() << "\"\n";
480 os << "height_special \"" << height_special << "\"\n";
484 void InsetBoxParams::read(LyXLex & lex)
487 type = lex.getString();
493 token = lex.getString();
494 if (token == "position") {
496 // The [0] is needed. We need the first and only char in
498 pos = lex.getString()[0];
500 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
501 lex.pushToken(token);
506 token = lex.getString();
507 if (token == "hor_pos") {
509 hor_pos = lex.getString()[0];
511 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
512 lex.pushToken(token);
517 token = lex.getString();
518 if (token == "has_inner_box") {
520 inner_box = lex.getInteger();
522 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
523 lex.pushToken(token);
529 token = lex.getString();
530 if (token == "inner_pos") {
532 inner_pos = lex.getString()[0];
534 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
536 lex.pushToken(token);
541 token = lex.getString();
542 if (token == "use_parbox") {
544 use_parbox = lex.getInteger();
546 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
547 lex.pushToken(token);
552 token = lex.getString();
553 if (token == "width") {
555 width = LyXLength(lex.getString());
557 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
558 lex.pushToken(token);
563 token = lex.getString();
564 if (token == "special") {
566 special = lex.getString();
568 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
569 lex.pushToken(token);
574 token = lex.getString();
575 if (token == "height") {
577 height = LyXLength(lex.getString());
579 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
580 lex.pushToken(token);
585 token = lex.getString();
586 if (token == "height_special") {
588 height_special = lex.getString();
590 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
591 lex.pushToken(token);