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();
93 setInsetName(from_ascii("Box"));
98 InsetBox::InsetBox(BufferParams const & bp, string const & label)
99 : InsetCollapsable(bp), params_(label)
105 InsetBox::InsetBox(InsetBox const & in)
106 : InsetCollapsable(in), params_(in.params_)
112 InsetBox::~InsetBox()
114 InsetBoxMailer(*this).hideDialog();
118 auto_ptr<InsetBase> InsetBox::doClone() const
120 return auto_ptr<InsetBase>(new InsetBox(*this));
124 docstring const InsetBox::editMessage() const
126 return _("Opened Box Inset");
130 void InsetBox::write(Buffer const & buf, ostream & os) const
133 InsetCollapsable::write(buf, os);
137 void InsetBox::read(Buffer const & buf, Lexer & lex)
140 InsetCollapsable::read(buf, lex);
145 void InsetBox::setButtonLabel()
147 LyXFont font(LyXFont::ALL_SANE);
151 BoxType btype = boxtranslator().find(params_.type);
156 if (btype == Frameless) {
157 if (params_.use_parbox)
158 label += _("Parbox");
160 label += _("Minipage");
162 label += boxtranslator_loc().find(btype);
167 font.setColor(LColor::foreground);
168 setBackgroundColor(LColor::background);
173 bool InsetBox::hasFixedWidth() const
175 return params_.inner_box || params_.special != "width";
179 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
182 // first round in order to know the minimum size.
183 InsetCollapsable::metrics(mi, dim);
184 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
187 std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET),
188 params_.width.inPixels(m.base.textwidth));
189 InsetCollapsable::metrics(mi, dim);
190 bool const changed = dim_ != dim;
196 bool InsetBox::forceDefaultParagraphs(idx_type) const
198 return !params_.inner_box;
202 bool InsetBox::showInsetDialog(BufferView * bv) const
204 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
209 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
211 switch (cmd.action) {
213 case LFUN_INSET_MODIFY: {
214 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
215 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
220 case LFUN_INSET_DIALOG_UPDATE:
221 InsetBoxMailer(*this).updateDialog(&cur.bv());
224 case LFUN_MOUSE_RELEASE:
225 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
226 InsetBoxMailer(*this).showDialog(&cur.bv());
229 InsetCollapsable::doDispatch(cur, cmd);
233 InsetCollapsable::doDispatch(cur, cmd);
239 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
240 FuncStatus & flag) const
242 switch (cmd.action) {
244 case LFUN_INSET_MODIFY:
245 case LFUN_INSET_DIALOG_UPDATE:
248 case LFUN_BREAK_PARAGRAPH:
249 if (params_.inner_box) {
250 return InsetCollapsable::getStatus(cur, cmd, flag);
257 return InsetCollapsable::getStatus(cur, cmd, flag);
262 int InsetBox::latex(Buffer const & buf, odocstream & os,
263 OutputParams const & runparams) const
265 BoxType btype = boxtranslator().find(params_.type);
267 string width_string = params_.width.asLatexString();
268 bool stdwidth(false);
269 if (params_.inner_box &&
270 (width_string.find("1.0\\columnwidth") != string::npos
271 || width_string.find("1.0\\textwidth") != string::npos)) {
277 width_string += " - 2\\fboxsep - 2\\fboxrule";
280 width_string += " - 2\\fboxsep - 0.8pt";
283 width_string += " - 2\\fboxsep - 1.6pt";
286 // Shadow falls outside right margin... opinions?
287 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
290 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
297 // Adapt to column/text width correctly also if paragraphs indented:
306 if (!params_.inner_box) {
308 // Special widths, see usrguide §3.5
310 if (params_.special != "none") {
311 os << "[" << params_.width.value()
312 << '\\' << from_utf8(params_.special)
315 os << '[' << from_ascii(width_string)
317 if (params_.hor_pos != 'c')
318 os << "[" << params_.hor_pos << "]";
330 os << "\\shadowbox{";
333 os << "\\doublebox{";
337 if (params_.inner_box) {
338 if (params_.use_parbox)
341 os << "\\begin{minipage}";
343 os << "[" << params_.pos << "]";
344 if (params_.height_special == "none") {
346 os << '[' << from_ascii(params_.height.asLatexString())
351 os << "[" << params_.height.value()
352 << '\\' << from_utf8(params_.height_special)
355 if (params_.inner_pos != params_.pos)
356 os << "[" << params_.inner_pos << "]";
359 os << '{' << from_ascii(width_string) << '}';
361 if (params_.use_parbox)
367 i += InsetText::latex(buf, os, runparams);
369 if (params_.inner_box) {
370 if (params_.use_parbox)
373 os << "%\n\\end{minipage}";
380 if (!params_.inner_box)
381 os << "}"; // for makebox
399 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
400 OutputParams const & runparams) const
402 BoxType const btype = boxtranslator().find(params_.type);
405 case Frameless: break;
406 case Boxed: os << "[\n"; break;
407 case ovalbox: os << "(\n"; break;
408 case Ovalbox: os << "((\n"; break;
409 case Shadowbox: os << "[/\n"; break;
410 case Doublebox: os << "[[\n"; break;
413 InsetText::plaintext(buf, os, runparams);
417 case Frameless: os << "\n"; break;
418 case Boxed: os << "\n]"; len = 1; break;
419 case ovalbox: os << "\n)"; len = 1; break;
420 case Ovalbox: os << "\n))"; len = 2; break;
421 case Shadowbox: os << "\n/]"; len = 2; break;
422 case Doublebox: os << "\n]]"; len = 2; break;
425 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
429 int InsetBox::docbook(Buffer const & buf, odocstream & os,
430 OutputParams const & runparams) const
432 return InsetText::docbook(buf, os, runparams);
436 void InsetBox::validate(LaTeXFeatures & features) const
438 BoxType btype = boxtranslator().find(params_.type);
443 features.require("calc");
449 features.require("calc");
450 features.require("fancybox");
453 InsetText::validate(features);
457 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
462 string const InsetBoxMailer::name_ = "box";
465 string const InsetBoxMailer::inset2string(Buffer const &) const
467 return params2string(inset_.params());
471 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
474 data << "box" << ' ';
480 void InsetBoxMailer::string2params(string const & in,
481 InsetBoxParams & params)
483 params = InsetBoxParams(string());
487 istringstream data(in);
493 if (!lex || name != name_)
494 return print_mailer_error("InsetBoxMailer", in, 1, name_);
496 // This is part of the inset proper that is usually swallowed
497 // by LyXText::readInset
500 if (!lex || id != "Box")
501 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
507 InsetBoxParams::InsetBoxParams(string const & label)
511 width(LyXLength("100col%")),
516 height(LyXLength("1in")),
517 height_special("totalheight") // default is 1\\totalheight
521 void InsetBoxParams::write(ostream & os) const
523 os << "Box " << type << "\n";
524 os << "position \"" << pos << "\"\n";
525 os << "hor_pos \"" << hor_pos << "\"\n";
526 os << "has_inner_box " << inner_box << "\n";
527 os << "inner_pos \"" << inner_pos << "\"\n";
528 os << "use_parbox " << use_parbox << "\n";
529 os << "width \"" << width.asString() << "\"\n";
530 os << "special \"" << special << "\"\n";
531 os << "height \"" << height.asString() << "\"\n";
532 os << "height_special \"" << height_special << "\"\n";
536 void InsetBoxParams::read(Lexer & lex)
542 type = lex.getString();
549 token = lex.getString();
552 if (token == "position") {
554 // The [0] is needed. We need the first and only char in
556 pos = lex.getString()[0];
558 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
559 lex.pushToken(token);
563 token = lex.getString();
566 if (token == "hor_pos") {
568 hor_pos = lex.getString()[0];
570 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
571 lex.pushToken(token);
575 token = lex.getString();
578 if (token == "has_inner_box") {
580 inner_box = lex.getInteger();
582 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
583 lex.pushToken(token);
587 token = lex.getString();
590 if (token == "inner_pos") {
592 inner_pos = lex.getString()[0];
594 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
596 lex.pushToken(token);
600 token = lex.getString();
603 if (token == "use_parbox") {
605 use_parbox = lex.getInteger();
607 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
608 lex.pushToken(token);
612 token = lex.getString();
615 if (token == "width") {
617 width = LyXLength(lex.getString());
619 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
620 lex.pushToken(token);
624 token = lex.getString();
627 if (token == "special") {
629 special = lex.getString();
631 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
632 lex.pushToken(token);
636 token = lex.getString();
639 if (token == "height") {
641 height = LyXLength(lex.getString());
643 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
644 lex.pushToken(token);
648 token = lex.getString();
651 if (token == "height_special") {
653 height_special = lex.getString();
655 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
656 lex.pushToken(token);