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 "funcrequest.h"
22 #include "LaTeXFeatures.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
28 #include "support/translator.h"
34 using std::istringstream;
36 using std::ostringstream;
42 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
44 BoxTranslator const init_boxtranslator() {
45 BoxTranslator translator("Boxed", InsetBox::Boxed);
46 translator.addPair("Frameless", InsetBox::Frameless);
47 translator.addPair("ovalbox", InsetBox::ovalbox);
48 translator.addPair("Ovalbox", InsetBox::Ovalbox);
49 translator.addPair("Shadowbox", InsetBox::Shadowbox);
50 translator.addPair("Doublebox",InsetBox::Doublebox);
55 BoxTranslator const init_boxtranslator_loc() {
56 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
57 translator.addPair(_("Frameless"), InsetBox::Frameless);
58 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
59 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
60 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
61 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
66 BoxTranslator const & boxtranslator() {
67 static BoxTranslator translator = init_boxtranslator();
72 BoxTranslator const & boxtranslator_loc() {
73 static BoxTranslator translator = init_boxtranslator_loc();
87 InsetBox::InsetBox(BufferParams const & bp, string const & label)
88 : InsetCollapsable(bp), params_(label)
94 InsetBox::InsetBox(InsetBox const & in)
95 : InsetCollapsable(in), params_(in.params_)
101 InsetBox::~InsetBox()
103 InsetBoxMailer(*this).hideDialog();
107 auto_ptr<InsetBase> InsetBox::clone() const
109 return auto_ptr<InsetBase>(new InsetBox(*this));
113 string const InsetBox::editMessage() const
115 return _("Opened Box Inset");
119 void InsetBox::write(Buffer const & buf, ostream & os) const
122 InsetCollapsable::write(buf, os);
126 void InsetBox::read(Buffer const & buf, LyXLex & lex)
129 InsetCollapsable::read(buf, lex);
134 void InsetBox::setButtonLabel()
136 LyXFont font(LyXFont::ALL_SANE);
140 BoxType btype = boxtranslator().find(params_.type);
141 if (btype == Frameless) {
142 if (params_.use_parbox)
143 setLabel(_("Box") + " (" + _("Parbox") + ")");
145 setLabel(_("Box") + " (" + _("Minipage") + ")");
147 setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
149 font.setColor(LColor::foreground);
150 setBackgroundColor(LColor::background);
155 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
158 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
159 InsetCollapsable::metrics(mi, dim);
160 //if (params_.inner_box && isOpen())
161 // dim.wid = mi.base.textwidth;
166 bool InsetBox::showInsetDialog(BufferView * bv) const
168 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
173 void InsetBox::priv_dispatch(LCursor & cur, FuncRequest & cmd)
175 switch (cmd.action) {
177 case LFUN_INSET_MODIFY: {
178 lyxerr << "InsetBox::dispatch MODIFY" << endl;
179 InsetBoxMailer::string2params(cmd.argument, params_);
184 case LFUN_INSET_DIALOG_UPDATE:
185 InsetBoxMailer(*this).updateDialog(&cur.bv());
188 case LFUN_MOUSE_RELEASE:
189 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
190 InsetBoxMailer(*this).showDialog(&cur.bv());
193 InsetCollapsable::priv_dispatch(cur, cmd);
197 InsetCollapsable::priv_dispatch(cur, cmd);
203 int InsetBox::latex(Buffer const & buf, ostream & os,
204 OutputParams const & runparams) const
206 BoxType btype = boxtranslator().find(params_.type);
208 string width_string = params_.width.asLatexString();
209 bool stdwidth(false);
210 if (params_.inner_box &&
211 (width_string.find("1.0\\columnwidth") != string::npos
212 || width_string.find("1.0\\textwidth") != string::npos)) {
218 width_string += " - 2\\fboxsep - 2\\fboxrule";
221 width_string += " - 2\\fboxsep - 0.8pt";
224 width_string += " - 2\\fboxsep - 1.6pt";
227 // Shadow falls outside right margin... opinions?
228 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
231 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
238 // Adapt to column/text width correctly also if paragraphs indented:
247 if (!params_.inner_box) {
249 // Special widths, see usrguide §3.5
250 if (params_.special != "none") {
251 os << "[" << params_.width.value()
252 << "\\" << params_.special << "]";
254 os << "[" << width_string << "]";
255 if (params_.hor_pos != 'c')
256 os << "[" << params_.hor_pos << "]";
268 os << "\\shadowbox{";
271 os << "\\doublebox{";
275 if (params_.inner_box) {
276 if (params_.use_parbox)
279 os << "\\begin{minipage}";
281 os << "[" << params_.pos << "]";
282 if (params_.height_special == "none") {
283 os << "[" << params_.height.asLatexString() << "]";
286 os << "[" << params_.height.value()
287 << "\\" << params_.height_special << "]";
289 if (params_.inner_pos != params_.pos)
290 os << "[" << params_.inner_pos << "]";
292 os << "{" << width_string << "}";
294 if (params_.use_parbox)
300 i += InsetText::latex(buf, os, runparams);
302 if (params_.inner_box) {
303 if (params_.use_parbox)
306 os << "%\n\\end{minipage}";
313 if (!params_.inner_box)
314 os << "}"; // for makebox
332 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
333 OutputParams const & runparams) const
335 return InsetText::linuxdoc(buf, os, runparams);
339 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
340 OutputParams const & runparams) const
342 return InsetText::docbook(buf, os, runparams);
346 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
347 OutputParams const & runparams) const
349 BoxType const btype = boxtranslator().find(params_.type);
352 case Frameless: break;
353 case Boxed: os << "["; break;
354 case ovalbox: os << "("; break;
355 case Ovalbox: os << "(("; break;
356 case Shadowbox: os << "["; break;
357 case Doublebox: os << "[["; break;
360 int i = InsetText::plaintext(buf, os, runparams);
363 case Frameless: break;
364 case Boxed: os << "]"; break;
365 case ovalbox: os << ")"; break;
366 case Ovalbox: os << "))"; break;
367 case Shadowbox: os << "]/"; break;
368 case Doublebox: os << "]]"; break;
375 void InsetBox::validate(LaTeXFeatures & features) const
377 features.require("calc");
378 BoxType btype = boxtranslator().find(params_.type);
387 features.require("fancybox");
390 InsetText::validate(features);
394 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
399 string const InsetBoxMailer::name_ = "box";
402 string const InsetBoxMailer::inset2string(Buffer const &) const
404 return params2string(inset_.params());
408 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
411 data << "box" << ' ';
417 void InsetBoxMailer::string2params(string const & in,
418 InsetBoxParams & params)
420 params = InsetBoxParams(string());
424 istringstream data(in);
430 if (!lex || name != name_)
431 return print_mailer_error("InsetBoxMailer", in, 1, name_);
433 // This is part of the inset proper that is usually swallowed
434 // by LyXText::readInset
437 if (!lex || id != "Box")
438 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
444 InsetBoxParams::InsetBoxParams(string const & label)
448 width(LyXLength("100col%")),
453 height(LyXLength("1in")),
454 height_special("totalheight") // default is 1\\totalheight
458 void InsetBoxParams::write(ostream & os) const
460 os << "Box " << type << "\n";
461 os << "position \"" << pos << "\"\n";
462 os << "hor_pos \"" << hor_pos << "\"\n";
463 os << "has_inner_box " << inner_box << "\n";
464 os << "inner_pos \"" << inner_pos << "\"\n";
465 os << "use_parbox " << use_parbox << "\n";
466 os << "width \"" << width.asString() << "\"\n";
467 os << "special \"" << special << "\"\n";
468 os << "height \"" << height.asString() << "\"\n";
469 os << "height_special \"" << height_special << "\"\n";
473 void InsetBoxParams::read(LyXLex & lex)
480 type = lex.getString();
486 token = lex.getString();
487 if (token == "position") {
489 // The [0] is needed. We need the first and only char in
491 pos = lex.getString()[0];
493 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
494 lex.pushToken(token);
499 token = lex.getString();
500 if (token == "hor_pos") {
502 hor_pos = lex.getString()[0];
504 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
505 lex.pushToken(token);
510 token = lex.getString();
511 if (token == "has_inner_box") {
513 inner_box = lex.getInteger();
515 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
516 lex.pushToken(token);
522 token = lex.getString();
523 if (token == "inner_pos") {
525 inner_pos = lex.getString()[0];
527 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
529 lex.pushToken(token);
534 token = lex.getString();
535 if (token == "use_parbox") {
537 use_parbox = lex.getInteger();
539 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
540 lex.pushToken(token);
545 token = lex.getString();
546 if (token == "width") {
548 width = LyXLength(lex.getString());
550 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
551 lex.pushToken(token);
556 token = lex.getString();
557 if (token == "special") {
559 special = lex.getString();
561 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
562 lex.pushToken(token);
567 token = lex.getString();
568 if (token == "height") {
570 height = LyXLength(lex.getString());
572 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
573 lex.pushToken(token);
578 token = lex.getString();
579 if (token == "height_special") {
581 height_special = lex.getString();
583 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
584 lex.pushToken(token);