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 #include "insetminipage.h"
18 #include "BufferView.h"
19 #include "frontends/LyXView.h"
20 #include "frontends/Dialogs.h"
22 #include "insets/insettext.h"
23 #include "support/LOstream.h"
24 #include "support/lstrings.h"
33 // Some information about Minipages in LaTeX:
34 // A minipage is a complete miniversion of a page and can contain
35 // its own footnotes, paragraphs, and array, tabular, and multicols
36 // environments. However it cannot contain floats or \marginpar's,
37 // but it can appear inside floats.
39 // The minipage environment is defined like this:
41 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
44 // pos [opt] = is the vertical placement of the box with respect
45 // to the text baseline, [c], [t] and [b].
46 // height [opt] = the height of the box
47 // inner-pos [opt] = the position of the text within the box.
48 // It can be t, c, b or s, if unspecified the value
50 // width = the width of the box
52 // In LyX we should try to support all these parameters, settable in a
54 // In this pop-up diallog it should also be possible to set all margin
55 // values that is usable in the minipage.
56 // With regard to different formats (like DocBook) I guess a minipage
57 // can be used there also. Perhaps not in the latex way, but we do not
58 // have to output "" for minipages.
61 InsetMinipage::InsetMinipage(BufferParams const & bp)
62 : InsetCollapsable(bp), pos_(center),
63 inner_pos_(inner_center), width_(100, LyXLength::PCW)
65 setLabel(_("minipage"));
66 LyXFont font(LyXFont::ALL_SANE);
69 font.setColor(LColor::collapsable);
72 setAutoCollapse(false);
77 #warning Remove this color definitions before 1.2.0 final!
79 // just for experimentation :)
80 setBackgroundColor(LColor::green);
83 inset.setFrameColor(0, LColor::blue);
84 setInsetName("Minipage");
88 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
89 : InsetCollapsable(in, same_id),
90 pos_(in.pos_), inner_pos_(in.inner_pos_),
91 height_(in.height_), width_(in.width_)
95 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
97 return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
101 InsetMinipage::~InsetMinipage()
107 void InsetMinipage::write(Buffer const * buf, ostream & os) const
109 os << getInsetName() << '\n'
110 << "position " << pos_ << '\n'
111 << "inner_position " << inner_pos_ << '\n'
112 << "height \"" << height_.asString() << "\"\n"
113 << "width \"" << width_.asString() << "\"\n";
114 InsetCollapsable::write(buf, os);
118 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
122 string const token = lex.getString();
123 if (token == "position") {
125 pos_ = static_cast<Position>(lex.getInteger());
127 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
129 // take countermeasures
130 lex.pushToken(token);
135 string const token = lex.getString();
136 if (token == "inner_position") {
138 inner_pos_ = static_cast<InnerPosition>(lex.getInteger());
140 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
142 // take countermeasures
143 lex.pushToken(token);
148 string const token = lex.getString();
149 if (token == "height") {
151 height_ = LyXLength(lex.getString());
153 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
155 // take countermeasures
156 lex.pushToken(token);
161 string const token = lex.getString();
162 if (token == "width") {
164 width_ = LyXLength(lex.getString());
166 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
168 // take countermeasures
169 lex.pushToken(token);
172 InsetCollapsable::read(buf, lex);
176 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
179 return ascent_collapsed();
181 // Take placement into account.
185 i = InsetCollapsable::ascent(bv, font);
188 i = (InsetCollapsable::ascent(bv, font)
189 + InsetCollapsable::descent(bv, font)) / 2;
192 i = InsetCollapsable::descent(bv, font);
200 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
203 return descent_collapsed();
205 // Take placement into account.
209 i = InsetCollapsable::descent(bv, font);
212 i = (InsetCollapsable::ascent(bv, font)
213 + InsetCollapsable::descent(bv, font)) / 2;
216 i = InsetCollapsable::ascent(bv, font);
224 string const InsetMinipage::editMessage() const
226 return _("Opened Minipage Inset");
230 int InsetMinipage::latex(Buffer const * buf,
231 ostream & os, bool fragile, bool fp) const
245 os << "\\begin{minipage}[" << s_pos << "]{"
246 << width_.asLatexString() << "}%\n";
248 int i = inset.latex(buf, os, fragile, fp);
250 os << "\\end{minipage}%\n";
255 bool InsetMinipage::insetAllowed(Inset::Code code) const
257 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
260 return InsetCollapsable::insetAllowed(code);
264 InsetMinipage::Position InsetMinipage::pos() const
270 void InsetMinipage::pos(InsetMinipage::Position p)
279 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
285 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
291 LyXLength const & InsetMinipage::pageHeight() const
297 void InsetMinipage::pageHeight(LyXLength const & ll)
306 LyXLength const & InsetMinipage::pageWidth() const
312 void InsetMinipage::pageWidth(LyXLength const & ll)
321 bool InsetMinipage::showInsetDialog(BufferView * bv) const
323 if (!inset.showInsetDialog(bv))
324 bv->owner()->getDialogs().showMinipage(const_cast<InsetMinipage *>(this));
329 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
333 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
336 if (!width_.zero()) {
337 int ww1 = latexTextWidth(bv);
338 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
339 if (ww2 > 0 && ww2 < ww1) {
344 // this should not happen!
345 return InsetCollapsable::getMaxWidth(bv, inset);
349 int InsetMinipage::latexTextWidth(BufferView * bv) const
351 return width_.inPixels(InsetCollapsable::latexTextWidth(bv));