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 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(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, odocstream & 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
296 if (params_.special != "none") {
297 os << "[" << params_.width.value()
298 << '\\' << from_utf8(params_.special)
301 os << '[' << from_ascii(width_string)
303 if (params_.hor_pos != 'c')
304 os << "[" << params_.hor_pos << "]";
316 os << "\\shadowbox{";
319 os << "\\doublebox{";
323 if (params_.inner_box) {
324 if (params_.use_parbox)
327 os << "\\begin{minipage}";
329 os << "[" << params_.pos << "]";
330 if (params_.height_special == "none") {
332 os << '[' << from_ascii(params_.height.asLatexString())
337 os << "[" << params_.height.value()
338 << '\\' << from_utf8(params_.height_special)
341 if (params_.inner_pos != params_.pos)
342 os << "[" << params_.inner_pos << "]";
345 os << '{' << from_ascii(width_string) << '}';
347 if (params_.use_parbox)
353 i += InsetText::latex(buf, os, runparams);
355 if (params_.inner_box) {
356 if (params_.use_parbox)
359 os << "%\n\\end{minipage}";
366 if (!params_.inner_box)
367 os << "}"; // for makebox
385 int InsetBox::docbook(Buffer const & buf, odocstream & os,
386 OutputParams const & runparams) const
388 return InsetText::docbook(buf, os, runparams);
392 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
393 OutputParams const & runparams) const
395 BoxType const btype = boxtranslator().find(params_.type);
398 case Frameless: break;
399 case Boxed: os << "["; break;
400 case ovalbox: os << "("; break;
401 case Ovalbox: os << "(("; break;
402 case Shadowbox: os << "["; break;
403 case Doublebox: os << "[["; break;
406 int i = InsetText::plaintext(buf, os, runparams);
409 case Frameless: break;
410 case Boxed: os << "]"; break;
411 case ovalbox: os << ")"; break;
412 case Ovalbox: os << "))"; break;
413 case Shadowbox: os << "]/"; break;
414 case Doublebox: os << "]]"; break;
421 void InsetBox::validate(LaTeXFeatures & features) const
423 features.require("calc");
424 BoxType btype = boxtranslator().find(params_.type);
433 features.require("fancybox");
436 InsetText::validate(features);
440 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
445 string const InsetBoxMailer::name_ = "box";
448 string const InsetBoxMailer::inset2string(Buffer const &) const
450 return params2string(inset_.params());
454 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
457 data << "box" << ' ';
463 void InsetBoxMailer::string2params(string const & in,
464 InsetBoxParams & params)
466 params = InsetBoxParams(string());
470 istringstream data(in);
476 if (!lex || name != name_)
477 return print_mailer_error("InsetBoxMailer", in, 1, name_);
479 // This is part of the inset proper that is usually swallowed
480 // by LyXText::readInset
483 if (!lex || id != "Box")
484 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
490 InsetBoxParams::InsetBoxParams(string const & label)
494 width(LyXLength("100col%")),
499 height(LyXLength("1in")),
500 height_special("totalheight") // default is 1\\totalheight
504 void InsetBoxParams::write(ostream & os) const
506 os << "Box " << type << "\n";
507 os << "position \"" << pos << "\"\n";
508 os << "hor_pos \"" << hor_pos << "\"\n";
509 os << "has_inner_box " << inner_box << "\n";
510 os << "inner_pos \"" << inner_pos << "\"\n";
511 os << "use_parbox " << use_parbox << "\n";
512 os << "width \"" << width.asString() << "\"\n";
513 os << "special \"" << special << "\"\n";
514 os << "height \"" << height.asString() << "\"\n";
515 os << "height_special \"" << height_special << "\"\n";
519 void InsetBoxParams::read(LyXLex & lex)
526 type = lex.getString();
532 token = lex.getString();
533 if (token == "position") {
535 // The [0] is needed. We need the first and only char in
537 pos = lex.getString()[0];
539 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
540 lex.pushToken(token);
545 token = lex.getString();
546 if (token == "hor_pos") {
548 hor_pos = lex.getString()[0];
550 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
551 lex.pushToken(token);
556 token = lex.getString();
557 if (token == "has_inner_box") {
559 inner_box = lex.getInteger();
561 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
562 lex.pushToken(token);
568 token = lex.getString();
569 if (token == "inner_pos") {
571 inner_pos = lex.getString()[0];
573 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
575 lex.pushToken(token);
580 token = lex.getString();
581 if (token == "use_parbox") {
583 use_parbox = lex.getInteger();
585 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
586 lex.pushToken(token);
591 token = lex.getString();
592 if (token == "width") {
594 width = LyXLength(lex.getString());
596 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
597 lex.pushToken(token);
602 token = lex.getString();
603 if (token == "special") {
605 special = lex.getString();
607 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
608 lex.pushToken(token);
613 token = lex.getString();
614 if (token == "height") {
616 height = LyXLength(lex.getString());
618 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
619 lex.pushToken(token);
624 token = lex.getString();
625 if (token == "height_special") {
627 height_special = lex.getString();
629 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
630 lex.pushToken(token);