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 "dispatchresult.h"
20 #include "FuncStatus.h"
21 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
29 #include "support/translator.h"
38 using std::istringstream;
40 using std::ostringstream;
46 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
48 BoxTranslator const init_boxtranslator()
50 BoxTranslator translator("Boxed", InsetBox::Boxed);
51 translator.addPair("Frameless", InsetBox::Frameless);
52 translator.addPair("ovalbox", InsetBox::ovalbox);
53 translator.addPair("Ovalbox", InsetBox::Ovalbox);
54 translator.addPair("Shadowbox", InsetBox::Shadowbox);
55 translator.addPair("Doublebox",InsetBox::Doublebox);
60 BoxTranslator const init_boxtranslator_loc()
63 BoxTranslator translator(to_utf8(_("Boxed")), InsetBox::Boxed);
64 translator.addPair(to_utf8(_("Frameless")), InsetBox::Frameless);
65 translator.addPair(to_utf8(_("ovalbox")), InsetBox::ovalbox);
66 translator.addPair(to_utf8(_("Ovalbox")), InsetBox::Ovalbox);
67 translator.addPair(to_utf8(_("Shadowbox")), InsetBox::Shadowbox);
68 translator.addPair(to_utf8(_("Doublebox")), InsetBox::Doublebox);
73 BoxTranslator const & boxtranslator()
75 static BoxTranslator translator = init_boxtranslator();
80 BoxTranslator const & boxtranslator_loc()
82 static BoxTranslator translator = init_boxtranslator_loc();
91 setInsetName(from_ascii("Box"));
96 InsetBox::InsetBox(BufferParams const & bp, string const & label)
97 : InsetCollapsable(bp), params_(label)
103 InsetBox::InsetBox(InsetBox const & in)
104 : InsetCollapsable(in), params_(in.params_)
110 InsetBox::~InsetBox()
112 InsetBoxMailer(*this).hideDialog();
116 auto_ptr<InsetBase> InsetBox::doClone() const
118 return auto_ptr<InsetBase>(new InsetBox(*this));
122 docstring const InsetBox::editMessage() const
124 return _("Opened Box Inset");
128 void InsetBox::write(Buffer const & buf, ostream & os) const
131 InsetCollapsable::write(buf, os);
135 void InsetBox::read(Buffer const & buf, LyXLex & lex)
138 InsetCollapsable::read(buf, lex);
143 void InsetBox::setButtonLabel()
145 LyXFont font(LyXFont::ALL_SANE);
149 BoxType btype = boxtranslator().find(params_.type);
154 if (btype == Frameless) {
155 if (params_.use_parbox)
156 label += _("Parbox");
158 label += _("Minipage");
161 label += from_utf8(boxtranslator_loc().find(btype));
166 font.setColor(LColor::foreground);
167 setBackgroundColor(LColor::background);
172 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
175 if (params_.inner_box || params_.special != "width")
176 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
177 InsetCollapsable::metrics(mi, dim);
178 bool const changed = dim_ != dim;
184 bool InsetBox::forceDefaultParagraphs(idx_type) const
186 return !params_.inner_box;
190 bool InsetBox::showInsetDialog(BufferView * bv) const
192 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
197 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
199 switch (cmd.action) {
201 case LFUN_INSET_MODIFY: {
202 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
203 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
208 case LFUN_INSET_DIALOG_UPDATE:
209 InsetBoxMailer(*this).updateDialog(&cur.bv());
212 case LFUN_MOUSE_RELEASE:
213 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
214 InsetBoxMailer(*this).showDialog(&cur.bv());
217 InsetCollapsable::doDispatch(cur, cmd);
221 InsetCollapsable::doDispatch(cur, cmd);
227 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
228 FuncStatus & flag) const
230 switch (cmd.action) {
232 case LFUN_INSET_MODIFY:
233 case LFUN_INSET_DIALOG_UPDATE:
236 case LFUN_BREAK_PARAGRAPH:
237 if (params_.inner_box) {
238 return InsetCollapsable::getStatus(cur, cmd, flag);
245 return InsetCollapsable::getStatus(cur, cmd, flag);
250 int InsetBox::latex(Buffer const & buf, odocstream & os,
251 OutputParams const & runparams) const
253 BoxType btype = boxtranslator().find(params_.type);
255 string width_string = params_.width.asLatexString();
256 bool stdwidth(false);
257 if (params_.inner_box &&
258 (width_string.find("1.0\\columnwidth") != string::npos
259 || 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:
294 if (!params_.inner_box) {
296 // Special widths, see usrguide §3.5
298 if (params_.special != "none") {
299 os << "[" << params_.width.value()
300 << '\\' << from_utf8(params_.special)
303 os << '[' << from_ascii(width_string)
305 if (params_.hor_pos != 'c')
306 os << "[" << params_.hor_pos << "]";
318 os << "\\shadowbox{";
321 os << "\\doublebox{";
325 if (params_.inner_box) {
326 if (params_.use_parbox)
329 os << "\\begin{minipage}";
331 os << "[" << params_.pos << "]";
332 if (params_.height_special == "none") {
334 os << '[' << from_ascii(params_.height.asLatexString())
339 os << "[" << params_.height.value()
340 << '\\' << from_utf8(params_.height_special)
343 if (params_.inner_pos != params_.pos)
344 os << "[" << params_.inner_pos << "]";
347 os << '{' << from_ascii(width_string) << '}';
349 if (params_.use_parbox)
355 i += InsetText::latex(buf, os, runparams);
357 if (params_.inner_box) {
358 if (params_.use_parbox)
361 os << "%\n\\end{minipage}";
368 if (!params_.inner_box)
369 os << "}"; // for makebox
387 int InsetBox::docbook(Buffer const & buf, odocstream & os,
388 OutputParams const & runparams) const
390 return InsetText::docbook(buf, os, runparams);
394 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
395 OutputParams const & runparams) const
397 BoxType const btype = boxtranslator().find(params_.type);
400 case Frameless: break;
401 case Boxed: os << "["; break;
402 case ovalbox: os << "("; break;
403 case Ovalbox: os << "(("; break;
404 case Shadowbox: os << "["; break;
405 case Doublebox: os << "[["; break;
408 int i = InsetText::plaintext(buf, os, runparams);
411 case Frameless: break;
412 case Boxed: os << "]"; break;
413 case ovalbox: os << ")"; break;
414 case Ovalbox: os << "))"; break;
415 case Shadowbox: os << "]/"; break;
416 case Doublebox: os << "]]"; break;
423 void InsetBox::validate(LaTeXFeatures & features) const
425 features.require("calc");
426 BoxType btype = boxtranslator().find(params_.type);
435 features.require("fancybox");
438 InsetText::validate(features);
442 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
447 string const InsetBoxMailer::name_ = "box";
450 string const InsetBoxMailer::inset2string(Buffer const &) const
452 return params2string(inset_.params());
456 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
459 data << "box" << ' ';
465 void InsetBoxMailer::string2params(string const & in,
466 InsetBoxParams & params)
468 params = InsetBoxParams(string());
472 istringstream data(in);
478 if (!lex || name != name_)
479 return print_mailer_error("InsetBoxMailer", in, 1, name_);
481 // This is part of the inset proper that is usually swallowed
482 // by LyXText::readInset
485 if (!lex || id != "Box")
486 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
492 InsetBoxParams::InsetBoxParams(string const & label)
496 width(LyXLength("100col%")),
501 height(LyXLength("1in")),
502 height_special("totalheight") // default is 1\\totalheight
506 void InsetBoxParams::write(ostream & os) const
508 os << "Box " << type << "\n";
509 os << "position \"" << pos << "\"\n";
510 os << "hor_pos \"" << hor_pos << "\"\n";
511 os << "has_inner_box " << inner_box << "\n";
512 os << "inner_pos \"" << inner_pos << "\"\n";
513 os << "use_parbox " << use_parbox << "\n";
514 os << "width \"" << width.asString() << "\"\n";
515 os << "special \"" << special << "\"\n";
516 os << "height \"" << height.asString() << "\"\n";
517 os << "height_special \"" << height_special << "\"\n";
521 void InsetBoxParams::read(LyXLex & lex)
528 type = lex.getString();
534 token = lex.getString();
535 if (token == "position") {
537 // The [0] is needed. We need the first and only char in
539 pos = lex.getString()[0];
541 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
542 lex.pushToken(token);
547 token = lex.getString();
548 if (token == "hor_pos") {
550 hor_pos = lex.getString()[0];
552 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
553 lex.pushToken(token);
558 token = lex.getString();
559 if (token == "has_inner_box") {
561 inner_box = lex.getInteger();
563 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
564 lex.pushToken(token);
570 token = lex.getString();
571 if (token == "inner_pos") {
573 inner_pos = lex.getString()[0];
575 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
577 lex.pushToken(token);
582 token = lex.getString();
583 if (token == "use_parbox") {
585 use_parbox = lex.getInteger();
587 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
588 lex.pushToken(token);
593 token = lex.getString();
594 if (token == "width") {
596 width = LyXLength(lex.getString());
598 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
599 lex.pushToken(token);
604 token = lex.getString();
605 if (token == "special") {
607 special = lex.getString();
609 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
610 lex.pushToken(token);
615 token = lex.getString();
616 if (token == "height") {
618 height = LyXLength(lex.getString());
620 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
621 lex.pushToken(token);
626 token = lex.getString();
627 if (token == "height_special") {
629 height_special = lex.getString();
631 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
632 lex.pushToken(token);