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"
28 #include "MetricsInfo.h"
30 #include "support/Translator.h"
38 using std::istringstream;
40 using std::ostringstream;
46 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
47 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
49 BoxTranslator const init_boxtranslator()
51 BoxTranslator translator("Boxed", InsetBox::Boxed);
52 translator.addPair("Frameless", InsetBox::Frameless);
53 translator.addPair("ovalbox", InsetBox::ovalbox);
54 translator.addPair("Ovalbox", InsetBox::Ovalbox);
55 translator.addPair("Shadowbox", InsetBox::Shadowbox);
56 translator.addPair("Doublebox",InsetBox::Doublebox);
61 BoxTranslatorLoc const init_boxtranslator_loc()
63 BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
64 translator.addPair(_("Frameless"), InsetBox::Frameless);
65 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
66 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
67 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
68 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
73 BoxTranslator const & boxtranslator()
75 static BoxTranslator translator = init_boxtranslator();
80 BoxTranslatorLoc const & boxtranslator_loc()
82 static BoxTranslatorLoc translator = init_boxtranslator_loc();
95 InsetBox::InsetBox(BufferParams const & bp, string const & label)
96 : InsetCollapsable(bp), params_(label)
103 InsetBox::InsetBox(InsetBox const & in)
104 : InsetCollapsable(in), params_(in.params_)
110 InsetBox::~InsetBox()
112 InsetBoxMailer(*this).hideDialog();
116 Inset * InsetBox::clone() const
118 return new InsetBox(*this);
122 docstring const InsetBox::editMessage() const
124 return _("Opened Box Inset");
128 void InsetBox::write(Buffer const & buf, ostream & os) const
131 InsetCollapsable::write(buf, os);
135 void InsetBox::read(Buffer const & buf, Lexer & lex)
138 InsetCollapsable::read(buf, lex);
139 setLayout(buf.params());
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 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
179 // back up textwidth.
180 int textwidth_backup = m.base.textwidth;
182 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
183 InsetCollapsable::metrics(m, dim);
185 m.base.textwidth = textwidth_backup;
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_);
209 setLayout(cur.buffer().params());
214 case LFUN_INSET_DIALOG_UPDATE:
215 InsetBoxMailer(*this).updateDialog(&cur.bv());
218 case LFUN_MOUSE_RELEASE:
219 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
220 InsetBoxMailer(*this).showDialog(&cur.bv());
223 InsetCollapsable::doDispatch(cur, cmd);
227 InsetCollapsable::doDispatch(cur, cmd);
233 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
234 FuncStatus & flag) const
236 switch (cmd.action) {
238 case LFUN_INSET_MODIFY:
239 case LFUN_INSET_DIALOG_UPDATE:
242 case LFUN_BREAK_PARAGRAPH:
243 if (params_.inner_box) {
244 return InsetCollapsable::getStatus(cur, cmd, flag);
251 return InsetCollapsable::getStatus(cur, cmd, flag);
256 int InsetBox::latex(Buffer const & buf, odocstream & os,
257 OutputParams const & runparams) const
259 BoxType btype = boxtranslator().find(params_.type);
261 string width_string = params_.width.asLatexString();
262 bool stdwidth(false);
263 if (params_.inner_box &&
264 (width_string.find("1.0\\columnwidth") != string::npos
265 || width_string.find("1.0\\textwidth") != string::npos)) {
271 width_string += " - 2\\fboxsep - 2\\fboxrule";
274 width_string += " - 2\\fboxsep - 0.8pt";
277 width_string += " - 2\\fboxsep - 1.6pt";
280 // Shadow falls outside right margin... opinions?
281 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
284 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
291 // Adapt to column/text width correctly also if paragraphs indented:
300 if (!params_.inner_box) {
302 // Special widths, see usrguide §3.5
304 if (params_.special != "none") {
305 os << "[" << params_.width.value()
306 << '\\' << from_utf8(params_.special)
309 os << '[' << from_ascii(width_string)
311 if (params_.hor_pos != 'c')
312 os << "[" << params_.hor_pos << "]";
324 os << "\\shadowbox{";
327 os << "\\doublebox{";
331 if (params_.inner_box) {
332 if (params_.use_parbox)
335 os << "\\begin{minipage}";
337 os << "[" << params_.pos << "]";
338 if (params_.height_special == "none") {
340 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
343 // set no optional argument when the value is the default "1\height"
344 // (special units like \height are handled as "in")
345 // but when the user has chosen a non-default inner_pos, the height
346 // must be given: \minipage[pos][height][inner-pos]{width}
347 if ((params_.height != Length("1in") ||
348 params_.height_special != "totalheight") ||
349 params_.inner_pos != params_.pos) {
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 Text::readInset
500 if (!lex || id != "Box")
501 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
507 InsetBoxParams::InsetBoxParams(string const & label)
511 width(Length("100col%")),
516 height(Length("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 = Length(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 = Length(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);