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"
34 using lyx::odocstream;
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(lyx::to_utf8(_("Boxed")), InsetBox::Boxed);
64 translator.addPair(lyx::to_utf8(_("Frameless")), InsetBox::Frameless);
65 translator.addPair(lyx::to_utf8(_("ovalbox")), InsetBox::ovalbox);
66 translator.addPair(lyx::to_utf8(_("Ovalbox")), InsetBox::Ovalbox);
67 translator.addPair(lyx::to_utf8(_("Shadowbox")), InsetBox::Shadowbox);
68 translator.addPair(lyx::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();
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);
155 if (btype == Frameless) {
156 if (params_.use_parbox)
157 label += _("Parbox");
159 label += _("Minipage");
162 label += lyx::from_utf8(boxtranslator_loc().find(btype));
167 font.setColor(LColor::foreground);
168 setBackgroundColor(LColor::background);
173 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
176 if (params_.inner_box || params_.special != "width")
177 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
178 InsetCollapsable::metrics(mi, dim);
183 bool InsetBox::forceDefaultParagraphs(idx_type) const
185 return !params_.inner_box;
189 bool InsetBox::showInsetDialog(BufferView * bv) const
191 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
196 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
198 switch (cmd.action) {
200 case LFUN_INSET_MODIFY: {
201 lyxerr << "InsetBox::dispatch MODIFY" << endl;
202 InsetBoxMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
207 case LFUN_INSET_DIALOG_UPDATE:
208 InsetBoxMailer(*this).updateDialog(&cur.bv());
211 case LFUN_MOUSE_RELEASE:
212 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
213 InsetBoxMailer(*this).showDialog(&cur.bv());
216 InsetCollapsable::doDispatch(cur, cmd);
220 InsetCollapsable::doDispatch(cur, cmd);
226 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
227 FuncStatus & flag) const
229 switch (cmd.action) {
231 case LFUN_INSET_MODIFY:
232 case LFUN_INSET_DIALOG_UPDATE:
235 case LFUN_BREAK_PARAGRAPH:
236 if (params_.inner_box) {
237 return InsetCollapsable::getStatus(cur, cmd, flag);
244 return InsetCollapsable::getStatus(cur, cmd, flag);
249 int InsetBox::latex(Buffer const & buf, odocstream & os,
250 OutputParams const & runparams) const
252 BoxType btype = boxtranslator().find(params_.type);
254 string width_string = params_.width.asLatexString();
255 bool stdwidth(false);
256 if (params_.inner_box &&
257 (width_string.find("1.0\\columnwidth") != string::npos
258 || width_string.find("1.0\\textwidth") != string::npos)) {
264 width_string += " - 2\\fboxsep - 2\\fboxrule";
267 width_string += " - 2\\fboxsep - 0.8pt";
270 width_string += " - 2\\fboxsep - 1.6pt";
273 // Shadow falls outside right margin... opinions?
274 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
277 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
284 // Adapt to column/text width correctly also if paragraphs indented:
293 if (!params_.inner_box) {
295 // Special widths, see usrguide §3.5
297 if (params_.special != "none") {
298 os << "[" << params_.width.value()
299 << '\\' << lyx::from_utf8(params_.special)
302 os << '[' << lyx::from_ascii(width_string)
304 if (params_.hor_pos != 'c')
305 os << "[" << params_.hor_pos << "]";
317 os << "\\shadowbox{";
320 os << "\\doublebox{";
324 if (params_.inner_box) {
325 if (params_.use_parbox)
328 os << "\\begin{minipage}";
330 os << "[" << params_.pos << "]";
331 if (params_.height_special == "none") {
333 os << '[' << lyx::from_ascii(params_.height.asLatexString())
338 os << "[" << params_.height.value()
339 << '\\' << lyx::from_utf8(params_.height_special)
342 if (params_.inner_pos != params_.pos)
343 os << "[" << params_.inner_pos << "]";
346 os << '{' << lyx::from_ascii(width_string) << '}';
348 if (params_.use_parbox)
354 i += InsetText::latex(buf, os, runparams);
356 if (params_.inner_box) {
357 if (params_.use_parbox)
360 os << "%\n\\end{minipage}";
367 if (!params_.inner_box)
368 os << "}"; // for makebox
386 int InsetBox::docbook(Buffer const & buf, lyx::odocstream & os,
387 OutputParams const & runparams) const
389 return InsetText::docbook(buf, os, runparams);
393 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
394 OutputParams const & runparams) const
396 BoxType const btype = boxtranslator().find(params_.type);
399 case Frameless: break;
400 case Boxed: os << "["; break;
401 case ovalbox: os << "("; break;
402 case Ovalbox: os << "(("; break;
403 case Shadowbox: os << "["; break;
404 case Doublebox: os << "[["; break;
407 int i = InsetText::plaintext(buf, os, runparams);
410 case Frameless: break;
411 case Boxed: os << "]"; break;
412 case ovalbox: os << ")"; break;
413 case Ovalbox: os << "))"; break;
414 case Shadowbox: os << "]/"; break;
415 case Doublebox: os << "]]"; break;
422 void InsetBox::validate(LaTeXFeatures & features) const
424 features.require("calc");
425 BoxType btype = boxtranslator().find(params_.type);
434 features.require("fancybox");
437 InsetText::validate(features);
441 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
446 string const InsetBoxMailer::name_ = "box";
449 string const InsetBoxMailer::inset2string(Buffer const &) const
451 return params2string(inset_.params());
455 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
458 data << "box" << ' ';
464 void InsetBoxMailer::string2params(string const & in,
465 InsetBoxParams & params)
467 params = InsetBoxParams(string());
471 istringstream data(in);
477 if (!lex || name != name_)
478 return print_mailer_error("InsetBoxMailer", in, 1, name_);
480 // This is part of the inset proper that is usually swallowed
481 // by LyXText::readInset
484 if (!lex || id != "Box")
485 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
491 InsetBoxParams::InsetBoxParams(string const & label)
495 width(LyXLength("100col%")),
500 height(LyXLength("1in")),
501 height_special("totalheight") // default is 1\\totalheight
505 void InsetBoxParams::write(ostream & os) const
507 os << "Box " << type << "\n";
508 os << "position \"" << pos << "\"\n";
509 os << "hor_pos \"" << hor_pos << "\"\n";
510 os << "has_inner_box " << inner_box << "\n";
511 os << "inner_pos \"" << inner_pos << "\"\n";
512 os << "use_parbox " << use_parbox << "\n";
513 os << "width \"" << width.asString() << "\"\n";
514 os << "special \"" << special << "\"\n";
515 os << "height \"" << height.asString() << "\"\n";
516 os << "height_special \"" << height_special << "\"\n";
520 void InsetBoxParams::read(LyXLex & lex)
527 type = lex.getString();
533 token = lex.getString();
534 if (token == "position") {
536 // The [0] is needed. We need the first and only char in
538 pos = lex.getString()[0];
540 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
541 lex.pushToken(token);
546 token = lex.getString();
547 if (token == "hor_pos") {
549 hor_pos = lex.getString()[0];
551 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
552 lex.pushToken(token);
557 token = lex.getString();
558 if (token == "has_inner_box") {
560 inner_box = lex.getInteger();
562 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
563 lex.pushToken(token);
569 token = lex.getString();
570 if (token == "inner_pos") {
572 inner_pos = lex.getString()[0];
574 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
576 lex.pushToken(token);
581 token = lex.getString();
582 if (token == "use_parbox") {
584 use_parbox = lex.getInteger();
586 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
587 lex.pushToken(token);
592 token = lex.getString();
593 if (token == "width") {
595 width = LyXLength(lex.getString());
597 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
598 lex.pushToken(token);
603 token = lex.getString();
604 if (token == "special") {
606 special = lex.getString();
608 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
609 lex.pushToken(token);
614 token = lex.getString();
615 if (token == "height") {
617 height = LyXLength(lex.getString());
619 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
620 lex.pushToken(token);
625 token = lex.getString();
626 if (token == "height_special") {
628 height_special = lex.getString();
630 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
631 lex.pushToken(token);