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 "BufferView.h"
20 #include "DispatchResult.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
25 #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)
102 InsetBox::InsetBox(InsetBox const & in)
103 : InsetCollapsable(in), params_(in.params_)
109 InsetBox::~InsetBox()
111 InsetBoxMailer(*this).hideDialog();
115 Inset * InsetBox::clone() const
117 return new InsetBox(*this);
121 docstring const InsetBox::editMessage() const
123 return _("Opened Box Inset");
127 void InsetBox::write(Buffer const & buf, ostream & os) const
130 InsetCollapsable::write(buf, os);
134 void InsetBox::read(Buffer const & buf, Lexer & lex)
137 InsetCollapsable::read(buf, lex);
138 setLayout(buf.params());
143 void InsetBox::setButtonLabel()
145 FontInfo font = sane_font;
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 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
178 // back up textwidth.
179 int textwidth_backup = m.base.textwidth;
181 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
182 InsetCollapsable::metrics(m, dim);
184 m.base.textwidth = textwidth_backup;
188 bool InsetBox::forceDefaultParagraphs(idx_type) const
190 return !params_.inner_box;
194 bool InsetBox::showInsetDialog(BufferView * bv) const
196 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
201 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
203 switch (cmd.action) {
205 case LFUN_INSET_MODIFY: {
206 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
207 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
208 setLayout(cur.buffer().params());
213 case LFUN_INSET_DIALOG_UPDATE:
214 InsetBoxMailer(*this).updateDialog(&cur.bv());
217 case LFUN_MOUSE_RELEASE:
218 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
219 InsetBoxMailer(*this).showDialog(&cur.bv());
222 InsetCollapsable::doDispatch(cur, cmd);
226 InsetCollapsable::doDispatch(cur, cmd);
232 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
233 FuncStatus & flag) const
235 switch (cmd.action) {
237 case LFUN_INSET_MODIFY:
238 case LFUN_INSET_DIALOG_UPDATE:
241 case LFUN_BREAK_PARAGRAPH:
242 if (params_.inner_box) {
243 return InsetCollapsable::getStatus(cur, cmd, flag);
250 return InsetCollapsable::getStatus(cur, cmd, flag);
255 int InsetBox::latex(Buffer const & buf, odocstream & os,
256 OutputParams const & runparams) const
258 BoxType btype = boxtranslator().find(params_.type);
260 string width_string = params_.width.asLatexString();
261 bool stdwidth(false);
262 if (params_.inner_box &&
263 (width_string.find("1.0\\columnwidth") != string::npos
264 || width_string.find("1.0\\textwidth") != string::npos)) {
270 width_string += " - 2\\fboxsep - 2\\fboxrule";
273 width_string += " - 2\\fboxsep - 0.8pt";
276 width_string += " - 2\\fboxsep - 1.6pt";
279 // Shadow falls outside right margin... opinions?
280 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
283 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
290 // Adapt to column/text width correctly also if paragraphs indented:
299 if (!params_.inner_box) {
301 // Special widths, see usrguide §3.5
303 if (params_.special != "none") {
304 os << "[" << params_.width.value()
305 << '\\' << from_utf8(params_.special)
308 os << '[' << from_ascii(width_string)
310 if (params_.hor_pos != 'c')
311 os << "[" << params_.hor_pos << "]";
323 os << "\\shadowbox{";
326 os << "\\doublebox{";
330 if (params_.inner_box) {
331 if (params_.use_parbox)
334 os << "\\begin{minipage}";
336 os << "[" << params_.pos << "]";
337 if (params_.height_special == "none") {
339 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
342 // set no optional argument when the value is the default "1\height"
343 // (special units like \height are handled as "in")
344 // but when the user has chosen a non-default inner_pos, the height
345 // must be given: \minipage[pos][height][inner-pos]{width}
346 if ((params_.height != Length("1in") ||
347 params_.height_special != "totalheight") ||
348 params_.inner_pos != params_.pos) {
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 Text::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);