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"
37 using std::istringstream;
39 using std::ostringstream;
45 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
47 BoxTranslator const init_boxtranslator()
49 BoxTranslator translator("Boxed", InsetBox::Boxed);
50 translator.addPair("Frameless", InsetBox::Frameless);
51 translator.addPair("ovalbox", InsetBox::ovalbox);
52 translator.addPair("Ovalbox", InsetBox::Ovalbox);
53 translator.addPair("Shadowbox", InsetBox::Shadowbox);
54 translator.addPair("Doublebox",InsetBox::Doublebox);
59 BoxTranslator const init_boxtranslator_loc()
62 BoxTranslator translator(lyx::to_utf8(_("Boxed")), InsetBox::Boxed);
63 translator.addPair(lyx::to_utf8(_("Frameless")), InsetBox::Frameless);
64 translator.addPair(lyx::to_utf8(_("ovalbox")), InsetBox::ovalbox);
65 translator.addPair(lyx::to_utf8(_("Ovalbox")), InsetBox::Ovalbox);
66 translator.addPair(lyx::to_utf8(_("Shadowbox")), InsetBox::Shadowbox);
67 translator.addPair(lyx::to_utf8(_("Doublebox")), InsetBox::Doublebox);
72 BoxTranslator const & boxtranslator()
74 static BoxTranslator translator = init_boxtranslator();
79 BoxTranslator const & boxtranslator_loc()
81 static BoxTranslator translator = init_boxtranslator_loc();
95 InsetBox::InsetBox(BufferParams const & bp, string const & label)
96 : InsetCollapsable(bp), params_(label)
102 InsetBox::InsetBox(InsetBox const & in)
103 : InsetCollapsable(in), params_(in.params_)
109 InsetBox::~InsetBox()
111 InsetBoxMailer(*this).hideDialog();
115 auto_ptr<InsetBase> InsetBox::doClone() const
117 return auto_ptr<InsetBase>(new InsetBox(*this));
121 docstring const InsetBox::editMessage() const
123 return _("Opened Box Inset");
127 void InsetBox::write(Buffer const & buf, ostream & os) const
130 InsetCollapsable::write(buf, os);
134 void InsetBox::read(Buffer const & buf, LyXLex & lex)
137 InsetCollapsable::read(buf, lex);
142 void InsetBox::setButtonLabel()
144 LyXFont font(LyXFont::ALL_SANE);
148 BoxType btype = boxtranslator().find(params_.type);
152 label += lyx::to_utf8(_("Box"));
154 if (btype == Frameless) {
155 if (params_.use_parbox)
156 label += lyx::to_utf8(_("Parbox"));
158 label += lyx::to_utf8(_("Minipage"));
160 label += boxtranslator_loc().find(btype);
165 font.setColor(LColor::foreground);
166 setBackgroundColor(LColor::background);
171 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
174 if (params_.inner_box || params_.special != "width")
175 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
176 InsetCollapsable::metrics(mi, dim);
181 bool InsetBox::forceDefaultParagraphs(idx_type) const
183 return !params_.inner_box;
187 bool InsetBox::showInsetDialog(BufferView * bv) const
189 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
194 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
196 switch (cmd.action) {
198 case LFUN_INSET_MODIFY: {
199 lyxerr << "InsetBox::dispatch MODIFY" << endl;
200 InsetBoxMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
205 case LFUN_INSET_DIALOG_UPDATE:
206 InsetBoxMailer(*this).updateDialog(&cur.bv());
209 case LFUN_MOUSE_RELEASE:
210 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
211 InsetBoxMailer(*this).showDialog(&cur.bv());
214 InsetCollapsable::doDispatch(cur, cmd);
218 InsetCollapsable::doDispatch(cur, cmd);
224 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
225 FuncStatus & flag) const
227 switch (cmd.action) {
229 case LFUN_INSET_MODIFY:
230 case LFUN_INSET_DIALOG_UPDATE:
233 case LFUN_BREAK_PARAGRAPH:
234 if (params_.inner_box) {
235 return InsetCollapsable::getStatus(cur, cmd, flag);
242 return InsetCollapsable::getStatus(cur, cmd, flag);
247 int InsetBox::latex(Buffer const & buf, ostream & os,
248 OutputParams const & runparams) const
250 BoxType btype = boxtranslator().find(params_.type);
252 string width_string = params_.width.asLatexString();
253 bool stdwidth(false);
254 if (params_.inner_box &&
255 (width_string.find("1.0\\columnwidth") != string::npos
256 || width_string.find("1.0\\textwidth") != string::npos)) {
262 width_string += " - 2\\fboxsep - 2\\fboxrule";
265 width_string += " - 2\\fboxsep - 0.8pt";
268 width_string += " - 2\\fboxsep - 1.6pt";
271 // Shadow falls outside right margin... opinions?
272 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
275 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
282 // Adapt to column/text width correctly also if paragraphs indented:
291 if (!params_.inner_box) {
293 // Special widths, see usrguide §3.5
294 if (params_.special != "none") {
295 os << "[" << params_.width.value()
296 << "\\" << params_.special << "]";
298 os << "[" << width_string << "]";
299 if (params_.hor_pos != 'c')
300 os << "[" << params_.hor_pos << "]";
312 os << "\\shadowbox{";
315 os << "\\doublebox{";
319 if (params_.inner_box) {
320 if (params_.use_parbox)
323 os << "\\begin{minipage}";
325 os << "[" << params_.pos << "]";
326 if (params_.height_special == "none") {
327 os << "[" << params_.height.asLatexString() << "]";
330 os << "[" << params_.height.value()
331 << "\\" << params_.height_special << "]";
333 if (params_.inner_pos != params_.pos)
334 os << "[" << params_.inner_pos << "]";
336 os << "{" << width_string << "}";
338 if (params_.use_parbox)
344 i += InsetText::latex(buf, os, runparams);
346 if (params_.inner_box) {
347 if (params_.use_parbox)
350 os << "%\n\\end{minipage}";
357 if (!params_.inner_box)
358 os << "}"; // for makebox
376 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
377 OutputParams const & runparams) const
379 return InsetText::docbook(buf, os, runparams);
383 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
384 OutputParams const & runparams) const
386 BoxType const btype = boxtranslator().find(params_.type);
389 case Frameless: break;
390 case Boxed: os << "["; break;
391 case ovalbox: os << "("; break;
392 case Ovalbox: os << "(("; break;
393 case Shadowbox: os << "["; break;
394 case Doublebox: os << "[["; break;
397 int i = InsetText::plaintext(buf, os, runparams);
400 case Frameless: break;
401 case Boxed: os << "]"; break;
402 case ovalbox: os << ")"; break;
403 case Ovalbox: os << "))"; break;
404 case Shadowbox: os << "]/"; break;
405 case Doublebox: os << "]]"; break;
412 void InsetBox::validate(LaTeXFeatures & features) const
414 features.require("calc");
415 BoxType btype = boxtranslator().find(params_.type);
424 features.require("fancybox");
427 InsetText::validate(features);
431 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
436 string const InsetBoxMailer::name_ = "box";
439 string const InsetBoxMailer::inset2string(Buffer const &) const
441 return params2string(inset_.params());
445 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
448 data << "box" << ' ';
454 void InsetBoxMailer::string2params(string const & in,
455 InsetBoxParams & params)
457 params = InsetBoxParams(string());
461 istringstream data(in);
467 if (!lex || name != name_)
468 return print_mailer_error("InsetBoxMailer", in, 1, name_);
470 // This is part of the inset proper that is usually swallowed
471 // by LyXText::readInset
474 if (!lex || id != "Box")
475 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
481 InsetBoxParams::InsetBoxParams(string const & label)
485 width(LyXLength("100col%")),
490 height(LyXLength("1in")),
491 height_special("totalheight") // default is 1\\totalheight
495 void InsetBoxParams::write(ostream & os) const
497 os << "Box " << type << "\n";
498 os << "position \"" << pos << "\"\n";
499 os << "hor_pos \"" << hor_pos << "\"\n";
500 os << "has_inner_box " << inner_box << "\n";
501 os << "inner_pos \"" << inner_pos << "\"\n";
502 os << "use_parbox " << use_parbox << "\n";
503 os << "width \"" << width.asString() << "\"\n";
504 os << "special \"" << special << "\"\n";
505 os << "height \"" << height.asString() << "\"\n";
506 os << "height_special \"" << height_special << "\"\n";
510 void InsetBoxParams::read(LyXLex & lex)
517 type = lex.getString();
523 token = lex.getString();
524 if (token == "position") {
526 // The [0] is needed. We need the first and only char in
528 pos = lex.getString()[0];
530 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
531 lex.pushToken(token);
536 token = lex.getString();
537 if (token == "hor_pos") {
539 hor_pos = lex.getString()[0];
541 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
542 lex.pushToken(token);
547 token = lex.getString();
548 if (token == "has_inner_box") {
550 inner_box = lex.getInteger();
552 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
553 lex.pushToken(token);
559 token = lex.getString();
560 if (token == "inner_pos") {
562 inner_pos = lex.getString()[0];
564 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
566 lex.pushToken(token);
571 token = lex.getString();
572 if (token == "use_parbox") {
574 use_parbox = lex.getInteger();
576 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
577 lex.pushToken(token);
582 token = lex.getString();
583 if (token == "width") {
585 width = LyXLength(lex.getString());
587 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
588 lex.pushToken(token);
593 token = lex.getString();
594 if (token == "special") {
596 special = lex.getString();
598 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
599 lex.pushToken(token);
604 token = lex.getString();
605 if (token == "height") {
607 height = LyXLength(lex.getString());
609 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
610 lex.pushToken(token);
615 token = lex.getString();
616 if (token == "height_special") {
618 height_special = lex.getString();
620 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
621 lex.pushToken(token);