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(lyx::to_utf8(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_CLIPBOARD_PASTE:
203 case LFUN_PRIMARY_SELECTION_PASTE:
204 InsetCollapsable::doDispatch(cur, cmd);
205 if (!params_.inner_box)
206 forceParagraphsToDefault(cur);
210 InsetCollapsable::doDispatch(cur, cmd);
216 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
217 FuncStatus & flag) const
219 switch (cmd.action) {
221 case LFUN_INSET_MODIFY:
222 case LFUN_INSET_DIALOG_UPDATE:
225 case LFUN_BREAK_PARAGRAPH:
226 if (params_.inner_box) {
227 return InsetCollapsable::getStatus(cur, cmd, flag);
234 return InsetCollapsable::getStatus(cur, cmd, flag);
239 int InsetBox::latex(Buffer const & buf, ostream & os,
240 OutputParams const & runparams) const
242 BoxType btype = boxtranslator().find(params_.type);
244 string width_string = params_.width.asLatexString();
245 bool stdwidth(false);
246 if (params_.inner_box &&
247 (width_string.find("1.0\\columnwidth") != string::npos
248 || width_string.find("1.0\\textwidth") != string::npos)) {
254 width_string += " - 2\\fboxsep - 2\\fboxrule";
257 width_string += " - 2\\fboxsep - 0.8pt";
260 width_string += " - 2\\fboxsep - 1.6pt";
263 // Shadow falls outside right margin... opinions?
264 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
267 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
274 // Adapt to column/text width correctly also if paragraphs indented:
283 if (!params_.inner_box) {
285 // Special widths, see usrguide §3.5
286 if (params_.special != "none") {
287 os << "[" << params_.width.value()
288 << "\\" << params_.special << "]";
290 os << "[" << width_string << "]";
291 if (params_.hor_pos != 'c')
292 os << "[" << params_.hor_pos << "]";
304 os << "\\shadowbox{";
307 os << "\\doublebox{";
311 if (params_.inner_box) {
312 if (params_.use_parbox)
315 os << "\\begin{minipage}";
317 os << "[" << params_.pos << "]";
318 if (params_.height_special == "none") {
319 os << "[" << params_.height.asLatexString() << "]";
322 os << "[" << params_.height.value()
323 << "\\" << params_.height_special << "]";
325 if (params_.inner_pos != params_.pos)
326 os << "[" << params_.inner_pos << "]";
328 os << "{" << width_string << "}";
330 if (params_.use_parbox)
336 i += InsetText::latex(buf, os, runparams);
338 if (params_.inner_box) {
339 if (params_.use_parbox)
342 os << "%\n\\end{minipage}";
349 if (!params_.inner_box)
350 os << "}"; // for makebox
368 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
369 OutputParams const & runparams) const
371 return InsetText::docbook(buf, os, runparams);
375 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
376 OutputParams const & runparams) const
378 BoxType const btype = boxtranslator().find(params_.type);
381 case Frameless: break;
382 case Boxed: os << "["; break;
383 case ovalbox: os << "("; break;
384 case Ovalbox: os << "(("; break;
385 case Shadowbox: os << "["; break;
386 case Doublebox: os << "[["; break;
389 int i = InsetText::plaintext(buf, os, runparams);
392 case Frameless: break;
393 case Boxed: os << "]"; break;
394 case ovalbox: os << ")"; break;
395 case Ovalbox: os << "))"; break;
396 case Shadowbox: os << "]/"; break;
397 case Doublebox: os << "]]"; break;
404 void InsetBox::validate(LaTeXFeatures & features) const
406 features.require("calc");
407 BoxType btype = boxtranslator().find(params_.type);
416 features.require("fancybox");
419 InsetText::validate(features);
423 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
428 string const InsetBoxMailer::name_ = "box";
431 string const InsetBoxMailer::inset2string(Buffer const &) const
433 return params2string(inset_.params());
437 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
440 data << "box" << ' ';
446 void InsetBoxMailer::string2params(string const & in,
447 InsetBoxParams & params)
449 params = InsetBoxParams(string());
453 istringstream data(in);
459 if (!lex || name != name_)
460 return print_mailer_error("InsetBoxMailer", in, 1, name_);
462 // This is part of the inset proper that is usually swallowed
463 // by LyXText::readInset
466 if (!lex || id != "Box")
467 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
473 InsetBoxParams::InsetBoxParams(string const & label)
477 width(LyXLength("100col%")),
482 height(LyXLength("1in")),
483 height_special("totalheight") // default is 1\\totalheight
487 void InsetBoxParams::write(ostream & os) const
489 os << "Box " << type << "\n";
490 os << "position \"" << pos << "\"\n";
491 os << "hor_pos \"" << hor_pos << "\"\n";
492 os << "has_inner_box " << inner_box << "\n";
493 os << "inner_pos \"" << inner_pos << "\"\n";
494 os << "use_parbox " << use_parbox << "\n";
495 os << "width \"" << width.asString() << "\"\n";
496 os << "special \"" << special << "\"\n";
497 os << "height \"" << height.asString() << "\"\n";
498 os << "height_special \"" << height_special << "\"\n";
502 void InsetBoxParams::read(LyXLex & lex)
509 type = lex.getString();
515 token = lex.getString();
516 if (token == "position") {
518 // The [0] is needed. We need the first and only char in
520 pos = lex.getString()[0];
522 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
523 lex.pushToken(token);
528 token = lex.getString();
529 if (token == "hor_pos") {
531 hor_pos = lex.getString()[0];
533 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
534 lex.pushToken(token);
539 token = lex.getString();
540 if (token == "has_inner_box") {
542 inner_box = lex.getInteger();
544 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
545 lex.pushToken(token);
551 token = lex.getString();
552 if (token == "inner_pos") {
554 inner_pos = lex.getString()[0];
556 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
558 lex.pushToken(token);
563 token = lex.getString();
564 if (token == "use_parbox") {
566 use_parbox = lex.getInteger();
568 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
569 lex.pushToken(token);
574 token = lex.getString();
575 if (token == "width") {
577 width = LyXLength(lex.getString());
579 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
580 lex.pushToken(token);
585 token = lex.getString();
586 if (token == "special") {
588 special = lex.getString();
590 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
591 lex.pushToken(token);
596 token = lex.getString();
597 if (token == "height") {
599 height = LyXLength(lex.getString());
601 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
602 lex.pushToken(token);
607 token = lex.getString();
608 if (token == "height_special") {
610 height_special = lex.getString();
612 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
613 lex.pushToken(token);