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
14 #include "insetminipage.h"
15 #include "insettext.h"
17 #include "BufferView.h"
19 #include "dimension.h"
20 #include "funcrequest.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/Dialogs.h"
31 #include "support/LOstream.h"
37 // Some information about Minipages in LaTeX:
38 // A minipage is a complete miniversion of a page and can contain
39 // its own footnotes, paragraphs, and array, tabular, and multicols
40 // environments. However it cannot contain floats or \marginpar's,
41 // but it can appear inside floats.
43 // The minipage environment is defined like this:
45 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
48 // pos [opt] = is the vertical placement of the box with respect
49 // to the text baseline, [c], [t] and [b].
50 // height [opt] = the height of the box
51 // inner-pos [opt] = the position of the text within the box.
52 // It can be t, c, b or s, if unspecified the value
54 // width = the width of the box
56 // In LyX we should try to support all these parameters, settable in a
58 // In this pop-up diallog it should also be possible to set all margin
59 // values that is usable in the minipage.
60 // With regard to different formats (like DocBook) I guess a minipage
61 // can be used there also. Perhaps not in the latex way, but we do not
62 // have to output "" for minipages.
65 InsetMinipage::InsetMinipage(BufferParams const & bp)
66 : InsetCollapsable(bp)
68 setLabel(_("minipage"));
69 LyXFont font(LyXFont::ALL_SANE);
72 font.setColor(LColor::collapsable);
75 setAutoCollapse(false);
80 #warning Remove this color definitions before 1.2.0 final!
82 // just for experimentation :)
83 setBackgroundColor(LColor::green);
86 inset.setFrameColor(0, LColor::blue);
87 setInsetName("Minipage");
91 InsetMinipage::InsetMinipage(InsetMinipage const & in)
92 : InsetCollapsable(in), params_(in.params_)
96 // InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
97 // : InsetCollapsable(in, same_id), params_(in.params_)
101 Inset * InsetMinipage::clone(Buffer const &) const
103 return new InsetMinipage(*const_cast<InsetMinipage *>(this));
107 // Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
109 // return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
113 InsetMinipage::~InsetMinipage()
115 InsetMinipageMailer mailer(*this);
120 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
122 switch (cmd.action) {
123 case LFUN_INSET_MODIFY: {
124 InsetMinipage::Params params;
125 InsetMinipageMailer::string2params(cmd.argument, params);
127 params_.pos = params.pos;
128 params_.width = params.width;
130 /* FIXME: I refuse to believe we have to live
131 * with ugliness like this ... */
132 LyXText * t = inset.getLyXText(cmd.view());
133 t->need_break_row = t->rows().begin();
135 inset.update(cmd.view(), true);
136 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
137 cmd.view()->updateInset(this);
141 case LFUN_INSET_DIALOG_UPDATE:
142 InsetMinipageMailer(*this).updateDialog(cmd.view());
146 return InsetCollapsable::localDispatch(cmd);
151 void InsetMinipage::Params::write(ostream & os) const
153 os << "Minipage" << '\n'
154 << "position " << pos << '\n'
155 << "inner_position " << inner_pos << '\n'
156 << "height \"" << height.asString() << "\"\n"
157 << "width \"" << width.asString() << "\"\n";
161 void InsetMinipage::Params::read(LyXLex & lex)
165 string const token = lex.getString();
166 if (token == "position") {
168 pos = static_cast<Position>(lex.getInteger());
170 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
172 // take countermeasures
173 lex.pushToken(token);
178 string const token = lex.getString();
179 if (token == "inner_position") {
181 inner_pos = static_cast<InnerPosition>(lex.getInteger());
183 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
185 // take countermeasures
186 lex.pushToken(token);
191 string const token = lex.getString();
192 if (token == "height") {
194 height = LyXLength(lex.getString());
196 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
198 // take countermeasures
199 lex.pushToken(token);
204 string const token = lex.getString();
205 if (token == "width") {
207 width = LyXLength(lex.getString());
209 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
211 // take countermeasures
212 lex.pushToken(token);
218 void InsetMinipage::write(Buffer const * buf, ostream & os) const
221 InsetCollapsable::write(buf, os);
225 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
228 InsetCollapsable::read(buf, lex);
232 void InsetMinipage::dimension(BufferView * bv, LyXFont const & font,
233 Dimension & dim) const
236 dimension_collapsed(dim);
239 InsetCollapsable::dimension(bv, font, dim);
240 switch (params_.pos) {
246 dim.a = d.height() / 2;
258 string const InsetMinipage::editMessage() const
260 return _("Opened Minipage Inset");
264 int InsetMinipage::latex(Buffer const * buf, ostream & os,
265 LatexRunParams const & runparams) const
268 switch (params_.pos) {
279 os << "\\begin{minipage}[" << s_pos << "]{"
280 << params_.width.asLatexString() << "}%\n";
282 int i = inset.latex(buf, os, runparams);
284 os << "\\end{minipage}%\n";
289 bool InsetMinipage::insetAllowed(Inset::Code code) const
291 if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
294 return InsetCollapsable::insetAllowed(code);
298 bool InsetMinipage::showInsetDialog(BufferView * bv) const
300 if (!inset.showInsetDialog(bv)) {
301 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
302 InsetMinipageMailer mailer(*tmp);
303 mailer.showDialog(bv);
310 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
314 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
317 if (!params_.width.zero()) {
318 int ww1 = latexTextWidth(bv);
319 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
320 if (ww2 > 0 && ww2 < ww1) {
325 // this should not happen!
326 return InsetCollapsable::getMaxWidth(bv, inset);
330 int InsetMinipage::latexTextWidth(BufferView * bv) const
332 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
336 InsetMinipage::Params::Params()
338 inner_pos(inner_center),
339 width(100, LyXLength::PCW)
343 string const InsetMinipageMailer:: name_("minipage");
345 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
350 string const InsetMinipageMailer::inset2string() const
352 return params2string(inset_.params());
356 void InsetMinipageMailer::string2params(string const & in,
357 InsetMinipage::Params & params)
359 params = InsetMinipage::Params();
364 istringstream data(STRCONV(in));
370 string const token = lex.getString();
371 if (token != "minipage")
375 // This is part of the inset proper that is usually swallowed
376 // by Buffer::readInset
379 string const token = lex.getString();
380 if (token != "Minipage")
391 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
394 data << name_ << ' ';
396 return STRCONV(data.str());