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/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33 #include "support/Translator.h"
35 #include "frontends/Application.h"
40 using namespace lyx::support;
46 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
47 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
49 BoxTranslator initBoxtranslator()
51 BoxTranslator translator("Boxed", InsetBox::Boxed);
52 translator.addPair("Frameless", InsetBox::Frameless);
53 translator.addPair("Framed", InsetBox::Framed);
54 translator.addPair("ovalbox", InsetBox::ovalbox);
55 translator.addPair("Ovalbox", InsetBox::Ovalbox);
56 translator.addPair("Shadowbox", InsetBox::Shadowbox);
57 translator.addPair("Shaded", InsetBox::Shaded);
58 translator.addPair("Doublebox",InsetBox::Doublebox);
63 BoxTranslatorLoc initBoxtranslatorLoc()
65 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
66 translator.addPair(_("frameless"), InsetBox::Frameless);
67 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
68 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
69 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
70 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
71 translator.addPair(_("shaded background"), InsetBox::Shaded);
72 translator.addPair(_("double frame"), InsetBox::Doublebox);
77 BoxTranslator const & boxtranslator()
79 static BoxTranslator translator = initBoxtranslator();
84 BoxTranslatorLoc const & boxtranslator_loc()
86 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
93 /////////////////////////////////////////////////////////////////////////
97 /////////////////////////////////////////////////////////////////////////
99 InsetBox::InsetBox(Buffer * buffer, string const & label)
100 : InsetCollapsable(buffer), params_(label)
104 InsetBox::~InsetBox()
106 hideDialogs("box", this);
110 docstring InsetBox::name() const
114 if (boxtranslator().find(params_.type) == Shaded)
116 return from_ascii(name);
120 void InsetBox::write(ostream & os) const
123 InsetCollapsable::write(os);
127 void InsetBox::read(Lexer & lex)
130 InsetCollapsable::read(lex);
134 void InsetBox::setButtonLabel()
136 BoxType const btype = boxtranslator().find(params_.type);
138 docstring const type = _("Box");
141 if (params_.inner_box) {
142 if (params_.use_parbox)
145 inner = _("Minipage");
149 if (btype != Frameless)
150 frame = boxtranslator_loc().find(btype);
153 if (inner.empty() && frame.empty())
155 else if (inner.empty())
156 label = bformat(_("%1$s (%2$s)"),
158 else if (frame.empty())
159 label = bformat(_("%1$s (%2$s)"),
162 label = bformat(_("%1$s (%2$s, %3$s)"),
168 bool InsetBox::hasFixedWidth() const
170 return params_.inner_box || params_.special != "width";
174 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
176 // back up textwidth.
177 int textwidth_backup = m.base.textwidth;
179 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
180 InsetCollapsable::metrics(m, dim);
182 m.base.textwidth = textwidth_backup;
186 bool InsetBox::forcePlainLayout(idx_type) const
188 return !params_.inner_box && params_.type != "Framed";
192 bool InsetBox::showInsetDialog(BufferView * bv) const
194 bv->showDialog("box", params2string(params_),
195 const_cast<InsetBox *>(this));
200 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
202 switch (cmd.action) {
204 case LFUN_INSET_MODIFY: {
205 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
206 if (cmd.getArg(0) == "changetype")
207 params_.type = cmd.getArg(1);
209 string2params(to_utf8(cmd.argument()), params_);
213 case LFUN_INSET_DIALOG_UPDATE:
214 cur.bv().updateDialog("box", params2string(params_));
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 if (cmd.getArg(0) == "changetype")
231 flag.setOnOff(cmd.getArg(1) == params_.type);
232 flag.setEnabled(true);
235 case LFUN_INSET_DIALOG_UPDATE:
236 flag.setEnabled(true);
239 case LFUN_BREAK_PARAGRAPH:
240 if (params_.inner_box || params_.type == "Framed")
241 return InsetCollapsable::getStatus(cur, cmd, flag);
242 flag.setEnabled(false);
246 return InsetCollapsable::getStatus(cur, cmd, flag);
251 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
253 BoxType btype = boxtranslator().find(params_.type);
255 string width_string = params_.width.asLatexString();
256 bool stdwidth = false;
257 if (params_.inner_box &&
258 (width_string.find("1.0\\columnwidth") != string::npos
259 || width_string.find("1.0\\textwidth") != string::npos)) {
267 width_string += " - 2\\fboxsep - 2\\fboxrule";
270 width_string += " - 2\\fboxsep - 0.8pt";
273 width_string += " - 2\\fboxsep - 1.6pt";
276 // Shadow falls outside right margin... opinions?
277 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
280 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
287 // Adapt to column/text width correctly also if paragraphs indented:
295 os << "\\begin{framed}%\n";
300 if (!params_.inner_box) {
302 // Special widths, see usrguide §3.5
304 if (params_.special != "none") {
305 os << "[" << params_.width.value()
306 << '\\' << from_utf8(params_.special)
309 os << '[' << from_ascii(width_string)
311 if (params_.hor_pos != 'c')
312 os << "[" << params_.hor_pos << "]";
324 os << "\\shadowbox{";
330 os << "\\doublebox{";
334 if (params_.inner_box) {
335 if (params_.use_parbox)
338 os << "\\begin{minipage}";
340 os << "[" << params_.pos << "]";
341 if (params_.height_special == "none") {
343 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
346 // set no optional argument when the value is the default "1\height"
347 // (special units like \height are handled as "in")
348 // but when the user has chosen a non-default inner_pos, the height
349 // must be given: \minipage[pos][height][inner-pos]{width}
350 if ((params_.height != Length("1in") ||
351 params_.height_special != "totalheight") ||
352 params_.inner_pos != params_.pos) {
354 os << "[" << params_.height.value()
355 << "\\" << from_utf8(params_.height_special) << "]";
358 if (params_.inner_pos != params_.pos)
359 os << "[" << params_.inner_pos << "]";
362 os << '{' << from_ascii(width_string) << '}';
364 if (params_.use_parbox)
369 if (btype == Shaded) {
370 os << "\\begin{shaded}%\n";
374 i += InsetText::latex(os, runparams);
377 os << "\\end{shaded}";
379 if (params_.inner_box) {
380 if (params_.use_parbox)
383 os << "%\n\\end{minipage}";
390 os << "\\end{framed}";
393 if (!params_.inner_box)
394 os << "}"; // for makebox
414 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
416 BoxType const btype = boxtranslator().find(params_.type);
440 InsetText::plaintext(os, runparams);
471 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
475 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
477 return InsetText::docbook(os, runparams);
481 docstring InsetBox::xhtml(odocstream &, OutputParams const & runparams) const
484 if (!params_.width.empty())
485 style += ("width: " + params_.width.asHTMLString() + ";");
486 if (!params_.height.empty())
487 style += ("height: " + params_.height.asHTMLString() + ";");
489 docstring retval = from_ascii("<div class='" + params_.type + "'");
491 retval += from_ascii(" style='" + style + "'");
494 docstring defer = InsetText::xhtml(os, runparams);
496 retval += "</div>\n";
497 retval += defer + "\n";
502 void InsetBox::validate(LaTeXFeatures & features) const
504 BoxType btype = boxtranslator().find(params_.type);
509 features.require("framed");
512 features.require("calc");
518 features.require("calc");
519 features.require("fancybox");
522 features.require("color");
523 features.require("framed");
526 InsetCollapsable::validate(features);
530 docstring InsetBox::contextMenu(BufferView const &, int, int) const
532 return from_ascii("context-box");
536 string InsetBox::params2string(InsetBoxParams const & params)
539 data << "box" << ' ';
545 void InsetBox::string2params(string const & in, InsetBoxParams & params)
547 params = InsetBoxParams(string());
551 istringstream data(in);
557 if (!lex || name != "box") {
558 LYXERR0("InsetBox::string2params(" << in << ")\n"
559 "Expected arg 1 to be \"box\"\n");
563 // This is part of the inset proper that is usually swallowed
564 // by Text::readInset
567 if (!lex || id != "Box") {
568 LYXERR0("InsetBox::string2params(" << in << ")\n"
569 "Expected arg 2 to be \"Box\"\n");
576 /////////////////////////////////////////////////////////////////////////
580 /////////////////////////////////////////////////////////////////////////
582 InsetBoxParams::InsetBoxParams(string const & label)
586 width(Length("100col%")),
591 height(Length("1in")),
592 height_special("totalheight") // default is 1\\totalheight
596 void InsetBoxParams::write(ostream & os) const
598 os << "Box " << type << "\n";
599 os << "position \"" << pos << "\"\n";
600 os << "hor_pos \"" << hor_pos << "\"\n";
601 os << "has_inner_box " << inner_box << "\n";
602 os << "inner_pos \"" << inner_pos << "\"\n";
603 os << "use_parbox " << use_parbox << "\n";
604 os << "width \"" << width.asString() << "\"\n";
605 os << "special \"" << special << "\"\n";
606 os << "height \"" << height.asString() << "\"\n";
607 os << "height_special \"" << height_special << "\"\n";
611 void InsetBoxParams::read(Lexer & lex)
613 lex.setContext("InsetBoxParams::read");
615 lex >> "position" >> pos;
616 lex >> "hor_pos" >> hor_pos;
617 lex >> "has_inner_box" >> inner_box;
618 if (type == "Framed")
620 lex >> "inner_pos" >> inner_pos;
621 lex >> "use_parbox" >> use_parbox;
622 lex >> "width" >> width;
623 lex >> "special" >> special;
624 lex >> "height" >> height;
625 lex >> "height_special" >> height_special;