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);
217 case LFUN_CLIPBOARD_PASTE:
218 case LFUN_PRIMARY_SELECTION_PASTE:
219 InsetCollapsable::doDispatch(cur, cmd);
220 if (!params_.inner_box)
221 forceParagraphsToDefault(cur);
225 InsetCollapsable::doDispatch(cur, cmd);
231 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
232 FuncStatus & flag) const
234 switch (cmd.action) {
236 case LFUN_INSET_MODIFY:
237 case LFUN_INSET_DIALOG_UPDATE:
240 case LFUN_BREAK_PARAGRAPH:
241 if (params_.inner_box) {
242 return InsetCollapsable::getStatus(cur, cmd, flag);
249 return InsetCollapsable::getStatus(cur, cmd, flag);
254 int InsetBox::latex(Buffer const & buf, ostream & os,
255 OutputParams const & runparams) const
257 BoxType btype = boxtranslator().find(params_.type);
259 string width_string = params_.width.asLatexString();
260 bool stdwidth(false);
261 if (params_.inner_box &&
262 (width_string.find("1.0\\columnwidth") != string::npos
263 || width_string.find("1.0\\textwidth") != string::npos)) {
269 width_string += " - 2\\fboxsep - 2\\fboxrule";
272 width_string += " - 2\\fboxsep - 0.8pt";
275 width_string += " - 2\\fboxsep - 1.6pt";
278 // Shadow falls outside right margin... opinions?
279 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
282 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
289 // Adapt to column/text width correctly also if paragraphs indented:
298 if (!params_.inner_box) {
300 // Special widths, see usrguide §3.5
301 if (params_.special != "none") {
302 os << "[" << params_.width.value()
303 << "\\" << params_.special << "]";
305 os << "[" << width_string << "]";
306 if (params_.hor_pos != 'c')
307 os << "[" << params_.hor_pos << "]";
319 os << "\\shadowbox{";
322 os << "\\doublebox{";
326 if (params_.inner_box) {
327 if (params_.use_parbox)
330 os << "\\begin{minipage}";
332 os << "[" << params_.pos << "]";
333 if (params_.height_special == "none") {
334 os << "[" << params_.height.asLatexString() << "]";
337 os << "[" << params_.height.value()
338 << "\\" << params_.height_special << "]";
340 if (params_.inner_pos != params_.pos)
341 os << "[" << params_.inner_pos << "]";
343 os << "{" << width_string << "}";
345 if (params_.use_parbox)
351 i += InsetText::latex(buf, os, runparams);
353 if (params_.inner_box) {
354 if (params_.use_parbox)
357 os << "%\n\\end{minipage}";
364 if (!params_.inner_box)
365 os << "}"; // for makebox
383 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
384 OutputParams const & runparams) const
386 return InsetText::docbook(buf, os, runparams);
390 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
391 OutputParams const & runparams) const
393 BoxType const btype = boxtranslator().find(params_.type);
396 case Frameless: break;
397 case Boxed: os << "["; break;
398 case ovalbox: os << "("; break;
399 case Ovalbox: os << "(("; break;
400 case Shadowbox: os << "["; break;
401 case Doublebox: os << "[["; break;
404 int i = InsetText::plaintext(buf, os, runparams);
407 case Frameless: break;
408 case Boxed: os << "]"; break;
409 case ovalbox: os << ")"; break;
410 case Ovalbox: os << "))"; break;
411 case Shadowbox: os << "]/"; break;
412 case Doublebox: os << "]]"; break;
419 void InsetBox::validate(LaTeXFeatures & features) const
421 features.require("calc");
422 BoxType btype = boxtranslator().find(params_.type);
431 features.require("fancybox");
434 InsetText::validate(features);
438 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
443 string const InsetBoxMailer::name_ = "box";
446 string const InsetBoxMailer::inset2string(Buffer const &) const
448 return params2string(inset_.params());
452 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
455 data << "box" << ' ';
461 void InsetBoxMailer::string2params(string const & in,
462 InsetBoxParams & params)
464 params = InsetBoxParams(string());
468 istringstream data(in);
474 if (!lex || name != name_)
475 return print_mailer_error("InsetBoxMailer", in, 1, name_);
477 // This is part of the inset proper that is usually swallowed
478 // by LyXText::readInset
481 if (!lex || id != "Box")
482 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
488 InsetBoxParams::InsetBoxParams(string const & label)
492 width(LyXLength("100col%")),
497 height(LyXLength("1in")),
498 height_special("totalheight") // default is 1\\totalheight
502 void InsetBoxParams::write(ostream & os) const
504 os << "Box " << type << "\n";
505 os << "position \"" << pos << "\"\n";
506 os << "hor_pos \"" << hor_pos << "\"\n";
507 os << "has_inner_box " << inner_box << "\n";
508 os << "inner_pos \"" << inner_pos << "\"\n";
509 os << "use_parbox " << use_parbox << "\n";
510 os << "width \"" << width.asString() << "\"\n";
511 os << "special \"" << special << "\"\n";
512 os << "height \"" << height.asString() << "\"\n";
513 os << "height_special \"" << height_special << "\"\n";
517 void InsetBoxParams::read(LyXLex & lex)
524 type = lex.getString();
530 token = lex.getString();
531 if (token == "position") {
533 // The [0] is needed. We need the first and only char in
535 pos = lex.getString()[0];
537 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
538 lex.pushToken(token);
543 token = lex.getString();
544 if (token == "hor_pos") {
546 hor_pos = lex.getString()[0];
548 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
549 lex.pushToken(token);
554 token = lex.getString();
555 if (token == "has_inner_box") {
557 inner_box = lex.getInteger();
559 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
560 lex.pushToken(token);
566 token = lex.getString();
567 if (token == "inner_pos") {
569 inner_pos = lex.getString()[0];
571 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
573 lex.pushToken(token);
578 token = lex.getString();
579 if (token == "use_parbox") {
581 use_parbox = lex.getInteger();
583 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
584 lex.pushToken(token);
589 token = lex.getString();
590 if (token == "width") {
592 width = LyXLength(lex.getString());
594 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
595 lex.pushToken(token);
600 token = lex.getString();
601 if (token == "special") {
603 special = lex.getString();
605 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
606 lex.pushToken(token);
611 token = lex.getString();
612 if (token == "height") {
614 height = LyXLength(lex.getString());
616 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
617 lex.pushToken(token);
622 token = lex.getString();
623 if (token == "height_special") {
625 height_special = lex.getString();
627 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
628 lex.pushToken(token);