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().layout(bp.textClass().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 const 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(Buffer const & buf, ostream & os) const
132 InsetCollapsable::write(buf, os);
136 void InsetBox::read(Buffer const & buf, Lexer & lex)
139 InsetCollapsable::read(buf, 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(Buffer const &) const
249 BoxType btype = boxtranslator().find(params_.type);
250 return btype != Frameless || params_.inner_box;
254 int InsetBox::latex(Buffer const & buf, odocstream & os,
255 OutputParams const & runparams) const
257 BoxType btype = boxtranslator().find(params_.type);
259 string width_string = params_.width.asLatexString();
260 bool stdwidth(false);
261 if (params_.inner_box &&
262 (width_string.find("1.0\\columnwidth") != string::npos
263 || width_string.find("1.0\\textwidth") != string::npos)) {
271 width_string += " - 2\\fboxsep - 2\\fboxrule";
274 width_string += " - 2\\fboxsep - 0.8pt";
277 width_string += " - 2\\fboxsep - 1.6pt";
280 // Shadow falls outside right margin... opinions?
281 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
284 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
291 // Adapt to column/text width correctly also if paragraphs indented:
299 os << "\\begin{framed}%\n";
304 if (!params_.inner_box) {
306 // Special widths, see usrguide §3.5
308 if (params_.special != "none") {
309 os << "[" << params_.width.value()
310 << '\\' << from_utf8(params_.special)
313 os << '[' << from_ascii(width_string)
315 if (params_.hor_pos != 'c')
316 os << "[" << params_.hor_pos << "]";
328 os << "\\shadowbox{";
334 os << "\\doublebox{";
338 if (params_.inner_box) {
339 if (params_.use_parbox)
342 os << "\\begin{minipage}";
344 os << "[" << params_.pos << "]";
345 if (params_.height_special == "none") {
347 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
350 // set no optional argument when the value is the default "1\height"
351 // (special units like \height are handled as "in")
352 // but when the user has chosen a non-default inner_pos, the height
353 // must be given: \minipage[pos][height][inner-pos]{width}
354 if ((params_.height != Length("1in") ||
355 params_.height_special != "totalheight") ||
356 params_.inner_pos != params_.pos) {
358 os << "[" << params_.height.value()
359 << "\\" << from_utf8(params_.height_special) << "]";
362 if (params_.inner_pos != params_.pos)
363 os << "[" << params_.inner_pos << "]";
366 os << '{' << from_ascii(width_string) << '}';
368 if (params_.use_parbox)
374 os << "\\begin{shaded}%\n";
377 i += InsetText::latex(buf, os, runparams);
380 os << "\\end{shaded}";
382 if (params_.inner_box) {
383 if (params_.use_parbox)
386 os << "%\n\\end{minipage}";
393 os << "\\end{framed}";
396 if (!params_.inner_box)
397 os << "}"; // for makebox
417 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
418 OutputParams const & runparams) const
420 BoxType const btype = boxtranslator().find(params_.type);
444 InsetText::plaintext(buf, os, runparams);
475 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
479 int InsetBox::docbook(Buffer const & buf, odocstream & os,
480 OutputParams const & runparams) const
482 return InsetText::docbook(buf, os, runparams);
486 void InsetBox::validate(LaTeXFeatures & features) const
488 BoxType btype = boxtranslator().find(params_.type);
493 features.require("framed");
496 features.require("calc");
502 features.require("calc");
503 features.require("fancybox");
506 features.require("color");
507 features.require("framed");
510 InsetText::validate(features);
514 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
519 string const InsetBoxMailer::name_ = "box";
522 string const InsetBoxMailer::inset2string(Buffer const &) const
524 return params2string(inset_.params());
528 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
531 data << "box" << ' ';
537 void InsetBoxMailer::string2params(string const & in,
538 InsetBoxParams & params)
540 params = InsetBoxParams(string());
544 istringstream data(in);
550 if (!lex || name != name_)
551 return print_mailer_error("InsetBoxMailer", in, 1, name_);
553 // This is part of the inset proper that is usually swallowed
554 // by Text::readInset
557 if (!lex || id != "Box")
558 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
564 InsetBoxParams::InsetBoxParams(string const & label)
568 width(Length("100col%")),
573 height(Length("1in")),
574 height_special("totalheight") // default is 1\\totalheight
578 void InsetBoxParams::write(ostream & os) const
580 os << "Box " << type << "\n";
581 os << "position \"" << pos << "\"\n";
582 os << "hor_pos \"" << hor_pos << "\"\n";
583 os << "has_inner_box " << inner_box << "\n";
584 os << "inner_pos \"" << inner_pos << "\"\n";
585 os << "use_parbox " << use_parbox << "\n";
586 os << "width \"" << width.asString() << "\"\n";
587 os << "special \"" << special << "\"\n";
588 os << "height \"" << height.asString() << "\"\n";
589 os << "height_special \"" << height_special << "\"\n";
593 void InsetBoxParams::read(Lexer & lex)
599 type = lex.getString();
606 token = lex.getString();
609 if (token == "position") {
611 // The [0] is needed. We need the first and only char in
613 pos = lex.getString()[0];
615 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
616 lex.pushToken(token);
620 token = lex.getString();
623 if (token == "hor_pos") {
625 hor_pos = lex.getString()[0];
627 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
628 lex.pushToken(token);
632 token = lex.getString();
635 if (token == "has_inner_box") {
637 inner_box = lex.getInteger();
639 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
640 lex.pushToken(token);
644 token = lex.getString();
647 if (token == "inner_pos") {
649 inner_pos = lex.getString()[0];
651 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
653 lex.pushToken(token);
657 token = lex.getString();
660 if (token == "use_parbox") {
662 use_parbox = lex.getInteger();
664 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
665 lex.pushToken(token);
669 token = lex.getString();
672 if (token == "width") {
674 width = Length(lex.getString());
676 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
677 lex.pushToken(token);
681 token = lex.getString();
684 if (token == "special") {
686 special = lex.getString();
688 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
689 lex.pushToken(token);
693 token = lex.getString();
696 if (token == "height") {
698 height = Length(lex.getString());
700 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
701 lex.pushToken(token);
705 token = lex.getString();
708 if (token == "height_special") {
710 height_special = lex.getString();
712 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
713 lex.pushToken(token);