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"
19 #include "DispatchResult.h"
21 #include "FuncStatus.h"
22 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
28 #include "TextMetrics.h"
30 #include "support/Translator.h"
39 using std::istringstream;
41 using std::ostringstream;
47 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
48 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
50 BoxTranslator const init_boxtranslator()
52 BoxTranslator translator("Boxed", InsetBox::Boxed);
53 translator.addPair("Frameless", InsetBox::Frameless);
54 translator.addPair("ovalbox", InsetBox::ovalbox);
55 translator.addPair("Ovalbox", InsetBox::Ovalbox);
56 translator.addPair("Shadowbox", InsetBox::Shadowbox);
57 translator.addPair("Doublebox",InsetBox::Doublebox);
62 BoxTranslatorLoc const init_boxtranslator_loc()
64 BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
65 translator.addPair(_("Frameless"), InsetBox::Frameless);
66 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
67 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
68 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
69 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
74 BoxTranslator const & boxtranslator()
76 static BoxTranslator translator = init_boxtranslator();
81 BoxTranslatorLoc const & boxtranslator_loc()
83 static BoxTranslatorLoc 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<Inset> InsetBox::doClone() const
118 return auto_ptr<Inset>(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, Lexer & lex)
138 InsetCollapsable::read(buf, lex);
143 void InsetBox::setButtonLabel()
145 Font font(Font::ALL_SANE);
149 BoxType btype = boxtranslator().find(params_.type);
154 if (btype == Frameless) {
155 if (params_.use_parbox)
156 label += _("Parbox");
158 label += _("Minipage");
160 label += boxtranslator_loc().find(btype);
165 font.setColor(Color::foreground);
170 bool InsetBox::hasFixedWidth() const
172 return params_.inner_box || params_.special != "width";
176 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
179 // first round in order to know the minimum size.
180 InsetCollapsable::metrics(mi, dim);
181 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
184 std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET),
185 params_.width.inPixels(m.base.textwidth));
186 InsetCollapsable::metrics(mi, dim);
187 bool const changed = dim_ != dim;
193 bool InsetBox::forceDefaultParagraphs(idx_type) const
195 return !params_.inner_box;
199 bool InsetBox::showInsetDialog(BufferView * bv) const
201 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
206 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
208 switch (cmd.action) {
210 case LFUN_INSET_MODIFY: {
211 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
212 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
217 case LFUN_INSET_DIALOG_UPDATE:
218 InsetBoxMailer(*this).updateDialog(&cur.bv());
221 case LFUN_MOUSE_RELEASE:
222 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
223 InsetBoxMailer(*this).showDialog(&cur.bv());
226 InsetCollapsable::doDispatch(cur, cmd);
230 InsetCollapsable::doDispatch(cur, cmd);
236 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
237 FuncStatus & flag) const
239 switch (cmd.action) {
241 case LFUN_INSET_MODIFY:
242 case LFUN_INSET_DIALOG_UPDATE:
245 case LFUN_BREAK_PARAGRAPH:
246 if (params_.inner_box) {
247 return InsetCollapsable::getStatus(cur, cmd, flag);
254 return InsetCollapsable::getStatus(cur, cmd, flag);
259 int InsetBox::latex(Buffer const & buf, odocstream & os,
260 OutputParams const & runparams) const
262 BoxType btype = boxtranslator().find(params_.type);
264 string width_string = params_.width.asLatexString();
265 bool stdwidth(false);
266 if (params_.inner_box &&
267 (width_string.find("1.0\\columnwidth") != string::npos
268 || width_string.find("1.0\\textwidth") != string::npos)) {
274 width_string += " - 2\\fboxsep - 2\\fboxrule";
277 width_string += " - 2\\fboxsep - 0.8pt";
280 width_string += " - 2\\fboxsep - 1.6pt";
283 // Shadow falls outside right margin... opinions?
284 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
287 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
294 // Adapt to column/text width correctly also if paragraphs indented:
303 if (!params_.inner_box) {
305 // Special widths, see usrguide §3.5
307 if (params_.special != "none") {
308 os << "[" << params_.width.value()
309 << '\\' << from_utf8(params_.special)
312 os << '[' << from_ascii(width_string)
314 if (params_.hor_pos != 'c')
315 os << "[" << params_.hor_pos << "]";
327 os << "\\shadowbox{";
330 os << "\\doublebox{";
334 if (params_.inner_box) {
335 if (params_.use_parbox)
338 os << "\\begin{minipage}";
340 os << "[" << params_.pos << "]";
341 if (params_.height_special == "none") {
343 os << '[' << from_ascii(params_.height.asLatexString())
348 os << "[" << params_.height.value()
349 << '\\' << from_utf8(params_.height_special)
352 if (params_.inner_pos != params_.pos)
353 os << "[" << params_.inner_pos << "]";
356 os << '{' << from_ascii(width_string) << '}';
358 if (params_.use_parbox)
364 i += InsetText::latex(buf, os, runparams);
366 if (params_.inner_box) {
367 if (params_.use_parbox)
370 os << "%\n\\end{minipage}";
377 if (!params_.inner_box)
378 os << "}"; // for makebox
396 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
397 OutputParams const & runparams) const
399 BoxType const btype = boxtranslator().find(params_.type);
402 case Frameless: break;
403 case Boxed: os << "[\n"; break;
404 case ovalbox: os << "(\n"; break;
405 case Ovalbox: os << "((\n"; break;
406 case Shadowbox: os << "[/\n"; break;
407 case Doublebox: os << "[[\n"; break;
410 InsetText::plaintext(buf, os, runparams);
414 case Frameless: os << "\n"; break;
415 case Boxed: os << "\n]"; len = 1; break;
416 case ovalbox: os << "\n)"; len = 1; break;
417 case Ovalbox: os << "\n))"; len = 2; break;
418 case Shadowbox: os << "\n/]"; len = 2; break;
419 case Doublebox: os << "\n]]"; len = 2; break;
422 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
426 int InsetBox::docbook(Buffer const & buf, odocstream & os,
427 OutputParams const & runparams) const
429 return InsetText::docbook(buf, os, runparams);
433 void InsetBox::validate(LaTeXFeatures & features) const
435 BoxType btype = boxtranslator().find(params_.type);
440 features.require("calc");
446 features.require("calc");
447 features.require("fancybox");
450 InsetText::validate(features);
454 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
459 string const InsetBoxMailer::name_ = "box";
462 string const InsetBoxMailer::inset2string(Buffer const &) const
464 return params2string(inset_.params());
468 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
471 data << "box" << ' ';
477 void InsetBoxMailer::string2params(string const & in,
478 InsetBoxParams & params)
480 params = InsetBoxParams(string());
484 istringstream data(in);
490 if (!lex || name != name_)
491 return print_mailer_error("InsetBoxMailer", in, 1, name_);
493 // This is part of the inset proper that is usually swallowed
494 // by Text::readInset
497 if (!lex || id != "Box")
498 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
504 InsetBoxParams::InsetBoxParams(string const & label)
508 width(Length("100col%")),
513 height(Length("1in")),
514 height_special("totalheight") // default is 1\\totalheight
518 void InsetBoxParams::write(ostream & os) const
520 os << "Box " << type << "\n";
521 os << "position \"" << pos << "\"\n";
522 os << "hor_pos \"" << hor_pos << "\"\n";
523 os << "has_inner_box " << inner_box << "\n";
524 os << "inner_pos \"" << inner_pos << "\"\n";
525 os << "use_parbox " << use_parbox << "\n";
526 os << "width \"" << width.asString() << "\"\n";
527 os << "special \"" << special << "\"\n";
528 os << "height \"" << height.asString() << "\"\n";
529 os << "height_special \"" << height_special << "\"\n";
533 void InsetBoxParams::read(Lexer & lex)
539 type = lex.getString();
546 token = lex.getString();
549 if (token == "position") {
551 // The [0] is needed. We need the first and only char in
553 pos = lex.getString()[0];
555 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
556 lex.pushToken(token);
560 token = lex.getString();
563 if (token == "hor_pos") {
565 hor_pos = lex.getString()[0];
567 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
568 lex.pushToken(token);
572 token = lex.getString();
575 if (token == "has_inner_box") {
577 inner_box = lex.getInteger();
579 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
580 lex.pushToken(token);
584 token = lex.getString();
587 if (token == "inner_pos") {
589 inner_pos = lex.getString()[0];
591 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
593 lex.pushToken(token);
597 token = lex.getString();
600 if (token == "use_parbox") {
602 use_parbox = lex.getInteger();
604 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
605 lex.pushToken(token);
609 token = lex.getString();
612 if (token == "width") {
614 width = Length(lex.getString());
616 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
617 lex.pushToken(token);
621 token = lex.getString();
624 if (token == "special") {
626 special = lex.getString();
628 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
629 lex.pushToken(token);
633 token = lex.getString();
636 if (token == "height") {
638 height = Length(lex.getString());
640 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
641 lex.pushToken(token);
645 token = lex.getString();
648 if (token == "height_special") {
650 height_special = lex.getString();
652 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
653 lex.pushToken(token);