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"
19 #include "DispatchResult.h"
21 #include "FuncStatus.h"
22 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
28 #include "TextMetrics.h"
30 #include "support/Translator.h"
38 using std::istringstream;
40 using std::ostringstream;
46 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
47 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
49 BoxTranslator const init_boxtranslator()
51 BoxTranslator translator("Boxed", InsetBox::Boxed);
52 translator.addPair("Frameless", InsetBox::Frameless);
53 translator.addPair("ovalbox", InsetBox::ovalbox);
54 translator.addPair("Ovalbox", InsetBox::Ovalbox);
55 translator.addPair("Shadowbox", InsetBox::Shadowbox);
56 translator.addPair("Doublebox",InsetBox::Doublebox);
61 BoxTranslatorLoc const init_boxtranslator_loc()
63 BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
64 translator.addPair(_("Frameless"), InsetBox::Frameless);
65 translator.addPair(_("ovalbox"), InsetBox::ovalbox);
66 translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
67 translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
68 translator.addPair(_("Doublebox"), InsetBox::Doublebox);
73 BoxTranslator const & boxtranslator()
75 static BoxTranslator translator = init_boxtranslator();
80 BoxTranslatorLoc const & boxtranslator_loc()
82 static BoxTranslatorLoc translator = init_boxtranslator_loc();
95 InsetBox::InsetBox(BufferParams const & bp, string const & label)
96 : 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);
142 void InsetBox::setButtonLabel()
144 Font font(Font::ALL_SANE);
148 BoxType btype = boxtranslator().find(params_.type);
153 if (btype == Frameless) {
154 if (params_.use_parbox)
155 label += _("Parbox");
157 label += _("Minipage");
159 label += boxtranslator_loc().find(btype);
164 font.setColor(Color::foreground);
169 bool InsetBox::hasFixedWidth() const
171 return params_.inner_box || params_.special != "width";
175 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
177 // back up textwidth.
178 int textwidth_backup = m.base.textwidth;
180 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
181 InsetCollapsable::metrics(m, dim);
183 m.base.textwidth = textwidth_backup;
184 bool const changed = dim_ != dim;
190 bool InsetBox::forceDefaultParagraphs(idx_type) const
192 return !params_.inner_box;
196 bool InsetBox::showInsetDialog(BufferView * bv) const
198 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
203 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
205 switch (cmd.action) {
207 case LFUN_INSET_MODIFY: {
208 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
209 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
214 case LFUN_INSET_DIALOG_UPDATE:
215 InsetBoxMailer(*this).updateDialog(&cur.bv());
218 case LFUN_MOUSE_RELEASE:
219 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
220 InsetBoxMailer(*this).showDialog(&cur.bv());
223 InsetCollapsable::doDispatch(cur, cmd);
227 InsetCollapsable::doDispatch(cur, cmd);
233 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
234 FuncStatus & flag) const
236 switch (cmd.action) {
238 case LFUN_INSET_MODIFY:
239 case LFUN_INSET_DIALOG_UPDATE:
242 case LFUN_BREAK_PARAGRAPH:
243 if (params_.inner_box) {
244 return InsetCollapsable::getStatus(cur, cmd, flag);
251 return InsetCollapsable::getStatus(cur, cmd, flag);
256 int InsetBox::latex(Buffer const & buf, odocstream & os,
257 OutputParams const & runparams) const
259 BoxType btype = boxtranslator().find(params_.type);
261 string width_string = params_.width.asLatexString();
262 bool stdwidth(false);
263 if (params_.inner_box &&
264 (width_string.find("1.0\\columnwidth") != string::npos
265 || width_string.find("1.0\\textwidth") != string::npos)) {
271 width_string += " - 2\\fboxsep - 2\\fboxrule";
274 width_string += " - 2\\fboxsep - 0.8pt";
277 width_string += " - 2\\fboxsep - 1.6pt";
280 // Shadow falls outside right margin... opinions?
281 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
284 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
291 // Adapt to column/text width correctly also if paragraphs indented:
300 if (!params_.inner_box) {
302 // Special widths, see usrguide §3.5
304 if (params_.special != "none") {
305 os << "[" << params_.width.value()
306 << '\\' << from_utf8(params_.special)
309 os << '[' << from_ascii(width_string)
311 if (params_.hor_pos != 'c')
312 os << "[" << params_.hor_pos << "]";
324 os << "\\shadowbox{";
327 os << "\\doublebox{";
331 if (params_.inner_box) {
332 if (params_.use_parbox)
335 os << "\\begin{minipage}";
337 os << "[" << params_.pos << "]";
338 if (params_.height_special == "none") {
340 os << '[' << from_ascii(params_.height.asLatexString())
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);