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 cmd.view()->updateInset(this, true);
125 case LFUN_INSET_DIALOG_UPDATE: {
126 InsetMinipageMailer mailer(*this);
127 mailer.updateDialog();
132 result = InsetCollapsable::localDispatch(cmd);
139 void InsetMinipage::Params::write(ostream & os) const
141 os << "Minipage" << '\n'
142 << "position " << pos << '\n'
143 << "inner_position " << inner_pos << '\n'
144 << "height \"" << height.asString() << "\"\n"
145 << "width \"" << width.asString() << "\"\n";
149 void InsetMinipage::Params::read(LyXLex & lex)
153 string const token = lex.getString();
154 if (token == "position") {
156 pos = static_cast<Position>(lex.getInteger());
158 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
160 // take countermeasures
161 lex.pushToken(token);
166 string const token = lex.getString();
167 if (token == "inner_position") {
169 inner_pos = static_cast<InnerPosition>(lex.getInteger());
171 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
173 // take countermeasures
174 lex.pushToken(token);
179 string const token = lex.getString();
180 if (token == "height") {
182 height = LyXLength(lex.getString());
184 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
186 // take countermeasures
187 lex.pushToken(token);
192 string const token = lex.getString();
193 if (token == "width") {
195 width = LyXLength(lex.getString());
197 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
199 // take countermeasures
200 lex.pushToken(token);
206 void InsetMinipage::write(Buffer const * buf, ostream & os) const
209 InsetCollapsable::write(buf, os);
213 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
216 InsetCollapsable::read(buf, lex);
220 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
223 return ascent_collapsed();
225 // Take placement into account.
227 switch (params_.pos) {
229 i = InsetCollapsable::ascent(bv, font);
232 i = (InsetCollapsable::ascent(bv, font)
233 + InsetCollapsable::descent(bv, font)) / 2;
236 i = InsetCollapsable::descent(bv, font);
244 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
247 return descent_collapsed();
249 // Take placement into account.
251 switch (params_.pos) {
253 i = InsetCollapsable::descent(bv, font);
256 i = (InsetCollapsable::ascent(bv, font)
257 + InsetCollapsable::descent(bv, font)) / 2;
260 i = InsetCollapsable::ascent(bv, font);
268 string const InsetMinipage::editMessage() const
270 return _("Opened Minipage Inset");
274 int InsetMinipage::latex(Buffer const * buf,
275 ostream & os, bool fragile, bool fp) const
278 switch (params_.pos) {
289 os << "\\begin{minipage}[" << s_pos << "]{"
290 << params_.width.asLatexString() << "}%\n";
292 int i = inset.latex(buf, os, fragile, fp);
294 os << "\\end{minipage}%\n";
299 bool InsetMinipage::insetAllowed(Inset::Code code) const
301 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
304 return InsetCollapsable::insetAllowed(code);
308 InsetMinipage::Position InsetMinipage::pos() const
314 void InsetMinipage::pos(InsetMinipage::Position p)
316 if (params_.pos != p) {
323 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
325 return params_.inner_pos;
329 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
331 params_.inner_pos = ip;
335 LyXLength const & InsetMinipage::pageHeight() const
337 return params_.height;
341 void InsetMinipage::pageHeight(LyXLength const & ll)
343 if (params_.height != ll) {
350 LyXLength const & InsetMinipage::pageWidth() const
352 return params_.width;
356 void InsetMinipage::pageWidth(LyXLength const & ll)
358 if (ll != params_.width) {
365 bool InsetMinipage::showInsetDialog(BufferView * bv) const
367 if (!inset.showInsetDialog(bv)) {
368 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
369 InsetMinipageMailer mailer(*tmp);
377 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
381 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
384 if (!params_.width.zero()) {
385 int ww1 = latexTextWidth(bv);
386 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
387 if (ww2 > 0 && ww2 < ww1) {
392 // this should not happen!
393 return InsetCollapsable::getMaxWidth(bv, inset);
397 int InsetMinipage::latexTextWidth(BufferView * bv) const
399 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
403 InsetMinipage::Params::Params()
405 inner_pos(inner_center),
406 width(100, LyXLength::PCW)
410 string const InsetMinipageMailer:: name_("minipage");
412 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
417 string const InsetMinipageMailer::inset2string() const
419 return params2string(inset_.params());
423 void InsetMinipageMailer::string2params(string const & in,
424 InsetMinipage::Params & params)
426 params = InsetMinipage::Params();
428 istringstream data(in);
434 string const token = lex.getString();
435 if (token != "minipage")
439 // This is part of the inset proper that is usually swallowed
440 // by Buffer::readInset
443 string const token = lex.getString();
444 if (token != "Minipage")
453 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
456 data << name_ << ' ';