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"
32 using std::istringstream;
34 using std::ostringstream;
40 BoxTranslator const init_boxtranslator() {
41 BoxTranslator translator("Boxed", InsetBox::Boxed);
42 translator.addPair("Frameless", InsetBox::Frameless);
43 translator.addPair("ovalbox", InsetBox::ovalbox);
44 translator.addPair("Ovalbox", InsetBox::Ovalbox);
45 translator.addPair("Shadowbox", InsetBox::Shadowbox);
46 translator.addPair("Doublebox",InsetBox::Doublebox);
51 BoxTranslator const init_boxtranslator_loc() {
52 BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
53 translator.addPair(_("Frameless"), InsetBox::Frameless);
54 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
55 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
56 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
57 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
62 BoxTranslator const & boxtranslator() {
63 static BoxTranslator translator = init_boxtranslator();
68 BoxTranslator const & boxtranslator_loc() {
69 static BoxTranslator translator = init_boxtranslator_loc();
83 InsetBox::InsetBox(BufferParams const & bp, string const & label)
84 : InsetCollapsable(bp), params_(label)
90 InsetBox::InsetBox(InsetBox const & in)
91 : InsetCollapsable(in), params_(in.params_)
99 InsetBoxMailer mailer(*this);
104 auto_ptr<InsetBase> InsetBox::clone() const
106 return auto_ptr<InsetBase>(new InsetBox(*this));
110 string const InsetBox::editMessage() const
112 return _("Opened Box Inset");
116 void InsetBox::write(Buffer const & buf, ostream & os) const
119 InsetCollapsable::write(buf, os);
123 void InsetBox::read(Buffer const & buf, LyXLex & lex)
126 InsetCollapsable::read(buf, lex);
131 void InsetBox::setButtonLabel()
133 LyXFont font(LyXFont::ALL_SANE);
137 BoxType btype = boxtranslator().find(params_.type);
138 if (btype == Frameless) {
139 if (params_.use_parbox)
140 setLabel(_("Parbox"));
142 setLabel(_("Minipage"));
144 setLabel(boxtranslator_loc().find(btype));
146 font.setColor(LColor::foreground);
147 setBackgroundColor(LColor::background);
152 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
155 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
156 InsetCollapsable::metrics(mi, dim);
157 //if (params_.inner_box && isOpen())
158 // dim.wid = mi.base.textwidth;
163 bool InsetBox::showInsetDialog(BufferView * bv) const
165 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
171 InsetBox::priv_dispatch(FuncRequest const & cmd,
172 idx_type & idx, pos_type & pos)
174 DispatchResult result(false);
175 BufferView * bv = cmd.view();
177 switch (cmd.action) {
179 case LFUN_INSET_MODIFY: {
180 lyxerr << "InsetBox::dispatch MODIFY" << endl;
181 InsetBoxMailer::string2params(cmd.argument, params_);
183 bv->updateInset(this);
184 result.dispatched(true);
189 case LFUN_INSET_DIALOG_UPDATE:
190 InsetBoxMailer(*this).updateDialog(bv);
191 result.dispatched(true);
194 case LFUN_MOUSE_RELEASE:
195 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
196 InsetBoxMailer(*this).showDialog(bv);
197 return DispatchResult(true);
199 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
202 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
207 int InsetBox::latex(Buffer const & buf, ostream & os,
208 OutputParams const & runparams) const
210 BoxType btype = boxtranslator().find(params_.type);
212 string width_string = params_.width.asLatexString();
213 bool stdwidth(false);
214 if (params_.inner_box &&
215 (width_string.find("1.0\\columnwidth") != string::npos
216 || width_string.find("1.0\\textwidth") != string::npos)) {
222 width_string += " - 2\\fboxsep - 2\\fboxrule";
225 width_string += " - 2\\fboxsep - 0.8pt";
228 width_string += " - 2\\fboxsep - 1.6pt";
231 // Shadow falls outside right margin... opinions?
232 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
235 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
242 // Adapt to column/text width correctly also if paragraphs indented:
251 if (!params_.inner_box) {
253 // Special widths, see usrguide §3.5
254 if (params_.special != "none") {
255 os << "[" << params_.width.value()
256 << "\\" << params_.special << "]";
258 os << "[" << width_string << "]";
259 if (params_.hor_pos != 'c')
260 os << "[" << params_.hor_pos << "]";
272 os << "\\shadowbox{";
275 os << "\\doublebox{";
279 if (params_.inner_box) {
280 if (params_.use_parbox)
283 os << "\\begin{minipage}";
285 os << "[" << params_.pos << "]";
286 if (params_.height_special == "none") {
287 os << "[" << params_.height.asLatexString() << "]";
290 os << "[" << params_.height.value()
291 << "\\" << params_.height_special << "]";
293 if (params_.inner_pos != params_.pos)
294 os << "[" << params_.inner_pos << "]";
296 os << "{" << width_string << "}";
298 if (params_.use_parbox)
304 i += inset.latex(buf, os, runparams);
306 if (params_.inner_box) {
307 if (params_.use_parbox)
310 os << "%\n\\end{minipage}";
317 if (!params_.inner_box)
318 os << "}"; // for makebox
336 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
337 OutputParams const & runparams) const
339 return inset.linuxdoc(buf, os, runparams);
343 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
344 OutputParams const & runparams) const
346 return inset.docbook(buf, os, runparams);
350 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
351 OutputParams const & runparams) const
353 BoxType const btype = boxtranslator().find(params_.type);
356 case Frameless: break;
357 case Boxed: os << "["; break;
358 case ovalbox: os << "("; break;
359 case Ovalbox: os << "(("; break;
360 case Shadowbox: os << "["; break;
361 case Doublebox: os << "[["; break;
364 int i = inset.plaintext(buf, os, runparams);
367 case Frameless: break;
368 case Boxed: os << "]"; break;
369 case ovalbox: os << ")"; break;
370 case Ovalbox: os << "))"; break;
371 case Shadowbox: os << "]/"; break;
372 case Doublebox: os << "]]"; break;
379 void InsetBox::validate(LaTeXFeatures & features) const
381 features.require("calc");
382 BoxType btype = boxtranslator().find(params_.type);
391 features.require("fancybox");
394 inset.validate(features);
398 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
404 string const InsetBoxMailer::name_ = "box";
407 string const InsetBoxMailer::inset2string(Buffer const &) const
409 return params2string(inset_.params());
413 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
416 data << "box" << ' ';
422 void InsetBoxMailer::string2params(string const & in,
423 InsetBoxParams & params)
425 params = InsetBoxParams(string());
430 istringstream data(in);
436 token = lex.getString();
443 InsetBoxParams::InsetBoxParams(string const & label)
447 width(LyXLength("100col%")),
452 height(LyXLength("1in")),
453 height_special("totalheight") // default is 1\\totalheight
457 void InsetBoxParams::write(ostream & os) const
460 os << "position \"" << pos << "\"\n";
461 os << "hor_pos \"" << hor_pos << "\"\n";
462 os << "has_inner_box " << inner_box << "\n";
463 os << "inner_pos \"" << inner_pos << "\"\n";
464 os << "use_parbox " << use_parbox << "\n";
465 os << "width \"" << width.asString() << "\"\n";
466 os << "special \"" << special << "\"\n";
467 os << "height \"" << height.asString() << "\"\n";
468 os << "height_special \"" << height_special << "\"\n";
472 void InsetBoxParams::read(LyXLex & lex)
475 type = lex.getString();
481 token = lex.getString();
482 if (token == "position") {
484 // The [0] is needed. We need the first and only char in
486 pos = lex.getString()[0];
488 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
489 lex.pushToken(token);
494 token = lex.getString();
495 if (token == "hor_pos") {
497 hor_pos = lex.getString()[0];
499 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
500 lex.pushToken(token);
505 token = lex.getString();
506 if (token == "has_inner_box") {
508 inner_box = lex.getInteger();
510 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
511 lex.pushToken(token);
517 token = lex.getString();
518 if (token == "inner_pos") {
520 inner_pos = lex.getString()[0];
522 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
524 lex.pushToken(token);
529 token = lex.getString();
530 if (token == "use_parbox") {
532 use_parbox = lex.getInteger();
534 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
535 lex.pushToken(token);
540 token = lex.getString();
541 if (token == "width") {
543 width = LyXLength(lex.getString());
545 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
546 lex.pushToken(token);
551 token = lex.getString();
552 if (token == "special") {
554 special = lex.getString();
556 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
557 lex.pushToken(token);
562 token = lex.getString();
563 if (token == "height") {
565 height = LyXLength(lex.getString());
567 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
568 lex.pushToken(token);
573 token = lex.getString();
574 if (token == "height_special") {
576 height_special = lex.getString();
578 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
579 lex.pushToken(token);