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"
16 #include "insettext.h"
18 #include "BufferView.h"
20 #include "funcrequest.h"
26 #include "frontends/LyXView.h"
27 #include "frontends/Dialogs.h"
29 #include "support/LOstream.h"
30 #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)
67 setLabel(_("minipage"));
68 LyXFont font(LyXFont::ALL_SANE);
71 font.setColor(LColor::collapsable);
74 setAutoCollapse(false);
79 #warning Remove this color definitions before 1.2.0 final!
81 // just for experimentation :)
82 setBackgroundColor(LColor::green);
85 inset.setFrameColor(0, LColor::blue);
86 setInsetName("Minipage");
90 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
91 : InsetCollapsable(in, same_id), params_(in.params_)
95 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
97 return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
101 InsetMinipage::~InsetMinipage()
103 InsetMinipageMailer mailer(*this);
108 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
110 Inset::RESULT result = UNDISPATCHED;
112 switch (cmd.action) {
113 case LFUN_INSET_MODIFY: {
114 InsetMinipage::Params params;
115 InsetMinipageMailer::string2params(cmd.argument, params);
117 params_.pos = params.pos;
118 params_.width = params.width;
120 // FIXME: what magical mysterious commands are actually
121 // needed here to update the bloody size of the inset !!!
122 cmd.view()->updateInset(this);
127 case LFUN_INSET_DIALOG_UPDATE: {
128 InsetMinipageMailer mailer(*this);
129 mailer.updateDialog(cmd.view());
134 result = InsetCollapsable::localDispatch(cmd);
141 void InsetMinipage::Params::write(ostream & os) const
143 os << "Minipage" << '\n'
144 << "position " << pos << '\n'
145 << "inner_position " << inner_pos << '\n'
146 << "height \"" << height.asString() << "\"\n"
147 << "width \"" << width.asString() << "\"\n";
151 void InsetMinipage::Params::read(LyXLex & lex)
155 string const token = lex.getString();
156 if (token == "position") {
158 pos = static_cast<Position>(lex.getInteger());
160 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
162 // take countermeasures
163 lex.pushToken(token);
168 string const token = lex.getString();
169 if (token == "inner_position") {
171 inner_pos = static_cast<InnerPosition>(lex.getInteger());
173 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
175 // take countermeasures
176 lex.pushToken(token);
181 string const token = lex.getString();
182 if (token == "height") {
184 height = LyXLength(lex.getString());
186 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
188 // take countermeasures
189 lex.pushToken(token);
194 string const token = lex.getString();
195 if (token == "width") {
197 width = LyXLength(lex.getString());
199 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
201 // take countermeasures
202 lex.pushToken(token);
208 void InsetMinipage::write(Buffer const * buf, ostream & os) const
211 InsetCollapsable::write(buf, os);
215 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
218 InsetCollapsable::read(buf, lex);
222 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
225 return ascent_collapsed();
227 // Take placement into account.
229 switch (params_.pos) {
231 i = InsetCollapsable::ascent(bv, font);
234 i = (InsetCollapsable::ascent(bv, font)
235 + InsetCollapsable::descent(bv, font)) / 2;
238 i = InsetCollapsable::descent(bv, font);
246 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
249 return descent_collapsed();
251 // Take placement into account.
253 switch (params_.pos) {
255 i = InsetCollapsable::descent(bv, font);
258 i = (InsetCollapsable::ascent(bv, font)
259 + InsetCollapsable::descent(bv, font)) / 2;
262 i = InsetCollapsable::ascent(bv, font);
270 string const InsetMinipage::editMessage() const
272 return _("Opened Minipage Inset");
276 int InsetMinipage::latex(Buffer const * buf,
277 ostream & os, bool fragile, bool fp) const
280 switch (params_.pos) {
291 os << "\\begin{minipage}[" << s_pos << "]{"
292 << params_.width.asLatexString() << "}%\n";
294 int i = inset.latex(buf, os, fragile, fp);
296 os << "\\end{minipage}%\n";
301 bool InsetMinipage::insetAllowed(Inset::Code code) const
303 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
306 return InsetCollapsable::insetAllowed(code);
310 bool InsetMinipage::showInsetDialog(BufferView * bv) const
312 if (!inset.showInsetDialog(bv)) {
313 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
314 InsetMinipageMailer mailer(*tmp);
315 mailer.showDialog(bv);
322 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
326 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
329 if (!params_.width.zero()) {
330 int ww1 = latexTextWidth(bv);
331 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
332 if (ww2 > 0 && ww2 < ww1) {
337 // this should not happen!
338 return InsetCollapsable::getMaxWidth(bv, inset);
342 int InsetMinipage::latexTextWidth(BufferView * bv) const
344 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
348 InsetMinipage::Params::Params()
350 inner_pos(inner_center),
351 width(100, LyXLength::PCW)
355 string const InsetMinipageMailer:: name_("minipage");
357 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
362 string const InsetMinipageMailer::inset2string() const
364 return params2string(inset_.params());
368 void InsetMinipageMailer::string2params(string const & in,
369 InsetMinipage::Params & params)
371 params = InsetMinipage::Params();
373 istringstream data(in);
379 string const token = lex.getString();
380 if (token != "minipage")
384 // This is part of the inset proper that is usually swallowed
385 // by Buffer::readInset
388 string const token = lex.getString();
389 if (token != "Minipage")
400 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
403 data << name_ << ' ';