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"
27 #include "metricsinfo.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/Dialogs.h"
32 #include "support/LOstream.h"
39 // Some information about Minipages in LaTeX:
40 // A minipage is a complete miniversion of a page and can contain
41 // its own footnotes, paragraphs, and array, tabular, and multicols
42 // environments. However it cannot contain floats or \marginpar's,
43 // but it can appear inside floats.
45 // The minipage environment is defined like this:
47 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
50 // pos [opt] = is the vertical placement of the box with respect
51 // to the text baseline, [c], [t] and [b].
52 // height [opt] = the height of the box
53 // inner-pos [opt] = the position of the text within the box.
54 // It can be t, c, b or s, if unspecified the value
56 // width = the width of the box
58 // In LyX we should try to support all these parameters, settable in a
60 // In this pop-up diallog it should also be possible to set all margin
61 // values that is usable in the minipage.
62 // With regard to different formats (like DocBook) I guess a minipage
63 // can be used there also. Perhaps not in the latex way, but we do not
64 // have to output "" for minipages.
67 InsetMinipage::InsetMinipage(BufferParams const & bp)
68 : InsetCollapsable(bp)
70 setLabel(_("minipage"));
71 LyXFont font(LyXFont::ALL_SANE);
74 font.setColor(LColor::collapsable);
77 setAutoCollapse(false);
82 #warning Remove this color definitions before 1.2.0 final!
84 // just for experimentation :)
85 setBackgroundColor(LColor::green);
88 inset.setFrameColor(0, LColor::blue);
89 setInsetName("Minipage");
93 InsetMinipage::InsetMinipage(InsetMinipage const & in)
94 : InsetCollapsable(in), params_(in.params_)
98 auto_ptr<InsetBase> InsetMinipage::clone() const
100 return auto_ptr<InsetBase>(new InsetMinipage(*this));
104 InsetMinipage::~InsetMinipage()
106 InsetMinipageMailer mailer(*this);
111 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
113 switch (cmd.action) {
114 case LFUN_INSET_MODIFY: {
115 InsetMinipage::Params params;
116 InsetMinipageMailer::string2params(cmd.argument, params);
118 params_.pos = params.pos;
119 params_.width = params.width;
121 /* FIXME: I refuse to believe we have to live
122 * with ugliness like this ... */
123 inset.getLyXText(cmd.view())->fullRebreak();
124 cmd.view()->updateInset(this);
128 case LFUN_INSET_DIALOG_UPDATE:
129 InsetMinipageMailer(*this).updateDialog(cmd.view());
133 return InsetCollapsable::localDispatch(cmd);
138 void InsetMinipage::Params::write(ostream & os) const
140 os << "Minipage" << '\n'
141 << "position " << pos << '\n'
142 << "inner_position " << inner_pos << '\n'
143 << "height \"" << height.asString() << "\"\n"
144 << "width \"" << width.asString() << "\"\n";
148 void InsetMinipage::Params::read(LyXLex & lex)
152 string const token = lex.getString();
153 if (token == "position") {
155 pos = static_cast<Position>(lex.getInteger());
157 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
159 // take countermeasures
160 lex.pushToken(token);
165 string const token = lex.getString();
166 if (token == "inner_position") {
168 inner_pos = static_cast<InnerPosition>(lex.getInteger());
170 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
172 // take countermeasures
173 lex.pushToken(token);
178 string const token = lex.getString();
179 if (token == "height") {
181 height = LyXLength(lex.getString());
183 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
185 // take countermeasures
186 lex.pushToken(token);
191 string const token = lex.getString();
192 if (token == "width") {
194 width = LyXLength(lex.getString());
196 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
198 // take countermeasures
199 lex.pushToken(token);
205 void InsetMinipage::write(Buffer const * buf, ostream & os) const
208 InsetCollapsable::write(buf, os);
212 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
215 InsetCollapsable::read(buf, lex);
219 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
222 dimension_collapsed(dim);
226 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
227 InsetCollapsable::metrics(m, d);
228 switch (params_.pos) {
234 dim.asc = d.ascent() + d.descent() / 2;
248 string const InsetMinipage::editMessage() const
250 return _("Opened Minipage Inset");
254 int InsetMinipage::latex(Buffer const * buf, ostream & os,
255 LatexRunParams const & runparams) const
258 switch (params_.pos) {
269 os << "\\begin{minipage}[" << s_pos << "]{"
270 << params_.width.asLatexString() << "}%\n";
272 int i = inset.latex(buf, os, runparams);
274 os << "\\end{minipage}%\n";
279 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
281 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
284 return InsetCollapsable::insetAllowed(code);
288 bool InsetMinipage::showInsetDialog(BufferView * bv) const
290 if (!inset.showInsetDialog(bv)) {
291 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
292 InsetMinipageMailer mailer(*tmp);
293 mailer.showDialog(bv);
300 int InsetMinipage::latexTextWidth(BufferView * bv) const
302 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
306 InsetMinipage::Params::Params()
308 inner_pos(inner_center),
309 width(100, LyXLength::PCW)
313 string const InsetMinipageMailer:: name_("minipage");
315 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
320 string const InsetMinipageMailer::inset2string(Buffer const &) const
322 return params2string(inset_.params());
326 void InsetMinipageMailer::string2params(string const & in,
327 InsetMinipage::Params & params)
329 params = InsetMinipage::Params();
334 istringstream data(STRCONV(in));
340 string const token = lex.getString();
341 if (token != "minipage")
345 // This is part of the inset proper that is usually swallowed
346 // by Buffer::readInset
349 string const token = lex.getString();
350 if (token != "Minipage")
361 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
364 data << name_ << ' ';
366 return STRCONV(data.str());