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"
33 #include "frontends/Application.h"
43 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
44 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
46 BoxTranslator initBoxtranslator()
48 BoxTranslator translator("Boxed", InsetBox::Boxed);
49 translator.addPair("Frameless", InsetBox::Frameless);
50 translator.addPair("Framed", InsetBox::Framed);
51 translator.addPair("ovalbox", InsetBox::ovalbox);
52 translator.addPair("Ovalbox", InsetBox::Ovalbox);
53 translator.addPair("Shadowbox", InsetBox::Shadowbox);
54 translator.addPair("Shaded", InsetBox::Shaded);
55 translator.addPair("Doublebox",InsetBox::Doublebox);
60 BoxTranslatorLoc initBoxtranslatorLoc()
62 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
63 translator.addPair(_("frameless"), InsetBox::Frameless);
64 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
65 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
66 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
67 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
68 translator.addPair(_("shaded background"), InsetBox::Shaded);
69 translator.addPair(_("double frame"), InsetBox::Doublebox);
74 BoxTranslator const & boxtranslator()
76 static BoxTranslator translator = initBoxtranslator();
81 BoxTranslatorLoc const & boxtranslator_loc()
83 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
90 /////////////////////////////////////////////////////////////////////////
94 /////////////////////////////////////////////////////////////////////////
96 InsetBox::InsetBox(Buffer const & buffer, string const & label)
97 : InsetCollapsable(buffer), params_(label)
99 if (forceEmptyLayout())
100 paragraphs().back().setLayout(buffer.params().documentClass().emptyLayout());
104 InsetBox::~InsetBox()
106 hideDialogs("box", this);
110 docstring InsetBox::editMessage() const
112 return _("Opened Box Inset");
116 docstring InsetBox::name() const
120 if (boxtranslator().find(params_.type) == Shaded)
122 return from_ascii(name);
126 void InsetBox::write(ostream & os) const
129 InsetCollapsable::write(os);
133 void InsetBox::read(Lexer & lex)
136 InsetCollapsable::read(lex);
140 void InsetBox::setButtonLabel()
142 BoxType btype = boxtranslator().find(params_.type);
147 if (btype == Frameless) {
148 if (params_.use_parbox)
149 label += _("Parbox");
151 label += _("Minipage");
153 label += boxtranslator_loc().find(btype);
161 bool InsetBox::hasFixedWidth() const
163 return params_.inner_box || params_.special != "width";
167 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
169 // back up textwidth.
170 int textwidth_backup = m.base.textwidth;
172 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
173 InsetCollapsable::metrics(m, dim);
175 m.base.textwidth = textwidth_backup;
179 bool InsetBox::forceEmptyLayout(idx_type) const
181 return !params_.inner_box;
185 bool InsetBox::showInsetDialog(BufferView * bv) const
187 bv->showDialog("box", params2string(params_),
188 const_cast<InsetBox *>(this));
193 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
195 switch (cmd.action) {
197 case LFUN_INSET_MODIFY: {
198 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
199 if (cmd.getArg(0) == "changetype")
200 params_.type = cmd.getArg(1);
202 string2params(to_utf8(cmd.argument()), params_);
203 setLayout(cur.buffer().params());
207 case LFUN_INSET_DIALOG_UPDATE:
208 cur.bv().updateDialog("box", params2string(params_));
212 InsetCollapsable::doDispatch(cur, cmd);
218 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
219 FuncStatus & flag) const
221 switch (cmd.action) {
223 case LFUN_INSET_MODIFY:
224 if (cmd.getArg(0) == "changetype")
225 flag.setOnOff(cmd.getArg(1) == params_.type);
230 case LFUN_INSET_DIALOG_UPDATE:
234 case LFUN_BREAK_PARAGRAPH:
235 if (params_.inner_box)
236 return InsetCollapsable::getStatus(cur, cmd, flag);
241 return InsetCollapsable::getStatus(cur, cmd, flag);
246 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
248 BoxType btype = boxtranslator().find(params_.type);
250 string width_string = params_.width.asLatexString();
251 bool stdwidth = false;
252 if (params_.inner_box &&
253 (width_string.find("1.0\\columnwidth") != string::npos
254 || width_string.find("1.0\\textwidth") != string::npos)) {
262 width_string += " - 2\\fboxsep - 2\\fboxrule";
265 width_string += " - 2\\fboxsep - 0.8pt";
268 width_string += " - 2\\fboxsep - 1.6pt";
271 // Shadow falls outside right margin... opinions?
272 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
275 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
282 // Adapt to column/text width correctly also if paragraphs indented:
290 os << "\\begin{framed}%\n";
295 if (!params_.inner_box) {
297 // Special widths, see usrguide §3.5
299 if (params_.special != "none") {
300 os << "[" << params_.width.value()
301 << '\\' << from_utf8(params_.special)
304 os << '[' << from_ascii(width_string)
306 if (params_.hor_pos != 'c')
307 os << "[" << params_.hor_pos << "]";
319 os << "\\shadowbox{";
325 os << "\\doublebox{";
329 if (params_.inner_box) {
330 if (params_.use_parbox)
333 os << "\\begin{minipage}";
335 os << "[" << params_.pos << "]";
336 if (params_.height_special == "none") {
338 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
341 // set no optional argument when the value is the default "1\height"
342 // (special units like \height are handled as "in")
343 // but when the user has chosen a non-default inner_pos, the height
344 // must be given: \minipage[pos][height][inner-pos]{width}
345 if ((params_.height != Length("1in") ||
346 params_.height_special != "totalheight") ||
347 params_.inner_pos != params_.pos) {
349 os << "[" << params_.height.value()
350 << "\\" << from_utf8(params_.height_special) << "]";
353 if (params_.inner_pos != params_.pos)
354 os << "[" << params_.inner_pos << "]";
357 os << '{' << from_ascii(width_string) << '}';
359 if (params_.use_parbox)
364 if (btype == Shaded) {
365 os << "\\begin{shaded}%\n";
369 i += InsetText::latex(os, runparams);
372 os << "\\end{shaded}";
374 if (params_.inner_box) {
375 if (params_.use_parbox)
378 os << "%\n\\end{minipage}";
385 os << "\\end{framed}";
388 if (!params_.inner_box)
389 os << "}"; // for makebox
409 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
411 BoxType const btype = boxtranslator().find(params_.type);
435 InsetText::plaintext(os, runparams);
466 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
470 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
472 return InsetText::docbook(os, runparams);
476 void InsetBox::validate(LaTeXFeatures & features) const
478 BoxType btype = boxtranslator().find(params_.type);
483 features.require("framed");
486 features.require("calc");
492 features.require("calc");
493 features.require("fancybox");
496 features.require("color");
497 features.require("framed");
500 InsetText::validate(features);
504 docstring InsetBox::contextMenu(BufferView const &, int, int) const
506 return from_ascii("context-box");
510 string InsetBox::params2string(InsetBoxParams const & params)
513 data << "box" << ' ';
519 void InsetBox::string2params(string const & in, InsetBoxParams & params)
521 params = InsetBoxParams(string());
525 istringstream data(in);
531 if (!lex || name != "box") {
532 LYXERR0("InsetBox::string2params(" << in << ")\n"
533 "Expected arg 1 to be \"box\"\n");
537 // This is part of the inset proper that is usually swallowed
538 // by Text::readInset
541 if (!lex || id != "Box") {
542 LYXERR0("InsetBox::string2params(" << in << ")\n"
543 "Expected arg 2 to be \"Box\"\n");
550 /////////////////////////////////////////////////////////////////////////
554 /////////////////////////////////////////////////////////////////////////
556 InsetBoxParams::InsetBoxParams(string const & label)
560 width(Length("100col%")),
565 height(Length("1in")),
566 height_special("totalheight") // default is 1\\totalheight
570 void InsetBoxParams::write(ostream & os) const
572 os << "Box " << type << "\n";
573 os << "position \"" << pos << "\"\n";
574 os << "hor_pos \"" << hor_pos << "\"\n";
575 os << "has_inner_box " << inner_box << "\n";
576 os << "inner_pos \"" << inner_pos << "\"\n";
577 os << "use_parbox " << use_parbox << "\n";
578 os << "width \"" << width.asString() << "\"\n";
579 os << "special \"" << special << "\"\n";
580 os << "height \"" << height.asString() << "\"\n";
581 os << "height_special \"" << height_special << "\"\n";
585 void InsetBoxParams::read(Lexer & lex)
591 type = lex.getString();
598 token = lex.getString();
601 if (token == "position") {
603 // The [0] is needed. We need the first and only char in
605 pos = lex.getString()[0];
607 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
608 lex.pushToken(token);
612 token = lex.getString();
615 if (token == "hor_pos") {
617 hor_pos = lex.getString()[0];
619 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
620 lex.pushToken(token);
624 token = lex.getString();
627 if (token == "has_inner_box") {
629 inner_box = lex.getInteger();
630 if (type == "Framed")
633 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
634 lex.pushToken(token);
638 token = lex.getString();
641 if (token == "inner_pos") {
643 inner_pos = lex.getString()[0];
645 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
647 lex.pushToken(token);
651 token = lex.getString();
654 if (token == "use_parbox") {
656 use_parbox = lex.getInteger();
658 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
659 lex.pushToken(token);
663 token = lex.getString();
666 if (token == "width") {
668 width = Length(lex.getString());
670 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
671 lex.pushToken(token);
675 token = lex.getString();
678 if (token == "special") {
680 special = lex.getString();
682 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
683 lex.pushToken(token);
687 token = lex.getString();
690 if (token == "height") {
692 height = Length(lex.getString());
694 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
695 lex.pushToken(token);
699 token = lex.getString();
702 if (token == "height_special") {
704 height_special = lex.getString();
706 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
707 lex.pushToken(token);