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)
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");
82 width_ = "100%"; // set default to 100% of column_width
86 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
87 : InsetCollapsable(in, same_id),
88 pos_(in.pos_), inner_pos_(in.inner_pos_),
89 height_(in.height_), width_(in.width_)
93 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
95 return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
99 InsetMinipage::~InsetMinipage()
105 void InsetMinipage::write(Buffer const * buf, ostream & os) const
107 os << getInsetName() << "\n"
108 << "position " << pos_ << "\n"
109 << "inner_position " << inner_pos_ << "\n"
110 << "height \"" << height_ << "\"\n"
111 << "width \"" << width_ << "\"\n";
112 InsetCollapsable::write(buf, os);
116 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
120 string const token = lex.getString();
121 if (token == "position") {
123 pos_ = static_cast<Position>(lex.getInteger());
125 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
127 // take countermeasures
128 lex.pushToken(token);
133 string const token = lex.getString();
134 if (token == "inner_position") {
136 inner_pos_ = static_cast<InnerPosition>(lex.getInteger());
138 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
140 // take countermeasures
141 lex.pushToken(token);
146 string const token = lex.getString();
147 if (token == "height") {
149 height_ = lex.getString();
151 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
153 // take countermeasures
154 lex.pushToken(token);
159 string const token = lex.getString();
160 if (token == "width") {
162 width_ = lex.getString();
164 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
166 // take countermeasures
167 lex.pushToken(token);
170 InsetCollapsable::read(buf, lex);
174 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
177 return ascent_collapsed();
179 // Take placement into account.
183 i = InsetCollapsable::ascent(bv, font);
186 i = (InsetCollapsable::ascent(bv, font)
187 + InsetCollapsable::descent(bv, font)) / 2;
190 i = InsetCollapsable::descent(bv, font);
198 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
201 return descent_collapsed();
203 // Take placement into account.
207 i = InsetCollapsable::descent(bv, font);
210 i = (InsetCollapsable::ascent(bv, font)
211 + InsetCollapsable::descent(bv, font)) / 2;
214 i = InsetCollapsable::ascent(bv, font);
222 string const InsetMinipage::editMessage() const
224 return _("Opened Minipage Inset");
228 int InsetMinipage::latex(Buffer const * buf,
229 ostream & os, bool fragile, bool fp) const
243 os << "\\begin{minipage}[" << s_pos << "]{"
244 << LyXLength(width_).asLatexString() << "}%\n";
246 int i = inset.latex(buf, os, fragile, fp);
248 os << "\\end{minipage}%\n";
253 bool InsetMinipage::insetAllowed(Inset::Code code) const
255 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
258 return InsetCollapsable::insetAllowed(code);
262 InsetMinipage::Position InsetMinipage::pos() const
268 void InsetMinipage::pos(InsetMinipage::Position p)
277 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
283 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
289 string const & InsetMinipage::height() const
295 void InsetMinipage::height(string const & ll)
304 string const & InsetMinipage::width() const
310 void InsetMinipage::width(string const & ll)
319 bool InsetMinipage::showInsetDialog(BufferView * bv) const
321 if (!inset.showInsetDialog(bv))
322 bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
327 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
331 (static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0))
335 if (!width_.empty()) {
336 LyXLength len(width_);
338 case LyXLength::PW: // Always % of workarea
342 return (InsetCollapsable::getMaxWidth(bv, inset) * (int)len.value()) / 100;
345 int ww1 = VSpace(width_).inPixels(bv);
346 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
347 if (ww2 > 0 && ww2 < ww1) {
354 // this should not happen!
355 return InsetCollapsable::getMaxWidth(bv, inset);