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"
37 using std::istringstream;
39 using std::ostringstream;
45 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
47 BoxTranslator const init_boxtranslator()
49 BoxTranslator translator("Boxed", InsetBox::Boxed);
50 translator.addPair("Frameless", InsetBox::Frameless);
51 translator.addPair("ovalbox", InsetBox::ovalbox);
52 translator.addPair("Ovalbox", InsetBox::Ovalbox);
53 translator.addPair("Shadowbox", InsetBox::Shadowbox);
54 translator.addPair("Doublebox",InsetBox::Doublebox);
59 BoxTranslator const init_boxtranslator_loc()
62 BoxTranslator translator(lyx::to_utf8(_("Boxed")), InsetBox::Boxed);
63 translator.addPair(lyx::to_utf8(_("Frameless")), InsetBox::Frameless);
64 translator.addPair(lyx::to_utf8(_("ovalbox")), InsetBox::ovalbox);
65 translator.addPair(lyx::to_utf8(_("Ovalbox")), InsetBox::Ovalbox);
66 translator.addPair(lyx::to_utf8(_("Shadowbox")), InsetBox::Shadowbox);
67 translator.addPair(lyx::to_utf8(_("Doublebox")), InsetBox::Doublebox);
72 BoxTranslator const & boxtranslator()
74 static BoxTranslator translator = init_boxtranslator();
79 BoxTranslator const & boxtranslator_loc()
81 static BoxTranslator translator = init_boxtranslator_loc();
95 InsetBox::InsetBox(BufferParams const & bp, string const & label)
96 : InsetCollapsable(bp), params_(label)
102 InsetBox::InsetBox(InsetBox const & in)
103 : InsetCollapsable(in), params_(in.params_)
109 InsetBox::~InsetBox()
111 InsetBoxMailer(*this).hideDialog();
115 auto_ptr<InsetBase> InsetBox::doClone() const
117 return auto_ptr<InsetBase>(new InsetBox(*this));
121 docstring const InsetBox::editMessage() const
123 return _("Opened Box Inset");
127 void InsetBox::write(Buffer const & buf, ostream & os) const
130 InsetCollapsable::write(buf, os);
134 void InsetBox::read(Buffer const & buf, LyXLex & lex)
137 InsetCollapsable::read(buf, lex);
142 void InsetBox::setButtonLabel()
144 LyXFont font(LyXFont::ALL_SANE);
148 BoxType btype = boxtranslator().find(params_.type);
154 if (btype == Frameless) {
155 if (params_.use_parbox)
156 label += _("Parbox");
158 label += _("Minipage");
161 label += lyx::from_utf8(boxtranslator_loc().find(btype));
166 font.setColor(LColor::foreground);
167 setBackgroundColor(LColor::background);
172 void 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);
182 bool InsetBox::forceDefaultParagraphs(idx_type) const
184 return !params_.inner_box;
188 bool InsetBox::showInsetDialog(BufferView * bv) const
190 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
195 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
197 switch (cmd.action) {
199 case LFUN_INSET_MODIFY: {
200 lyxerr << "InsetBox::dispatch MODIFY" << endl;
201 InsetBoxMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
206 case LFUN_INSET_DIALOG_UPDATE:
207 InsetBoxMailer(*this).updateDialog(&cur.bv());
210 case LFUN_MOUSE_RELEASE:
211 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
212 InsetBoxMailer(*this).showDialog(&cur.bv());
215 InsetCollapsable::doDispatch(cur, cmd);
219 InsetCollapsable::doDispatch(cur, cmd);
225 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
226 FuncStatus & flag) const
228 switch (cmd.action) {
230 case LFUN_INSET_MODIFY:
231 case LFUN_INSET_DIALOG_UPDATE:
234 case LFUN_BREAK_PARAGRAPH:
235 if (params_.inner_box) {
236 return InsetCollapsable::getStatus(cur, cmd, flag);
243 return InsetCollapsable::getStatus(cur, cmd, flag);
248 int InsetBox::latex(Buffer const & buf, ostream & os,
249 OutputParams const & runparams) const
251 BoxType btype = boxtranslator().find(params_.type);
253 string width_string = params_.width.asLatexString();
254 bool stdwidth(false);
255 if (params_.inner_box &&
256 (width_string.find("1.0\\columnwidth") != string::npos
257 || width_string.find("1.0\\textwidth") != string::npos)) {
263 width_string += " - 2\\fboxsep - 2\\fboxrule";
266 width_string += " - 2\\fboxsep - 0.8pt";
269 width_string += " - 2\\fboxsep - 1.6pt";
272 // Shadow falls outside right margin... opinions?
273 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
276 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
283 // Adapt to column/text width correctly also if paragraphs indented:
292 if (!params_.inner_box) {
294 // Special widths, see usrguide §3.5
295 if (params_.special != "none") {
296 os << "[" << params_.width.value()
297 << "\\" << params_.special << "]";
299 os << "[" << width_string << "]";
300 if (params_.hor_pos != 'c')
301 os << "[" << params_.hor_pos << "]";
313 os << "\\shadowbox{";
316 os << "\\doublebox{";
320 if (params_.inner_box) {
321 if (params_.use_parbox)
324 os << "\\begin{minipage}";
326 os << "[" << params_.pos << "]";
327 if (params_.height_special == "none") {
328 os << "[" << params_.height.asLatexString() << "]";
331 os << "[" << params_.height.value()
332 << "\\" << params_.height_special << "]";
334 if (params_.inner_pos != params_.pos)
335 os << "[" << params_.inner_pos << "]";
337 os << "{" << width_string << "}";
339 if (params_.use_parbox)
345 i += InsetText::latex(buf, os, runparams);
347 if (params_.inner_box) {
348 if (params_.use_parbox)
351 os << "%\n\\end{minipage}";
358 if (!params_.inner_box)
359 os << "}"; // for makebox
377 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
378 OutputParams const & runparams) const
380 return InsetText::docbook(buf, os, runparams);
384 int InsetBox::plaintext(Buffer const & buf, lyx::odocstream & os,
385 OutputParams const & runparams) const
387 BoxType const btype = boxtranslator().find(params_.type);
390 case Frameless: break;
391 case Boxed: os << "["; break;
392 case ovalbox: os << "("; break;
393 case Ovalbox: os << "(("; break;
394 case Shadowbox: os << "["; break;
395 case Doublebox: os << "[["; break;
398 int i = InsetText::plaintext(buf, os, runparams);
401 case Frameless: break;
402 case Boxed: os << "]"; break;
403 case ovalbox: os << ")"; break;
404 case Ovalbox: os << "))"; break;
405 case Shadowbox: os << "]/"; break;
406 case Doublebox: os << "]]"; break;
413 void InsetBox::validate(LaTeXFeatures & features) const
415 features.require("calc");
416 BoxType btype = boxtranslator().find(params_.type);
425 features.require("fancybox");
428 InsetText::validate(features);
432 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
437 string const InsetBoxMailer::name_ = "box";
440 string const InsetBoxMailer::inset2string(Buffer const &) const
442 return params2string(inset_.params());
446 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
449 data << "box" << ' ';
455 void InsetBoxMailer::string2params(string const & in,
456 InsetBoxParams & params)
458 params = InsetBoxParams(string());
462 istringstream data(in);
468 if (!lex || name != name_)
469 return print_mailer_error("InsetBoxMailer", in, 1, name_);
471 // This is part of the inset proper that is usually swallowed
472 // by LyXText::readInset
475 if (!lex || id != "Box")
476 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
482 InsetBoxParams::InsetBoxParams(string const & label)
486 width(LyXLength("100col%")),
491 height(LyXLength("1in")),
492 height_special("totalheight") // default is 1\\totalheight
496 void InsetBoxParams::write(ostream & os) const
498 os << "Box " << type << "\n";
499 os << "position \"" << pos << "\"\n";
500 os << "hor_pos \"" << hor_pos << "\"\n";
501 os << "has_inner_box " << inner_box << "\n";
502 os << "inner_pos \"" << inner_pos << "\"\n";
503 os << "use_parbox " << use_parbox << "\n";
504 os << "width \"" << width.asString() << "\"\n";
505 os << "special \"" << special << "\"\n";
506 os << "height \"" << height.asString() << "\"\n";
507 os << "height_special \"" << height_special << "\"\n";
511 void InsetBoxParams::read(LyXLex & lex)
518 type = lex.getString();
524 token = lex.getString();
525 if (token == "position") {
527 // The [0] is needed. We need the first and only char in
529 pos = lex.getString()[0];
531 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
532 lex.pushToken(token);
537 token = lex.getString();
538 if (token == "hor_pos") {
540 hor_pos = lex.getString()[0];
542 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
543 lex.pushToken(token);
548 token = lex.getString();
549 if (token == "has_inner_box") {
551 inner_box = lex.getInteger();
553 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
554 lex.pushToken(token);
560 token = lex.getString();
561 if (token == "inner_pos") {
563 inner_pos = lex.getString()[0];
565 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
567 lex.pushToken(token);
572 token = lex.getString();
573 if (token == "use_parbox") {
575 use_parbox = lex.getInteger();
577 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
578 lex.pushToken(token);
583 token = lex.getString();
584 if (token == "width") {
586 width = LyXLength(lex.getString());
588 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
589 lex.pushToken(token);
594 token = lex.getString();
595 if (token == "special") {
597 special = lex.getString();
599 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
600 lex.pushToken(token);
605 token = lex.getString();
606 if (token == "height") {
608 height = LyXLength(lex.getString());
610 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
611 lex.pushToken(token);
616 token = lex.getString();
617 if (token == "height_special") {
619 height_special = lex.getString();
621 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
622 lex.pushToken(token);