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 result = InsetCollapsable::localDispatch(cmd);
132 void InsetMinipage::Params::write(ostream & os) const
134 os << "Minipage" << '\n'
135 << "position " << pos << '\n'
136 << "inner_position " << inner_pos << '\n'
137 << "height \"" << height.asString() << "\"\n"
138 << "width \"" << width.asString() << "\"\n";
142 void InsetMinipage::Params::read(LyXLex & lex)
146 string const token = lex.getString();
147 if (token == "position") {
149 pos = static_cast<Position>(lex.getInteger());
151 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
153 // take countermeasures
154 lex.pushToken(token);
159 string const token = lex.getString();
160 if (token == "inner_position") {
162 inner_pos = static_cast<InnerPosition>(lex.getInteger());
164 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
166 // take countermeasures
167 lex.pushToken(token);
172 string const token = lex.getString();
173 if (token == "height") {
175 height = LyXLength(lex.getString());
177 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
179 // take countermeasures
180 lex.pushToken(token);
185 string const token = lex.getString();
186 if (token == "width") {
188 width = LyXLength(lex.getString());
190 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
192 // take countermeasures
193 lex.pushToken(token);
199 void InsetMinipage::write(Buffer const * buf, ostream & os) const
202 InsetCollapsable::write(buf, os);
206 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
209 InsetCollapsable::read(buf, lex);
213 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
216 return ascent_collapsed();
218 // Take placement into account.
220 switch (params_.pos) {
222 i = InsetCollapsable::ascent(bv, font);
225 i = (InsetCollapsable::ascent(bv, font)
226 + InsetCollapsable::descent(bv, font)) / 2;
229 i = InsetCollapsable::descent(bv, font);
237 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
240 return descent_collapsed();
242 // Take placement into account.
244 switch (params_.pos) {
246 i = InsetCollapsable::descent(bv, font);
249 i = (InsetCollapsable::ascent(bv, font)
250 + InsetCollapsable::descent(bv, font)) / 2;
253 i = InsetCollapsable::ascent(bv, font);
261 string const InsetMinipage::editMessage() const
263 return _("Opened Minipage Inset");
267 int InsetMinipage::latex(Buffer const * buf,
268 ostream & os, bool fragile, bool fp) const
271 switch (params_.pos) {
282 os << "\\begin{minipage}[" << s_pos << "]{"
283 << params_.width.asLatexString() << "}%\n";
285 int i = inset.latex(buf, os, fragile, fp);
287 os << "\\end{minipage}%\n";
292 bool InsetMinipage::insetAllowed(Inset::Code code) const
294 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
297 return InsetCollapsable::insetAllowed(code);
301 InsetMinipage::Position InsetMinipage::pos() const
307 void InsetMinipage::pos(InsetMinipage::Position p)
309 if (params_.pos != p) {
316 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
318 return params_.inner_pos;
322 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
324 params_.inner_pos = ip;
328 LyXLength const & InsetMinipage::pageHeight() const
330 return params_.height;
334 void InsetMinipage::pageHeight(LyXLength const & ll)
336 if (params_.height != ll) {
343 LyXLength const & InsetMinipage::pageWidth() const
345 return params_.width;
349 void InsetMinipage::pageWidth(LyXLength const & ll)
351 if (ll != params_.width) {
358 bool InsetMinipage::showInsetDialog(BufferView * bv) const
360 if (!inset.showInsetDialog(bv)) {
361 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
362 InsetMinipageMailer mailer(*tmp);
370 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
374 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
377 if (!params_.width.zero()) {
378 int ww1 = latexTextWidth(bv);
379 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
380 if (ww2 > 0 && ww2 < ww1) {
385 // this should not happen!
386 return InsetCollapsable::getMaxWidth(bv, inset);
390 int InsetMinipage::latexTextWidth(BufferView * bv) const
392 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
396 InsetMinipage::Params::Params()
398 inner_pos(inner_center),
399 width(100, LyXLength::PCW)
403 string const InsetMinipageMailer:: name_("minipage");
405 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
410 string const InsetMinipageMailer::inset2string() const
412 return params2string(inset_.params());
416 void InsetMinipageMailer::string2params(string const & in,
417 InsetMinipage::Params & params)
419 params = InsetMinipage::Params();
421 istringstream data(in);
427 string const token = lex.getString();
428 if (token != "minipage")
432 // This is part of the inset proper that is usually swallowed
433 // by Buffer::readInset
436 string const token = lex.getString();
437 if (token != "Minipage")
446 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
449 data << name_ << ' ';