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"
22 #include "DispatchResult.h"
23 #include "FuncStatus.h"
24 #include "FuncRequest.h"
25 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/Translator.h"
37 #include "frontends/Application.h"
42 using namespace lyx::support;
48 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
49 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
51 BoxTranslator initBoxtranslator()
53 BoxTranslator translator("Boxed", InsetBox::Boxed);
54 translator.addPair("Frameless", InsetBox::Frameless);
55 translator.addPair("Framed", InsetBox::Framed);
56 translator.addPair("ovalbox", InsetBox::ovalbox);
57 translator.addPair("Ovalbox", InsetBox::Ovalbox);
58 translator.addPair("Shadowbox", InsetBox::Shadowbox);
59 translator.addPair("Shaded", InsetBox::Shaded);
60 translator.addPair("Doublebox",InsetBox::Doublebox);
65 BoxTranslatorLoc initBoxtranslatorLoc()
67 BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
68 translator.addPair(_("frameless"), InsetBox::Frameless);
69 translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
70 translator.addPair(_("oval, thin"), InsetBox::ovalbox);
71 translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
72 translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
73 translator.addPair(_("shaded background"), InsetBox::Shaded);
74 translator.addPair(_("double frame"), InsetBox::Doublebox);
79 BoxTranslator const & boxtranslator()
81 static BoxTranslator translator = initBoxtranslator();
86 BoxTranslatorLoc const & boxtranslator_loc()
88 static BoxTranslatorLoc translator = initBoxtranslatorLoc();
95 /////////////////////////////////////////////////////////////////////////
99 /////////////////////////////////////////////////////////////////////////
101 InsetBox::InsetBox(Buffer * buffer, string const & label)
102 : InsetCollapsable(buffer), params_(label)
106 docstring InsetBox::layoutName() const
109 return from_ascii("Box:" + params_.type);
113 void InsetBox::write(ostream & os) const
116 InsetCollapsable::write(os);
120 void InsetBox::read(Lexer & lex)
123 InsetCollapsable::read(lex);
127 void InsetBox::setButtonLabel()
129 BoxType const btype = boxtranslator().find(params_.type);
131 docstring const type = _("Box");
134 if (params_.inner_box) {
135 if (params_.use_parbox)
137 else if (params_.use_makebox)
138 inner = _("Makebox");
140 inner = _("Minipage");
144 if (btype != Frameless)
145 frame = boxtranslator_loc().find(btype);
148 if (inner.empty() && frame.empty())
150 else if (inner.empty())
151 label = bformat(_("%1$s (%2$s)"),
153 else if (frame.empty())
154 label = bformat(_("%1$s (%2$s)"),
157 label = bformat(_("%1$s (%2$s, %3$s)"),
163 bool InsetBox::hasFixedWidth() const
165 return params_.inner_box || params_.special != "width";
169 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
171 // back up textwidth.
172 int textwidth_backup = m.base.textwidth;
174 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
175 InsetCollapsable::metrics(m, dim);
177 m.base.textwidth = textwidth_backup;
181 bool InsetBox::forcePlainLayout(idx_type) const
183 return (!params_.inner_box || params_.use_makebox)
184 && params_.type != "Shaded" && params_.type != "Framed";
188 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
190 if (params_.type != "Shaded")
191 return getLayout().bgcolor();
192 // FIXME: This hardcoded color is a hack!
193 if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
194 return getLayout().bgcolor();
195 ColorCode c = lcolor.getFromLyXName("boxbgcolor");
197 return getLayout().bgcolor();
202 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
204 switch (cmd.action()) {
206 case LFUN_INSET_MODIFY: {
207 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
208 string const first_arg = cmd.getArg(0);
209 bool const change_type = first_arg == "changetype";
210 bool const for_box = first_arg == "box";
211 if (!change_type && !for_box) {
213 // this will not be handled higher up
217 cur.recordUndoInset(ATOMIC_UNDO, this);
219 params_.type = cmd.getArg(1);
221 string2params(to_utf8(cmd.argument()), 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 string const first_arg = cmd.getArg(0);
240 if (first_arg == "changetype") {
241 string const type = cmd.getArg(1);
242 flag.setOnOff(type == params_.type);
243 flag.setEnabled(!params_.inner_box || type != "Framed");
246 if (first_arg == "box") {
247 flag.setEnabled(true);
250 return InsetCollapsable::getStatus(cur, cmd, flag);
253 case LFUN_INSET_DIALOG_UPDATE:
254 flag.setEnabled(true);
257 case LFUN_PARAGRAPH_BREAK:
258 if ((params_.inner_box && !params_.use_makebox)
259 || params_.type == "Shaded" || params_.type == "Framed")
260 return InsetCollapsable::getStatus(cur, cmd, flag);
261 flag.setEnabled(false);
265 return InsetCollapsable::getStatus(cur, cmd, flag);
270 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
272 BoxType btype = boxtranslator().find(params_.type);
274 string width_string = params_.width.asLatexString();
275 bool stdwidth = false;
276 if (params_.inner_box &&
277 (width_string.find("1.0\\columnwidth") != string::npos
278 || width_string.find("1.0\\textwidth") != string::npos)) {
286 width_string += " - 2\\fboxsep - 2\\fboxrule";
289 width_string += " - 2\\fboxsep - 0.8pt";
292 width_string += " - 2\\fboxsep - 1.6pt";
295 // Shadow falls outside right margin... opinions?
296 width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
299 width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
305 if (runparams.lastid != -1)
306 os.texrow().start(runparams.lastid, runparams.lastpos);
308 // Adapt to column/text width correctly also if paragraphs indented:
316 os << "\\begin{framed}%\n";
320 if (!params_.inner_box) {
321 // Special widths, see usrguide §3.5
323 if (params_.special != "none") {
324 os << "[" << params_.width.value()
325 << '\\' << from_utf8(params_.special)
328 os << '[' << from_ascii(width_string)
330 if (params_.hor_pos != 'c')
331 os << "[" << params_.hor_pos << "]";
342 os << "\\shadowbox{";
345 // must be set later because e.g. the width settings only work when
346 // it is inside a minipage or parbox
349 os << "\\doublebox{";
353 if (params_.inner_box) {
354 if (params_.use_parbox)
356 else if (params_.use_makebox) {
359 // output the width and horizontal position
360 if (params_.special != "none") {
361 os << "[" << params_.width.value()
362 << '\\' << from_utf8(params_.special)
365 os << '[' << from_ascii(width_string)
367 if (params_.hor_pos != 'c')
368 os << "[" << params_.hor_pos << "]";
372 os << "\\begin{minipage}";
374 // output parameters for parbox and minipage
375 if (!params_.use_makebox) {
376 os << "[" << params_.pos << "]";
377 if (params_.height_special == "none") {
379 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
382 // set no optional argument when the value is the default "1\height"
383 // (special units like \height are handled as "in")
384 // but when the user has chosen a non-default inner_pos, the height
385 // must be given: \minipage[pos][height][inner-pos]{width}
386 if ((params_.height != Length("1in") ||
387 params_.height_special != "totalheight") ||
388 params_.inner_pos != params_.pos) {
390 os << "[" << params_.height.value()
391 << "\\" << from_utf8(params_.height_special) << "]";
394 if (params_.inner_pos != params_.pos)
395 os << "[" << params_.inner_pos << "]";
397 os << '{' << from_ascii(width_string) << '}';
398 if (params_.use_parbox)
403 } // end if inner_box
405 if (btype == Shaded) {
406 os << "\\begin{shaded}%\n";
409 InsetText::latex(os, runparams);
412 os << "\\end{shaded}";
414 if (params_.inner_box) {
415 if (params_.use_parbox || params_.use_makebox)
418 os << "%\n\\end{minipage}";
425 os << "\\end{framed}";
443 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
445 BoxType const btype = boxtranslator().find(params_.type);
469 InsetText::plaintext(os, runparams);
500 return PLAINTEXT_NEWLINE + len; // len chars on a separate line
504 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
506 return InsetText::docbook(os, runparams);
510 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
512 // construct attributes
513 string attrs = "class='" + params_.type + "'";
515 if (!params_.width.empty()) {
516 string w = params_.width.asHTMLString();
518 style += ("width: " + params_.width.asHTMLString() + "; ");
520 // The special heights don't really mean anything for us.
521 if (!params_.height.empty() && params_.height_special == "none")
522 style += ("height: " + params_.height.asHTMLString() + "; ");
524 attrs += " style='" + style + "'";
526 xs << html::StartTag("div", attrs);
527 XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
528 docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
529 xs << html::EndTag("div");
535 void InsetBox::validate(LaTeXFeatures & features) const
537 BoxType btype = boxtranslator().find(params_.type);
542 features.require("framed");
545 features.require("calc");
551 features.require("calc");
552 features.require("fancybox");
555 features.require("color");
556 features.require("framed");
559 InsetCollapsable::validate(features);
563 string InsetBox::contextMenuName() const
565 return "context-box";
569 string InsetBox::params2string(InsetBoxParams const & params)
572 data << "box" << ' ';
578 void InsetBox::string2params(string const & in, InsetBoxParams & params)
583 istringstream data(in);
589 if (!lex || name != "box") {
590 LYXERR0("InsetBox::string2params(" << in << ")\n"
591 "Expected arg 1 to be \"box\"\n");
595 // This is part of the inset proper that is usually swallowed
596 // by Text::readInset
599 if (!lex || id != "Box") {
600 LYXERR0("InsetBox::string2params(" << in << ")\n"
601 "Expected arg 2 to be \"Box\"\n");
604 params = InsetBoxParams(string());
609 /////////////////////////////////////////////////////////////////////////
613 /////////////////////////////////////////////////////////////////////////
615 InsetBoxParams::InsetBoxParams(string const & label)
620 width(Length("100col%")),
625 height(Length("1in")),
626 height_special("totalheight") // default is 1\\totalheight
630 void InsetBoxParams::write(ostream & os) const
632 os << "Box " << type << "\n";
633 os << "position \"" << pos << "\"\n";
634 os << "hor_pos \"" << hor_pos << "\"\n";
635 os << "has_inner_box " << inner_box << "\n";
636 os << "inner_pos \"" << inner_pos << "\"\n";
637 os << "use_parbox " << use_parbox << "\n";
638 os << "use_makebox " << use_makebox << "\n";
639 os << "width \"" << width.asString() << "\"\n";
640 os << "special \"" << special << "\"\n";
641 os << "height \"" << height.asString() << "\"\n";
642 os << "height_special \"" << height_special << "\"\n";
646 void InsetBoxParams::read(Lexer & lex)
648 lex.setContext("InsetBoxParams::read");
650 lex >> "position" >> pos;
651 lex >> "hor_pos" >> hor_pos;
652 lex >> "has_inner_box" >> inner_box;
653 if (type == "Framed")
655 lex >> "inner_pos" >> inner_pos;
656 lex >> "use_parbox" >> use_parbox;
657 lex >> "use_makebox" >> use_makebox;
658 lex >> "width" >> width;
659 lex >> "special" >> special;
660 lex >> "height" >> height;
661 lex >> "height_special" >> height_special;