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"
29 #include "support/Translator.h"
37 using std::istringstream;
39 using std::ostringstream;
45 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
46 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
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 BoxTranslatorLoc const init_boxtranslator_loc()
62 BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
63 translator.addPair(_("Frameless"), InsetBox::Frameless);
64 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
65 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
66 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
67 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
72 BoxTranslator const & boxtranslator()
74 static BoxTranslator translator = init_boxtranslator();
79 BoxTranslatorLoc const & boxtranslator_loc()
81 static BoxTranslatorLoc translator = init_boxtranslator_loc();
94 InsetBox::InsetBox(BufferParams const & bp, string const & label)
95 : InsetCollapsable(bp), params_(label)
101 InsetBox::InsetBox(InsetBox const & in)
102 : InsetCollapsable(in), params_(in.params_)
108 InsetBox::~InsetBox()
110 InsetBoxMailer(*this).hideDialog();
114 Inset * InsetBox::clone() const
116 return new InsetBox(*this);
120 docstring const InsetBox::editMessage() const
122 return _("Opened Box Inset");
126 void InsetBox::write(Buffer const & buf, ostream & os) const
129 InsetCollapsable::write(buf, os);
133 void InsetBox::read(Buffer const & buf, Lexer & lex)
136 InsetCollapsable::read(buf, lex);
141 void InsetBox::setButtonLabel()
143 Font font(Font::ALL_SANE);
147 BoxType btype = boxtranslator().find(params_.type);
152 if (btype == Frameless) {
153 if (params_.use_parbox)
154 label += _("Parbox");
156 label += _("Minipage");
158 label += boxtranslator_loc().find(btype);
163 font.setColor(Color::foreground);
168 bool InsetBox::hasFixedWidth() const
170 return params_.inner_box || params_.special != "width";
174 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
176 // back up textwidth.
177 int textwidth_backup = m.base.textwidth;
179 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
180 InsetCollapsable::metrics(m, dim);
182 m.base.textwidth = textwidth_backup;
186 bool InsetBox::forceDefaultParagraphs(idx_type) const
188 return !params_.inner_box;
192 bool InsetBox::showInsetDialog(BufferView * bv) const
194 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
199 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
201 switch (cmd.action) {
203 case LFUN_INSET_MODIFY: {
204 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
205 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
210 case LFUN_INSET_DIALOG_UPDATE:
211 InsetBoxMailer(*this).updateDialog(&cur.bv());
214 case LFUN_MOUSE_RELEASE:
215 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
216 InsetBoxMailer(*this).showDialog(&cur.bv());
219 InsetCollapsable::doDispatch(cur, cmd);
223 InsetCollapsable::doDispatch(cur, cmd);
229 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
230 FuncStatus & flag) const
232 switch (cmd.action) {
234 case LFUN_INSET_MODIFY:
235 case LFUN_INSET_DIALOG_UPDATE:
238 case LFUN_BREAK_PARAGRAPH:
239 if (params_.inner_box) {
240 return InsetCollapsable::getStatus(cur, cmd, flag);
247 return InsetCollapsable::getStatus(cur, cmd, flag);
252 int InsetBox::latex(Buffer const & buf, odocstream & os,
253 OutputParams const & runparams) const
255 BoxType btype = boxtranslator().find(params_.type);
257 string width_string = params_.width.asLatexString();
258 bool stdwidth(false);
259 if (params_.inner_box &&
260 (width_string.find("1.0\\columnwidth") != string::npos
261 || width_string.find("1.0\\textwidth") != string::npos)) {
267 width_string += " - 2\\fboxsep - 2\\fboxrule";
270 width_string += " - 2\\fboxsep - 0.8pt";
273 width_string += " - 2\\fboxsep - 1.6pt";
276 // Shadow falls outside right margin... opinions?
277 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
280 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
287 // Adapt to column/text width correctly also if paragraphs indented:
296 if (!params_.inner_box) {
298 // Special widths, see usrguide §3.5
300 if (params_.special != "none") {
301 os << "[" << params_.width.value()
302 << '\\' << from_utf8(params_.special)
305 os << '[' << from_ascii(width_string)
307 if (params_.hor_pos != 'c')
308 os << "[" << params_.hor_pos << "]";
320 os << "\\shadowbox{";
323 os << "\\doublebox{";
327 if (params_.inner_box) {
328 if (params_.use_parbox)
331 os << "\\begin{minipage}";
333 os << "[" << params_.pos << "]";
334 if (params_.height_special == "none") {
336 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
339 // set no optional argument when the value is the default "1\height"
340 // (special units like \height are handled as "in")
341 // but when the user has chosen a non-default inner_pos, the height
342 // must be given: \minipage[pos][height][inner-pos]{width}
343 if ((params_.height != Length("1in") ||
344 params_.height_special != "totalheight") ||
345 params_.inner_pos != params_.pos) {
347 os << "[" << params_.height.value()
348 << "\\" << from_utf8(params_.height_special) << "]";
351 if (params_.inner_pos != params_.pos)
352 os << "[" << params_.inner_pos << "]";
355 os << '{' << from_ascii(width_string) << '}';
357 if (params_.use_parbox)
363 i += InsetText::latex(buf, os, runparams);
365 if (params_.inner_box) {
366 if (params_.use_parbox)
369 os << "%\n\\end{minipage}";
376 if (!params_.inner_box)
377 os << "}"; // for makebox
395 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
396 OutputParams const & runparams) const
398 BoxType const btype = boxtranslator().find(params_.type);
401 case Frameless: break;
402 case Boxed: os << "[\n"; break;
403 case ovalbox: os << "(\n"; break;
404 case Ovalbox: os << "((\n"; break;
405 case Shadowbox: os << "[/\n"; break;
406 case Doublebox: os << "[[\n"; break;
409 InsetText::plaintext(buf, os, runparams);
413 case Frameless: os << "\n"; break;
414 case Boxed: os << "\n]"; len = 1; break;
415 case ovalbox: os << "\n)"; len = 1; break;
416 case Ovalbox: os << "\n))"; len = 2; break;
417 case Shadowbox: os << "\n/]"; len = 2; break;
418 case Doublebox: os << "\n]]"; len = 2; break;
421 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
425 int InsetBox::docbook(Buffer const & buf, odocstream & os,
426 OutputParams const & runparams) const
428 return InsetText::docbook(buf, os, runparams);
432 void InsetBox::validate(LaTeXFeatures & features) const
434 BoxType btype = boxtranslator().find(params_.type);
439 features.require("calc");
445 features.require("calc");
446 features.require("fancybox");
449 InsetText::validate(features);
453 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
458 string const InsetBoxMailer::name_ = "box";
461 string const InsetBoxMailer::inset2string(Buffer const &) const
463 return params2string(inset_.params());
467 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
470 data << "box" << ' ';
476 void InsetBoxMailer::string2params(string const & in,
477 InsetBoxParams & params)
479 params = InsetBoxParams(string());
483 istringstream data(in);
489 if (!lex || name != name_)
490 return print_mailer_error("InsetBoxMailer", in, 1, name_);
492 // This is part of the inset proper that is usually swallowed
493 // by Text::readInset
496 if (!lex || id != "Box")
497 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
503 InsetBoxParams::InsetBoxParams(string const & label)
507 width(Length("100col%")),
512 height(Length("1in")),
513 height_special("totalheight") // default is 1\\totalheight
517 void InsetBoxParams::write(ostream & os) const
519 os << "Box " << type << "\n";
520 os << "position \"" << pos << "\"\n";
521 os << "hor_pos \"" << hor_pos << "\"\n";
522 os << "has_inner_box " << inner_box << "\n";
523 os << "inner_pos \"" << inner_pos << "\"\n";
524 os << "use_parbox " << use_parbox << "\n";
525 os << "width \"" << width.asString() << "\"\n";
526 os << "special \"" << special << "\"\n";
527 os << "height \"" << height.asString() << "\"\n";
528 os << "height_special \"" << height_special << "\"\n";
532 void InsetBoxParams::read(Lexer & lex)
538 type = lex.getString();
545 token = lex.getString();
548 if (token == "position") {
550 // The [0] is needed. We need the first and only char in
552 pos = lex.getString()[0];
554 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
555 lex.pushToken(token);
559 token = lex.getString();
562 if (token == "hor_pos") {
564 hor_pos = lex.getString()[0];
566 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
567 lex.pushToken(token);
571 token = lex.getString();
574 if (token == "has_inner_box") {
576 inner_box = lex.getInteger();
578 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
579 lex.pushToken(token);
583 token = lex.getString();
586 if (token == "inner_pos") {
588 inner_pos = lex.getString()[0];
590 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
592 lex.pushToken(token);
596 token = lex.getString();
599 if (token == "use_parbox") {
601 use_parbox = lex.getInteger();
603 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
604 lex.pushToken(token);
608 token = lex.getString();
611 if (token == "width") {
613 width = Length(lex.getString());
615 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
616 lex.pushToken(token);
620 token = lex.getString();
623 if (token == "special") {
625 special = lex.getString();
627 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
628 lex.pushToken(token);
632 token = lex.getString();
635 if (token == "height") {
637 height = Length(lex.getString());
639 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
640 lex.pushToken(token);
644 token = lex.getString();
647 if (token == "height_special") {
649 height_special = lex.getString();
651 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
652 lex.pushToken(token);