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 & 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 += InsetText::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 InsetText::linuxdoc(buf, os, runparams);
338 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
339 OutputParams const & runparams) const
341 return InsetText::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 = InsetText::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 InsetText::validate(features);
393 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
398 string const InsetBoxMailer::name_ = "box";
401 string const InsetBoxMailer::inset2string(Buffer const &) const
403 return params2string(inset_.params());
407 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
410 data << "box" << ' ';
416 void InsetBoxMailer::string2params(string const & in,
417 InsetBoxParams & params)
419 params = InsetBoxParams(string());
423 istringstream data(in);
429 if (!lex || name != name_)
430 return print_mailer_error("InsetBoxMailer", in, 1, name_);
432 // This is part of the inset proper that is usually swallowed
433 // by LyXText::readInset
436 if (!lex || id != "Box")
437 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
443 InsetBoxParams::InsetBoxParams(string const & label)
447 width(LyXLength("100col%")),
452 height(LyXLength("1in")),
453 height_special("totalheight") // default is 1\\totalheight
457 void InsetBoxParams::write(ostream & os) const
459 os << "Box " << type << "\n";
460 os << "position \"" << pos << "\"\n";
461 os << "hor_pos \"" << hor_pos << "\"\n";
462 os << "has_inner_box " << inner_box << "\n";
463 os << "inner_pos \"" << inner_pos << "\"\n";
464 os << "use_parbox " << use_parbox << "\n";
465 os << "width \"" << width.asString() << "\"\n";
466 os << "special \"" << special << "\"\n";
467 os << "height \"" << height.asString() << "\"\n";
468 os << "height_special \"" << height_special << "\"\n";
472 void InsetBoxParams::read(LyXLex & lex)
479 type = lex.getString();
485 token = lex.getString();
486 if (token == "position") {
488 // The [0] is needed. We need the first and only char in
490 pos = lex.getString()[0];
492 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
493 lex.pushToken(token);
498 token = lex.getString();
499 if (token == "hor_pos") {
501 hor_pos = lex.getString()[0];
503 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
504 lex.pushToken(token);
509 token = lex.getString();
510 if (token == "has_inner_box") {
512 inner_box = lex.getInteger();
514 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
515 lex.pushToken(token);
521 token = lex.getString();
522 if (token == "inner_pos") {
524 inner_pos = lex.getString()[0];
526 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
528 lex.pushToken(token);
533 token = lex.getString();
534 if (token == "use_parbox") {
536 use_parbox = lex.getInteger();
538 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
539 lex.pushToken(token);
544 token = lex.getString();
545 if (token == "width") {
547 width = LyXLength(lex.getString());
549 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
550 lex.pushToken(token);
555 token = lex.getString();
556 if (token == "special") {
558 special = lex.getString();
560 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
561 lex.pushToken(token);
566 token = lex.getString();
567 if (token == "height") {
569 height = LyXLength(lex.getString());
571 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
572 lex.pushToken(token);
577 token = lex.getString();
578 if (token == "height_special") {
580 height_special = lex.getString();
582 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
583 lex.pushToken(token);