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"
16 #include "BufferView.h"
18 #include "funcrequest.h"
21 #include "metricsinfo.h"
23 #include "support/std_sstream.h"
30 // Some information about Minipages in LaTeX:
31 // A minipage is a complete miniversion of a page and can contain
32 // its own footnotes, paragraphs, and array, tabular, and multicols
33 // environments. However it cannot contain floats or \marginpar's,
34 // but it can appear inside floats.
36 // The minipage environment is defined like this:
38 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
41 // pos [opt] = is the vertical placement of the box with respect
42 // to the text baseline, [c], [t] and [b].
43 // height [opt] = the height of the box
44 // inner-pos [opt] = the position of the text within the box.
45 // It can be t, c, b or s, if unspecified the value
47 // width = the width of the box
49 // In LyX we should try to support all these parameters, settable in a
51 // In this pop-up diallog it should also be possible to set all margin
52 // values that is usable in the minipage.
53 // With regard to different formats (like DocBook) I guess a minipage
54 // can be used there also. Perhaps not in the latex way, but we do not
55 // have to output "" for minipages.
58 InsetMinipage::InsetMinipage(BufferParams const & bp)
59 : InsetCollapsable(bp)
61 setLabel(_("minipage"));
62 LyXFont font(LyXFont::ALL_SANE);
65 font.setColor(LColor::collapsable);
68 setAutoCollapse(false);
70 inset.setFrameColor(LColor::blue);
71 setInsetName("Minipage");
75 InsetMinipage::InsetMinipage(InsetMinipage const & in)
76 : InsetCollapsable(in), params_(in.params_)
80 auto_ptr<InsetBase> InsetMinipage::clone() const
82 return auto_ptr<InsetBase>(new InsetMinipage(*this));
86 InsetMinipage::~InsetMinipage()
88 InsetMinipageMailer(*this).hideDialog();
92 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
95 case LFUN_INSET_MODIFY: {
96 InsetMinipage::Params params;
97 InsetMinipageMailer::string2params(cmd.argument, params);
99 params_.pos = params.pos;
100 params_.width = params.width;
102 /* FIXME: I refuse to believe we have to live
103 * with ugliness like this ... */
104 inset.getLyXText(cmd.view())->fullRebreak();
105 cmd.view()->updateInset(this);
109 case LFUN_INSET_DIALOG_UPDATE:
110 InsetMinipageMailer(*this).updateDialog(cmd.view());
114 return InsetCollapsable::localDispatch(cmd);
119 void InsetMinipage::Params::write(ostream & os) const
121 os << "Minipage" << '\n'
122 << "position " << pos << '\n'
123 << "inner_position " << inner_pos << '\n'
124 << "height \"" << height.asString() << "\"\n"
125 << "width \"" << width.asString() << "\"\n";
129 void InsetMinipage::Params::read(LyXLex & lex)
133 string const token = lex.getString();
134 if (token == "position") {
136 pos = static_cast<Position>(lex.getInteger());
138 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
140 // take countermeasures
141 lex.pushToken(token);
146 string const token = lex.getString();
147 if (token == "inner_position") {
149 inner_pos = static_cast<InnerPosition>(lex.getInteger());
151 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
153 // take countermeasures
154 lex.pushToken(token);
159 string const token = lex.getString();
160 if (token == "height") {
162 height = LyXLength(lex.getString());
164 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
166 // take countermeasures
167 lex.pushToken(token);
172 string const token = lex.getString();
173 if (token == "width") {
175 width = LyXLength(lex.getString());
177 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
179 // take countermeasures
180 lex.pushToken(token);
186 void InsetMinipage::write(Buffer const & buf, ostream & os) const
189 InsetCollapsable::write(buf, os);
193 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
196 InsetCollapsable::read(buf, lex);
200 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
203 dimension_collapsed(dim);
207 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
208 InsetCollapsable::metrics(m, d);
209 switch (params_.pos) {
215 dim.asc = d.ascent() + d.descent() / 2;
229 string const InsetMinipage::editMessage() const
231 return _("Opened Minipage Inset");
235 int InsetMinipage::latex(Buffer const & buf, ostream & os,
236 LatexRunParams const & runparams) const
239 switch (params_.pos) {
250 os << "\\begin{minipage}[" << s_pos << "]{"
251 << params_.width.asLatexString() << "}%\n";
253 int i = inset.latex(buf, os, runparams);
255 os << "\\end{minipage}%\n";
260 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
262 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
265 return InsetCollapsable::insetAllowed(code);
269 bool InsetMinipage::showInsetDialog(BufferView * bv) const
271 if (!inset.showInsetDialog(bv)) {
272 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
273 InsetMinipageMailer mailer(*tmp);
274 mailer.showDialog(bv);
281 int InsetMinipage::latexTextWidth(BufferView * bv) const
283 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
287 InsetMinipage::Params::Params()
289 inner_pos(inner_center),
290 width(100, LyXLength::PCW)
294 string const InsetMinipageMailer:: name_("minipage");
296 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
301 string const InsetMinipageMailer::inset2string(Buffer const &) const
303 return params2string(inset_.params());
307 void InsetMinipageMailer::string2params(string const & in,
308 InsetMinipage::Params & params)
310 params = InsetMinipage::Params();
315 istringstream data(STRCONV(in));
321 string const token = lex.getString();
322 if (token != "minipage")
326 // This is part of the inset proper that is usually swallowed
327 // by Buffer::readInset
330 string const token = lex.getString();
331 if (token != "Minipage")
342 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
345 data << name_ << ' ';
347 return STRCONV(data.str());