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 & mi, Dimension & dim) const
154 InsetCollapsable::metrics(mi, dim);
155 if (params_.inner_box && isOpen())
156 dim.wid = mi.base.textwidth;
161 bool InsetBox::showInsetDialog(BufferView * bv) const
163 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
169 InsetBox::priv_dispatch(FuncRequest const & cmd,
170 idx_type & idx, pos_type & pos)
172 DispatchResult result(false);
173 BufferView * bv = cmd.view();
175 switch (cmd.action) {
177 case LFUN_INSET_MODIFY: {
178 InsetBoxMailer::string2params(cmd.argument, params_);
180 bv->updateInset(this);
181 result.dispatched(true);
185 case LFUN_INSET_DIALOG_UPDATE:
186 InsetBoxMailer(*this).updateDialog(bv);
187 result.dispatched(true);
190 case LFUN_MOUSE_RELEASE:
191 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
192 InsetBoxMailer(*this).showDialog(bv);
193 return DispatchResult(true);
198 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
206 int InsetBox::latex(Buffer const & buf, ostream & os,
207 LatexRunParams const & runparams) const
209 BoxType btype = boxtranslator().find(params_.type);
211 string width_string = params_.width.asLatexString();
212 bool stdwidth(false);
213 if (params_.inner_box &&
214 (width_string.find("1.0\\columnwidth") != string::npos
215 || width_string.find("1.0\\textwidth") != string::npos)) {
221 width_string += " - 2\\fboxsep - 2\\fboxrule";
224 width_string += " - 2\\fboxsep - 0.8pt";
227 width_string += " - 2\\fboxsep - 1.6pt";
230 // Shadow falls outside right margin... opinions?
231 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
234 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
241 // Adapt to column/text width correctly also if paragraphs indented:
250 if (!params_.inner_box) {
252 // Special widths, see usrguide §3.5
253 if (params_.special != "none") {
254 os << "[" << params_.width.value()
255 << "\\" << params_.special << "]";
257 os << "[" << width_string << "]";
258 if (params_.hor_pos != 'c')
259 os << "[" << params_.hor_pos << "]";
271 os << "\\shadowbox{";
274 os << "\\doublebox{";
278 if (params_.inner_box) {
279 if (params_.use_parbox)
282 os << "\\begin{minipage}";
284 os << "[" << params_.pos << "]";
285 if (params_.height_special == "none") {
286 os << "[" << params_.height.asLatexString() << "]";
289 os << "[" << params_.height.value()
290 << "\\" << params_.height_special << "]";
292 if (params_.inner_pos != params_.pos)
293 os << "[" << params_.inner_pos << "]";
295 os << "{" << width_string << "}";
297 if (params_.use_parbox)
303 i += inset.latex(buf, os, runparams);
305 if (params_.inner_box) {
306 if (params_.use_parbox)
309 os << "%\n\\end{minipage}";
316 if (!params_.inner_box)
317 os << "}"; // for makebox
335 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
336 LatexRunParams const & runparams) const
338 return inset.linuxdoc(buf, os, runparams);
342 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
343 LatexRunParams const & runparams) const
345 return inset.docbook(buf, os, runparams);
349 int InsetBox::ascii(Buffer const & buf, std::ostream & os,
350 LatexRunParams const & runparams) const
353 string const pt = params_.type;
354 BoxType btype = boxtranslator().find(params_.type);
375 i = inset.ascii(buf, os, runparams);
401 void InsetBox::validate(LaTeXFeatures & features) const
403 features.require("calc");
404 BoxType btype = boxtranslator().find(params_.type);
413 features.require("fancybox");
416 inset.validate(features);
420 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
426 string const InsetBoxMailer::name_ = "box";
429 string const InsetBoxMailer::inset2string(Buffer const &) const
431 return params2string(inset_.params());
435 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
438 data << "box" << ' ';
444 void InsetBoxMailer::string2params(string const & in,
445 InsetBoxParams & params)
447 params = InsetBoxParams(string());
452 istringstream data(in);
458 token = lex.getString();
465 InsetBoxParams::InsetBoxParams(string const & label)
469 width(LyXLength("100col%")),
474 height(LyXLength("1in")),
475 height_special("totalheight") // default is 1\\totalheight
479 void InsetBoxParams::write(ostream & os) const
482 os << "position \"" << pos << "\"\n";
483 os << "hor_pos \"" << hor_pos << "\"\n";
484 os << "has_inner_box " << inner_box << "\n";
485 os << "inner_pos \"" << inner_pos << "\"\n";
486 os << "use_parbox " << use_parbox << "\n";
487 os << "width \"" << width.asString() << "\"\n";
488 os << "special \"" << special << "\"\n";
489 os << "height \"" << height.asString() << "\"\n";
490 os << "height_special \"" << height_special << "\"\n";
494 void InsetBoxParams::read(LyXLex & lex)
497 type = lex.getString();
503 token = lex.getString();
504 if (token == "position") {
506 // The [0] is needed. We need the first and only char in
508 pos = lex.getString()[0];
510 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
511 lex.pushToken(token);
516 token = lex.getString();
517 if (token == "hor_pos") {
519 hor_pos = lex.getString()[0];
521 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
522 lex.pushToken(token);
527 token = lex.getString();
528 if (token == "has_inner_box") {
530 inner_box = lex.getInteger();
532 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
533 lex.pushToken(token);
539 token = lex.getString();
540 if (token == "inner_pos") {
542 inner_pos = lex.getString()[0];
544 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
546 lex.pushToken(token);
551 token = lex.getString();
552 if (token == "use_parbox") {
554 use_parbox = lex.getInteger();
556 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
557 lex.pushToken(token);
562 token = lex.getString();
563 if (token == "width") {
565 width = LyXLength(lex.getString());
567 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
568 lex.pushToken(token);
573 token = lex.getString();
574 if (token == "special") {
576 special = lex.getString();
578 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
579 lex.pushToken(token);
584 token = lex.getString();
585 if (token == "height") {
587 height = LyXLength(lex.getString());
589 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
590 lex.pushToken(token);
595 token = lex.getString();
596 if (token == "height_special") {
598 height_special = lex.getString();
600 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
601 lex.pushToken(token);