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(*this).hideDialog();
103 auto_ptr<InsetBase> InsetBox::clone() const
105 return auto_ptr<InsetBase>(new InsetBox(*this));
109 string const InsetBox::editMessage() const
111 return _("Opened Box Inset");
115 void InsetBox::write(Buffer const & buf, ostream & os) const
118 InsetCollapsable::write(buf, os);
122 void InsetBox::read(Buffer const & buf, LyXLex & lex)
125 InsetCollapsable::read(buf, lex);
130 void InsetBox::setButtonLabel()
132 LyXFont font(LyXFont::ALL_SANE);
136 BoxType btype = boxtranslator().find(params_.type);
137 if (btype == Frameless) {
138 if (params_.use_parbox)
139 setLabel(_("Parbox"));
141 setLabel(_("Minipage"));
143 setLabel(boxtranslator_loc().find(btype));
145 font.setColor(LColor::foreground);
146 setBackgroundColor(LColor::background);
151 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
154 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
155 InsetCollapsable::metrics(mi, dim);
156 //if (params_.inner_box && isOpen())
157 // dim.wid = mi.base.textwidth;
162 bool InsetBox::showInsetDialog(BufferView * bv) const
164 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
170 InsetBox::priv_dispatch(FuncRequest const & cmd,
171 idx_type & idx, pos_type & pos)
173 DispatchResult result(false);
174 BufferView * bv = cmd.view();
176 switch (cmd.action) {
178 case LFUN_INSET_MODIFY: {
179 lyxerr << "InsetBox::dispatch MODIFY" << endl;
180 InsetBoxMailer::string2params(cmd.argument, params_);
182 result.dispatched(true);
187 case LFUN_INSET_DIALOG_UPDATE:
188 InsetBoxMailer(*this).updateDialog(bv);
189 result.dispatched(true);
192 case LFUN_MOUSE_RELEASE:
193 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
194 InsetBoxMailer(*this).showDialog(bv);
195 return DispatchResult(true);
197 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
200 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
205 int InsetBox::latex(Buffer const & buf, ostream & os,
206 OutputParams const & runparams) const
208 BoxType btype = boxtranslator().find(params_.type);
210 string width_string = params_.width.asLatexString();
211 bool stdwidth(false);
212 if (params_.inner_box &&
213 (width_string.find("1.0\\columnwidth") != string::npos
214 || width_string.find("1.0\\textwidth") != string::npos)) {
220 width_string += " - 2\\fboxsep - 2\\fboxrule";
223 width_string += " - 2\\fboxsep - 0.8pt";
226 width_string += " - 2\\fboxsep - 1.6pt";
229 // Shadow falls outside right margin... opinions?
230 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
233 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
240 // Adapt to column/text width correctly also if paragraphs indented:
249 if (!params_.inner_box) {
251 // Special widths, see usrguide §3.5
252 if (params_.special != "none") {
253 os << "[" << params_.width.value()
254 << "\\" << params_.special << "]";
256 os << "[" << width_string << "]";
257 if (params_.hor_pos != 'c')
258 os << "[" << params_.hor_pos << "]";
270 os << "\\shadowbox{";
273 os << "\\doublebox{";
277 if (params_.inner_box) {
278 if (params_.use_parbox)
281 os << "\\begin{minipage}";
283 os << "[" << params_.pos << "]";
284 if (params_.height_special == "none") {
285 os << "[" << params_.height.asLatexString() << "]";
288 os << "[" << params_.height.value()
289 << "\\" << params_.height_special << "]";
291 if (params_.inner_pos != params_.pos)
292 os << "[" << params_.inner_pos << "]";
294 os << "{" << width_string << "}";
296 if (params_.use_parbox)
302 i += inset.latex(buf, os, runparams);
304 if (params_.inner_box) {
305 if (params_.use_parbox)
308 os << "%\n\\end{minipage}";
315 if (!params_.inner_box)
316 os << "}"; // for makebox
334 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
335 OutputParams const & runparams) const
337 return inset.linuxdoc(buf, os, runparams);
341 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
342 OutputParams const & runparams) const
344 return inset.docbook(buf, os, runparams);
348 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
349 OutputParams const & runparams) const
351 BoxType const btype = boxtranslator().find(params_.type);
354 case Frameless: break;
355 case Boxed: os << "["; break;
356 case ovalbox: os << "("; break;
357 case Ovalbox: os << "(("; break;
358 case Shadowbox: os << "["; break;
359 case Doublebox: os << "[["; break;
362 int i = inset.plaintext(buf, os, runparams);
365 case Frameless: break;
366 case Boxed: os << "]"; break;
367 case ovalbox: os << ")"; break;
368 case Ovalbox: os << "))"; break;
369 case Shadowbox: os << "]/"; break;
370 case Doublebox: os << "]]"; break;
377 void InsetBox::validate(LaTeXFeatures & features) const
379 features.require("calc");
380 BoxType btype = boxtranslator().find(params_.type);
389 features.require("fancybox");
392 inset.validate(features);
396 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
402 string const InsetBoxMailer::name_ = "box";
405 string const InsetBoxMailer::inset2string(Buffer const &) const
407 return params2string(inset_.params());
411 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
414 data << "box" << ' ';
420 void InsetBoxMailer::string2params(string const & in,
421 InsetBoxParams & params)
423 params = InsetBoxParams(string());
428 istringstream data(in);
434 token = lex.getString();
441 InsetBoxParams::InsetBoxParams(string const & label)
445 width(LyXLength("100col%")),
450 height(LyXLength("1in")),
451 height_special("totalheight") // default is 1\\totalheight
455 void InsetBoxParams::write(ostream & os) const
458 os << "position \"" << pos << "\"\n";
459 os << "hor_pos \"" << hor_pos << "\"\n";
460 os << "has_inner_box " << inner_box << "\n";
461 os << "inner_pos \"" << inner_pos << "\"\n";
462 os << "use_parbox " << use_parbox << "\n";
463 os << "width \"" << width.asString() << "\"\n";
464 os << "special \"" << special << "\"\n";
465 os << "height \"" << height.asString() << "\"\n";
466 os << "height_special \"" << height_special << "\"\n";
470 void InsetBoxParams::read(LyXLex & lex)
473 type = lex.getString();
479 token = lex.getString();
480 if (token == "position") {
482 // The [0] is needed. We need the first and only char in
484 pos = lex.getString()[0];
486 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
487 lex.pushToken(token);
492 token = lex.getString();
493 if (token == "hor_pos") {
495 hor_pos = lex.getString()[0];
497 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
498 lex.pushToken(token);
503 token = lex.getString();
504 if (token == "has_inner_box") {
506 inner_box = lex.getInteger();
508 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
509 lex.pushToken(token);
515 token = lex.getString();
516 if (token == "inner_pos") {
518 inner_pos = lex.getString()[0];
520 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
522 lex.pushToken(token);
527 token = lex.getString();
528 if (token == "use_parbox") {
530 use_parbox = lex.getInteger();
532 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
533 lex.pushToken(token);
538 token = lex.getString();
539 if (token == "width") {
541 width = LyXLength(lex.getString());
543 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
544 lex.pushToken(token);
549 token = lex.getString();
550 if (token == "special") {
552 special = lex.getString();
554 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
555 lex.pushToken(token);
560 token = lex.getString();
561 if (token == "height") {
563 height = LyXLength(lex.getString());
565 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
566 lex.pushToken(token);
571 token = lex.getString();
572 if (token == "height_special") {
574 height_special = lex.getString();
576 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
577 lex.pushToken(token);