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"
20 #include "frontends/LyXView.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(BufferParams const & bp)
63 : InsetCollapsable(bp), 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);
78 #warning Remove this color definitions before 1.2.0 final!
80 // just for experimentation :)
81 setBackgroundColor(LColor::green);
84 inset.setFrameColor(0, LColor::blue);
85 setInsetName("Minipage");
89 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
90 : InsetCollapsable(in, same_id),
91 pos_(in.pos_), inner_pos_(in.inner_pos_),
92 height_(in.height_), width_(in.width_)
96 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
98 return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
102 InsetMinipage::~InsetMinipage()
108 void InsetMinipage::write(Buffer const * buf, ostream & os) const
110 os << getInsetName() << "\n"
111 << "position " << pos_ << "\n"
112 << "inner_position " << inner_pos_ << "\n"
113 << "height \"" << height_.asString() << "\"\n"
114 << "width \"" << width_.asString() << "\"\n";
115 InsetCollapsable::write(buf, os);
119 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
123 string const token = lex.getString();
124 if (token == "position") {
126 pos_ = static_cast<Position>(lex.getInteger());
128 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
130 // take countermeasures
131 lex.pushToken(token);
136 string const token = lex.getString();
137 if (token == "inner_position") {
139 inner_pos_ = static_cast<InnerPosition>(lex.getInteger());
141 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
143 // take countermeasures
144 lex.pushToken(token);
149 string const token = lex.getString();
150 if (token == "height") {
152 height_ = LyXLength(lex.getString());
154 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
156 // take countermeasures
157 lex.pushToken(token);
162 string const token = lex.getString();
163 if (token == "width") {
165 width_ = LyXLength(lex.getString());
167 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
169 // take countermeasures
170 lex.pushToken(token);
173 InsetCollapsable::read(buf, lex);
177 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
180 return ascent_collapsed();
182 // Take placement into account.
186 i = InsetCollapsable::ascent(bv, font);
189 i = (InsetCollapsable::ascent(bv, font)
190 + InsetCollapsable::descent(bv, font)) / 2;
193 i = InsetCollapsable::descent(bv, font);
201 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
204 return descent_collapsed();
206 // Take placement into account.
210 i = InsetCollapsable::descent(bv, font);
213 i = (InsetCollapsable::ascent(bv, font)
214 + InsetCollapsable::descent(bv, font)) / 2;
217 i = InsetCollapsable::ascent(bv, font);
225 string const InsetMinipage::editMessage() const
227 return _("Opened Minipage Inset");
231 int InsetMinipage::latex(Buffer const * buf,
232 ostream & os, bool fragile, bool fp) const
246 os << "\\begin{minipage}[" << s_pos << "]{"
247 << width_.asLatexString() << "}%\n";
249 int i = inset.latex(buf, os, fragile, fp);
251 os << "\\end{minipage}%\n";
256 bool InsetMinipage::insetAllowed(Inset::Code code) const
258 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
261 return InsetCollapsable::insetAllowed(code);
265 InsetMinipage::Position InsetMinipage::pos() const
271 void InsetMinipage::pos(InsetMinipage::Position p)
280 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
286 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
292 LyXLength const & InsetMinipage::pageHeight() const
298 void InsetMinipage::pageHeight(LyXLength const & ll)
307 LyXLength const & InsetMinipage::pageWidth() const
313 void InsetMinipage::pageWidth(LyXLength const & ll)
322 bool InsetMinipage::showInsetDialog(BufferView * bv) const
324 if (!inset.showInsetDialog(bv))
325 bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
330 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
334 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
337 if (!width_.zero()) {
338 int ww1 = latexTextWidth(bv);
339 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
340 if (ww2 > 0 && ww2 < ww1) {
345 // this should not happen!
346 return InsetCollapsable::getMaxWidth(bv, inset);
350 int InsetMinipage::latexTextWidth(BufferView * bv) const
352 return width_.inPixels(InsetCollapsable::latexTextWidth(bv),
353 bv->text->defaultHeight());