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 "output_xhtml.h"
28 #include "TextClass.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/Translator.h"
36 #include "frontends/Application.h"
41 using namespace lyx::support;
47 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
48 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
50 BoxTranslator initBoxtranslator()
52 BoxTranslator translator("Boxed", InsetBox::Boxed);
53 translator.addPair("Frameless", InsetBox::Frameless);
54 translator.addPair("Framed", InsetBox::Framed);
55 translator.addPair("ovalbox", InsetBox::ovalbox);
56 translator.addPair("Ovalbox", InsetBox::Ovalbox);
57 translator.addPair("Shadowbox", InsetBox::Shadowbox);
58 translator.addPair("Shaded", InsetBox::Shaded);
59 translator.addPair("Doublebox",InsetBox::Doublebox);
64 BoxTranslatorLoc initBoxtranslatorLoc()
66 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
67 translator.addPair(_("frameless"), InsetBox::Frameless);
68 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
69 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
70 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
71 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
72 translator.addPair(_("shaded background"), InsetBox::Shaded);
73 translator.addPair(_("double frame"), InsetBox::Doublebox);
78 BoxTranslator const & boxtranslator()
80 static BoxTranslator translator = initBoxtranslator();
85 BoxTranslatorLoc const & boxtranslator_loc()
87 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
94 /////////////////////////////////////////////////////////////////////////
98 /////////////////////////////////////////////////////////////////////////
100 InsetBox::InsetBox(Buffer * buffer, string const & label)
101 : InsetCollapsable(buffer), params_(label)
105 docstring InsetBox::layoutName() const
108 return from_ascii("Box:" + params_.type);
112 void InsetBox::write(ostream & os) const
115 InsetCollapsable::write(os);
119 void InsetBox::read(Lexer & lex)
122 InsetCollapsable::read(lex);
126 void InsetBox::setButtonLabel()
128 BoxType const btype = boxtranslator().find(params_.type);
130 docstring const type = _("Box");
133 if (params_.inner_box) {
134 if (params_.use_parbox)
136 else if (params_.use_makebox)
137 inner = _("Makebox");
139 inner = _("Minipage");
143 if (btype != Frameless)
144 frame = boxtranslator_loc().find(btype);
147 if (inner.empty() && frame.empty())
149 else if (inner.empty())
150 label = bformat(_("%1$s (%2$s)"),
152 else if (frame.empty())
153 label = bformat(_("%1$s (%2$s)"),
156 label = bformat(_("%1$s (%2$s, %3$s)"),
162 bool InsetBox::hasFixedWidth() const
164 return params_.inner_box || params_.special != "width";
168 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
170 // back up textwidth.
171 int textwidth_backup = m.base.textwidth;
173 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
174 InsetCollapsable::metrics(m, dim);
176 m.base.textwidth = textwidth_backup;
180 bool InsetBox::forcePlainLayout(idx_type) const
182 return (!params_.inner_box || params_.use_makebox)
183 && params_.type != "Shaded" && params_.type != "Framed";
187 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
189 switch (cmd.action()) {
191 case LFUN_INSET_MODIFY: {
192 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
193 cur.recordUndoInset(ATOMIC_UNDO, this);
194 if (cmd.getArg(0) == "changetype") {
195 params_.type = cmd.getArg(1);
197 string2params(to_utf8(cmd.argument()), params_);
203 InsetCollapsable::doDispatch(cur, cmd);
209 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
210 FuncStatus & flag) const
212 switch (cmd.action()) {
214 case LFUN_INSET_MODIFY: {
215 string const first_arg = cmd.getArg(0);
216 if (first_arg == "changetype") {
217 string const type = cmd.getArg(1);
218 flag.setOnOff(type == params_.type);
219 flag.setEnabled(!params_.inner_box || type != "Framed");
222 if (first_arg == "box") {
223 flag.setEnabled(true);
226 return InsetCollapsable::getStatus(cur, cmd, flag);
229 case LFUN_INSET_DIALOG_UPDATE:
230 flag.setEnabled(true);
233 case LFUN_BREAK_PARAGRAPH:
234 if ((params_.inner_box && !params_.use_makebox)
235 || params_.type == "Shaded" || params_.type == "Framed")
236 return InsetCollapsable::getStatus(cur, cmd, flag);
237 flag.setEnabled(false);
241 return InsetCollapsable::getStatus(cur, cmd, flag);
246 void InsetBox::latex(otexstream & 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";
281 if (runparams.lastid != -1)
282 os.texrow().start(runparams.lastid, runparams.lastpos);
284 // Adapt to column/text width correctly also if paragraphs indented:
292 os << "\\begin{framed}%\n";
296 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 << "]";
318 os << "\\shadowbox{";
321 // must be set later because e.g. the width settings only work when
322 // it is inside a minipage or parbox
325 os << "\\doublebox{";
329 if (params_.inner_box) {
330 if (params_.use_parbox)
332 else if (params_.use_makebox) {
335 // output the width and horizontal position
336 if (params_.special != "none") {
337 os << "[" << params_.width.value()
338 << '\\' << from_utf8(params_.special)
341 os << '[' << from_ascii(width_string)
343 if (params_.hor_pos != 'c')
344 os << "[" << params_.hor_pos << "]";
348 os << "\\begin{minipage}";
350 // output parameters for parbox and minipage
351 if (!params_.use_makebox) {
352 os << "[" << params_.pos << "]";
353 if (params_.height_special == "none") {
355 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
358 // set no optional argument when the value is the default "1\height"
359 // (special units like \height are handled as "in")
360 // but when the user has chosen a non-default inner_pos, the height
361 // must be given: \minipage[pos][height][inner-pos]{width}
362 if ((params_.height != Length("1in") ||
363 params_.height_special != "totalheight") ||
364 params_.inner_pos != params_.pos) {
366 os << "[" << params_.height.value()
367 << "\\" << from_utf8(params_.height_special) << "]";
370 if (params_.inner_pos != params_.pos)
371 os << "[" << params_.inner_pos << "]";
373 os << '{' << from_ascii(width_string) << '}';
374 if (params_.use_parbox)
379 } // end if inner_box
381 if (btype == Shaded) {
382 os << "\\begin{shaded}%\n";
385 InsetText::latex(os, runparams);
388 os << "\\end{shaded}";
390 if (params_.inner_box) {
391 if (params_.use_parbox || params_.use_makebox)
394 os << "%\n\\end{minipage}";
401 os << "\\end{framed}";
419 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
421 BoxType const btype = boxtranslator().find(params_.type);
445 InsetText::plaintext(os, runparams);
476 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
480 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
482 return InsetText::docbook(os, runparams);
486 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
488 // construct attributes
489 string attrs = "class='" + params_.type + "'";
491 if (!params_.width.empty()) {
492 string w = params_.width.asHTMLString();
494 style += ("width: " + params_.width.asHTMLString() + "; ");
496 // The special heights don't really mean anything for us.
497 if (!params_.height.empty() && params_.height_special == "none")
498 style += ("height: " + params_.height.asHTMLString() + "; ");
500 attrs += " style='" + style + "'";
502 xs << html::StartTag("div", attrs);
503 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
504 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
505 xs << html::EndTag("div");
511 void InsetBox::validate(LaTeXFeatures & features) const
513 BoxType btype = boxtranslator().find(params_.type);
518 features.require("framed");
521 features.require("calc");
527 features.require("calc");
528 features.require("fancybox");
531 features.require("color");
532 features.require("framed");
535 InsetCollapsable::validate(features);
539 string InsetBox::contextMenuName() const
541 return "context-box";
545 string InsetBox::params2string(InsetBoxParams const & params)
548 data << "box" << ' ';
554 void InsetBox::string2params(string const & in, InsetBoxParams & params)
559 istringstream data(in);
565 if (!lex || name != "box") {
566 LYXERR0("InsetBox::string2params(" << in << ")\n"
567 "Expected arg 1 to be \"box\"\n");
571 // This is part of the inset proper that is usually swallowed
572 // by Text::readInset
575 if (!lex || id != "Box") {
576 LYXERR0("InsetBox::string2params(" << in << ")\n"
577 "Expected arg 2 to be \"Box\"\n");
580 params = InsetBoxParams(string());
585 /////////////////////////////////////////////////////////////////////////
589 /////////////////////////////////////////////////////////////////////////
591 InsetBoxParams::InsetBoxParams(string const & label)
596 width(Length("100col%")),
601 height(Length("1in")),
602 height_special("totalheight") // default is 1\\totalheight
606 void InsetBoxParams::write(ostream & os) const
608 os << "Box " << type << "\n";
609 os << "position \"" << pos << "\"\n";
610 os << "hor_pos \"" << hor_pos << "\"\n";
611 os << "has_inner_box " << inner_box << "\n";
612 os << "inner_pos \"" << inner_pos << "\"\n";
613 os << "use_parbox " << use_parbox << "\n";
614 os << "use_makebox " << use_makebox << "\n";
615 os << "width \"" << width.asString() << "\"\n";
616 os << "special \"" << special << "\"\n";
617 os << "height \"" << height.asString() << "\"\n";
618 os << "height_special \"" << height_special << "\"\n";
622 void InsetBoxParams::read(Lexer & lex)
624 lex.setContext("InsetBoxParams::read");
626 lex >> "position" >> pos;
627 lex >> "hor_pos" >> hor_pos;
628 lex >> "has_inner_box" >> inner_box;
629 if (type == "Framed")
631 lex >> "inner_pos" >> inner_pos;
632 lex >> "use_parbox" >> use_parbox;
633 lex >> "use_makebox" >> use_makebox;
634 lex >> "width" >> width;
635 lex >> "special" >> special;
636 lex >> "height" >> height;
637 lex >> "height_special" >> height_special;