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/std_sstream.h"
29 #include "support/translator.h"
33 using std::istringstream;
35 using std::ostringstream;
41 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
43 BoxTranslator const init_boxtranslator() {
44 BoxTranslator translator("Boxed", InsetBox::Boxed);
45 translator.addPair("Frameless", InsetBox::Frameless);
46 translator.addPair("ovalbox", InsetBox::ovalbox);
47 translator.addPair("Ovalbox", InsetBox::Ovalbox);
48 translator.addPair("Shadowbox", InsetBox::Shadowbox);
49 translator.addPair("Doublebox",InsetBox::Doublebox);
54 BoxTranslator const init_boxtranslator_loc() {
55 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
56 translator.addPair(_("Frameless"), InsetBox::Frameless);
57 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
58 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
59 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
60 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
65 BoxTranslator const & boxtranslator() {
66 static BoxTranslator translator = init_boxtranslator();
71 BoxTranslator const & boxtranslator_loc() {
72 static BoxTranslator translator = init_boxtranslator_loc();
86 InsetBox::InsetBox(BufferParams const & bp, string const & label)
87 : InsetCollapsable(bp), params_(label)
93 InsetBox::InsetBox(InsetBox const & in)
94 : InsetCollapsable(in), params_(in.params_)
100 InsetBox::~InsetBox()
102 InsetBoxMailer(*this).hideDialog();
106 auto_ptr<InsetBase> InsetBox::clone() const
108 return auto_ptr<InsetBase>(new InsetBox(*this));
112 string const InsetBox::editMessage() const
114 return _("Opened Box Inset");
118 void InsetBox::write(Buffer const & buf, ostream & os) const
121 InsetCollapsable::write(buf, os);
125 void InsetBox::read(Buffer const & buf, LyXLex & lex)
128 InsetCollapsable::read(buf, lex);
133 void InsetBox::setButtonLabel()
135 LyXFont font(LyXFont::ALL_SANE);
139 BoxType btype = boxtranslator().find(params_.type);
140 if (btype == Frameless) {
141 if (params_.use_parbox)
142 setLabel(_("Box") + " (" + _("Parbox") + ")");
144 setLabel(_("Box") + " (" + _("Minipage") + ")");
146 setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
148 font.setColor(LColor::foreground);
149 setBackgroundColor(LColor::background);
154 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
157 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
158 InsetCollapsable::metrics(mi, dim);
159 //if (params_.inner_box && isOpen())
160 // dim.wid = mi.base.textwidth;
165 bool InsetBox::showInsetDialog(BufferView * bv) const
167 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
172 void InsetBox::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
174 switch (cmd.action) {
176 case LFUN_INSET_MODIFY: {
177 lyxerr << "InsetBox::dispatch MODIFY" << endl;
178 InsetBoxMailer::string2params(cmd.argument, params_);
183 case LFUN_INSET_DIALOG_UPDATE:
184 InsetBoxMailer(*this).updateDialog(&cur.bv());
187 case LFUN_MOUSE_RELEASE:
188 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
189 InsetBoxMailer(*this).showDialog(&cur.bv());
192 InsetCollapsable::priv_dispatch(cur, cmd);
196 InsetCollapsable::priv_dispatch(cur, cmd);
202 int InsetBox::latex(Buffer const & buf, ostream & os,
203 OutputParams const & runparams) const
205 BoxType btype = boxtranslator().find(params_.type);
207 string width_string = params_.width.asLatexString();
208 bool stdwidth(false);
209 if (params_.inner_box &&
210 (width_string.find("1.0\\columnwidth") != string::npos
211 || width_string.find("1.0\\textwidth") != string::npos)) {
217 width_string += " - 2\\fboxsep - 2\\fboxrule";
220 width_string += " - 2\\fboxsep - 0.8pt";
223 width_string += " - 2\\fboxsep - 1.6pt";
226 // Shadow falls outside right margin... opinions?
227 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
230 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
237 // Adapt to column/text width correctly also if paragraphs indented:
246 if (!params_.inner_box) {
248 // Special widths, see usrguide §3.5
249 if (params_.special != "none") {
250 os << "[" << params_.width.value()
251 << "\\" << params_.special << "]";
253 os << "[" << width_string << "]";
254 if (params_.hor_pos != 'c')
255 os << "[" << params_.hor_pos << "]";
267 os << "\\shadowbox{";
270 os << "\\doublebox{";
274 if (params_.inner_box) {
275 if (params_.use_parbox)
278 os << "\\begin{minipage}";
280 os << "[" << params_.pos << "]";
281 if (params_.height_special == "none") {
282 os << "[" << params_.height.asLatexString() << "]";
285 os << "[" << params_.height.value()
286 << "\\" << params_.height_special << "]";
288 if (params_.inner_pos != params_.pos)
289 os << "[" << params_.inner_pos << "]";
291 os << "{" << width_string << "}";
293 if (params_.use_parbox)
299 i += inset.latex(buf, os, runparams);
301 if (params_.inner_box) {
302 if (params_.use_parbox)
305 os << "%\n\\end{minipage}";
312 if (!params_.inner_box)
313 os << "}"; // for makebox
331 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
332 OutputParams const & runparams) const
334 return inset.linuxdoc(buf, os, runparams);
338 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
339 OutputParams const & runparams) const
341 return inset.docbook(buf, os, runparams);
345 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
346 OutputParams const & runparams) const
348 BoxType const btype = boxtranslator().find(params_.type);
351 case Frameless: break;
352 case Boxed: os << "["; break;
353 case ovalbox: os << "("; break;
354 case Ovalbox: os << "(("; break;
355 case Shadowbox: os << "["; break;
356 case Doublebox: os << "[["; break;
359 int i = inset.plaintext(buf, os, runparams);
362 case Frameless: break;
363 case Boxed: os << "]"; break;
364 case ovalbox: os << ")"; break;
365 case Ovalbox: os << "))"; break;
366 case Shadowbox: os << "]/"; break;
367 case Doublebox: os << "]]"; break;
374 void InsetBox::validate(LaTeXFeatures & features) const
376 features.require("calc");
377 BoxType btype = boxtranslator().find(params_.type);
386 features.require("fancybox");
389 inset.validate(features);
393 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);