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 "Paragraph.h"
29 #include "TextMetrics.h"
31 #include "support/Translator.h"
40 using std::istringstream;
42 using std::ostringstream;
48 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
49 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
51 BoxTranslator const init_boxtranslator()
53 BoxTranslator translator("Boxed", InsetBox::Boxed);
54 translator.addPair("Frameless", InsetBox::Frameless);
55 translator.addPair("ovalbox", InsetBox::ovalbox);
56 translator.addPair("Ovalbox", InsetBox::Ovalbox);
57 translator.addPair("Shadowbox", InsetBox::Shadowbox);
58 translator.addPair("Doublebox",InsetBox::Doublebox);
63 BoxTranslatorLoc const init_boxtranslator_loc()
65 BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
66 translator.addPair(_("Frameless"), InsetBox::Frameless);
67 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
68 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
69 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
70 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
75 BoxTranslator const & boxtranslator()
77 static BoxTranslator translator = init_boxtranslator();
82 BoxTranslatorLoc const & boxtranslator_loc()
84 static BoxTranslatorLoc translator = init_boxtranslator_loc();
97 InsetBox::InsetBox(BufferParams const & bp, string const & label)
98 : InsetCollapsable(bp), params_(label)
104 InsetBox::InsetBox(InsetBox const & in)
105 : InsetCollapsable(in), params_(in.params_)
111 InsetBox::~InsetBox()
113 InsetBoxMailer(*this).hideDialog();
117 auto_ptr<Inset> InsetBox::doClone() const
119 return auto_ptr<Inset>(new InsetBox(*this));
123 docstring const InsetBox::editMessage() const
125 return _("Opened Box Inset");
129 void InsetBox::write(Buffer const & buf, ostream & os) const
132 InsetCollapsable::write(buf, os);
136 void InsetBox::read(Buffer const & buf, Lexer & lex)
139 InsetCollapsable::read(buf, lex);
144 void InsetBox::setButtonLabel()
146 LyXFont font(LyXFont::ALL_SANE);
150 BoxType btype = boxtranslator().find(params_.type);
155 if (btype == Frameless) {
156 if (params_.use_parbox)
157 label += _("Parbox");
159 label += _("Minipage");
161 label += boxtranslator_loc().find(btype);
166 font.setColor(Color::foreground);
167 setBackgroundColor(Color::background);
172 bool InsetBox::hasFixedWidth() const
174 return params_.inner_box || params_.special != "width";
178 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
181 // first round in order to know the minimum size.
182 InsetCollapsable::metrics(mi, dim);
183 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
186 std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET),
187 params_.width.inPixels(m.base.textwidth));
188 InsetCollapsable::metrics(mi, dim);
189 bool const changed = dim_ != dim;
195 bool InsetBox::forceDefaultParagraphs(idx_type) const
197 return !params_.inner_box;
201 bool InsetBox::showInsetDialog(BufferView * bv) const
203 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
208 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
210 switch (cmd.action) {
212 case LFUN_INSET_MODIFY: {
213 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
214 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
219 case LFUN_INSET_DIALOG_UPDATE:
220 InsetBoxMailer(*this).updateDialog(&cur.bv());
223 case LFUN_MOUSE_RELEASE:
224 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
225 InsetBoxMailer(*this).showDialog(&cur.bv());
228 InsetCollapsable::doDispatch(cur, cmd);
232 InsetCollapsable::doDispatch(cur, cmd);
238 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
239 FuncStatus & flag) const
241 switch (cmd.action) {
243 case LFUN_INSET_MODIFY:
244 case LFUN_INSET_DIALOG_UPDATE:
247 case LFUN_BREAK_PARAGRAPH:
248 if (params_.inner_box) {
249 return InsetCollapsable::getStatus(cur, cmd, flag);
256 return InsetCollapsable::getStatus(cur, cmd, flag);
261 int InsetBox::latex(Buffer const & buf, odocstream & os,
262 OutputParams const & runparams) const
264 BoxType btype = boxtranslator().find(params_.type);
266 string width_string = params_.width.asLatexString();
267 bool stdwidth(false);
268 if (params_.inner_box &&
269 (width_string.find("1.0\\columnwidth") != string::npos
270 || width_string.find("1.0\\textwidth") != string::npos)) {
276 width_string += " - 2\\fboxsep - 2\\fboxrule";
279 width_string += " - 2\\fboxsep - 0.8pt";
282 width_string += " - 2\\fboxsep - 1.6pt";
285 // Shadow falls outside right margin... opinions?
286 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
289 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
296 // Adapt to column/text width correctly also if paragraphs indented:
305 if (!params_.inner_box) {
307 // Special widths, see usrguide §3.5
309 if (params_.special != "none") {
310 os << "[" << params_.width.value()
311 << '\\' << from_utf8(params_.special)
314 os << '[' << from_ascii(width_string)
316 if (params_.hor_pos != 'c')
317 os << "[" << params_.hor_pos << "]";
329 os << "\\shadowbox{";
332 os << "\\doublebox{";
336 if (params_.inner_box) {
337 if (params_.use_parbox)
340 os << "\\begin{minipage}";
342 os << "[" << params_.pos << "]";
343 if (params_.height_special == "none") {
345 os << '[' << from_ascii(params_.height.asLatexString())
350 os << "[" << params_.height.value()
351 << '\\' << from_utf8(params_.height_special)
354 if (params_.inner_pos != params_.pos)
355 os << "[" << params_.inner_pos << "]";
358 os << '{' << from_ascii(width_string) << '}';
360 if (params_.use_parbox)
366 i += InsetText::latex(buf, os, runparams);
368 if (params_.inner_box) {
369 if (params_.use_parbox)
372 os << "%\n\\end{minipage}";
379 if (!params_.inner_box)
380 os << "}"; // for makebox
398 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
399 OutputParams const & runparams) const
401 BoxType const btype = boxtranslator().find(params_.type);
404 case Frameless: break;
405 case Boxed: os << "[\n"; break;
406 case ovalbox: os << "(\n"; break;
407 case Ovalbox: os << "((\n"; break;
408 case Shadowbox: os << "[/\n"; break;
409 case Doublebox: os << "[[\n"; break;
412 InsetText::plaintext(buf, os, runparams);
416 case Frameless: os << "\n"; break;
417 case Boxed: os << "\n]"; len = 1; break;
418 case ovalbox: os << "\n)"; len = 1; break;
419 case Ovalbox: os << "\n))"; len = 2; break;
420 case Shadowbox: os << "\n/]"; len = 2; break;
421 case Doublebox: os << "\n]]"; len = 2; break;
424 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
428 int InsetBox::docbook(Buffer const & buf, odocstream & os,
429 OutputParams const & runparams) const
431 return InsetText::docbook(buf, os, runparams);
435 void InsetBox::validate(LaTeXFeatures & features) const
437 BoxType btype = boxtranslator().find(params_.type);
442 features.require("calc");
448 features.require("calc");
449 features.require("fancybox");
452 InsetText::validate(features);
456 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
461 string const InsetBoxMailer::name_ = "box";
464 string const InsetBoxMailer::inset2string(Buffer const &) const
466 return params2string(inset_.params());
470 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
473 data << "box" << ' ';
479 void InsetBoxMailer::string2params(string const & in,
480 InsetBoxParams & params)
482 params = InsetBoxParams(string());
486 istringstream data(in);
492 if (!lex || name != name_)
493 return print_mailer_error("InsetBoxMailer", in, 1, name_);
495 // This is part of the inset proper that is usually swallowed
496 // by LyXText::readInset
499 if (!lex || id != "Box")
500 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
506 InsetBoxParams::InsetBoxParams(string const & label)
510 width(Length("100col%")),
515 height(Length("1in")),
516 height_special("totalheight") // default is 1\\totalheight
520 void InsetBoxParams::write(ostream & os) const
522 os << "Box " << type << "\n";
523 os << "position \"" << pos << "\"\n";
524 os << "hor_pos \"" << hor_pos << "\"\n";
525 os << "has_inner_box " << inner_box << "\n";
526 os << "inner_pos \"" << inner_pos << "\"\n";
527 os << "use_parbox " << use_parbox << "\n";
528 os << "width \"" << width.asString() << "\"\n";
529 os << "special \"" << special << "\"\n";
530 os << "height \"" << height.asString() << "\"\n";
531 os << "height_special \"" << height_special << "\"\n";
535 void InsetBoxParams::read(Lexer & lex)
541 type = lex.getString();
548 token = lex.getString();
551 if (token == "position") {
553 // The [0] is needed. We need the first and only char in
555 pos = lex.getString()[0];
557 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
558 lex.pushToken(token);
562 token = lex.getString();
565 if (token == "hor_pos") {
567 hor_pos = lex.getString()[0];
569 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
570 lex.pushToken(token);
574 token = lex.getString();
577 if (token == "has_inner_box") {
579 inner_box = lex.getInteger();
581 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
582 lex.pushToken(token);
586 token = lex.getString();
589 if (token == "inner_pos") {
591 inner_pos = lex.getString()[0];
593 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
595 lex.pushToken(token);
599 token = lex.getString();
602 if (token == "use_parbox") {
604 use_parbox = lex.getInteger();
606 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
607 lex.pushToken(token);
611 token = lex.getString();
614 if (token == "width") {
616 width = Length(lex.getString());
618 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
619 lex.pushToken(token);
623 token = lex.getString();
626 if (token == "special") {
628 special = lex.getString();
630 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
631 lex.pushToken(token);
635 token = lex.getString();
638 if (token == "height") {
640 height = Length(lex.getString());
642 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
643 lex.pushToken(token);
647 token = lex.getString();
650 if (token == "height_special") {
652 height_special = lex.getString();
654 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
655 lex.pushToken(token);