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 bool 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;
183 bool const changed = dim_ != dim;
189 bool InsetBox::forceDefaultParagraphs(idx_type) const
191 return !params_.inner_box;
195 bool InsetBox::showInsetDialog(BufferView * bv) const
197 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
202 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
204 switch (cmd.action) {
206 case LFUN_INSET_MODIFY: {
207 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
208 InsetBoxMailer::string2params(to_utf8(cmd.argument()), 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())
344 os << "[" << params_.height.value()
345 << '\\' << from_utf8(params_.height_special)
348 if (params_.inner_pos != params_.pos)
349 os << "[" << params_.inner_pos << "]";
352 os << '{' << from_ascii(width_string) << '}';
354 if (params_.use_parbox)
360 i += InsetText::latex(buf, os, runparams);
362 if (params_.inner_box) {
363 if (params_.use_parbox)
366 os << "%\n\\end{minipage}";
373 if (!params_.inner_box)
374 os << "}"; // for makebox
392 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
393 OutputParams const & runparams) const
395 BoxType const btype = boxtranslator().find(params_.type);
398 case Frameless: break;
399 case Boxed: os << "[\n"; break;
400 case ovalbox: os << "(\n"; break;
401 case Ovalbox: os << "((\n"; break;
402 case Shadowbox: os << "[/\n"; break;
403 case Doublebox: os << "[[\n"; break;
406 InsetText::plaintext(buf, os, runparams);
410 case Frameless: os << "\n"; break;
411 case Boxed: os << "\n]"; len = 1; break;
412 case ovalbox: os << "\n)"; len = 1; break;
413 case Ovalbox: os << "\n))"; len = 2; break;
414 case Shadowbox: os << "\n/]"; len = 2; break;
415 case Doublebox: os << "\n]]"; len = 2; break;
418 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
422 int InsetBox::docbook(Buffer const & buf, odocstream & os,
423 OutputParams const & runparams) const
425 return InsetText::docbook(buf, os, runparams);
429 void InsetBox::validate(LaTeXFeatures & features) const
431 BoxType btype = boxtranslator().find(params_.type);
436 features.require("calc");
442 features.require("calc");
443 features.require("fancybox");
446 InsetText::validate(features);
450 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
455 string const InsetBoxMailer::name_ = "box";
458 string const InsetBoxMailer::inset2string(Buffer const &) const
460 return params2string(inset_.params());
464 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
467 data << "box" << ' ';
473 void InsetBoxMailer::string2params(string const & in,
474 InsetBoxParams & params)
476 params = InsetBoxParams(string());
480 istringstream data(in);
486 if (!lex || name != name_)
487 return print_mailer_error("InsetBoxMailer", in, 1, name_);
489 // This is part of the inset proper that is usually swallowed
490 // by Text::readInset
493 if (!lex || id != "Box")
494 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
500 InsetBoxParams::InsetBoxParams(string const & label)
504 width(Length("100col%")),
509 height(Length("1in")),
510 height_special("totalheight") // default is 1\\totalheight
514 void InsetBoxParams::write(ostream & os) const
516 os << "Box " << type << "\n";
517 os << "position \"" << pos << "\"\n";
518 os << "hor_pos \"" << hor_pos << "\"\n";
519 os << "has_inner_box " << inner_box << "\n";
520 os << "inner_pos \"" << inner_pos << "\"\n";
521 os << "use_parbox " << use_parbox << "\n";
522 os << "width \"" << width.asString() << "\"\n";
523 os << "special \"" << special << "\"\n";
524 os << "height \"" << height.asString() << "\"\n";
525 os << "height_special \"" << height_special << "\"\n";
529 void InsetBoxParams::read(Lexer & lex)
535 type = lex.getString();
542 token = lex.getString();
545 if (token == "position") {
547 // The [0] is needed. We need the first and only char in
549 pos = lex.getString()[0];
551 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
552 lex.pushToken(token);
556 token = lex.getString();
559 if (token == "hor_pos") {
561 hor_pos = lex.getString()[0];
563 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
564 lex.pushToken(token);
568 token = lex.getString();
571 if (token == "has_inner_box") {
573 inner_box = lex.getInteger();
575 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
576 lex.pushToken(token);
580 token = lex.getString();
583 if (token == "inner_pos") {
585 inner_pos = lex.getString()[0];
587 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
589 lex.pushToken(token);
593 token = lex.getString();
596 if (token == "use_parbox") {
598 use_parbox = lex.getInteger();
600 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
601 lex.pushToken(token);
605 token = lex.getString();
608 if (token == "width") {
610 width = Length(lex.getString());
612 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
613 lex.pushToken(token);
617 token = lex.getString();
620 if (token == "special") {
622 special = lex.getString();
624 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
625 lex.pushToken(token);
629 token = lex.getString();
632 if (token == "height") {
634 height = Length(lex.getString());
636 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
637 lex.pushToken(token);
641 token = lex.getString();
644 if (token == "height_special") {
646 height_special = lex.getString();
648 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
649 lex.pushToken(token);