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.
17 #include "BufferView.h"
18 #include "dispatchresult.h"
20 #include "funcrequest.h"
22 #include "LaTeXFeatures.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
28 #include "support/std_sstream.h"
29 #include "support/translator.h"
33 using std::istringstream;
35 using std::ostringstream;
41 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
43 BoxTranslator const init_boxtranslator() {
44 BoxTranslator translator("Boxed", InsetBox::Boxed);
45 translator.addPair("Frameless", InsetBox::Frameless);
46 translator.addPair("ovalbox", InsetBox::ovalbox);
47 translator.addPair("Ovalbox", InsetBox::Ovalbox);
48 translator.addPair("Shadowbox", InsetBox::Shadowbox);
49 translator.addPair("Doublebox",InsetBox::Doublebox);
54 BoxTranslator const init_boxtranslator_loc() {
55 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
56 translator.addPair(_("Frameless"), InsetBox::Frameless);
57 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
58 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
59 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
60 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
65 BoxTranslator const & boxtranslator() {
66 static BoxTranslator translator = init_boxtranslator();
71 BoxTranslator const & boxtranslator_loc() {
72 static BoxTranslator translator = init_boxtranslator_loc();
86 InsetBox::InsetBox(BufferParams const & bp, string const & label)
87 : InsetCollapsable(bp), params_(label)
93 InsetBox::InsetBox(InsetBox const & in)
94 : InsetCollapsable(in), params_(in.params_)
100 InsetBox::~InsetBox()
102 InsetBoxMailer(*this).hideDialog();
106 auto_ptr<InsetBase> InsetBox::clone() const
108 return auto_ptr<InsetBase>(new InsetBox(*this));
112 string const InsetBox::editMessage() const
114 return _("Opened Box Inset");
118 void InsetBox::write(Buffer const & buf, ostream & os) const
121 InsetCollapsable::write(buf, os);
125 void InsetBox::read(Buffer const & buf, LyXLex & lex)
128 InsetCollapsable::read(buf, lex);
133 void InsetBox::setButtonLabel()
135 LyXFont font(LyXFont::ALL_SANE);
139 BoxType btype = boxtranslator().find(params_.type);
140 if (btype == Frameless) {
141 if (params_.use_parbox)
142 setLabel(_("Box") + " (" + _("Parbox") + ")");
144 setLabel(_("Box") + " (" + _("Minipage") + ")");
146 setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
148 font.setColor(LColor::foreground);
149 setBackgroundColor(LColor::background);
154 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
157 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
158 InsetCollapsable::metrics(mi, dim);
159 //if (params_.inner_box && isOpen())
160 // dim.wid = mi.base.textwidth;
165 bool InsetBox::showInsetDialog(BufferView * bv) const
167 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
173 InsetBox::priv_dispatch(FuncRequest const & cmd,
174 idx_type & idx, pos_type & pos)
176 DispatchResult result(false);
177 BufferView * bv = cmd.view();
179 switch (cmd.action) {
181 case LFUN_INSET_MODIFY: {
182 lyxerr << "InsetBox::dispatch MODIFY" << endl;
183 InsetBoxMailer::string2params(cmd.argument, params_);
185 result.dispatched(true);
190 case LFUN_INSET_DIALOG_UPDATE:
191 InsetBoxMailer(*this).updateDialog(bv);
192 result.dispatched(true);
195 case LFUN_MOUSE_RELEASE:
196 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
197 InsetBoxMailer(*this).showDialog(bv);
198 return DispatchResult(true);
200 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
203 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
208 int InsetBox::latex(Buffer const & buf, ostream & os,
209 OutputParams const & runparams) const
211 BoxType btype = boxtranslator().find(params_.type);
213 string width_string = params_.width.asLatexString();
214 bool stdwidth(false);
215 if (params_.inner_box &&
216 (width_string.find("1.0\\columnwidth") != string::npos
217 || width_string.find("1.0\\textwidth") != string::npos)) {
223 width_string += " - 2\\fboxsep - 2\\fboxrule";
226 width_string += " - 2\\fboxsep - 0.8pt";
229 width_string += " - 2\\fboxsep - 1.6pt";
232 // Shadow falls outside right margin... opinions?
233 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
236 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
243 // Adapt to column/text width correctly also if paragraphs indented:
252 if (!params_.inner_box) {
254 // Special widths, see usrguide §3.5
255 if (params_.special != "none") {
256 os << "[" << params_.width.value()
257 << "\\" << params_.special << "]";
259 os << "[" << width_string << "]";
260 if (params_.hor_pos != 'c')
261 os << "[" << params_.hor_pos << "]";
273 os << "\\shadowbox{";
276 os << "\\doublebox{";
280 if (params_.inner_box) {
281 if (params_.use_parbox)
284 os << "\\begin{minipage}";
286 os << "[" << params_.pos << "]";
287 if (params_.height_special == "none") {
288 os << "[" << params_.height.asLatexString() << "]";
291 os << "[" << params_.height.value()
292 << "\\" << params_.height_special << "]";
294 if (params_.inner_pos != params_.pos)
295 os << "[" << params_.inner_pos << "]";
297 os << "{" << width_string << "}";
299 if (params_.use_parbox)
305 i += inset.latex(buf, os, runparams);
307 if (params_.inner_box) {
308 if (params_.use_parbox)
311 os << "%\n\\end{minipage}";
318 if (!params_.inner_box)
319 os << "}"; // for makebox
337 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
338 OutputParams const & runparams) const
340 return inset.linuxdoc(buf, os, runparams);
344 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
345 OutputParams const & runparams) const
347 return inset.docbook(buf, os, runparams);
351 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
352 OutputParams const & runparams) const
354 BoxType const btype = boxtranslator().find(params_.type);
357 case Frameless: break;
358 case Boxed: os << "["; break;
359 case ovalbox: os << "("; break;
360 case Ovalbox: os << "(("; break;
361 case Shadowbox: os << "["; break;
362 case Doublebox: os << "[["; break;
365 int i = inset.plaintext(buf, os, runparams);
368 case Frameless: break;
369 case Boxed: os << "]"; break;
370 case ovalbox: os << ")"; break;
371 case Ovalbox: os << "))"; break;
372 case Shadowbox: os << "]/"; break;
373 case Doublebox: os << "]]"; break;
380 void InsetBox::validate(LaTeXFeatures & features) const
382 features.require("calc");
383 BoxType btype = boxtranslator().find(params_.type);
392 features.require("fancybox");
395 inset.validate(features);
399 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
405 string const InsetBoxMailer::name_ = "box";
408 string const InsetBoxMailer::inset2string(Buffer const &) const
410 return params2string(inset_.params());
414 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
417 data << "box" << ' ';
423 void InsetBoxMailer::string2params(string const & in,
424 InsetBoxParams & params)
426 params = InsetBoxParams(string());
430 istringstream data(in);
436 if (!lex || name != name_)
437 return print_mailer_error("InsetBoxMailer", in, 1, name_);
439 // This is part of the inset proper that is usually swallowed
440 // by LyXText::readInset
443 if (!lex || id != "Box")
444 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
450 InsetBoxParams::InsetBoxParams(string const & label)
454 width(LyXLength("100col%")),
459 height(LyXLength("1in")),
460 height_special("totalheight") // default is 1\\totalheight
464 void InsetBoxParams::write(ostream & os) const
466 os << "Box " << type << "\n";
467 os << "position \"" << pos << "\"\n";
468 os << "hor_pos \"" << hor_pos << "\"\n";
469 os << "has_inner_box " << inner_box << "\n";
470 os << "inner_pos \"" << inner_pos << "\"\n";
471 os << "use_parbox " << use_parbox << "\n";
472 os << "width \"" << width.asString() << "\"\n";
473 os << "special \"" << special << "\"\n";
474 os << "height \"" << height.asString() << "\"\n";
475 os << "height_special \"" << height_special << "\"\n";
479 void InsetBoxParams::read(LyXLex & lex)
486 type = lex.getString();
492 token = lex.getString();
493 if (token == "position") {
495 // The [0] is needed. We need the first and only char in
497 pos = lex.getString()[0];
499 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
500 lex.pushToken(token);
505 token = lex.getString();
506 if (token == "hor_pos") {
508 hor_pos = lex.getString()[0];
510 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
511 lex.pushToken(token);
516 token = lex.getString();
517 if (token == "has_inner_box") {
519 inner_box = lex.getInteger();
521 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
522 lex.pushToken(token);
528 token = lex.getString();
529 if (token == "inner_pos") {
531 inner_pos = lex.getString()[0];
533 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
535 lex.pushToken(token);
540 token = lex.getString();
541 if (token == "use_parbox") {
543 use_parbox = lex.getInteger();
545 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
546 lex.pushToken(token);
551 token = lex.getString();
552 if (token == "width") {
554 width = LyXLength(lex.getString());
556 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
557 lex.pushToken(token);
562 token = lex.getString();
563 if (token == "special") {
565 special = lex.getString();
567 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
568 lex.pushToken(token);
573 token = lex.getString();
574 if (token == "height") {
576 height = LyXLength(lex.getString());
578 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
579 lex.pushToken(token);
584 token = lex.getString();
585 if (token == "height_special") {
587 height_special = lex.getString();
589 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
590 lex.pushToken(token);