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 "BufferParams.h"
19 #include "BufferView.h"
21 #include "DispatchResult.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
24 #include "support/gettext.h"
25 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
28 #include "TextClass.h"
30 #include "support/debug.h"
31 #include "support/Translator.h"
41 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
42 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
44 BoxTranslator const init_boxtranslator()
46 BoxTranslator translator("Boxed", InsetBox::Boxed);
47 translator.addPair("Frameless", InsetBox::Frameless);
48 translator.addPair("Framed", InsetBox::Framed);
49 translator.addPair("ovalbox", InsetBox::ovalbox);
50 translator.addPair("Ovalbox", InsetBox::Ovalbox);
51 translator.addPair("Shadowbox", InsetBox::Shadowbox);
52 translator.addPair("Shaded", InsetBox::Shaded);
53 translator.addPair("Doublebox",InsetBox::Doublebox);
58 BoxTranslatorLoc const init_boxtranslator_loc()
60 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
61 translator.addPair(_("frameless"), InsetBox::Frameless);
62 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
63 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
64 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
65 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
66 translator.addPair(_("shaded background"), InsetBox::Shaded);
67 translator.addPair(_("double frame"), 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(Buffer const & buffer, string const & label)
89 : InsetCollapsable(buffer), params_(label)
91 if (forceEmptyLayout())
92 paragraphs().back().setLayout(buffer.params().documentClass().emptyLayout());
98 InsetBoxMailer(*this).hideDialog();
102 docstring InsetBox::editMessage() const
104 return _("Opened Box Inset");
108 docstring InsetBox::name() const
112 if (boxtranslator().find(params_.type) == Shaded)
113 name += string(":Shaded");
114 return from_ascii(name);
118 void InsetBox::write(ostream & os) const
121 InsetCollapsable::write(os);
125 void InsetBox::read(Lexer & lex)
128 InsetCollapsable::read(lex);
132 void InsetBox::setButtonLabel()
134 BoxType btype = boxtranslator().find(params_.type);
139 if (btype == Frameless) {
140 if (params_.use_parbox)
141 label += _("Parbox");
143 label += _("Minipage");
145 label += boxtranslator_loc().find(btype);
152 bool InsetBox::hasFixedWidth() const
154 return params_.inner_box || params_.special != "width";
158 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
160 // back up textwidth.
161 int textwidth_backup = m.base.textwidth;
163 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
164 InsetCollapsable::metrics(m, dim);
166 m.base.textwidth = textwidth_backup;
170 bool InsetBox::forceEmptyLayout(idx_type) const
172 return !params_.inner_box;
176 bool InsetBox::showInsetDialog(BufferView * bv) const
178 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
183 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
185 switch (cmd.action) {
187 case LFUN_INSET_MODIFY: {
188 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
189 if (cmd.getArg(0) == "changetype")
190 params_.type = cmd.getArg(1);
192 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
193 setLayout(cur.buffer().params());
197 case LFUN_INSET_DIALOG_UPDATE:
198 InsetBoxMailer(*this).updateDialog(&cur.bv());
201 case LFUN_MOUSE_RELEASE:
202 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
203 InsetBoxMailer(*this).showDialog(&cur.bv());
206 InsetCollapsable::doDispatch(cur, cmd);
210 InsetCollapsable::doDispatch(cur, cmd);
216 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
217 FuncStatus & flag) const
219 switch (cmd.action) {
221 case LFUN_INSET_MODIFY:
222 if (cmd.getArg(0) == "changetype")
223 flag.setOnOff(cmd.getArg(1) == params_.type);
227 case LFUN_INSET_DIALOG_UPDATE:
230 case LFUN_BREAK_PARAGRAPH:
231 if (params_.inner_box) {
232 return InsetCollapsable::getStatus(cur, cmd, flag);
239 return InsetCollapsable::getStatus(cur, cmd, flag);
244 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
246 BoxType btype = boxtranslator().find(params_.type);
248 string width_string = params_.width.asLatexString();
249 bool stdwidth = false;
250 if (params_.inner_box &&
251 (width_string.find("1.0\\columnwidth") != string::npos
252 || 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 - 1pt";
280 // Adapt to column/text width correctly also if paragraphs indented:
288 os << "\\begin{framed}%\n";
293 if (!params_.inner_box) {
295 // Special widths, see usrguide §3.5
297 if (params_.special != "none") {
298 os << "[" << params_.width.value()
299 << '\\' << from_utf8(params_.special)
302 os << '[' << from_ascii(width_string)
304 if (params_.hor_pos != 'c')
305 os << "[" << params_.hor_pos << "]";
317 os << "\\shadowbox{";
323 os << "\\doublebox{";
327 if (params_.inner_box) {
328 if (params_.use_parbox)
331 os << "\\begin{minipage}";
333 os << "[" << params_.pos << "]";
334 if (params_.height_special == "none") {
336 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
339 // set no optional argument when the value is the default "1\height"
340 // (special units like \height are handled as "in")
341 // but when the user has chosen a non-default inner_pos, the height
342 // must be given: \minipage[pos][height][inner-pos]{width}
343 if ((params_.height != Length("1in") ||
344 params_.height_special != "totalheight") ||
345 params_.inner_pos != params_.pos) {
347 os << "[" << params_.height.value()
348 << "\\" << from_utf8(params_.height_special) << "]";
351 if (params_.inner_pos != params_.pos)
352 os << "[" << params_.inner_pos << "]";
355 os << '{' << from_ascii(width_string) << '}';
357 if (params_.use_parbox)
363 os << "\\begin{shaded}%\n";
366 i += InsetText::latex(os, runparams);
369 os << "\\end{shaded}";
371 if (params_.inner_box) {
372 if (params_.use_parbox)
375 os << "%\n\\end{minipage}";
382 os << "\\end{framed}";
385 if (!params_.inner_box)
386 os << "}"; // for makebox
406 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
408 BoxType const btype = boxtranslator().find(params_.type);
432 InsetText::plaintext(os, runparams);
463 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
467 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
469 return InsetText::docbook(os, runparams);
473 void InsetBox::validate(LaTeXFeatures & features) const
475 BoxType btype = boxtranslator().find(params_.type);
480 features.require("framed");
483 features.require("calc");
489 features.require("calc");
490 features.require("fancybox");
493 features.require("color");
494 features.require("framed");
497 InsetText::validate(features);
501 docstring InsetBox::contextMenu(BufferView const &, int, int) const
503 return from_ascii("context-box");
507 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
512 string const InsetBoxMailer::name_ = "box";
515 string const InsetBoxMailer::inset2string(Buffer const &) const
517 return params2string(inset_.params());
521 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
524 data << "box" << ' ';
530 void InsetBoxMailer::string2params(string const & in,
531 InsetBoxParams & params)
533 params = InsetBoxParams(string());
537 istringstream data(in);
543 if (!lex || name != name_)
544 return print_mailer_error("InsetBoxMailer", in, 1, name_);
546 // This is part of the inset proper that is usually swallowed
547 // by Text::readInset
550 if (!lex || id != "Box")
551 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
557 InsetBoxParams::InsetBoxParams(string const & label)
561 width(Length("100col%")),
566 height(Length("1in")),
567 height_special("totalheight") // default is 1\\totalheight
571 void InsetBoxParams::write(ostream & os) const
573 os << "Box " << type << "\n";
574 os << "position \"" << pos << "\"\n";
575 os << "hor_pos \"" << hor_pos << "\"\n";
576 os << "has_inner_box " << inner_box << "\n";
577 os << "inner_pos \"" << inner_pos << "\"\n";
578 os << "use_parbox " << use_parbox << "\n";
579 os << "width \"" << width.asString() << "\"\n";
580 os << "special \"" << special << "\"\n";
581 os << "height \"" << height.asString() << "\"\n";
582 os << "height_special \"" << height_special << "\"\n";
586 void InsetBoxParams::read(Lexer & lex)
592 type = lex.getString();
599 token = lex.getString();
602 if (token == "position") {
604 // The [0] is needed. We need the first and only char in
606 pos = lex.getString()[0];
608 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
609 lex.pushToken(token);
613 token = lex.getString();
616 if (token == "hor_pos") {
618 hor_pos = lex.getString()[0];
620 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
621 lex.pushToken(token);
625 token = lex.getString();
628 if (token == "has_inner_box") {
630 inner_box = lex.getInteger();
632 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
633 lex.pushToken(token);
637 token = lex.getString();
640 if (token == "inner_pos") {
642 inner_pos = lex.getString()[0];
644 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
646 lex.pushToken(token);
650 token = lex.getString();
653 if (token == "use_parbox") {
655 use_parbox = lex.getInteger();
657 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
658 lex.pushToken(token);
662 token = lex.getString();
665 if (token == "width") {
667 width = Length(lex.getString());
669 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
670 lex.pushToken(token);
674 token = lex.getString();
677 if (token == "special") {
679 special = lex.getString();
681 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
682 lex.pushToken(token);
686 token = lex.getString();
689 if (token == "height") {
691 height = Length(lex.getString());
693 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
694 lex.pushToken(token);
698 token = lex.getString();
701 if (token == "height_special") {
703 height_special = lex.getString();
705 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
706 lex.pushToken(token);