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()
47 BoxTranslator translator("Boxed", InsetBox::Boxed);
48 translator.addPair("Frameless", InsetBox::Frameless);
49 translator.addPair("ovalbox", InsetBox::ovalbox);
50 translator.addPair("Ovalbox", InsetBox::Ovalbox);
51 translator.addPair("Shadowbox", InsetBox::Shadowbox);
52 translator.addPair("Doublebox",InsetBox::Doublebox);
57 BoxTranslator const init_boxtranslator_loc()
60 BoxTranslator translator(lyx::to_utf8(_("Boxed")), InsetBox::Boxed);
61 translator.addPair(lyx::to_utf8(_("Frameless")), InsetBox::Frameless);
62 translator.addPair(lyx::to_utf8(_("ovalbox")), InsetBox::ovalbox);
63 translator.addPair(lyx::to_utf8(_("Ovalbox")), InsetBox::Ovalbox);
64 translator.addPair(lyx::to_utf8(_("Shadowbox")), InsetBox::Shadowbox);
65 translator.addPair(lyx::to_utf8(_("Doublebox")), InsetBox::Doublebox);
70 BoxTranslator const & boxtranslator()
72 static BoxTranslator translator = init_boxtranslator();
77 BoxTranslator const & boxtranslator_loc()
79 static BoxTranslator translator = init_boxtranslator_loc();
93 InsetBox::InsetBox(BufferParams const & bp, string const & label)
94 : InsetCollapsable(bp), params_(label)
100 InsetBox::InsetBox(InsetBox const & in)
101 : InsetCollapsable(in), params_(in.params_)
107 InsetBox::~InsetBox()
109 InsetBoxMailer(*this).hideDialog();
113 auto_ptr<InsetBase> InsetBox::doClone() const
115 return auto_ptr<InsetBase>(new InsetBox(*this));
119 string const InsetBox::editMessage() const
121 return lyx::to_utf8(_("Opened Box Inset"));
125 void InsetBox::write(Buffer const & buf, ostream & os) const
128 InsetCollapsable::write(buf, os);
132 void InsetBox::read(Buffer const & buf, LyXLex & lex)
135 InsetCollapsable::read(buf, lex);
140 void InsetBox::setButtonLabel()
142 LyXFont font(LyXFont::ALL_SANE);
146 BoxType btype = boxtranslator().find(params_.type);
150 label += lyx::to_utf8(_("Box"));
152 if (btype == Frameless) {
153 if (params_.use_parbox)
154 label += lyx::to_utf8(_("Parbox"));
156 label += lyx::to_utf8(_("Minipage"));
158 label += boxtranslator_loc().find(btype);
163 font.setColor(LColor::foreground);
164 setBackgroundColor(LColor::background);
169 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
172 if (params_.inner_box || params_.special != "width")
173 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
174 InsetCollapsable::metrics(mi, dim);
179 bool InsetBox::forceDefaultParagraphs(idx_type) const
181 return !params_.inner_box;
185 bool InsetBox::showInsetDialog(BufferView * bv) const
187 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
192 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
194 switch (cmd.action) {
196 case LFUN_INSET_MODIFY: {
197 lyxerr << "InsetBox::dispatch MODIFY" << endl;
198 InsetBoxMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
203 case LFUN_INSET_DIALOG_UPDATE:
204 InsetBoxMailer(*this).updateDialog(&cur.bv());
207 case LFUN_MOUSE_RELEASE:
208 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
209 InsetBoxMailer(*this).showDialog(&cur.bv());
212 InsetCollapsable::doDispatch(cur, cmd);
215 case LFUN_CLIPBOARD_PASTE:
216 case LFUN_PRIMARY_SELECTION_PASTE:
217 InsetCollapsable::doDispatch(cur, cmd);
218 if (!params_.inner_box)
219 forceParagraphsToDefault(cur);
223 InsetCollapsable::doDispatch(cur, cmd);
229 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
230 FuncStatus & flag) const
232 switch (cmd.action) {
234 case LFUN_INSET_MODIFY:
235 case LFUN_INSET_DIALOG_UPDATE:
238 case LFUN_BREAK_PARAGRAPH:
239 if (params_.inner_box) {
240 return InsetCollapsable::getStatus(cur, cmd, flag);
247 return InsetCollapsable::getStatus(cur, cmd, flag);
252 int InsetBox::latex(Buffer const & buf, ostream & os,
253 OutputParams const & runparams) const
255 BoxType btype = boxtranslator().find(params_.type);
257 string width_string = params_.width.asLatexString();
258 bool stdwidth(false);
259 if (params_.inner_box &&
260 (width_string.find("1.0\\columnwidth") != string::npos
261 || width_string.find("1.0\\textwidth") != string::npos)) {
267 width_string += " - 2\\fboxsep - 2\\fboxrule";
270 width_string += " - 2\\fboxsep - 0.8pt";
273 width_string += " - 2\\fboxsep - 1.6pt";
276 // Shadow falls outside right margin... opinions?
277 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
280 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
287 // Adapt to column/text width correctly also if paragraphs indented:
296 if (!params_.inner_box) {
298 // Special widths, see usrguide §3.5
299 if (params_.special != "none") {
300 os << "[" << params_.width.value()
301 << "\\" << params_.special << "]";
303 os << "[" << width_string << "]";
304 if (params_.hor_pos != 'c')
305 os << "[" << params_.hor_pos << "]";
317 os << "\\shadowbox{";
320 os << "\\doublebox{";
324 if (params_.inner_box) {
325 if (params_.use_parbox)
328 os << "\\begin{minipage}";
330 os << "[" << params_.pos << "]";
331 if (params_.height_special == "none") {
332 os << "[" << params_.height.asLatexString() << "]";
335 os << "[" << params_.height.value()
336 << "\\" << params_.height_special << "]";
338 if (params_.inner_pos != params_.pos)
339 os << "[" << params_.inner_pos << "]";
341 os << "{" << width_string << "}";
343 if (params_.use_parbox)
349 i += InsetText::latex(buf, os, runparams);
351 if (params_.inner_box) {
352 if (params_.use_parbox)
355 os << "%\n\\end{minipage}";
362 if (!params_.inner_box)
363 os << "}"; // for makebox
381 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
382 OutputParams const & runparams) const
384 return InsetText::docbook(buf, os, runparams);
388 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
389 OutputParams const & runparams) const
391 BoxType const btype = boxtranslator().find(params_.type);
394 case Frameless: break;
395 case Boxed: os << "["; break;
396 case ovalbox: os << "("; break;
397 case Ovalbox: os << "(("; break;
398 case Shadowbox: os << "["; break;
399 case Doublebox: os << "[["; break;
402 int i = InsetText::plaintext(buf, os, runparams);
405 case Frameless: break;
406 case Boxed: os << "]"; break;
407 case ovalbox: os << ")"; break;
408 case Ovalbox: os << "))"; break;
409 case Shadowbox: os << "]/"; break;
410 case Doublebox: os << "]]"; break;
417 void InsetBox::validate(LaTeXFeatures & features) const
419 features.require("calc");
420 BoxType btype = boxtranslator().find(params_.type);
429 features.require("fancybox");
432 InsetText::validate(features);
436 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
441 string const InsetBoxMailer::name_ = "box";
444 string const InsetBoxMailer::inset2string(Buffer const &) const
446 return params2string(inset_.params());
450 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
453 data << "box" << ' ';
459 void InsetBoxMailer::string2params(string const & in,
460 InsetBoxParams & params)
462 params = InsetBoxParams(string());
466 istringstream data(in);
472 if (!lex || name != name_)
473 return print_mailer_error("InsetBoxMailer", in, 1, name_);
475 // This is part of the inset proper that is usually swallowed
476 // by LyXText::readInset
479 if (!lex || id != "Box")
480 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
486 InsetBoxParams::InsetBoxParams(string const & label)
490 width(LyXLength("100col%")),
495 height(LyXLength("1in")),
496 height_special("totalheight") // default is 1\\totalheight
500 void InsetBoxParams::write(ostream & os) const
502 os << "Box " << type << "\n";
503 os << "position \"" << pos << "\"\n";
504 os << "hor_pos \"" << hor_pos << "\"\n";
505 os << "has_inner_box " << inner_box << "\n";
506 os << "inner_pos \"" << inner_pos << "\"\n";
507 os << "use_parbox " << use_parbox << "\n";
508 os << "width \"" << width.asString() << "\"\n";
509 os << "special \"" << special << "\"\n";
510 os << "height \"" << height.asString() << "\"\n";
511 os << "height_special \"" << height_special << "\"\n";
515 void InsetBoxParams::read(LyXLex & lex)
522 type = lex.getString();
528 token = lex.getString();
529 if (token == "position") {
531 // The [0] is needed. We need the first and only char in
533 pos = lex.getString()[0];
535 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
536 lex.pushToken(token);
541 token = lex.getString();
542 if (token == "hor_pos") {
544 hor_pos = lex.getString()[0];
546 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
547 lex.pushToken(token);
552 token = lex.getString();
553 if (token == "has_inner_box") {
555 inner_box = lex.getInteger();
557 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
558 lex.pushToken(token);
564 token = lex.getString();
565 if (token == "inner_pos") {
567 inner_pos = lex.getString()[0];
569 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
571 lex.pushToken(token);
576 token = lex.getString();
577 if (token == "use_parbox") {
579 use_parbox = lex.getInteger();
581 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
582 lex.pushToken(token);
587 token = lex.getString();
588 if (token == "width") {
590 width = LyXLength(lex.getString());
592 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
593 lex.pushToken(token);
598 token = lex.getString();
599 if (token == "special") {
601 special = lex.getString();
603 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
604 lex.pushToken(token);
609 token = lex.getString();
610 if (token == "height") {
612 height = LyXLength(lex.getString());
614 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
615 lex.pushToken(token);
620 token = lex.getString();
621 if (token == "height_special") {
623 height_special = lex.getString();
625 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
626 lex.pushToken(token);