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();
88 InsetBox::InsetBox(BufferParams const & bp, string const & label)
89 : InsetCollapsable(bp), params_(label)
93 InsetBox::InsetBox(InsetBox const & in)
94 : InsetCollapsable(in), params_(in.params_)
100 InsetBoxMailer(*this).hideDialog();
104 Inset * InsetBox::clone() const
106 return new InsetBox(*this);
110 docstring 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, Lexer & lex)
126 InsetCollapsable::read(buf, lex);
130 void InsetBox::setButtonLabel()
132 BoxType btype = boxtranslator().find(params_.type);
137 if (btype == Frameless) {
138 if (params_.use_parbox)
139 label += _("Parbox");
141 label += _("Minipage");
143 label += boxtranslator_loc().find(btype);
150 bool InsetBox::hasFixedWidth() const
152 return params_.inner_box || params_.special != "width";
156 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
158 // back up textwidth.
159 int textwidth_backup = m.base.textwidth;
161 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
162 InsetCollapsable::metrics(m, dim);
164 m.base.textwidth = textwidth_backup;
168 bool InsetBox::forceDefaultParagraphs(idx_type) const
170 return !params_.inner_box;
174 bool InsetBox::showInsetDialog(BufferView * bv) const
176 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
181 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
183 switch (cmd.action) {
185 case LFUN_INSET_MODIFY: {
186 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
187 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
188 setLayout(cur.buffer().params());
192 case LFUN_INSET_DIALOG_UPDATE:
193 InsetBoxMailer(*this).updateDialog(&cur.bv());
196 case LFUN_MOUSE_RELEASE:
197 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
198 InsetBoxMailer(*this).showDialog(&cur.bv());
201 InsetCollapsable::doDispatch(cur, cmd);
205 InsetCollapsable::doDispatch(cur, cmd);
211 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
212 FuncStatus & flag) const
214 switch (cmd.action) {
216 case LFUN_INSET_MODIFY:
217 case LFUN_INSET_DIALOG_UPDATE:
220 case LFUN_BREAK_PARAGRAPH:
221 if (params_.inner_box) {
222 return InsetCollapsable::getStatus(cur, cmd, flag);
229 return InsetCollapsable::getStatus(cur, cmd, flag);
234 int InsetBox::latex(Buffer const & buf, odocstream & os,
235 OutputParams const & runparams) const
237 BoxType btype = boxtranslator().find(params_.type);
239 string width_string = params_.width.asLatexString();
240 bool stdwidth(false);
241 if (params_.inner_box &&
242 (width_string.find("1.0\\columnwidth") != string::npos
243 || width_string.find("1.0\\textwidth") != string::npos)) {
249 width_string += " - 2\\fboxsep - 2\\fboxrule";
252 width_string += " - 2\\fboxsep - 0.8pt";
255 width_string += " - 2\\fboxsep - 1.6pt";
258 // Shadow falls outside right margin... opinions?
259 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
262 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
269 // Adapt to column/text width correctly also if paragraphs indented:
278 if (!params_.inner_box) {
280 // Special widths, see usrguide §3.5
282 if (params_.special != "none") {
283 os << "[" << params_.width.value()
284 << '\\' << from_utf8(params_.special)
287 os << '[' << from_ascii(width_string)
289 if (params_.hor_pos != 'c')
290 os << "[" << params_.hor_pos << "]";
302 os << "\\shadowbox{";
305 os << "\\doublebox{";
309 if (params_.inner_box) {
310 if (params_.use_parbox)
313 os << "\\begin{minipage}";
315 os << "[" << params_.pos << "]";
316 if (params_.height_special == "none") {
318 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
321 // set no optional argument when the value is the default "1\height"
322 // (special units like \height are handled as "in")
323 // but when the user has chosen a non-default inner_pos, the height
324 // must be given: \minipage[pos][height][inner-pos]{width}
325 if ((params_.height != Length("1in") ||
326 params_.height_special != "totalheight") ||
327 params_.inner_pos != params_.pos) {
329 os << "[" << params_.height.value()
330 << "\\" << from_utf8(params_.height_special) << "]";
333 if (params_.inner_pos != params_.pos)
334 os << "[" << params_.inner_pos << "]";
337 os << '{' << from_ascii(width_string) << '}';
339 if (params_.use_parbox)
345 i += InsetText::latex(buf, os, runparams);
347 if (params_.inner_box) {
348 if (params_.use_parbox)
351 os << "%\n\\end{minipage}";
358 if (!params_.inner_box)
359 os << "}"; // for makebox
377 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
378 OutputParams const & runparams) const
380 BoxType const btype = boxtranslator().find(params_.type);
383 case Frameless: break;
384 case Boxed: os << "[\n"; break;
385 case ovalbox: os << "(\n"; break;
386 case Ovalbox: os << "((\n"; break;
387 case Shadowbox: os << "[/\n"; break;
388 case Doublebox: os << "[[\n"; break;
391 InsetText::plaintext(buf, os, runparams);
395 case Frameless: os << "\n"; break;
396 case Boxed: os << "\n]"; len = 1; break;
397 case ovalbox: os << "\n)"; len = 1; break;
398 case Ovalbox: os << "\n))"; len = 2; break;
399 case Shadowbox: os << "\n/]"; len = 2; break;
400 case Doublebox: os << "\n]]"; len = 2; break;
403 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
407 int InsetBox::docbook(Buffer const & buf, odocstream & os,
408 OutputParams const & runparams) const
410 return InsetText::docbook(buf, os, runparams);
414 void InsetBox::validate(LaTeXFeatures & features) const
416 BoxType btype = boxtranslator().find(params_.type);
421 features.require("calc");
427 features.require("calc");
428 features.require("fancybox");
431 InsetText::validate(features);
435 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
440 string const InsetBoxMailer::name_ = "box";
443 string const InsetBoxMailer::inset2string(Buffer const &) const
445 return params2string(inset_.params());
449 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
452 data << "box" << ' ';
458 void InsetBoxMailer::string2params(string const & in,
459 InsetBoxParams & params)
461 params = InsetBoxParams(string());
465 istringstream data(in);
471 if (!lex || name != name_)
472 return print_mailer_error("InsetBoxMailer", in, 1, name_);
474 // This is part of the inset proper that is usually swallowed
475 // by Text::readInset
478 if (!lex || id != "Box")
479 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
485 InsetBoxParams::InsetBoxParams(string const & label)
489 width(Length("100col%")),
494 height(Length("1in")),
495 height_special("totalheight") // default is 1\\totalheight
499 void InsetBoxParams::write(ostream & os) const
501 os << "Box " << type << "\n";
502 os << "position \"" << pos << "\"\n";
503 os << "hor_pos \"" << hor_pos << "\"\n";
504 os << "has_inner_box " << inner_box << "\n";
505 os << "inner_pos \"" << inner_pos << "\"\n";
506 os << "use_parbox " << use_parbox << "\n";
507 os << "width \"" << width.asString() << "\"\n";
508 os << "special \"" << special << "\"\n";
509 os << "height \"" << height.asString() << "\"\n";
510 os << "height_special \"" << height_special << "\"\n";
514 void InsetBoxParams::read(Lexer & lex)
520 type = lex.getString();
527 token = lex.getString();
530 if (token == "position") {
532 // The [0] is needed. We need the first and only char in
534 pos = lex.getString()[0];
536 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
537 lex.pushToken(token);
541 token = lex.getString();
544 if (token == "hor_pos") {
546 hor_pos = lex.getString()[0];
548 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
549 lex.pushToken(token);
553 token = lex.getString();
556 if (token == "has_inner_box") {
558 inner_box = lex.getInteger();
560 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
561 lex.pushToken(token);
565 token = lex.getString();
568 if (token == "inner_pos") {
570 inner_pos = lex.getString()[0];
572 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
574 lex.pushToken(token);
578 token = lex.getString();
581 if (token == "use_parbox") {
583 use_parbox = lex.getInteger();
585 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
586 lex.pushToken(token);
590 token = lex.getString();
593 if (token == "width") {
595 width = Length(lex.getString());
597 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
598 lex.pushToken(token);
602 token = lex.getString();
605 if (token == "special") {
607 special = lex.getString();
609 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
610 lex.pushToken(token);
614 token = lex.getString();
617 if (token == "height") {
619 height = Length(lex.getString());
621 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
622 lex.pushToken(token);
626 token = lex.getString();
629 if (token == "height_special") {
631 height_special = lex.getString();
633 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
634 lex.pushToken(token);