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() 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 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
190 setLayout(cur.buffer().params());
194 case LFUN_INSET_DIALOG_UPDATE:
195 InsetBoxMailer(*this).updateDialog(&cur.bv());
198 case LFUN_MOUSE_RELEASE:
199 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
200 InsetBoxMailer(*this).showDialog(&cur.bv());
203 InsetCollapsable::doDispatch(cur, cmd);
207 InsetCollapsable::doDispatch(cur, cmd);
213 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
214 FuncStatus & flag) const
216 switch (cmd.action) {
218 case LFUN_INSET_MODIFY:
219 case LFUN_INSET_DIALOG_UPDATE:
222 case LFUN_BREAK_PARAGRAPH:
223 if (params_.inner_box) {
224 return InsetCollapsable::getStatus(cur, cmd, flag);
231 return InsetCollapsable::getStatus(cur, cmd, flag);
236 bool InsetBox::isMacroScope() const
238 BoxType btype = boxtranslator().find(params_.type);
239 return btype != Frameless || params_.inner_box;
243 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
245 BoxType btype = boxtranslator().find(params_.type);
247 string width_string = params_.width.asLatexString();
248 bool stdwidth = false;
249 if (params_.inner_box &&
250 (width_string.find("1.0\\columnwidth") != string::npos
251 || width_string.find("1.0\\textwidth") != string::npos)) {
259 width_string += " - 2\\fboxsep - 2\\fboxrule";
262 width_string += " - 2\\fboxsep - 0.8pt";
265 width_string += " - 2\\fboxsep - 1.6pt";
268 // Shadow falls outside right margin... opinions?
269 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
272 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
279 // Adapt to column/text width correctly also if paragraphs indented:
287 os << "\\begin{framed}%\n";
292 if (!params_.inner_box) {
294 // Special widths, see usrguide §3.5
296 if (params_.special != "none") {
297 os << "[" << params_.width.value()
298 << '\\' << from_utf8(params_.special)
301 os << '[' << from_ascii(width_string)
303 if (params_.hor_pos != 'c')
304 os << "[" << params_.hor_pos << "]";
316 os << "\\shadowbox{";
322 os << "\\doublebox{";
326 if (params_.inner_box) {
327 if (params_.use_parbox)
330 os << "\\begin{minipage}";
332 os << "[" << params_.pos << "]";
333 if (params_.height_special == "none") {
335 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
338 // set no optional argument when the value is the default "1\height"
339 // (special units like \height are handled as "in")
340 // but when the user has chosen a non-default inner_pos, the height
341 // must be given: \minipage[pos][height][inner-pos]{width}
342 if ((params_.height != Length("1in") ||
343 params_.height_special != "totalheight") ||
344 params_.inner_pos != params_.pos) {
346 os << "[" << params_.height.value()
347 << "\\" << from_utf8(params_.height_special) << "]";
350 if (params_.inner_pos != params_.pos)
351 os << "[" << params_.inner_pos << "]";
354 os << '{' << from_ascii(width_string) << '}';
356 if (params_.use_parbox)
362 os << "\\begin{shaded}%\n";
365 i += InsetText::latex(os, runparams);
368 os << "\\end{shaded}";
370 if (params_.inner_box) {
371 if (params_.use_parbox)
374 os << "%\n\\end{minipage}";
381 os << "\\end{framed}";
384 if (!params_.inner_box)
385 os << "}"; // for makebox
405 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
407 BoxType const btype = boxtranslator().find(params_.type);
431 InsetText::plaintext(os, runparams);
462 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
466 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
468 return InsetText::docbook(os, runparams);
472 void InsetBox::validate(LaTeXFeatures & features) const
474 BoxType btype = boxtranslator().find(params_.type);
479 features.require("framed");
482 features.require("calc");
488 features.require("calc");
489 features.require("fancybox");
492 features.require("color");
493 features.require("framed");
496 InsetText::validate(features);
500 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
505 string const InsetBoxMailer::name_ = "box";
508 string const InsetBoxMailer::inset2string(Buffer const &) const
510 return params2string(inset_.params());
514 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
517 data << "box" << ' ';
523 void InsetBoxMailer::string2params(string const & in,
524 InsetBoxParams & params)
526 params = InsetBoxParams(string());
530 istringstream data(in);
536 if (!lex || name != name_)
537 return print_mailer_error("InsetBoxMailer", in, 1, name_);
539 // This is part of the inset proper that is usually swallowed
540 // by Text::readInset
543 if (!lex || id != "Box")
544 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
550 InsetBoxParams::InsetBoxParams(string const & label)
554 width(Length("100col%")),
559 height(Length("1in")),
560 height_special("totalheight") // default is 1\\totalheight
564 void InsetBoxParams::write(ostream & os) const
566 os << "Box " << type << "\n";
567 os << "position \"" << pos << "\"\n";
568 os << "hor_pos \"" << hor_pos << "\"\n";
569 os << "has_inner_box " << inner_box << "\n";
570 os << "inner_pos \"" << inner_pos << "\"\n";
571 os << "use_parbox " << use_parbox << "\n";
572 os << "width \"" << width.asString() << "\"\n";
573 os << "special \"" << special << "\"\n";
574 os << "height \"" << height.asString() << "\"\n";
575 os << "height_special \"" << height_special << "\"\n";
579 void InsetBoxParams::read(Lexer & lex)
585 type = lex.getString();
592 token = lex.getString();
595 if (token == "position") {
597 // The [0] is needed. We need the first and only char in
599 pos = lex.getString()[0];
601 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
602 lex.pushToken(token);
606 token = lex.getString();
609 if (token == "hor_pos") {
611 hor_pos = lex.getString()[0];
613 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
614 lex.pushToken(token);
618 token = lex.getString();
621 if (token == "has_inner_box") {
623 inner_box = lex.getInteger();
625 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
626 lex.pushToken(token);
630 token = lex.getString();
633 if (token == "inner_pos") {
635 inner_pos = lex.getString()[0];
637 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
639 lex.pushToken(token);
643 token = lex.getString();
646 if (token == "use_parbox") {
648 use_parbox = lex.getInteger();
650 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
651 lex.pushToken(token);
655 token = lex.getString();
658 if (token == "width") {
660 width = Length(lex.getString());
662 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
663 lex.pushToken(token);
667 token = lex.getString();
670 if (token == "special") {
672 special = lex.getString();
674 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
675 lex.pushToken(token);
679 token = lex.getString();
682 if (token == "height") {
684 height = Length(lex.getString());
686 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
687 lex.pushToken(token);
691 token = lex.getString();
694 if (token == "height_special") {
696 height_special = lex.getString();
698 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
699 lex.pushToken(token);