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 BufferView * bv = cmd.view();
174 switch (cmd.action) {
176 case LFUN_INSET_MODIFY: {
177 InsetBoxMailer::string2params(cmd.argument, params_);
179 bv->updateInset(this);
180 return DispatchResult(true);
182 case LFUN_INSET_DIALOG_UPDATE:
183 InsetBoxMailer(*this).updateDialog(bv);
184 return DispatchResult(true);
186 case LFUN_MOUSE_RELEASE:
187 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
188 InsetBoxMailer(*this).showDialog(bv);
189 return DispatchResult(true);
194 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
199 int InsetBox::latex(Buffer const & buf, ostream & os,
200 LatexRunParams const & runparams) const
202 BoxType btype = boxtranslator().find(params_.type);
204 string width_string = params_.width.asLatexString();
205 bool stdwidth(false);
206 if (params_.inner_box &&
207 (width_string.find("1.0\\columnwidth") != string::npos
208 || width_string.find("1.0\\textwidth") != string::npos)) {
214 width_string += " - 2\\fboxsep - 2\\fboxrule";
217 width_string += " - 2\\fboxsep - 0.8pt";
220 width_string += " - 2\\fboxsep - 1.6pt";
223 // Shadow falls outside right margin... opinions?
224 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
227 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
234 // Adapt to column/text width correctly also if paragraphs indented:
243 if (!params_.inner_box) {
245 // Special widths, see usrguide §3.5
246 if (params_.special != "none") {
247 os << "[" << params_.width.value()
248 << "\\" << params_.special << "]";
250 os << "[" << width_string << "]";
251 if (params_.hor_pos != 'c')
252 os << "[" << params_.hor_pos << "]";
264 os << "\\shadowbox{";
267 os << "\\doublebox{";
271 if (params_.inner_box) {
272 if (params_.use_parbox)
275 os << "\\begin{minipage}";
277 os << "[" << params_.pos << "]";
278 if (params_.height_special == "none") {
279 os << "[" << params_.height.asLatexString() << "]";
282 os << "[" << params_.height.value()
283 << "\\" << params_.height_special << "]";
285 if (params_.inner_pos != params_.pos)
286 os << "[" << params_.inner_pos << "]";
288 os << "{" << width_string << "}";
290 if (params_.use_parbox)
296 i += inset.latex(buf, os, runparams);
298 if (params_.inner_box) {
299 if (params_.use_parbox)
302 os << "%\n\\end{minipage}";
309 if (!params_.inner_box)
310 os << "}"; // for makebox
328 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
329 LatexRunParams const & runparams) const
331 return inset.linuxdoc(buf, os, runparams);
335 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
336 LatexRunParams const & runparams) const
338 return inset.docbook(buf, os, runparams);
342 int InsetBox::ascii(Buffer const & buf, std::ostream & os,
343 LatexRunParams const & runparams) const
346 string const pt = params_.type;
347 BoxType btype = boxtranslator().find(params_.type);
368 i = inset.ascii(buf, os, runparams);
394 void InsetBox::validate(LaTeXFeatures & features) const
396 features.require("calc");
397 BoxType btype = boxtranslator().find(params_.type);
406 features.require("fancybox");
409 inset.validate(features);
413 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
419 string const InsetBoxMailer::name_ = "box";
422 string const InsetBoxMailer::inset2string(Buffer const &) const
424 return params2string(inset_.params());
428 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
431 data << "box" << ' ';
437 void InsetBoxMailer::string2params(string const & in,
438 InsetBoxParams & params)
440 params = InsetBoxParams(string());
445 istringstream data(in);
451 token = lex.getString();
458 InsetBoxParams::InsetBoxParams(string const & label)
462 width(LyXLength("100col%")),
467 height(LyXLength("1in")),
468 height_special("totalheight") // default is 1\\totalheight
472 void InsetBoxParams::write(ostream & os) const
475 os << "position \"" << pos << "\"\n";
476 os << "hor_pos \"" << hor_pos << "\"\n";
477 os << "has_inner_box " << inner_box << "\n";
478 os << "inner_pos \"" << inner_pos << "\"\n";
479 os << "use_parbox " << use_parbox << "\n";
480 os << "width \"" << width.asString() << "\"\n";
481 os << "special \"" << special << "\"\n";
482 os << "height \"" << height.asString() << "\"\n";
483 os << "height_special \"" << height_special << "\"\n";
487 void InsetBoxParams::read(LyXLex & lex)
490 type = lex.getString();
496 token = lex.getString();
497 if (token == "position") {
499 // The [0] is needed. We need the first and only char in
501 pos = lex.getString()[0];
503 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
504 lex.pushToken(token);
509 token = lex.getString();
510 if (token == "hor_pos") {
512 hor_pos = lex.getString()[0];
514 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
515 lex.pushToken(token);
520 token = lex.getString();
521 if (token == "has_inner_box") {
523 inner_box = lex.getInteger();
525 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
526 lex.pushToken(token);
532 token = lex.getString();
533 if (token == "inner_pos") {
535 inner_pos = lex.getString()[0];
537 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
539 lex.pushToken(token);
544 token = lex.getString();
545 if (token == "use_parbox") {
547 use_parbox = lex.getInteger();
549 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
550 lex.pushToken(token);
555 token = lex.getString();
556 if (token == "width") {
558 width = LyXLength(lex.getString());
560 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
561 lex.pushToken(token);
566 token = lex.getString();
567 if (token == "special") {
569 special = lex.getString();
571 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
572 lex.pushToken(token);
577 token = lex.getString();
578 if (token == "height") {
580 height = LyXLength(lex.getString());
582 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
583 lex.pushToken(token);
588 token = lex.getString();
589 if (token == "height_special") {
591 height_special = lex.getString();
593 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
594 lex.pushToken(token);