1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
16 #include "insetminipage.h"
19 #include "BufferView.h"
21 #include "frontends/Dialogs.h"
23 #include "insets/insettext.h"
24 #include "support/LOstream.h"
25 #include "support/lstrings.h"
34 // Some information about Minipages in LaTeX:
35 // A minipage is a complete miniversion of a page and can contain
36 // its own footnotes, paragraphs, and array, tabular, and multicols
37 // environments. However it cannot contain floats or \marginpar's,
38 // but it can appear inside floats.
40 // The minipage environment is defined like this:
42 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
45 // pos [opt] = is the vertical placement of the box with respect
46 // to the text baseline, [c], [t] and [b].
47 // height [opt] = the height of the box
48 // inner-pos [opt] = the position of the text within the box.
49 // It can be t, c, b or s, if unspecified the value
51 // width = the width of the box
53 // In LyX we should try to support all these parameters, settable in a
55 // In this pop-up diallog it should also be possible to set all margin
56 // values that is usable in the minipage.
57 // With regard to different formats (like DocBook) I guess a minipage
58 // can be used there also. Perhaps not in the latex way, but we do not
59 // have to output "" for minipages.
62 InsetMinipage::InsetMinipage()
63 : InsetCollapsable(), pos_(center),
64 inner_pos_(inner_center), width_(100, LyXLength::PW)
66 setLabel(_("minipage"));
67 LyXFont font(LyXFont::ALL_SANE);
70 font.setColor(LColor::collapsable);
73 setAutoCollapse(false);
76 #warning Remove this color definitions before 1.2.0 final!
78 // just for experimentation :)
79 setBackgroundColor(LColor::green);
80 inset.setFrameColor(0, LColor::blue);
81 setInsetName("Minipage");
85 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
86 : InsetCollapsable(in, same_id),
87 pos_(in.pos_), inner_pos_(in.inner_pos_),
88 height_(in.height_), width_(in.width_)
92 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
94 return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
98 InsetMinipage::~InsetMinipage()
104 void InsetMinipage::write(Buffer const * buf, ostream & os) const
106 os << getInsetName() << "\n"
107 << "position " << pos_ << "\n"
108 << "inner_position " << inner_pos_ << "\n"
109 << "height \"" << height_.asString() << "\"\n"
110 << "width \"" << width_.asString() << "\"\n";
111 InsetCollapsable::write(buf, os);
115 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
119 string const token = lex.getString();
120 if (token == "position") {
122 pos_ = static_cast<Position>(lex.getInteger());
124 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
126 // take countermeasures
127 lex.pushToken(token);
132 string const token = lex.getString();
133 if (token == "inner_position") {
135 inner_pos_ = static_cast<InnerPosition>(lex.getInteger());
137 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
139 // take countermeasures
140 lex.pushToken(token);
145 string const token = lex.getString();
146 if (token == "height") {
148 height_ = LyXLength(lex.getString());
150 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
152 // take countermeasures
153 lex.pushToken(token);
158 string const token = lex.getString();
159 if (token == "width") {
161 width_ = LyXLength(lex.getString());
163 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
165 // take countermeasures
166 lex.pushToken(token);
169 InsetCollapsable::read(buf, lex);
173 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
176 return ascent_collapsed();
178 // Take placement into account.
182 i = InsetCollapsable::ascent(bv, font);
185 i = (InsetCollapsable::ascent(bv, font)
186 + InsetCollapsable::descent(bv, font)) / 2;
189 i = InsetCollapsable::descent(bv, font);
197 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
200 return descent_collapsed();
202 // Take placement into account.
206 i = InsetCollapsable::descent(bv, font);
209 i = (InsetCollapsable::ascent(bv, font)
210 + InsetCollapsable::descent(bv, font)) / 2;
213 i = InsetCollapsable::ascent(bv, font);
221 string const InsetMinipage::editMessage() const
223 return _("Opened Minipage Inset");
227 int InsetMinipage::latex(Buffer const * buf,
228 ostream & os, bool fragile, bool fp) const
242 os << "\\begin{minipage}[" << s_pos << "]{"
243 << width_.asLatexString() << "}%\n";
245 int i = inset.latex(buf, os, fragile, fp);
247 os << "\\end{minipage}%\n";
252 bool InsetMinipage::insetAllowed(Inset::Code code) const
254 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
257 return InsetCollapsable::insetAllowed(code);
261 InsetMinipage::Position InsetMinipage::pos() const
267 void InsetMinipage::pos(InsetMinipage::Position p)
276 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
282 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
288 LyXLength const & InsetMinipage::pageHeight() const
294 void InsetMinipage::pageHeight(LyXLength const & ll)
303 LyXLength const & InsetMinipage::pageWidth() const
309 void InsetMinipage::pageWidth(LyXLength const & ll)
318 bool InsetMinipage::showInsetDialog(BufferView * bv) const
320 if (!inset.showInsetDialog(bv))
321 bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
326 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
330 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
333 if (!width_.zero()) {
334 int ww1 = latexTextWidth(bv);
335 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
336 if (ww2 > 0 && ww2 < ww1) {
341 // this should not happen!
342 return InsetCollapsable::getMaxWidth(bv, inset);
346 int InsetMinipage::latexTextWidth(BufferView * bv) const
348 return width_.inPixels(InsetCollapsable::latexTextWidth(bv),
349 bv->text->defaultHeight());