2 * \file insetminipage.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
18 #include "insetminipage.h"
21 #include "BufferView.h"
22 #include "frontends/LyXView.h"
23 #include "frontends/Dialogs.h"
25 #include "insets/insettext.h"
26 #include "support/LOstream.h"
27 #include "support/lstrings.h"
36 // Some information about Minipages in LaTeX:
37 // A minipage is a complete miniversion of a page and can contain
38 // its own footnotes, paragraphs, and array, tabular, and multicols
39 // environments. However it cannot contain floats or \marginpar's,
40 // but it can appear inside floats.
42 // The minipage environment is defined like this:
44 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
47 // pos [opt] = is the vertical placement of the box with respect
48 // to the text baseline, [c], [t] and [b].
49 // height [opt] = the height of the box
50 // inner-pos [opt] = the position of the text within the box.
51 // It can be t, c, b or s, if unspecified the value
53 // width = the width of the box
55 // In LyX we should try to support all these parameters, settable in a
57 // In this pop-up diallog it should also be possible to set all margin
58 // values that is usable in the minipage.
59 // With regard to different formats (like DocBook) I guess a minipage
60 // can be used there also. Perhaps not in the latex way, but we do not
61 // have to output "" for minipages.
64 InsetMinipage::InsetMinipage(BufferParams const & bp)
65 : InsetCollapsable(bp), pos_(center),
66 inner_pos_(inner_center), width_(100, LyXLength::PCW)
68 setLabel(_("minipage"));
69 LyXFont font(LyXFont::ALL_SANE);
72 font.setColor(LColor::collapsable);
75 setAutoCollapse(false);
80 #warning Remove this color definitions before 1.2.0 final!
82 // just for experimentation :)
83 setBackgroundColor(LColor::green);
86 inset.setFrameColor(0, LColor::blue);
87 setInsetName("Minipage");
91 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
92 : InsetCollapsable(in, same_id),
93 pos_(in.pos_), inner_pos_(in.inner_pos_),
94 height_(in.height_), width_(in.width_)
98 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
100 return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
104 InsetMinipage::~InsetMinipage()
110 void InsetMinipage::write(Buffer const * buf, ostream & os) const
112 os << getInsetName() << '\n'
113 << "position " << pos_ << '\n'
114 << "inner_position " << inner_pos_ << '\n'
115 << "height \"" << height_.asString() << "\"\n"
116 << "width \"" << width_.asString() << "\"\n";
117 InsetCollapsable::write(buf, os);
121 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
125 string const token = lex.getString();
126 if (token == "position") {
128 pos_ = static_cast<Position>(lex.getInteger());
130 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
132 // take countermeasures
133 lex.pushToken(token);
138 string const token = lex.getString();
139 if (token == "inner_position") {
141 inner_pos_ = static_cast<InnerPosition>(lex.getInteger());
143 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
145 // take countermeasures
146 lex.pushToken(token);
151 string const token = lex.getString();
152 if (token == "height") {
154 height_ = LyXLength(lex.getString());
156 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
158 // take countermeasures
159 lex.pushToken(token);
164 string const token = lex.getString();
165 if (token == "width") {
167 width_ = LyXLength(lex.getString());
169 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
171 // take countermeasures
172 lex.pushToken(token);
175 InsetCollapsable::read(buf, lex);
179 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
182 return ascent_collapsed();
184 // Take placement into account.
188 i = InsetCollapsable::ascent(bv, font);
191 i = (InsetCollapsable::ascent(bv, font)
192 + InsetCollapsable::descent(bv, font)) / 2;
195 i = InsetCollapsable::descent(bv, font);
203 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
206 return descent_collapsed();
208 // Take placement into account.
212 i = InsetCollapsable::descent(bv, font);
215 i = (InsetCollapsable::ascent(bv, font)
216 + InsetCollapsable::descent(bv, font)) / 2;
219 i = InsetCollapsable::ascent(bv, font);
227 string const InsetMinipage::editMessage() const
229 return _("Opened Minipage Inset");
233 int InsetMinipage::latex(Buffer const * buf,
234 ostream & os, bool fragile, bool fp) const
248 os << "\\begin{minipage}[" << s_pos << "]{"
249 << width_.asLatexString() << "}%\n";
251 int i = inset.latex(buf, os, fragile, fp);
253 os << "\\end{minipage}%\n";
258 bool InsetMinipage::insetAllowed(Inset::Code code) const
260 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
263 return InsetCollapsable::insetAllowed(code);
267 InsetMinipage::Position InsetMinipage::pos() const
273 void InsetMinipage::pos(InsetMinipage::Position p)
282 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
288 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
294 LyXLength const & InsetMinipage::pageHeight() const
300 void InsetMinipage::pageHeight(LyXLength const & ll)
309 LyXLength const & InsetMinipage::pageWidth() const
315 void InsetMinipage::pageWidth(LyXLength const & ll)
324 bool InsetMinipage::showInsetDialog(BufferView * bv) const
326 if (!inset.showInsetDialog(bv))
327 bv->owner()->getDialogs().showMinipage(const_cast<InsetMinipage *>(this));
332 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
336 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
339 if (!width_.zero()) {
340 int ww1 = latexTextWidth(bv);
341 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
342 if (ww2 > 0 && ww2 < ww1) {
347 // this should not happen!
348 return InsetCollapsable::getMaxWidth(bv, inset);
352 int InsetMinipage::latexTextWidth(BufferView * bv) const
354 return width_.inPixels(InsetCollapsable::latexTextWidth(bv));