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.
18 #include "BufferView.h"
20 #include "DispatchResult.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
25 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
29 #include "support/Translator.h"
37 using std::istringstream;
39 using std::ostringstream;
45 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
46 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
48 BoxTranslator const init_boxtranslator()
50 BoxTranslator translator("Boxed", InsetBox::Boxed);
51 translator.addPair("Frameless", InsetBox::Frameless);
52 translator.addPair("ovalbox", InsetBox::ovalbox);
53 translator.addPair("Ovalbox", InsetBox::Ovalbox);
54 translator.addPair("Shadowbox", InsetBox::Shadowbox);
55 translator.addPair("Doublebox",InsetBox::Doublebox);
60 BoxTranslatorLoc const init_boxtranslator_loc()
62 BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
63 translator.addPair(_("Frameless"), InsetBox::Frameless);
64 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
65 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
66 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
67 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
72 BoxTranslator const & boxtranslator()
74 static BoxTranslator translator = init_boxtranslator();
79 BoxTranslatorLoc const & boxtranslator_loc()
81 static BoxTranslatorLoc translator = init_boxtranslator_loc();
94 InsetBox::InsetBox(BufferParams const & bp, string const & label)
95 : InsetCollapsable(bp), params_(label)
102 InsetBox::InsetBox(InsetBox const & in)
103 : InsetCollapsable(in), params_(in.params_)
109 InsetBox::~InsetBox()
111 InsetBoxMailer(*this).hideDialog();
115 Inset * InsetBox::clone() const
117 return new InsetBox(*this);
121 docstring const InsetBox::editMessage() const
123 return _("Opened Box Inset");
127 void InsetBox::write(Buffer const & buf, ostream & os) const
130 InsetCollapsable::write(buf, os);
134 void InsetBox::read(Buffer const & buf, Lexer & lex)
137 InsetCollapsable::read(buf, lex);
138 setLayout(buf.params());
143 void InsetBox::setButtonLabel()
145 BoxType btype = boxtranslator().find(params_.type);
150 if (btype == Frameless) {
151 if (params_.use_parbox)
152 label += _("Parbox");
154 label += _("Minipage");
156 label += boxtranslator_loc().find(btype);
161 setLabelColor(Color_foreground);
165 bool InsetBox::hasFixedWidth() const
167 return params_.inner_box || params_.special != "width";
171 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
173 // back up textwidth.
174 int textwidth_backup = m.base.textwidth;
176 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
177 InsetCollapsable::metrics(m, dim);
179 m.base.textwidth = textwidth_backup;
183 bool InsetBox::forceDefaultParagraphs(idx_type) const
185 return !params_.inner_box;
189 bool InsetBox::showInsetDialog(BufferView * bv) const
191 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
196 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
198 switch (cmd.action) {
200 case LFUN_INSET_MODIFY: {
201 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
202 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
203 setLayout(cur.buffer().params());
208 case LFUN_INSET_DIALOG_UPDATE:
209 InsetBoxMailer(*this).updateDialog(&cur.bv());
212 case LFUN_MOUSE_RELEASE:
213 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
214 InsetBoxMailer(*this).showDialog(&cur.bv());
217 InsetCollapsable::doDispatch(cur, cmd);
221 InsetCollapsable::doDispatch(cur, cmd);
227 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
228 FuncStatus & flag) const
230 switch (cmd.action) {
232 case LFUN_INSET_MODIFY:
233 case LFUN_INSET_DIALOG_UPDATE:
236 case LFUN_BREAK_PARAGRAPH:
237 if (params_.inner_box) {
238 return InsetCollapsable::getStatus(cur, cmd, flag);
245 return InsetCollapsable::getStatus(cur, cmd, flag);
250 int InsetBox::latex(Buffer const & buf, odocstream & os,
251 OutputParams const & runparams) const
253 BoxType btype = boxtranslator().find(params_.type);
255 string width_string = params_.width.asLatexString();
256 bool stdwidth(false);
257 if (params_.inner_box &&
258 (width_string.find("1.0\\columnwidth") != string::npos
259 || width_string.find("1.0\\textwidth") != string::npos)) {
265 width_string += " - 2\\fboxsep - 2\\fboxrule";
268 width_string += " - 2\\fboxsep - 0.8pt";
271 width_string += " - 2\\fboxsep - 1.6pt";
274 // Shadow falls outside right margin... opinions?
275 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
278 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
285 // Adapt to column/text width correctly also if paragraphs indented:
294 if (!params_.inner_box) {
296 // Special widths, see usrguide §3.5
298 if (params_.special != "none") {
299 os << "[" << params_.width.value()
300 << '\\' << from_utf8(params_.special)
303 os << '[' << from_ascii(width_string)
305 if (params_.hor_pos != 'c')
306 os << "[" << params_.hor_pos << "]";
318 os << "\\shadowbox{";
321 os << "\\doublebox{";
325 if (params_.inner_box) {
326 if (params_.use_parbox)
329 os << "\\begin{minipage}";
331 os << "[" << params_.pos << "]";
332 if (params_.height_special == "none") {
334 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
337 // set no optional argument when the value is the default "1\height"
338 // (special units like \height are handled as "in")
339 // but when the user has chosen a non-default inner_pos, the height
340 // must be given: \minipage[pos][height][inner-pos]{width}
341 if ((params_.height != Length("1in") ||
342 params_.height_special != "totalheight") ||
343 params_.inner_pos != params_.pos) {
345 os << "[" << params_.height.value()
346 << "\\" << from_utf8(params_.height_special) << "]";
349 if (params_.inner_pos != params_.pos)
350 os << "[" << params_.inner_pos << "]";
353 os << '{' << from_ascii(width_string) << '}';
355 if (params_.use_parbox)
361 i += InsetText::latex(buf, os, runparams);
363 if (params_.inner_box) {
364 if (params_.use_parbox)
367 os << "%\n\\end{minipage}";
374 if (!params_.inner_box)
375 os << "}"; // for makebox
393 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
394 OutputParams const & runparams) const
396 BoxType const btype = boxtranslator().find(params_.type);
399 case Frameless: break;
400 case Boxed: os << "[\n"; break;
401 case ovalbox: os << "(\n"; break;
402 case Ovalbox: os << "((\n"; break;
403 case Shadowbox: os << "[/\n"; break;
404 case Doublebox: os << "[[\n"; break;
407 InsetText::plaintext(buf, os, runparams);
411 case Frameless: os << "\n"; break;
412 case Boxed: os << "\n]"; len = 1; break;
413 case ovalbox: os << "\n)"; len = 1; break;
414 case Ovalbox: os << "\n))"; len = 2; break;
415 case Shadowbox: os << "\n/]"; len = 2; break;
416 case Doublebox: os << "\n]]"; len = 2; break;
419 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
423 int InsetBox::docbook(Buffer const & buf, odocstream & os,
424 OutputParams const & runparams) const
426 return InsetText::docbook(buf, os, runparams);
430 void InsetBox::validate(LaTeXFeatures & features) const
432 BoxType btype = boxtranslator().find(params_.type);
437 features.require("calc");
443 features.require("calc");
444 features.require("fancybox");
447 InsetText::validate(features);
451 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
456 string const InsetBoxMailer::name_ = "box";
459 string const InsetBoxMailer::inset2string(Buffer const &) const
461 return params2string(inset_.params());
465 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
468 data << "box" << ' ';
474 void InsetBoxMailer::string2params(string const & in,
475 InsetBoxParams & params)
477 params = InsetBoxParams(string());
481 istringstream data(in);
487 if (!lex || name != name_)
488 return print_mailer_error("InsetBoxMailer", in, 1, name_);
490 // This is part of the inset proper that is usually swallowed
491 // by Text::readInset
494 if (!lex || id != "Box")
495 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
501 InsetBoxParams::InsetBoxParams(string const & label)
505 width(Length("100col%")),
510 height(Length("1in")),
511 height_special("totalheight") // default is 1\\totalheight
515 void InsetBoxParams::write(ostream & os) const
517 os << "Box " << type << "\n";
518 os << "position \"" << pos << "\"\n";
519 os << "hor_pos \"" << hor_pos << "\"\n";
520 os << "has_inner_box " << inner_box << "\n";
521 os << "inner_pos \"" << inner_pos << "\"\n";
522 os << "use_parbox " << use_parbox << "\n";
523 os << "width \"" << width.asString() << "\"\n";
524 os << "special \"" << special << "\"\n";
525 os << "height \"" << height.asString() << "\"\n";
526 os << "height_special \"" << height_special << "\"\n";
530 void InsetBoxParams::read(Lexer & lex)
536 type = lex.getString();
543 token = lex.getString();
546 if (token == "position") {
548 // The [0] is needed. We need the first and only char in
550 pos = lex.getString()[0];
552 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
553 lex.pushToken(token);
557 token = lex.getString();
560 if (token == "hor_pos") {
562 hor_pos = lex.getString()[0];
564 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
565 lex.pushToken(token);
569 token = lex.getString();
572 if (token == "has_inner_box") {
574 inner_box = lex.getInteger();
576 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
577 lex.pushToken(token);
581 token = lex.getString();
584 if (token == "inner_pos") {
586 inner_pos = lex.getString()[0];
588 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
590 lex.pushToken(token);
594 token = lex.getString();
597 if (token == "use_parbox") {
599 use_parbox = lex.getInteger();
601 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
602 lex.pushToken(token);
606 token = lex.getString();
609 if (token == "width") {
611 width = Length(lex.getString());
613 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
614 lex.pushToken(token);
618 token = lex.getString();
621 if (token == "special") {
623 special = lex.getString();
625 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
626 lex.pushToken(token);
630 token = lex.getString();
633 if (token == "height") {
635 height = Length(lex.getString());
637 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
638 lex.pushToken(token);
642 token = lex.getString();
645 if (token == "height_special") {
647 height_special = lex.getString();
649 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
650 lex.pushToken(token);