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 result.dispatched(true);
188 case LFUN_INSET_DIALOG_UPDATE:
189 InsetBoxMailer(*this).updateDialog(bv);
190 result.dispatched(true);
193 case LFUN_MOUSE_RELEASE:
194 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
195 InsetBoxMailer(*this).showDialog(bv);
196 return DispatchResult(true);
198 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
201 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
206 int InsetBox::latex(Buffer const & buf, ostream & os,
207 OutputParams 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 OutputParams const & runparams) const
338 return inset.linuxdoc(buf, os, runparams);
342 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
343 OutputParams const & runparams) const
345 return inset.docbook(buf, os, runparams);
349 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
350 OutputParams const & runparams) const
352 BoxType const btype = boxtranslator().find(params_.type);
355 case Frameless: break;
356 case Boxed: os << "["; break;
357 case ovalbox: os << "("; break;
358 case Ovalbox: os << "(("; break;
359 case Shadowbox: os << "["; break;
360 case Doublebox: os << "[["; break;
363 int i = inset.plaintext(buf, os, runparams);
366 case Frameless: break;
367 case Boxed: os << "]"; break;
368 case ovalbox: os << ")"; break;
369 case Ovalbox: os << "))"; break;
370 case Shadowbox: os << "]/"; break;
371 case Doublebox: os << "]]"; break;
378 void InsetBox::validate(LaTeXFeatures & features) const
380 features.require("calc");
381 BoxType btype = boxtranslator().find(params_.type);
390 features.require("fancybox");
393 inset.validate(features);
397 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
403 string const InsetBoxMailer::name_ = "box";
406 string const InsetBoxMailer::inset2string(Buffer const &) const
408 return params2string(inset_.params());
412 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
415 data << "box" << ' ';
421 void InsetBoxMailer::string2params(string const & in,
422 InsetBoxParams & params)
424 params = InsetBoxParams(string());
429 istringstream data(in);
435 token = lex.getString();
442 InsetBoxParams::InsetBoxParams(string const & label)
446 width(LyXLength("100col%")),
451 height(LyXLength("1in")),
452 height_special("totalheight") // default is 1\\totalheight
456 void InsetBoxParams::write(ostream & os) const
459 os << "position \"" << pos << "\"\n";
460 os << "hor_pos \"" << hor_pos << "\"\n";
461 os << "has_inner_box " << inner_box << "\n";
462 os << "inner_pos \"" << inner_pos << "\"\n";
463 os << "use_parbox " << use_parbox << "\n";
464 os << "width \"" << width.asString() << "\"\n";
465 os << "special \"" << special << "\"\n";
466 os << "height \"" << height.asString() << "\"\n";
467 os << "height_special \"" << height_special << "\"\n";
471 void InsetBoxParams::read(LyXLex & lex)
474 type = lex.getString();
480 token = lex.getString();
481 if (token == "position") {
483 // The [0] is needed. We need the first and only char in
485 pos = lex.getString()[0];
487 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
488 lex.pushToken(token);
493 token = lex.getString();
494 if (token == "hor_pos") {
496 hor_pos = lex.getString()[0];
498 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
499 lex.pushToken(token);
504 token = lex.getString();
505 if (token == "has_inner_box") {
507 inner_box = lex.getInteger();
509 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
510 lex.pushToken(token);
516 token = lex.getString();
517 if (token == "inner_pos") {
519 inner_pos = lex.getString()[0];
521 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
523 lex.pushToken(token);
528 token = lex.getString();
529 if (token == "use_parbox") {
531 use_parbox = lex.getInteger();
533 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
534 lex.pushToken(token);
539 token = lex.getString();
540 if (token == "width") {
542 width = LyXLength(lex.getString());
544 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
545 lex.pushToken(token);
550 token = lex.getString();
551 if (token == "special") {
553 special = lex.getString();
555 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
556 lex.pushToken(token);
561 token = lex.getString();
562 if (token == "height") {
564 height = LyXLength(lex.getString());
566 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
567 lex.pushToken(token);
572 token = lex.getString();
573 if (token == "height_special") {
575 height_special = lex.getString();
577 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
578 lex.pushToken(token);