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 Font font(Font::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);
171 bool InsetBox::hasFixedWidth() const
173 return params_.inner_box || params_.special != "width";
177 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
180 // first round in order to know the minimum size.
181 InsetCollapsable::metrics(mi, dim);
182 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
185 std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET),
186 params_.width.inPixels(m.base.textwidth));
187 InsetCollapsable::metrics(mi, dim);
188 bool const changed = dim_ != dim;
194 bool InsetBox::forceDefaultParagraphs(idx_type) const
196 return !params_.inner_box;
200 bool InsetBox::showInsetDialog(BufferView * bv) const
202 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
207 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
209 switch (cmd.action) {
211 case LFUN_INSET_MODIFY: {
212 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
213 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
218 case LFUN_INSET_DIALOG_UPDATE:
219 InsetBoxMailer(*this).updateDialog(&cur.bv());
222 case LFUN_MOUSE_RELEASE:
223 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
224 InsetBoxMailer(*this).showDialog(&cur.bv());
227 InsetCollapsable::doDispatch(cur, cmd);
231 InsetCollapsable::doDispatch(cur, cmd);
237 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
238 FuncStatus & flag) const
240 switch (cmd.action) {
242 case LFUN_INSET_MODIFY:
243 case LFUN_INSET_DIALOG_UPDATE:
246 case LFUN_BREAK_PARAGRAPH:
247 if (params_.inner_box) {
248 return InsetCollapsable::getStatus(cur, cmd, flag);
255 return InsetCollapsable::getStatus(cur, cmd, flag);
260 int InsetBox::latex(Buffer const & buf, odocstream & os,
261 OutputParams const & runparams) const
263 BoxType btype = boxtranslator().find(params_.type);
265 string width_string = params_.width.asLatexString();
266 bool stdwidth(false);
267 if (params_.inner_box &&
268 (width_string.find("1.0\\columnwidth") != string::npos
269 || width_string.find("1.0\\textwidth") != string::npos)) {
275 width_string += " - 2\\fboxsep - 2\\fboxrule";
278 width_string += " - 2\\fboxsep - 0.8pt";
281 width_string += " - 2\\fboxsep - 1.6pt";
284 // Shadow falls outside right margin... opinions?
285 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
288 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
295 // Adapt to column/text width correctly also if paragraphs indented:
304 if (!params_.inner_box) {
306 // Special widths, see usrguide §3.5
308 if (params_.special != "none") {
309 os << "[" << params_.width.value()
310 << '\\' << from_utf8(params_.special)
313 os << '[' << from_ascii(width_string)
315 if (params_.hor_pos != 'c')
316 os << "[" << params_.hor_pos << "]";
328 os << "\\shadowbox{";
331 os << "\\doublebox{";
335 if (params_.inner_box) {
336 if (params_.use_parbox)
339 os << "\\begin{minipage}";
341 os << "[" << params_.pos << "]";
342 if (params_.height_special == "none") {
344 os << '[' << from_ascii(params_.height.asLatexString())
349 os << "[" << params_.height.value()
350 << '\\' << from_utf8(params_.height_special)
353 if (params_.inner_pos != params_.pos)
354 os << "[" << params_.inner_pos << "]";
357 os << '{' << from_ascii(width_string) << '}';
359 if (params_.use_parbox)
365 i += InsetText::latex(buf, os, runparams);
367 if (params_.inner_box) {
368 if (params_.use_parbox)
371 os << "%\n\\end{minipage}";
378 if (!params_.inner_box)
379 os << "}"; // for makebox
397 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
398 OutputParams const & runparams) const
400 BoxType const btype = boxtranslator().find(params_.type);
403 case Frameless: break;
404 case Boxed: os << "[\n"; break;
405 case ovalbox: os << "(\n"; break;
406 case Ovalbox: os << "((\n"; break;
407 case Shadowbox: os << "[/\n"; break;
408 case Doublebox: os << "[[\n"; break;
411 InsetText::plaintext(buf, os, runparams);
415 case Frameless: os << "\n"; break;
416 case Boxed: os << "\n]"; len = 1; break;
417 case ovalbox: os << "\n)"; len = 1; break;
418 case Ovalbox: os << "\n))"; len = 2; break;
419 case Shadowbox: os << "\n/]"; len = 2; break;
420 case Doublebox: os << "\n]]"; len = 2; break;
423 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
427 int InsetBox::docbook(Buffer const & buf, odocstream & os,
428 OutputParams const & runparams) const
430 return InsetText::docbook(buf, os, runparams);
434 void InsetBox::validate(LaTeXFeatures & features) const
436 BoxType btype = boxtranslator().find(params_.type);
441 features.require("calc");
447 features.require("calc");
448 features.require("fancybox");
451 InsetText::validate(features);
455 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
460 string const InsetBoxMailer::name_ = "box";
463 string const InsetBoxMailer::inset2string(Buffer const &) const
465 return params2string(inset_.params());
469 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
472 data << "box" << ' ';
478 void InsetBoxMailer::string2params(string const & in,
479 InsetBoxParams & params)
481 params = InsetBoxParams(string());
485 istringstream data(in);
491 if (!lex || name != name_)
492 return print_mailer_error("InsetBoxMailer", in, 1, name_);
494 // This is part of the inset proper that is usually swallowed
495 // by Text::readInset
498 if (!lex || id != "Box")
499 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
505 InsetBoxParams::InsetBoxParams(string const & label)
509 width(Length("100col%")),
514 height(Length("1in")),
515 height_special("totalheight") // default is 1\\totalheight
519 void InsetBoxParams::write(ostream & os) const
521 os << "Box " << type << "\n";
522 os << "position \"" << pos << "\"\n";
523 os << "hor_pos \"" << hor_pos << "\"\n";
524 os << "has_inner_box " << inner_box << "\n";
525 os << "inner_pos \"" << inner_pos << "\"\n";
526 os << "use_parbox " << use_parbox << "\n";
527 os << "width \"" << width.asString() << "\"\n";
528 os << "special \"" << special << "\"\n";
529 os << "height \"" << height.asString() << "\"\n";
530 os << "height_special \"" << height_special << "\"\n";
534 void InsetBoxParams::read(Lexer & lex)
540 type = lex.getString();
547 token = lex.getString();
550 if (token == "position") {
552 // The [0] is needed. We need the first and only char in
554 pos = lex.getString()[0];
556 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
557 lex.pushToken(token);
561 token = lex.getString();
564 if (token == "hor_pos") {
566 hor_pos = lex.getString()[0];
568 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
569 lex.pushToken(token);
573 token = lex.getString();
576 if (token == "has_inner_box") {
578 inner_box = lex.getInteger();
580 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
581 lex.pushToken(token);
585 token = lex.getString();
588 if (token == "inner_pos") {
590 inner_pos = lex.getString()[0];
592 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
594 lex.pushToken(token);
598 token = lex.getString();
601 if (token == "use_parbox") {
603 use_parbox = lex.getInteger();
605 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
606 lex.pushToken(token);
610 token = lex.getString();
613 if (token == "width") {
615 width = Length(lex.getString());
617 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
618 lex.pushToken(token);
622 token = lex.getString();
625 if (token == "special") {
627 special = lex.getString();
629 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
630 lex.pushToken(token);
634 token = lex.getString();
637 if (token == "height") {
639 height = Length(lex.getString());
641 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
642 lex.pushToken(token);
646 token = lex.getString();
649 if (token == "height_special") {
651 height_special = lex.getString();
653 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
654 lex.pushToken(token);