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 "dispatchresult.h"
20 #include "FuncStatus.h"
21 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
29 #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();
91 setInsetName(from_ascii("Box"));
96 InsetBox::InsetBox(BufferParams const & bp, string const & label)
97 : InsetCollapsable(bp), params_(label)
103 InsetBox::InsetBox(InsetBox const & in)
104 : InsetCollapsable(in), params_(in.params_)
110 InsetBox::~InsetBox()
112 InsetBoxMailer(*this).hideDialog();
116 auto_ptr<InsetBase> InsetBox::doClone() const
118 return auto_ptr<InsetBase>(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, LyXLex & lex)
138 InsetCollapsable::read(buf, lex);
143 void InsetBox::setButtonLabel()
145 LyXFont font(LyXFont::ALL_SANE);
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(LColor::foreground);
166 setBackgroundColor(LColor::background);
171 bool InsetBox::hasFixedWidth() const
173 return params_.inner_box || params_.special != "width";
177 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
181 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
182 InsetCollapsable::metrics(mi, dim);
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(LCursor & 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(LCursor & 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: 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 len + runparams.linelen; // 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 features.require("calc");
432 BoxType btype = boxtranslator().find(params_.type);
441 features.require("fancybox");
444 InsetText::validate(features);
448 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
453 string const InsetBoxMailer::name_ = "box";
456 string const InsetBoxMailer::inset2string(Buffer const &) const
458 return params2string(inset_.params());
462 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
465 data << "box" << ' ';
471 void InsetBoxMailer::string2params(string const & in,
472 InsetBoxParams & params)
474 params = InsetBoxParams(string());
478 istringstream data(in);
484 if (!lex || name != name_)
485 return print_mailer_error("InsetBoxMailer", in, 1, name_);
487 // This is part of the inset proper that is usually swallowed
488 // by LyXText::readInset
491 if (!lex || id != "Box")
492 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
498 InsetBoxParams::InsetBoxParams(string const & label)
502 width(LyXLength("100col%")),
507 height(LyXLength("1in")),
508 height_special("totalheight") // default is 1\\totalheight
512 void InsetBoxParams::write(ostream & os) const
514 os << "Box " << type << "\n";
515 os << "position \"" << pos << "\"\n";
516 os << "hor_pos \"" << hor_pos << "\"\n";
517 os << "has_inner_box " << inner_box << "\n";
518 os << "inner_pos \"" << inner_pos << "\"\n";
519 os << "use_parbox " << use_parbox << "\n";
520 os << "width \"" << width.asString() << "\"\n";
521 os << "special \"" << special << "\"\n";
522 os << "height \"" << height.asString() << "\"\n";
523 os << "height_special \"" << height_special << "\"\n";
527 void InsetBoxParams::read(LyXLex & lex)
534 type = lex.getString();
540 token = lex.getString();
541 if (token == "position") {
543 // The [0] is needed. We need the first and only char in
545 pos = lex.getString()[0];
547 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
548 lex.pushToken(token);
553 token = lex.getString();
554 if (token == "hor_pos") {
556 hor_pos = lex.getString()[0];
558 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
559 lex.pushToken(token);
564 token = lex.getString();
565 if (token == "has_inner_box") {
567 inner_box = lex.getInteger();
569 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
570 lex.pushToken(token);
576 token = lex.getString();
577 if (token == "inner_pos") {
579 inner_pos = lex.getString()[0];
581 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
583 lex.pushToken(token);
588 token = lex.getString();
589 if (token == "use_parbox") {
591 use_parbox = lex.getInteger();
593 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
594 lex.pushToken(token);
599 token = lex.getString();
600 if (token == "width") {
602 width = LyXLength(lex.getString());
604 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
605 lex.pushToken(token);
610 token = lex.getString();
611 if (token == "special") {
613 special = lex.getString();
615 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
616 lex.pushToken(token);
621 token = lex.getString();
622 if (token == "height") {
624 height = LyXLength(lex.getString());
626 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
627 lex.pushToken(token);
632 token = lex.getString();
633 if (token == "height_special") {
635 height_special = lex.getString();
637 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
638 lex.pushToken(token);