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)
101 InsetBox::InsetBox(InsetBox const & in)
102 : InsetCollapsable(in), params_(in.params_)
108 InsetBox::~InsetBox()
110 InsetBoxMailer(*this).hideDialog();
114 Inset * InsetBox::clone() const
116 return new InsetBox(*this);
120 docstring const InsetBox::editMessage() const
122 return _("Opened Box Inset");
126 void InsetBox::write(Buffer const & buf, ostream & os) const
129 InsetCollapsable::write(buf, os);
133 void InsetBox::read(Buffer const & buf, Lexer & lex)
136 InsetCollapsable::read(buf, lex);
141 void InsetBox::setButtonLabel()
143 BoxType btype = boxtranslator().find(params_.type);
148 if (btype == Frameless) {
149 if (params_.use_parbox)
150 label += _("Parbox");
152 label += _("Minipage");
154 label += boxtranslator_loc().find(btype);
161 bool InsetBox::hasFixedWidth() const
163 return params_.inner_box || params_.special != "width";
167 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
169 // back up textwidth.
170 int textwidth_backup = m.base.textwidth;
172 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
173 InsetCollapsable::metrics(m, dim);
175 m.base.textwidth = textwidth_backup;
179 bool InsetBox::forceDefaultParagraphs(idx_type) const
181 return !params_.inner_box;
185 bool InsetBox::showInsetDialog(BufferView * bv) const
187 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
192 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
194 switch (cmd.action) {
196 case LFUN_INSET_MODIFY: {
197 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
198 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
199 setLayout(cur.buffer().params());
203 case LFUN_INSET_DIALOG_UPDATE:
204 InsetBoxMailer(*this).updateDialog(&cur.bv());
207 case LFUN_MOUSE_RELEASE:
208 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
209 InsetBoxMailer(*this).showDialog(&cur.bv());
212 InsetCollapsable::doDispatch(cur, cmd);
216 InsetCollapsable::doDispatch(cur, cmd);
222 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
223 FuncStatus & flag) const
225 switch (cmd.action) {
227 case LFUN_INSET_MODIFY:
228 case LFUN_INSET_DIALOG_UPDATE:
231 case LFUN_BREAK_PARAGRAPH:
232 if (params_.inner_box) {
233 return InsetCollapsable::getStatus(cur, cmd, flag);
240 return InsetCollapsable::getStatus(cur, cmd, flag);
245 int InsetBox::latex(Buffer const & buf, odocstream & os,
246 OutputParams const & runparams) const
248 BoxType btype = boxtranslator().find(params_.type);
250 string width_string = params_.width.asLatexString();
251 bool stdwidth(false);
252 if (params_.inner_box &&
253 (width_string.find("1.0\\columnwidth") != string::npos
254 || width_string.find("1.0\\textwidth") != string::npos)) {
260 width_string += " - 2\\fboxsep - 2\\fboxrule";
263 width_string += " - 2\\fboxsep - 0.8pt";
266 width_string += " - 2\\fboxsep - 1.6pt";
269 // Shadow falls outside right margin... opinions?
270 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
273 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
280 // Adapt to column/text width correctly also if paragraphs indented:
289 if (!params_.inner_box) {
291 // Special widths, see usrguide §3.5
293 if (params_.special != "none") {
294 os << "[" << params_.width.value()
295 << '\\' << from_utf8(params_.special)
298 os << '[' << from_ascii(width_string)
300 if (params_.hor_pos != 'c')
301 os << "[" << params_.hor_pos << "]";
313 os << "\\shadowbox{";
316 os << "\\doublebox{";
320 if (params_.inner_box) {
321 if (params_.use_parbox)
324 os << "\\begin{minipage}";
326 os << "[" << params_.pos << "]";
327 if (params_.height_special == "none") {
329 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
332 // set no optional argument when the value is the default "1\height"
333 // (special units like \height are handled as "in")
334 // but when the user has chosen a non-default inner_pos, the height
335 // must be given: \minipage[pos][height][inner-pos]{width}
336 if ((params_.height != Length("1in") ||
337 params_.height_special != "totalheight") ||
338 params_.inner_pos != params_.pos) {
340 os << "[" << params_.height.value()
341 << "\\" << from_utf8(params_.height_special) << "]";
344 if (params_.inner_pos != params_.pos)
345 os << "[" << params_.inner_pos << "]";
348 os << '{' << from_ascii(width_string) << '}';
350 if (params_.use_parbox)
356 i += InsetText::latex(buf, os, runparams);
358 if (params_.inner_box) {
359 if (params_.use_parbox)
362 os << "%\n\\end{minipage}";
369 if (!params_.inner_box)
370 os << "}"; // for makebox
388 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
389 OutputParams const & runparams) const
391 BoxType const btype = boxtranslator().find(params_.type);
394 case Frameless: break;
395 case Boxed: os << "[\n"; break;
396 case ovalbox: os << "(\n"; break;
397 case Ovalbox: os << "((\n"; break;
398 case Shadowbox: os << "[/\n"; break;
399 case Doublebox: os << "[[\n"; break;
402 InsetText::plaintext(buf, os, runparams);
406 case Frameless: os << "\n"; break;
407 case Boxed: os << "\n]"; len = 1; break;
408 case ovalbox: os << "\n)"; len = 1; break;
409 case Ovalbox: os << "\n))"; len = 2; break;
410 case Shadowbox: os << "\n/]"; len = 2; break;
411 case Doublebox: os << "\n]]"; len = 2; break;
414 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
418 int InsetBox::docbook(Buffer const & buf, odocstream & os,
419 OutputParams const & runparams) const
421 return InsetText::docbook(buf, os, runparams);
425 void InsetBox::validate(LaTeXFeatures & features) const
427 BoxType btype = boxtranslator().find(params_.type);
432 features.require("calc");
438 features.require("calc");
439 features.require("fancybox");
442 InsetText::validate(features);
446 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
451 string const InsetBoxMailer::name_ = "box";
454 string const InsetBoxMailer::inset2string(Buffer const &) const
456 return params2string(inset_.params());
460 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
463 data << "box" << ' ';
469 void InsetBoxMailer::string2params(string const & in,
470 InsetBoxParams & params)
472 params = InsetBoxParams(string());
476 istringstream data(in);
482 if (!lex || name != name_)
483 return print_mailer_error("InsetBoxMailer", in, 1, name_);
485 // This is part of the inset proper that is usually swallowed
486 // by Text::readInset
489 if (!lex || id != "Box")
490 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
496 InsetBoxParams::InsetBoxParams(string const & label)
500 width(Length("100col%")),
505 height(Length("1in")),
506 height_special("totalheight") // default is 1\\totalheight
510 void InsetBoxParams::write(ostream & os) const
512 os << "Box " << type << "\n";
513 os << "position \"" << pos << "\"\n";
514 os << "hor_pos \"" << hor_pos << "\"\n";
515 os << "has_inner_box " << inner_box << "\n";
516 os << "inner_pos \"" << inner_pos << "\"\n";
517 os << "use_parbox " << use_parbox << "\n";
518 os << "width \"" << width.asString() << "\"\n";
519 os << "special \"" << special << "\"\n";
520 os << "height \"" << height.asString() << "\"\n";
521 os << "height_special \"" << height_special << "\"\n";
525 void InsetBoxParams::read(Lexer & lex)
531 type = lex.getString();
538 token = lex.getString();
541 if (token == "position") {
543 // The [0] is needed. We need the first and only char in
545 pos = lex.getString()[0];
547 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
548 lex.pushToken(token);
552 token = lex.getString();
555 if (token == "hor_pos") {
557 hor_pos = lex.getString()[0];
559 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
560 lex.pushToken(token);
564 token = lex.getString();
567 if (token == "has_inner_box") {
569 inner_box = lex.getInteger();
571 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
572 lex.pushToken(token);
576 token = lex.getString();
579 if (token == "inner_pos") {
581 inner_pos = lex.getString()[0];
583 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
585 lex.pushToken(token);
589 token = lex.getString();
592 if (token == "use_parbox") {
594 use_parbox = lex.getInteger();
596 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
597 lex.pushToken(token);
601 token = lex.getString();
604 if (token == "width") {
606 width = Length(lex.getString());
608 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
609 lex.pushToken(token);
613 token = lex.getString();
616 if (token == "special") {
618 special = lex.getString();
620 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
621 lex.pushToken(token);
625 token = lex.getString();
628 if (token == "height") {
630 height = Length(lex.getString());
632 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
633 lex.pushToken(token);
637 token = lex.getString();
640 if (token == "height_special") {
642 height_special = lex.getString();
644 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
645 lex.pushToken(token);