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"
29 #include "support/translator.h"
33 using std::istringstream;
35 using std::ostringstream;
41 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
43 BoxTranslator const init_boxtranslator() {
44 BoxTranslator translator("Boxed", InsetBox::Boxed);
45 translator.addPair("Frameless", InsetBox::Frameless);
46 translator.addPair("ovalbox", InsetBox::ovalbox);
47 translator.addPair("Ovalbox", InsetBox::Ovalbox);
48 translator.addPair("Shadowbox", InsetBox::Shadowbox);
49 translator.addPair("Doublebox",InsetBox::Doublebox);
54 BoxTranslator const init_boxtranslator_loc() {
55 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
56 translator.addPair(_("Frameless"), InsetBox::Frameless);
57 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
58 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
59 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
60 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
65 BoxTranslator const & boxtranslator() {
66 static BoxTranslator translator = init_boxtranslator();
71 BoxTranslator const & boxtranslator_loc() {
72 static BoxTranslator translator = init_boxtranslator_loc();
86 InsetBox::InsetBox(BufferParams const & bp, string const & label)
87 : InsetCollapsable(bp), params_(label)
93 InsetBox::InsetBox(InsetBox const & in)
94 : InsetCollapsable(in), params_(in.params_)
100 InsetBox::~InsetBox()
102 InsetBoxMailer(*this).hideDialog();
106 auto_ptr<InsetBase> InsetBox::clone() const
108 return auto_ptr<InsetBase>(new InsetBox(*this));
112 string const InsetBox::editMessage() const
114 return _("Opened Box Inset");
118 void InsetBox::write(Buffer const & buf, ostream & os) const
121 InsetCollapsable::write(buf, os);
125 void InsetBox::read(Buffer const & buf, LyXLex & lex)
128 InsetCollapsable::read(buf, lex);
133 void InsetBox::setButtonLabel()
135 LyXFont font(LyXFont::ALL_SANE);
139 BoxType btype = boxtranslator().find(params_.type);
140 if (btype == Frameless) {
141 if (params_.use_parbox)
142 setLabel(_("Box") + " (" + _("Parbox") + ")");
144 setLabel(_("Box") + " (" + _("Minipage") + ")");
146 setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
148 font.setColor(LColor::foreground);
149 setBackgroundColor(LColor::background);
154 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
157 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
158 InsetCollapsable::metrics(mi, dim);
159 //if (params_.inner_box && isOpen())
160 // dim.wid = mi.base.textwidth;
165 bool InsetBox::showInsetDialog(BufferView * bv) const
167 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
173 InsetBox::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
175 switch (cmd.action) {
177 case LFUN_INSET_MODIFY: {
178 lyxerr << "InsetBox::dispatch MODIFY" << endl;
179 InsetBoxMailer::string2params(cmd.argument, params_);
181 return DispatchResult(true, true);
184 case LFUN_INSET_DIALOG_UPDATE:
185 InsetBoxMailer(*this).updateDialog(&bv);
186 return DispatchResult(true);
188 case LFUN_MOUSE_RELEASE:
189 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
190 InsetBoxMailer(*this).showDialog(&bv);
191 return DispatchResult(true);
193 return InsetCollapsable::priv_dispatch(bv, cmd);
196 return InsetCollapsable::priv_dispatch(bv, cmd);
201 int InsetBox::latex(Buffer const & buf, ostream & os,
202 OutputParams const & runparams) const
204 BoxType btype = boxtranslator().find(params_.type);
206 string width_string = params_.width.asLatexString();
207 bool stdwidth(false);
208 if (params_.inner_box &&
209 (width_string.find("1.0\\columnwidth") != string::npos
210 || width_string.find("1.0\\textwidth") != string::npos)) {
216 width_string += " - 2\\fboxsep - 2\\fboxrule";
219 width_string += " - 2\\fboxsep - 0.8pt";
222 width_string += " - 2\\fboxsep - 1.6pt";
225 // Shadow falls outside right margin... opinions?
226 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
229 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
236 // Adapt to column/text width correctly also if paragraphs indented:
245 if (!params_.inner_box) {
247 // Special widths, see usrguide §3.5
248 if (params_.special != "none") {
249 os << "[" << params_.width.value()
250 << "\\" << params_.special << "]";
252 os << "[" << width_string << "]";
253 if (params_.hor_pos != 'c')
254 os << "[" << params_.hor_pos << "]";
266 os << "\\shadowbox{";
269 os << "\\doublebox{";
273 if (params_.inner_box) {
274 if (params_.use_parbox)
277 os << "\\begin{minipage}";
279 os << "[" << params_.pos << "]";
280 if (params_.height_special == "none") {
281 os << "[" << params_.height.asLatexString() << "]";
284 os << "[" << params_.height.value()
285 << "\\" << params_.height_special << "]";
287 if (params_.inner_pos != params_.pos)
288 os << "[" << params_.inner_pos << "]";
290 os << "{" << width_string << "}";
292 if (params_.use_parbox)
298 i += inset.latex(buf, os, runparams);
300 if (params_.inner_box) {
301 if (params_.use_parbox)
304 os << "%\n\\end{minipage}";
311 if (!params_.inner_box)
312 os << "}"; // for makebox
330 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
331 OutputParams const & runparams) const
333 return inset.linuxdoc(buf, os, runparams);
337 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
338 OutputParams const & runparams) const
340 return inset.docbook(buf, os, runparams);
344 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
345 OutputParams const & runparams) const
347 BoxType const btype = boxtranslator().find(params_.type);
350 case Frameless: break;
351 case Boxed: os << "["; break;
352 case ovalbox: os << "("; break;
353 case Ovalbox: os << "(("; break;
354 case Shadowbox: os << "["; break;
355 case Doublebox: os << "[["; break;
358 int i = inset.plaintext(buf, os, runparams);
361 case Frameless: break;
362 case Boxed: os << "]"; break;
363 case ovalbox: os << ")"; break;
364 case Ovalbox: os << "))"; break;
365 case Shadowbox: os << "]/"; break;
366 case Doublebox: os << "]]"; break;
373 void InsetBox::validate(LaTeXFeatures & features) const
375 features.require("calc");
376 BoxType btype = boxtranslator().find(params_.type);
385 features.require("fancybox");
388 inset.validate(features);
392 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
398 string const InsetBoxMailer::name_ = "box";
401 string const InsetBoxMailer::inset2string(Buffer const &) const
403 return params2string(inset_.params());
407 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
410 data << "box" << ' ';
416 void InsetBoxMailer::string2params(string const & in,
417 InsetBoxParams & params)
419 params = InsetBoxParams(string());
423 istringstream data(in);
429 if (!lex || name != name_)
430 return print_mailer_error("InsetBoxMailer", in, 1, name_);
432 // This is part of the inset proper that is usually swallowed
433 // by LyXText::readInset
436 if (!lex || id != "Box")
437 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
443 InsetBoxParams::InsetBoxParams(string const & label)
447 width(LyXLength("100col%")),
452 height(LyXLength("1in")),
453 height_special("totalheight") // default is 1\\totalheight
457 void InsetBoxParams::write(ostream & os) const
459 os << "Box " << type << "\n";
460 os << "position \"" << pos << "\"\n";
461 os << "hor_pos \"" << hor_pos << "\"\n";
462 os << "has_inner_box " << inner_box << "\n";
463 os << "inner_pos \"" << inner_pos << "\"\n";
464 os << "use_parbox " << use_parbox << "\n";
465 os << "width \"" << width.asString() << "\"\n";
466 os << "special \"" << special << "\"\n";
467 os << "height \"" << height.asString() << "\"\n";
468 os << "height_special \"" << height_special << "\"\n";
472 void InsetBoxParams::read(LyXLex & lex)
479 type = lex.getString();
485 token = lex.getString();
486 if (token == "position") {
488 // The [0] is needed. We need the first and only char in
490 pos = lex.getString()[0];
492 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
493 lex.pushToken(token);
498 token = lex.getString();
499 if (token == "hor_pos") {
501 hor_pos = lex.getString()[0];
503 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
504 lex.pushToken(token);
509 token = lex.getString();
510 if (token == "has_inner_box") {
512 inner_box = lex.getInteger();
514 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
515 lex.pushToken(token);
521 token = lex.getString();
522 if (token == "inner_pos") {
524 inner_pos = lex.getString()[0];
526 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
528 lex.pushToken(token);
533 token = lex.getString();
534 if (token == "use_parbox") {
536 use_parbox = lex.getInteger();
538 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
539 lex.pushToken(token);
544 token = lex.getString();
545 if (token == "width") {
547 width = LyXLength(lex.getString());
549 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
550 lex.pushToken(token);
555 token = lex.getString();
556 if (token == "special") {
558 special = lex.getString();
560 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
561 lex.pushToken(token);
566 token = lex.getString();
567 if (token == "height") {
569 height = LyXLength(lex.getString());
571 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
572 lex.pushToken(token);
577 token = lex.getString();
578 if (token == "height_special") {
580 height_special = lex.getString();
582 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
583 lex.pushToken(token);