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::name() const
109 if (boxtranslator().find(params_.type) == Shaded)
111 return from_ascii(name);
115 void InsetBox::write(ostream & os) const
118 InsetCollapsable::write(os);
122 void InsetBox::read(Lexer & lex)
125 InsetCollapsable::read(lex);
129 void InsetBox::setButtonLabel()
131 BoxType const btype = boxtranslator().find(params_.type);
133 docstring const type = _("Box");
136 if (params_.inner_box) {
137 if (params_.use_parbox)
139 else if (params_.use_makebox)
140 inner = _("Makebox");
142 inner = _("Minipage");
146 if (btype != Frameless)
147 frame = boxtranslator_loc().find(btype);
150 if (inner.empty() && frame.empty())
152 else if (inner.empty())
153 label = bformat(_("%1$s (%2$s)"),
155 else if (frame.empty())
156 label = bformat(_("%1$s (%2$s)"),
159 label = bformat(_("%1$s (%2$s, %3$s)"),
165 bool InsetBox::hasFixedWidth() const
167 return params_.inner_box || params_.special != "width";
171 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
173 // back up textwidth.
174 int textwidth_backup = m.base.textwidth;
176 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
177 InsetCollapsable::metrics(m, dim);
179 m.base.textwidth = textwidth_backup;
183 bool InsetBox::forcePlainLayout(idx_type) const
185 return (!params_.inner_box || params_.use_makebox)
186 && params_.type != "Shaded" && params_.type != "Framed";
190 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
192 switch (cmd.action()) {
194 case LFUN_INSET_MODIFY: {
195 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
196 cur.recordUndoInset(ATOMIC_UNDO, this);
197 if (cmd.getArg(0) == "changetype") {
198 params_.type = cmd.getArg(1);
200 string2params(to_utf8(cmd.argument()), params_);
206 InsetCollapsable::doDispatch(cur, cmd);
212 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
213 FuncStatus & flag) const
215 switch (cmd.action()) {
217 case LFUN_INSET_MODIFY:
218 if (cmd.getArg(0) == "changetype")
219 flag.setOnOff(cmd.getArg(1) == params_.type);
220 flag.setEnabled(true);
223 case LFUN_INSET_DIALOG_UPDATE:
224 flag.setEnabled(true);
227 case LFUN_BREAK_PARAGRAPH:
228 if ((params_.inner_box && !params_.use_makebox)
229 || params_.type == "Shaded" || params_.type == "Framed")
230 return InsetCollapsable::getStatus(cur, cmd, flag);
231 flag.setEnabled(false);
235 return InsetCollapsable::getStatus(cur, cmd, flag);
240 int InsetBox::latex(otexstream & os, OutputParams const & runparams) const
242 BoxType btype = boxtranslator().find(params_.type);
244 string width_string = params_.width.asLatexString();
245 bool stdwidth = false;
246 if (params_.inner_box &&
247 (width_string.find("1.0\\columnwidth") != string::npos
248 || width_string.find("1.0\\textwidth") != string::npos)) {
256 width_string += " - 2\\fboxsep - 2\\fboxrule";
259 width_string += " - 2\\fboxsep - 0.8pt";
262 width_string += " - 2\\fboxsep - 1.6pt";
265 // Shadow falls outside right margin... opinions?
266 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
269 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
276 // Adapt to column/text width correctly also if paragraphs indented:
284 os << "\\begin{framed}%\n";
289 if (!params_.inner_box) {
290 // Special widths, see usrguide §3.5
292 if (params_.special != "none") {
293 os << "[" << params_.width.value()
294 << '\\' << from_utf8(params_.special)
297 os << '[' << from_ascii(width_string)
299 if (params_.hor_pos != 'c')
300 os << "[" << params_.hor_pos << "]";
311 os << "\\shadowbox{";
317 os << "\\doublebox{";
321 if (params_.inner_box) {
322 if (params_.use_parbox)
324 else if (params_.use_makebox) {
327 // output the width and horizontal position
328 if (params_.special != "none") {
329 os << "[" << params_.width.value()
330 << '\\' << from_utf8(params_.special)
333 os << '[' << from_ascii(width_string)
335 if (params_.hor_pos != 'c')
336 os << "[" << params_.hor_pos << "]";
340 os << "\\begin{minipage}";
342 // output parameters for parbox and minipage
343 if (!params_.use_makebox) {
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 << "]";
365 os << '{' << from_ascii(width_string) << '}';
366 if (params_.use_parbox)
372 } // end if inner_box
374 if (btype == Shaded) {
375 os << "\\begin{shaded}%\n";
379 i += InsetText::latex(os, runparams);
382 os << "\\end{shaded}";
384 if (params_.inner_box) {
385 if (params_.use_parbox || params_.use_makebox)
388 os << "%\n\\end{minipage}";
395 os << "\\end{framed}";
417 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
419 BoxType const btype = boxtranslator().find(params_.type);
443 InsetText::plaintext(os, runparams);
474 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
478 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
480 return InsetText::docbook(os, runparams);
484 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
486 // construct attributes
487 string attrs = "class='" + params_.type + "'";
489 if (!params_.width.empty())
490 style += ("width: " + params_.width.asHTMLString() + "; ");
491 // The special heights don't really mean anything for us.
492 if (!params_.height.empty() && params_.height_special == "none")
493 style += ("height: " + params_.height.asHTMLString() + "; ");
495 attrs += " style='" + style + "'";
497 xs << html::StartTag("div", attrs);
498 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
499 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
500 xs << html::EndTag("div");
506 void InsetBox::validate(LaTeXFeatures & features) const
508 BoxType btype = boxtranslator().find(params_.type);
513 features.require("framed");
516 features.require("calc");
522 features.require("calc");
523 features.require("fancybox");
526 features.require("color");
527 features.require("framed");
530 InsetCollapsable::validate(features);
534 docstring InsetBox::contextMenuName() const
536 return from_ascii("context-box");
540 string InsetBox::params2string(InsetBoxParams const & params)
543 data << "box" << ' ';
549 void InsetBox::string2params(string const & in, InsetBoxParams & params)
551 params = InsetBoxParams(string());
555 istringstream data(in);
561 if (!lex || name != "box") {
562 LYXERR0("InsetBox::string2params(" << in << ")\n"
563 "Expected arg 1 to be \"box\"\n");
567 // This is part of the inset proper that is usually swallowed
568 // by Text::readInset
571 if (!lex || id != "Box") {
572 LYXERR0("InsetBox::string2params(" << in << ")\n"
573 "Expected arg 2 to be \"Box\"\n");
580 /////////////////////////////////////////////////////////////////////////
584 /////////////////////////////////////////////////////////////////////////
586 InsetBoxParams::InsetBoxParams(string const & label)
591 width(Length("100col%")),
596 height(Length("1in")),
597 height_special("totalheight") // default is 1\\totalheight
601 void InsetBoxParams::write(ostream & os) const
603 os << "Box " << type << "\n";
604 os << "position \"" << pos << "\"\n";
605 os << "hor_pos \"" << hor_pos << "\"\n";
606 os << "has_inner_box " << inner_box << "\n";
607 os << "inner_pos \"" << inner_pos << "\"\n";
608 os << "use_parbox " << use_parbox << "\n";
609 os << "use_makebox " << use_makebox << "\n";
610 os << "width \"" << width.asString() << "\"\n";
611 os << "special \"" << special << "\"\n";
612 os << "height \"" << height.asString() << "\"\n";
613 os << "height_special \"" << height_special << "\"\n";
617 void InsetBoxParams::read(Lexer & lex)
619 lex.setContext("InsetBoxParams::read");
621 lex >> "position" >> pos;
622 lex >> "hor_pos" >> hor_pos;
623 lex >> "has_inner_box" >> inner_box;
624 if (type == "Framed")
626 lex >> "inner_pos" >> inner_pos;
627 lex >> "use_parbox" >> use_parbox;
628 lex >> "use_makebox" >> use_makebox;
629 lex >> "width" >> width;
630 lex >> "special" >> special;
631 lex >> "height" >> height;
632 lex >> "height_special" >> height_special;