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 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 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
238 BoxType btype = boxtranslator().find(params_.type);
240 string width_string = params_.width.asLatexString();
241 bool stdwidth = false;
242 if (params_.inner_box &&
243 (width_string.find("1.0\\columnwidth") != string::npos
244 || width_string.find("1.0\\textwidth") != string::npos)) {
252 width_string += " - 2\\fboxsep - 2\\fboxrule";
255 width_string += " - 2\\fboxsep - 0.8pt";
258 width_string += " - 2\\fboxsep - 1.6pt";
261 // Shadow falls outside right margin... opinions?
262 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
265 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
272 // Adapt to column/text width correctly also if paragraphs indented:
280 os << "\\begin{framed}%\n";
285 if (!params_.inner_box) {
287 // Special widths, see usrguide §3.5
289 if (params_.special != "none") {
290 os << "[" << params_.width.value()
291 << '\\' << from_utf8(params_.special)
294 os << '[' << from_ascii(width_string)
296 if (params_.hor_pos != 'c')
297 os << "[" << params_.hor_pos << "]";
309 os << "\\shadowbox{";
315 os << "\\doublebox{";
319 if (params_.inner_box) {
320 if (params_.use_parbox)
323 os << "\\begin{minipage}";
325 os << "[" << params_.pos << "]";
326 if (params_.height_special == "none") {
328 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
331 // set no optional argument when the value is the default "1\height"
332 // (special units like \height are handled as "in")
333 // but when the user has chosen a non-default inner_pos, the height
334 // must be given: \minipage[pos][height][inner-pos]{width}
335 if ((params_.height != Length("1in") ||
336 params_.height_special != "totalheight") ||
337 params_.inner_pos != params_.pos) {
339 os << "[" << params_.height.value()
340 << "\\" << from_utf8(params_.height_special) << "]";
343 if (params_.inner_pos != params_.pos)
344 os << "[" << params_.inner_pos << "]";
347 os << '{' << from_ascii(width_string) << '}';
349 if (params_.use_parbox)
355 os << "\\begin{shaded}%\n";
358 i += InsetText::latex(os, runparams);
361 os << "\\end{shaded}";
363 if (params_.inner_box) {
364 if (params_.use_parbox)
367 os << "%\n\\end{minipage}";
374 os << "\\end{framed}";
377 if (!params_.inner_box)
378 os << "}"; // for makebox
398 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
400 BoxType const btype = boxtranslator().find(params_.type);
424 InsetText::plaintext(os, runparams);
455 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
459 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
461 return InsetText::docbook(os, runparams);
465 void InsetBox::validate(LaTeXFeatures & features) const
467 BoxType btype = boxtranslator().find(params_.type);
472 features.require("framed");
475 features.require("calc");
481 features.require("calc");
482 features.require("fancybox");
485 features.require("color");
486 features.require("framed");
489 InsetText::validate(features);
493 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
498 string const InsetBoxMailer::name_ = "box";
501 string const InsetBoxMailer::inset2string(Buffer const &) const
503 return params2string(inset_.params());
507 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
510 data << "box" << ' ';
516 void InsetBoxMailer::string2params(string const & in,
517 InsetBoxParams & params)
519 params = InsetBoxParams(string());
523 istringstream data(in);
529 if (!lex || name != name_)
530 return print_mailer_error("InsetBoxMailer", in, 1, name_);
532 // This is part of the inset proper that is usually swallowed
533 // by Text::readInset
536 if (!lex || id != "Box")
537 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
543 InsetBoxParams::InsetBoxParams(string const & label)
547 width(Length("100col%")),
552 height(Length("1in")),
553 height_special("totalheight") // default is 1\\totalheight
557 void InsetBoxParams::write(ostream & os) const
559 os << "Box " << type << "\n";
560 os << "position \"" << pos << "\"\n";
561 os << "hor_pos \"" << hor_pos << "\"\n";
562 os << "has_inner_box " << inner_box << "\n";
563 os << "inner_pos \"" << inner_pos << "\"\n";
564 os << "use_parbox " << use_parbox << "\n";
565 os << "width \"" << width.asString() << "\"\n";
566 os << "special \"" << special << "\"\n";
567 os << "height \"" << height.asString() << "\"\n";
568 os << "height_special \"" << height_special << "\"\n";
572 void InsetBoxParams::read(Lexer & lex)
578 type = lex.getString();
585 token = lex.getString();
588 if (token == "position") {
590 // The [0] is needed. We need the first and only char in
592 pos = lex.getString()[0];
594 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
595 lex.pushToken(token);
599 token = lex.getString();
602 if (token == "hor_pos") {
604 hor_pos = lex.getString()[0];
606 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
607 lex.pushToken(token);
611 token = lex.getString();
614 if (token == "has_inner_box") {
616 inner_box = lex.getInteger();
618 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
619 lex.pushToken(token);
623 token = lex.getString();
626 if (token == "inner_pos") {
628 inner_pos = lex.getString()[0];
630 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
632 lex.pushToken(token);
636 token = lex.getString();
639 if (token == "use_parbox") {
641 use_parbox = lex.getInteger();
643 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
644 lex.pushToken(token);
648 token = lex.getString();
651 if (token == "width") {
653 width = Length(lex.getString());
655 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
656 lex.pushToken(token);
660 token = lex.getString();
663 if (token == "special") {
665 special = lex.getString();
667 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
668 lex.pushToken(token);
672 token = lex.getString();
675 if (token == "height") {
677 height = Length(lex.getString());
679 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
680 lex.pushToken(token);
684 token = lex.getString();
687 if (token == "height_special") {
689 height_special = lex.getString();
691 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
692 lex.pushToken(token);