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 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
160 InsetCollapsable::metrics(mi, dim);
161 //if (params_.inner_box && isOpen())
162 // dim.wid = mi.base.textwidth;
167 bool InsetBox::showInsetDialog(BufferView * bv) const
169 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
174 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
176 switch (cmd.action) {
178 case LFUN_INSET_MODIFY: {
179 lyxerr << "InsetBox::dispatch MODIFY" << endl;
180 InsetBoxMailer::string2params(cmd.argument, params_);
185 case LFUN_INSET_DIALOG_UPDATE:
186 InsetBoxMailer(*this).updateDialog(&cur.bv());
189 case LFUN_MOUSE_RELEASE:
190 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
191 InsetBoxMailer(*this).showDialog(&cur.bv());
194 InsetCollapsable::doDispatch(cur, cmd);
198 InsetCollapsable::doDispatch(cur, cmd);
204 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
205 FuncStatus & flag) const
207 switch (cmd.action) {
209 case LFUN_INSET_MODIFY:
210 case LFUN_INSET_DIALOG_UPDATE:
215 return InsetCollapsable::getStatus(cur, cmd, flag);
220 int InsetBox::latex(Buffer const & buf, ostream & os,
221 OutputParams const & runparams) const
223 BoxType btype = boxtranslator().find(params_.type);
225 string width_string = params_.width.asLatexString();
226 bool stdwidth(false);
227 if (params_.inner_box &&
228 (width_string.find("1.0\\columnwidth") != string::npos
229 || width_string.find("1.0\\textwidth") != string::npos)) {
235 width_string += " - 2\\fboxsep - 2\\fboxrule";
238 width_string += " - 2\\fboxsep - 0.8pt";
241 width_string += " - 2\\fboxsep - 1.6pt";
244 // Shadow falls outside right margin... opinions?
245 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
248 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
255 // Adapt to column/text width correctly also if paragraphs indented:
264 if (!params_.inner_box) {
266 // Special widths, see usrguide §3.5
267 if (params_.special != "none") {
268 os << "[" << params_.width.value()
269 << "\\" << params_.special << "]";
271 os << "[" << width_string << "]";
272 if (params_.hor_pos != 'c')
273 os << "[" << params_.hor_pos << "]";
285 os << "\\shadowbox{";
288 os << "\\doublebox{";
292 if (params_.inner_box) {
293 if (params_.use_parbox)
296 os << "\\begin{minipage}";
298 os << "[" << params_.pos << "]";
299 if (params_.height_special == "none") {
300 os << "[" << params_.height.asLatexString() << "]";
303 os << "[" << params_.height.value()
304 << "\\" << params_.height_special << "]";
306 if (params_.inner_pos != params_.pos)
307 os << "[" << params_.inner_pos << "]";
309 os << "{" << width_string << "}";
311 if (params_.use_parbox)
317 i += InsetText::latex(buf, os, runparams);
319 if (params_.inner_box) {
320 if (params_.use_parbox)
323 os << "%\n\\end{minipage}";
330 if (!params_.inner_box)
331 os << "}"; // for makebox
349 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
350 OutputParams const & runparams) const
352 return InsetText::linuxdoc(buf, os, runparams);
356 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
357 OutputParams const & runparams) const
359 return InsetText::docbook(buf, os, runparams);
363 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
364 OutputParams const & runparams) const
366 BoxType const btype = boxtranslator().find(params_.type);
369 case Frameless: break;
370 case Boxed: os << "["; break;
371 case ovalbox: os << "("; break;
372 case Ovalbox: os << "(("; break;
373 case Shadowbox: os << "["; break;
374 case Doublebox: os << "[["; break;
377 int i = InsetText::plaintext(buf, os, runparams);
380 case Frameless: break;
381 case Boxed: os << "]"; break;
382 case ovalbox: os << ")"; break;
383 case Ovalbox: os << "))"; break;
384 case Shadowbox: os << "]/"; break;
385 case Doublebox: os << "]]"; break;
392 void InsetBox::validate(LaTeXFeatures & features) const
394 features.require("calc");
395 BoxType btype = boxtranslator().find(params_.type);
404 features.require("fancybox");
407 InsetText::validate(features);
411 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
416 string const InsetBoxMailer::name_ = "box";
419 string const InsetBoxMailer::inset2string(Buffer const &) const
421 return params2string(inset_.params());
425 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
428 data << "box" << ' ';
434 void InsetBoxMailer::string2params(string const & in,
435 InsetBoxParams & params)
437 params = InsetBoxParams(string());
441 istringstream data(in);
447 if (!lex || name != name_)
448 return print_mailer_error("InsetBoxMailer", in, 1, name_);
450 // This is part of the inset proper that is usually swallowed
451 // by LyXText::readInset
454 if (!lex || id != "Box")
455 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
461 InsetBoxParams::InsetBoxParams(string const & label)
465 width(LyXLength("100col%")),
470 height(LyXLength("1in")),
471 height_special("totalheight") // default is 1\\totalheight
475 void InsetBoxParams::write(ostream & os) const
477 os << "Box " << type << "\n";
478 os << "position \"" << pos << "\"\n";
479 os << "hor_pos \"" << hor_pos << "\"\n";
480 os << "has_inner_box " << inner_box << "\n";
481 os << "inner_pos \"" << inner_pos << "\"\n";
482 os << "use_parbox " << use_parbox << "\n";
483 os << "width \"" << width.asString() << "\"\n";
484 os << "special \"" << special << "\"\n";
485 os << "height \"" << height.asString() << "\"\n";
486 os << "height_special \"" << height_special << "\"\n";
490 void InsetBoxParams::read(LyXLex & lex)
497 type = lex.getString();
503 token = lex.getString();
504 if (token == "position") {
506 // The [0] is needed. We need the first and only char in
508 pos = lex.getString()[0];
510 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
511 lex.pushToken(token);
516 token = lex.getString();
517 if (token == "hor_pos") {
519 hor_pos = lex.getString()[0];
521 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
522 lex.pushToken(token);
527 token = lex.getString();
528 if (token == "has_inner_box") {
530 inner_box = lex.getInteger();
532 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
533 lex.pushToken(token);
539 token = lex.getString();
540 if (token == "inner_pos") {
542 inner_pos = lex.getString()[0];
544 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
546 lex.pushToken(token);
551 token = lex.getString();
552 if (token == "use_parbox") {
554 use_parbox = lex.getInteger();
556 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
557 lex.pushToken(token);
562 token = lex.getString();
563 if (token == "width") {
565 width = LyXLength(lex.getString());
567 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
568 lex.pushToken(token);
573 token = lex.getString();
574 if (token == "special") {
576 special = lex.getString();
578 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
579 lex.pushToken(token);
584 token = lex.getString();
585 if (token == "height") {
587 height = LyXLength(lex.getString());
589 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
590 lex.pushToken(token);
595 token = lex.getString();
596 if (token == "height_special") {
598 height_special = lex.getString();
600 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
601 lex.pushToken(token);