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"
35 using std::istringstream;
37 using std::ostringstream;
43 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
45 BoxTranslator const init_boxtranslator() {
46 BoxTranslator translator("Boxed", InsetBox::Boxed);
47 translator.addPair("Frameless", InsetBox::Frameless);
48 translator.addPair("ovalbox", InsetBox::ovalbox);
49 translator.addPair("Ovalbox", InsetBox::Ovalbox);
50 translator.addPair("Shadowbox", InsetBox::Shadowbox);
51 translator.addPair("Doublebox",InsetBox::Doublebox);
56 BoxTranslator const init_boxtranslator_loc() {
57 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
58 translator.addPair(_("Frameless"), InsetBox::Frameless);
59 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
60 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
61 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
62 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
67 BoxTranslator const & boxtranslator() {
68 static BoxTranslator translator = init_boxtranslator();
73 BoxTranslator const & boxtranslator_loc() {
74 static BoxTranslator translator = init_boxtranslator_loc();
88 InsetBox::InsetBox(BufferParams const & bp, string const & label)
89 : InsetCollapsable(bp), params_(label)
95 InsetBox::InsetBox(InsetBox const & in)
96 : InsetCollapsable(in), params_(in.params_)
102 InsetBox::~InsetBox()
104 InsetBoxMailer(*this).hideDialog();
108 auto_ptr<InsetBase> InsetBox::doClone() const
110 return auto_ptr<InsetBase>(new InsetBox(*this));
114 string const InsetBox::editMessage() const
116 return _("Opened Box Inset");
120 void InsetBox::write(Buffer const & buf, ostream & os) const
123 InsetCollapsable::write(buf, os);
127 void InsetBox::read(Buffer const & buf, LyXLex & lex)
130 InsetCollapsable::read(buf, lex);
135 void InsetBox::setButtonLabel()
137 LyXFont font(LyXFont::ALL_SANE);
141 BoxType btype = boxtranslator().find(params_.type);
142 if (btype == Frameless) {
143 if (params_.use_parbox)
144 setLabel(_("Box") + " (" + _("Parbox") + ")");
146 setLabel(_("Box") + " (" + _("Minipage") + ")");
148 setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
150 font.setColor(LColor::foreground);
151 setBackgroundColor(LColor::background);
156 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
159 if (params_.inner_box || params_.special != "width")
160 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
161 InsetCollapsable::metrics(mi, dim);
166 bool InsetBox::forceDefaultParagraphs(idx_type) const
168 return !params_.inner_box;
172 bool InsetBox::showInsetDialog(BufferView * bv) const
174 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
179 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
181 switch (cmd.action) {
183 case LFUN_INSET_MODIFY: {
184 lyxerr << "InsetBox::dispatch MODIFY" << endl;
185 InsetBoxMailer::string2params(cmd.argument, params_);
190 case LFUN_INSET_DIALOG_UPDATE:
191 InsetBoxMailer(*this).updateDialog(&cur.bv());
194 case LFUN_MOUSE_RELEASE:
195 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
196 InsetBoxMailer(*this).showDialog(&cur.bv());
199 InsetCollapsable::doDispatch(cur, cmd);
202 case LFUN_PRIMARY_SELECTION_PASTE:
203 InsetCollapsable::doDispatch(cur, cmd);
204 if (!params_.inner_box)
205 forceParagraphsToDefault(cur);
209 InsetCollapsable::doDispatch(cur, cmd);
215 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
216 FuncStatus & flag) const
218 switch (cmd.action) {
220 case LFUN_INSET_MODIFY:
221 case LFUN_INSET_DIALOG_UPDATE:
224 case LFUN_BREAK_PARAGRAPH:
225 if (params_.inner_box) {
226 return InsetCollapsable::getStatus(cur, cmd, flag);
233 return InsetCollapsable::getStatus(cur, cmd, flag);
238 int InsetBox::latex(Buffer const & buf, ostream & os,
239 OutputParams const & runparams) const
241 BoxType btype = boxtranslator().find(params_.type);
243 string width_string = params_.width.asLatexString();
244 bool stdwidth(false);
245 if (params_.inner_box &&
246 (width_string.find("1.0\\columnwidth") != string::npos
247 || width_string.find("1.0\\textwidth") != string::npos)) {
253 width_string += " - 2\\fboxsep - 2\\fboxrule";
256 width_string += " - 2\\fboxsep - 0.8pt";
259 width_string += " - 2\\fboxsep - 1.6pt";
262 // Shadow falls outside right margin... opinions?
263 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
266 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
273 // Adapt to column/text width correctly also if paragraphs indented:
282 if (!params_.inner_box) {
284 // Special widths, see usrguide §3.5
285 if (params_.special != "none") {
286 os << "[" << params_.width.value()
287 << "\\" << params_.special << "]";
289 os << "[" << width_string << "]";
290 if (params_.hor_pos != 'c')
291 os << "[" << params_.hor_pos << "]";
303 os << "\\shadowbox{";
306 os << "\\doublebox{";
310 if (params_.inner_box) {
311 if (params_.use_parbox)
314 os << "\\begin{minipage}";
316 os << "[" << params_.pos << "]";
317 if (params_.height_special == "none") {
318 os << "[" << params_.height.asLatexString() << "]";
321 os << "[" << params_.height.value()
322 << "\\" << params_.height_special << "]";
324 if (params_.inner_pos != params_.pos)
325 os << "[" << params_.inner_pos << "]";
327 os << "{" << width_string << "}";
329 if (params_.use_parbox)
335 i += InsetText::latex(buf, os, runparams);
337 if (params_.inner_box) {
338 if (params_.use_parbox)
341 os << "%\n\\end{minipage}";
348 if (!params_.inner_box)
349 os << "}"; // for makebox
367 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
368 OutputParams const & runparams) const
370 return InsetText::linuxdoc(buf, os, runparams);
374 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
375 OutputParams const & runparams) const
377 return InsetText::docbook(buf, os, runparams);
381 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
382 OutputParams const & runparams) const
384 BoxType const btype = boxtranslator().find(params_.type);
387 case Frameless: break;
388 case Boxed: os << "["; break;
389 case ovalbox: os << "("; break;
390 case Ovalbox: os << "(("; break;
391 case Shadowbox: os << "["; break;
392 case Doublebox: os << "[["; break;
395 int i = InsetText::plaintext(buf, os, runparams);
398 case Frameless: break;
399 case Boxed: os << "]"; break;
400 case ovalbox: os << ")"; break;
401 case Ovalbox: os << "))"; break;
402 case Shadowbox: os << "]/"; break;
403 case Doublebox: os << "]]"; break;
410 void InsetBox::validate(LaTeXFeatures & features) const
412 features.require("calc");
413 BoxType btype = boxtranslator().find(params_.type);
422 features.require("fancybox");
425 InsetText::validate(features);
429 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
434 string const InsetBoxMailer::name_ = "box";
437 string const InsetBoxMailer::inset2string(Buffer const &) const
439 return params2string(inset_.params());
443 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
446 data << "box" << ' ';
452 void InsetBoxMailer::string2params(string const & in,
453 InsetBoxParams & params)
455 params = InsetBoxParams(string());
459 istringstream data(in);
465 if (!lex || name != name_)
466 return print_mailer_error("InsetBoxMailer", in, 1, name_);
468 // This is part of the inset proper that is usually swallowed
469 // by LyXText::readInset
472 if (!lex || id != "Box")
473 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
479 InsetBoxParams::InsetBoxParams(string const & label)
483 width(LyXLength("100col%")),
488 height(LyXLength("1in")),
489 height_special("totalheight") // default is 1\\totalheight
493 void InsetBoxParams::write(ostream & os) const
495 os << "Box " << type << "\n";
496 os << "position \"" << pos << "\"\n";
497 os << "hor_pos \"" << hor_pos << "\"\n";
498 os << "has_inner_box " << inner_box << "\n";
499 os << "inner_pos \"" << inner_pos << "\"\n";
500 os << "use_parbox " << use_parbox << "\n";
501 os << "width \"" << width.asString() << "\"\n";
502 os << "special \"" << special << "\"\n";
503 os << "height \"" << height.asString() << "\"\n";
504 os << "height_special \"" << height_special << "\"\n";
508 void InsetBoxParams::read(LyXLex & lex)
515 type = lex.getString();
521 token = lex.getString();
522 if (token == "position") {
524 // The [0] is needed. We need the first and only char in
526 pos = lex.getString()[0];
528 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
529 lex.pushToken(token);
534 token = lex.getString();
535 if (token == "hor_pos") {
537 hor_pos = lex.getString()[0];
539 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
540 lex.pushToken(token);
545 token = lex.getString();
546 if (token == "has_inner_box") {
548 inner_box = lex.getInteger();
550 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
551 lex.pushToken(token);
557 token = lex.getString();
558 if (token == "inner_pos") {
560 inner_pos = lex.getString()[0];
562 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
564 lex.pushToken(token);
569 token = lex.getString();
570 if (token == "use_parbox") {
572 use_parbox = lex.getInteger();
574 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
575 lex.pushToken(token);
580 token = lex.getString();
581 if (token == "width") {
583 width = LyXLength(lex.getString());
585 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
586 lex.pushToken(token);
591 token = lex.getString();
592 if (token == "special") {
594 special = lex.getString();
596 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
597 lex.pushToken(token);
602 token = lex.getString();
603 if (token == "height") {
605 height = LyXLength(lex.getString());
607 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
608 lex.pushToken(token);
613 token = lex.getString();
614 if (token == "height_special") {
616 height_special = lex.getString();
618 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
619 lex.pushToken(token);