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 "LaTeXFeatures.h"
26 #include "MetricsInfo.h"
27 #include "TextClass.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/Translator.h"
34 #include "frontends/Application.h"
39 using namespace lyx::support;
45 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
46 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
48 BoxTranslator initBoxtranslator()
50 BoxTranslator translator("Boxed", InsetBox::Boxed);
51 translator.addPair("Frameless", InsetBox::Frameless);
52 translator.addPair("Framed", InsetBox::Framed);
53 translator.addPair("ovalbox", InsetBox::ovalbox);
54 translator.addPair("Ovalbox", InsetBox::Ovalbox);
55 translator.addPair("Shadowbox", InsetBox::Shadowbox);
56 translator.addPair("Shaded", InsetBox::Shaded);
57 translator.addPair("Doublebox",InsetBox::Doublebox);
62 BoxTranslatorLoc initBoxtranslatorLoc()
64 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
65 translator.addPair(_("frameless"), InsetBox::Frameless);
66 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
67 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
68 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
69 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
70 translator.addPair(_("shaded background"), InsetBox::Shaded);
71 translator.addPair(_("double frame"), InsetBox::Doublebox);
76 BoxTranslator const & boxtranslator()
78 static BoxTranslator translator = initBoxtranslator();
83 BoxTranslatorLoc const & boxtranslator_loc()
85 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
92 /////////////////////////////////////////////////////////////////////////
96 /////////////////////////////////////////////////////////////////////////
98 InsetBox::InsetBox(Buffer const & buffer, string const & label)
99 : InsetCollapsable(buffer), params_(label)
101 if (forcePlainLayout())
102 paragraphs().back().setLayout(buffer.params().documentClass().plainLayout());
106 InsetBox::~InsetBox()
108 hideDialogs("box", this);
112 docstring InsetBox::editMessage() const
114 return _("Opened Box Inset");
118 docstring InsetBox::name() const
122 if (boxtranslator().find(params_.type) == Shaded)
124 return from_ascii(name);
128 void InsetBox::write(ostream & os) const
131 InsetCollapsable::write(os);
135 void InsetBox::read(Lexer & lex)
138 InsetCollapsable::read(lex);
142 void InsetBox::setButtonLabel()
144 BoxType const btype = boxtranslator().find(params_.type);
146 docstring const type = _("Box");
149 if (params_.inner_box) {
150 if (params_.use_parbox)
153 inner = _("Minipage");
157 if (btype != Frameless)
158 frame = boxtranslator_loc().find(btype);
161 if (inner.empty() && frame.empty())
163 else if (inner.empty())
164 label = bformat(_("%1$s (%2$s)"),
166 else if (frame.empty())
167 label = bformat(_("%1$s (%2$s)"),
170 label = bformat(_("%1$s (%2$s, %3$s)"),
176 bool InsetBox::hasFixedWidth() const
178 return params_.inner_box || params_.special != "width";
182 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
184 // back up textwidth.
185 int textwidth_backup = m.base.textwidth;
187 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
188 InsetCollapsable::metrics(m, dim);
190 m.base.textwidth = textwidth_backup;
194 bool InsetBox::forcePlainLayout(idx_type) const
196 return !params_.inner_box && params_.type != "Framed";
200 bool InsetBox::showInsetDialog(BufferView * bv) const
202 bv->showDialog("box", params2string(params_),
203 const_cast<InsetBox *>(this));
208 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
210 switch (cmd.action) {
212 case LFUN_INSET_MODIFY: {
213 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
214 if (cmd.getArg(0) == "changetype")
215 params_.type = cmd.getArg(1);
217 string2params(to_utf8(cmd.argument()), params_);
218 setLayout(cur.buffer().params());
222 case LFUN_INSET_DIALOG_UPDATE:
223 cur.bv().updateDialog("box", params2string(params_));
227 InsetCollapsable::doDispatch(cur, cmd);
233 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
234 FuncStatus & flag) const
236 switch (cmd.action) {
238 case LFUN_INSET_MODIFY:
239 if (cmd.getArg(0) == "changetype")
240 flag.setOnOff(cmd.getArg(1) == params_.type);
241 flag.setEnabled(true);
244 case LFUN_INSET_DIALOG_UPDATE:
245 flag.setEnabled(true);
248 case LFUN_BREAK_PARAGRAPH:
249 if (params_.inner_box || params_.type == "Framed")
250 return InsetCollapsable::getStatus(cur, cmd, flag);
251 flag.setEnabled(false);
255 return InsetCollapsable::getStatus(cur, cmd, flag);
260 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
262 BoxType btype = boxtranslator().find(params_.type);
264 string width_string = params_.width.asLatexString();
265 bool stdwidth = false;
266 if (params_.inner_box &&
267 (width_string.find("1.0\\columnwidth") != string::npos
268 || width_string.find("1.0\\textwidth") != string::npos)) {
276 width_string += " - 2\\fboxsep - 2\\fboxrule";
279 width_string += " - 2\\fboxsep - 0.8pt";
282 width_string += " - 2\\fboxsep - 1.6pt";
285 // Shadow falls outside right margin... opinions?
286 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
289 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
296 // Adapt to column/text width correctly also if paragraphs indented:
304 os << "\\begin{framed}%\n";
309 if (!params_.inner_box) {
311 // Special widths, see usrguide §3.5
313 if (params_.special != "none") {
314 os << "[" << params_.width.value()
315 << '\\' << from_utf8(params_.special)
318 os << '[' << from_ascii(width_string)
320 if (params_.hor_pos != 'c')
321 os << "[" << params_.hor_pos << "]";
333 os << "\\shadowbox{";
339 os << "\\doublebox{";
343 if (params_.inner_box) {
344 if (params_.use_parbox)
347 os << "\\begin{minipage}";
349 os << "[" << params_.pos << "]";
350 if (params_.height_special == "none") {
352 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
355 // set no optional argument when the value is the default "1\height"
356 // (special units like \height are handled as "in")
357 // but when the user has chosen a non-default inner_pos, the height
358 // must be given: \minipage[pos][height][inner-pos]{width}
359 if ((params_.height != Length("1in") ||
360 params_.height_special != "totalheight") ||
361 params_.inner_pos != params_.pos) {
363 os << "[" << params_.height.value()
364 << "\\" << from_utf8(params_.height_special) << "]";
367 if (params_.inner_pos != params_.pos)
368 os << "[" << params_.inner_pos << "]";
371 os << '{' << from_ascii(width_string) << '}';
373 if (params_.use_parbox)
378 if (btype == Shaded) {
379 os << "\\begin{shaded}%\n";
383 i += InsetText::latex(os, runparams);
386 os << "\\end{shaded}";
388 if (params_.inner_box) {
389 if (params_.use_parbox)
392 os << "%\n\\end{minipage}";
399 os << "\\end{framed}";
402 if (!params_.inner_box)
403 os << "}"; // for makebox
423 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
425 BoxType const btype = boxtranslator().find(params_.type);
449 InsetText::plaintext(os, runparams);
480 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
484 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
486 return InsetText::docbook(os, runparams);
490 void InsetBox::validate(LaTeXFeatures & features) const
492 BoxType btype = boxtranslator().find(params_.type);
497 features.require("framed");
500 features.require("calc");
506 features.require("calc");
507 features.require("fancybox");
510 features.require("color");
511 features.require("framed");
514 InsetText::validate(features);
518 docstring InsetBox::contextMenu(BufferView const &, int, int) const
520 return from_ascii("context-box");
524 string InsetBox::params2string(InsetBoxParams const & params)
527 data << "box" << ' ';
533 void InsetBox::string2params(string const & in, InsetBoxParams & params)
535 params = InsetBoxParams(string());
539 istringstream data(in);
545 if (!lex || name != "box") {
546 LYXERR0("InsetBox::string2params(" << in << ")\n"
547 "Expected arg 1 to be \"box\"\n");
551 // This is part of the inset proper that is usually swallowed
552 // by Text::readInset
555 if (!lex || id != "Box") {
556 LYXERR0("InsetBox::string2params(" << in << ")\n"
557 "Expected arg 2 to be \"Box\"\n");
564 /////////////////////////////////////////////////////////////////////////
568 /////////////////////////////////////////////////////////////////////////
570 InsetBoxParams::InsetBoxParams(string const & label)
574 width(Length("100col%")),
579 height(Length("1in")),
580 height_special("totalheight") // default is 1\\totalheight
584 void InsetBoxParams::write(ostream & os) const
586 os << "Box " << type << "\n";
587 os << "position \"" << pos << "\"\n";
588 os << "hor_pos \"" << hor_pos << "\"\n";
589 os << "has_inner_box " << inner_box << "\n";
590 os << "inner_pos \"" << inner_pos << "\"\n";
591 os << "use_parbox " << use_parbox << "\n";
592 os << "width \"" << width.asString() << "\"\n";
593 os << "special \"" << special << "\"\n";
594 os << "height \"" << height.asString() << "\"\n";
595 os << "height_special \"" << height_special << "\"\n";
599 void InsetBoxParams::read(Lexer & lex)
601 lex.setContext("InsetBoxParams::read");
603 lex >> "position" >> pos;
604 lex >> "hor_pos" >> hor_pos;
605 lex >> "has_inner_box" >> inner_box;
606 if (type == "Framed")
608 lex >> "inner_pos" >> inner_pos;
609 lex >> "use_parbox" >> use_parbox;
610 lex >> "width" >> width;
611 lex >> "special" >> special;
612 lex >> "height" >> height;
613 lex >> "height_special" >> height_special;