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(BufferParams const & bp, string const & label)
89 : InsetCollapsable(bp), params_(label)
91 if (forceEmptyLayout())
92 paragraphs().back().setLayout(bp.documentClass().emptyLayout());
96 InsetBox::InsetBox(InsetBox const & in)
97 : InsetCollapsable(in), params_(in.params_)
101 InsetBox::~InsetBox()
103 InsetBoxMailer(*this).hideDialog();
107 Inset * InsetBox::clone() const
109 return new InsetBox(*this);
113 docstring InsetBox::editMessage() const
115 return _("Opened Box Inset");
119 docstring InsetBox::name() const
122 string name = string("Box");
123 if (boxtranslator().find(params_.type) == Shaded)
124 name += string(":Shaded");
125 return from_ascii(name);
129 void InsetBox::write(ostream & os) const
132 InsetCollapsable::write(os);
136 void InsetBox::read(Lexer & lex)
139 InsetCollapsable::read(lex);
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);
163 bool InsetBox::hasFixedWidth() const
165 return params_.inner_box || params_.special != "width";
169 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
171 // back up textwidth.
172 int textwidth_backup = m.base.textwidth;
174 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
175 InsetCollapsable::metrics(m, dim);
177 m.base.textwidth = textwidth_backup;
181 bool InsetBox::forceEmptyLayout() const
183 return !params_.inner_box;
187 bool InsetBox::showInsetDialog(BufferView * bv) const
189 InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
194 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
196 switch (cmd.action) {
198 case LFUN_INSET_MODIFY: {
199 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
200 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
201 setLayout(cur.buffer().params());
205 case LFUN_INSET_DIALOG_UPDATE:
206 InsetBoxMailer(*this).updateDialog(&cur.bv());
209 case LFUN_MOUSE_RELEASE:
210 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
211 InsetBoxMailer(*this).showDialog(&cur.bv());
214 InsetCollapsable::doDispatch(cur, cmd);
218 InsetCollapsable::doDispatch(cur, cmd);
224 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
225 FuncStatus & flag) const
227 switch (cmd.action) {
229 case LFUN_INSET_MODIFY:
230 case LFUN_INSET_DIALOG_UPDATE:
233 case LFUN_BREAK_PARAGRAPH:
234 if (params_.inner_box) {
235 return InsetCollapsable::getStatus(cur, cmd, flag);
242 return InsetCollapsable::getStatus(cur, cmd, flag);
247 bool InsetBox::isMacroScope() const
249 BoxType btype = boxtranslator().find(params_.type);
250 return btype != Frameless || params_.inner_box;
254 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
256 BoxType btype = boxtranslator().find(params_.type);
258 string width_string = params_.width.asLatexString();
259 bool stdwidth = false;
260 if (params_.inner_box &&
261 (width_string.find("1.0\\columnwidth") != string::npos
262 || width_string.find("1.0\\textwidth") != string::npos)) {
270 width_string += " - 2\\fboxsep - 2\\fboxrule";
273 width_string += " - 2\\fboxsep - 0.8pt";
276 width_string += " - 2\\fboxsep - 1.6pt";
279 // Shadow falls outside right margin... opinions?
280 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
283 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
290 // Adapt to column/text width correctly also if paragraphs indented:
298 os << "\\begin{framed}%\n";
303 if (!params_.inner_box) {
305 // Special widths, see usrguide §3.5
307 if (params_.special != "none") {
308 os << "[" << params_.width.value()
309 << '\\' << from_utf8(params_.special)
312 os << '[' << from_ascii(width_string)
314 if (params_.hor_pos != 'c')
315 os << "[" << params_.hor_pos << "]";
327 os << "\\shadowbox{";
333 os << "\\doublebox{";
337 if (params_.inner_box) {
338 if (params_.use_parbox)
341 os << "\\begin{minipage}";
343 os << "[" << params_.pos << "]";
344 if (params_.height_special == "none") {
346 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
349 // set no optional argument when the value is the default "1\height"
350 // (special units like \height are handled as "in")
351 // but when the user has chosen a non-default inner_pos, the height
352 // must be given: \minipage[pos][height][inner-pos]{width}
353 if ((params_.height != Length("1in") ||
354 params_.height_special != "totalheight") ||
355 params_.inner_pos != params_.pos) {
357 os << "[" << params_.height.value()
358 << "\\" << from_utf8(params_.height_special) << "]";
361 if (params_.inner_pos != params_.pos)
362 os << "[" << params_.inner_pos << "]";
365 os << '{' << from_ascii(width_string) << '}';
367 if (params_.use_parbox)
373 os << "\\begin{shaded}%\n";
376 i += InsetText::latex(os, runparams);
379 os << "\\end{shaded}";
381 if (params_.inner_box) {
382 if (params_.use_parbox)
385 os << "%\n\\end{minipage}";
392 os << "\\end{framed}";
395 if (!params_.inner_box)
396 os << "}"; // for makebox
416 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
418 BoxType const btype = boxtranslator().find(params_.type);
442 InsetText::plaintext(os, runparams);
473 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
477 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
479 return InsetText::docbook(os, runparams);
483 void InsetBox::validate(LaTeXFeatures & features) const
485 BoxType btype = boxtranslator().find(params_.type);
490 features.require("framed");
493 features.require("calc");
499 features.require("calc");
500 features.require("fancybox");
503 features.require("color");
504 features.require("framed");
507 InsetText::validate(features);
511 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
516 string const InsetBoxMailer::name_ = "box";
519 string const InsetBoxMailer::inset2string(Buffer const &) const
521 return params2string(inset_.params());
525 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
528 data << "box" << ' ';
534 void InsetBoxMailer::string2params(string const & in,
535 InsetBoxParams & params)
537 params = InsetBoxParams(string());
541 istringstream data(in);
547 if (!lex || name != name_)
548 return print_mailer_error("InsetBoxMailer", in, 1, name_);
550 // This is part of the inset proper that is usually swallowed
551 // by Text::readInset
554 if (!lex || id != "Box")
555 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
561 InsetBoxParams::InsetBoxParams(string const & label)
565 width(Length("100col%")),
570 height(Length("1in")),
571 height_special("totalheight") // default is 1\\totalheight
575 void InsetBoxParams::write(ostream & os) const
577 os << "Box " << type << "\n";
578 os << "position \"" << pos << "\"\n";
579 os << "hor_pos \"" << hor_pos << "\"\n";
580 os << "has_inner_box " << inner_box << "\n";
581 os << "inner_pos \"" << inner_pos << "\"\n";
582 os << "use_parbox " << use_parbox << "\n";
583 os << "width \"" << width.asString() << "\"\n";
584 os << "special \"" << special << "\"\n";
585 os << "height \"" << height.asString() << "\"\n";
586 os << "height_special \"" << height_special << "\"\n";
590 void InsetBoxParams::read(Lexer & lex)
596 type = lex.getString();
603 token = lex.getString();
606 if (token == "position") {
608 // The [0] is needed. We need the first and only char in
610 pos = lex.getString()[0];
612 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
613 lex.pushToken(token);
617 token = lex.getString();
620 if (token == "hor_pos") {
622 hor_pos = lex.getString()[0];
624 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
625 lex.pushToken(token);
629 token = lex.getString();
632 if (token == "has_inner_box") {
634 inner_box = lex.getInteger();
636 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
637 lex.pushToken(token);
641 token = lex.getString();
644 if (token == "inner_pos") {
646 inner_pos = lex.getString()[0];
648 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
650 lex.pushToken(token);
654 token = lex.getString();
657 if (token == "use_parbox") {
659 use_parbox = lex.getInteger();
661 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
662 lex.pushToken(token);
666 token = lex.getString();
669 if (token == "width") {
671 width = Length(lex.getString());
673 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
674 lex.pushToken(token);
678 token = lex.getString();
681 if (token == "special") {
683 special = lex.getString();
685 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
686 lex.pushToken(token);
690 token = lex.getString();
693 if (token == "height") {
695 height = Length(lex.getString());
697 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
698 lex.pushToken(token);
702 token = lex.getString();
705 if (token == "height_special") {
707 height_special = lex.getString();
709 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
710 lex.pushToken(token);