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 InsetBase * InsetMinipage::clone() const
98 return new InsetMinipage(*this);
102 InsetMinipage::~InsetMinipage()
104 InsetMinipageMailer mailer(*this);
109 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
111 switch (cmd.action) {
112 case LFUN_INSET_MODIFY: {
113 InsetMinipage::Params params;
114 InsetMinipageMailer::string2params(cmd.argument, params);
116 params_.pos = params.pos;
117 params_.width = params.width;
119 /* FIXME: I refuse to believe we have to live
120 * with ugliness like this ... */
121 inset.getLyXText(cmd.view())->fullRebreak();
122 inset.update(cmd.view(), true);
123 cmd.view()->updateInset(this);
127 case LFUN_INSET_DIALOG_UPDATE:
128 InsetMinipageMailer(*this).updateDialog(cmd.view());
132 return InsetCollapsable::localDispatch(cmd);
137 void InsetMinipage::Params::write(ostream & os) const
139 os << "Minipage" << '\n'
140 << "position " << pos << '\n'
141 << "inner_position " << inner_pos << '\n'
142 << "height \"" << height.asString() << "\"\n"
143 << "width \"" << width.asString() << "\"\n";
147 void InsetMinipage::Params::read(LyXLex & lex)
151 string const token = lex.getString();
152 if (token == "position") {
154 pos = static_cast<Position>(lex.getInteger());
156 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
158 // take countermeasures
159 lex.pushToken(token);
164 string const token = lex.getString();
165 if (token == "inner_position") {
167 inner_pos = static_cast<InnerPosition>(lex.getInteger());
169 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
171 // take countermeasures
172 lex.pushToken(token);
177 string const token = lex.getString();
178 if (token == "height") {
180 height = LyXLength(lex.getString());
182 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
184 // take countermeasures
185 lex.pushToken(token);
190 string const token = lex.getString();
191 if (token == "width") {
193 width = LyXLength(lex.getString());
195 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
197 // take countermeasures
198 lex.pushToken(token);
204 void InsetMinipage::write(Buffer const * buf, ostream & os) const
207 InsetCollapsable::write(buf, os);
211 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
214 InsetCollapsable::read(buf, lex);
218 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
221 dimension_collapsed(dim);
224 InsetCollapsable::metrics(mi, d);
225 switch (params_.pos) {
231 dim.asc = d.ascent() + d.descent() / 2;
244 string const InsetMinipage::editMessage() const
246 return _("Opened Minipage Inset");
250 int InsetMinipage::latex(Buffer const * buf, ostream & os,
251 LatexRunParams const & runparams) const
254 switch (params_.pos) {
265 os << "\\begin{minipage}[" << s_pos << "]{"
266 << params_.width.asLatexString() << "}%\n";
268 int i = inset.latex(buf, os, runparams);
270 os << "\\end{minipage}%\n";
275 bool InsetMinipage::insetAllowed(Inset::Code code) const
277 if (code == Inset::FLOAT_CODE || code == Inset::MARGIN_CODE)
280 return InsetCollapsable::insetAllowed(code);
284 bool InsetMinipage::showInsetDialog(BufferView * bv) const
286 if (!inset.showInsetDialog(bv)) {
287 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
288 InsetMinipageMailer mailer(*tmp);
289 mailer.showDialog(bv);
296 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
300 static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
303 if (!params_.width.zero()) {
304 int ww1 = latexTextWidth(bv);
305 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
306 if (ww2 > 0 && ww2 < ww1) {
311 // this should not happen!
312 return InsetCollapsable::getMaxWidth(bv, inset);
316 int InsetMinipage::latexTextWidth(BufferView * bv) const
318 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
322 InsetMinipage::Params::Params()
324 inner_pos(inner_center),
325 width(100, LyXLength::PCW)
329 string const InsetMinipageMailer:: name_("minipage");
331 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
336 string const InsetMinipageMailer::inset2string() const
338 return params2string(inset_.params());
342 void InsetMinipageMailer::string2params(string const & in,
343 InsetMinipage::Params & params)
345 params = InsetMinipage::Params();
350 istringstream data(STRCONV(in));
356 string const token = lex.getString();
357 if (token != "minipage")
361 // This is part of the inset proper that is usually swallowed
362 // by Buffer::readInset
365 string const token = lex.getString();
366 if (token != "Minipage")
377 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
380 data << name_ << ' ';
382 return STRCONV(data.str());