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 "dispatchresult.h"
19 #include "funcrequest.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
26 #include "support/std_sstream.h"
32 using std::istringstream;
34 using std::ostringstream;
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);
77 inset.setFrameColor(LColor::blue);
78 setInsetName("Minipage");
82 InsetMinipage::InsetMinipage(InsetMinipage const & in)
83 : InsetCollapsable(in), params_(in.params_)
87 auto_ptr<InsetBase> InsetMinipage::clone() const
89 return auto_ptr<InsetBase>(new InsetMinipage(*this));
93 InsetMinipage::~InsetMinipage()
95 InsetMinipageMailer(*this).hideDialog();
100 InsetMinipage::priv_dispatch(FuncRequest const & cmd,
101 idx_type & idx, pos_type & pos)
103 switch (cmd.action) {
104 case LFUN_INSET_MODIFY: {
105 InsetMinipage::Params params;
106 InsetMinipageMailer::string2params(cmd.argument, params);
108 params_.pos = params.pos;
109 params_.width = params.width;
111 /* FIXME: I refuse to believe we have to live
112 * with ugliness like this ... */
113 inset.getLyXText(cmd.view())->fullRebreak();
114 cmd.view()->updateInset(this);
115 return DispatchResult(true, true);
118 case LFUN_INSET_DIALOG_UPDATE:
119 InsetMinipageMailer(*this).updateDialog(cmd.view());
120 return DispatchResult(true, true);
123 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
128 void InsetMinipage::Params::write(ostream & os) const
130 os << "Minipage" << '\n'
131 << "position " << pos << '\n'
132 << "inner_position " << inner_pos << '\n'
133 << "height \"" << height.asString() << "\"\n"
134 << "width \"" << width.asString() << "\"\n";
138 void InsetMinipage::Params::read(LyXLex & lex)
142 string const token = lex.getString();
143 if (token == "position") {
145 pos = static_cast<Position>(lex.getInteger());
147 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
149 // take countermeasures
150 lex.pushToken(token);
155 string const token = lex.getString();
156 if (token == "inner_position") {
158 inner_pos = static_cast<InnerPosition>(lex.getInteger());
160 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
162 // take countermeasures
163 lex.pushToken(token);
168 string const token = lex.getString();
169 if (token == "height") {
171 height = LyXLength(lex.getString());
173 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
175 // take countermeasures
176 lex.pushToken(token);
181 string const token = lex.getString();
182 if (token == "width") {
184 width = LyXLength(lex.getString());
186 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
188 // take countermeasures
189 lex.pushToken(token);
195 void InsetMinipage::write(Buffer const & buf, ostream & os) const
198 InsetCollapsable::write(buf, os);
202 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
205 InsetCollapsable::read(buf, lex);
209 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
214 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
215 InsetCollapsable::metrics(m, d);
216 switch (params_.pos) {
222 dim.asc = d.ascent() + d.descent() / 2;
232 dimension_collapsed(dim);
238 string const InsetMinipage::editMessage() const
240 return _("Opened Minipage Inset");
244 int InsetMinipage::latex(Buffer const & buf, ostream & os,
245 LatexRunParams const & runparams) const
248 switch (params_.pos) {
259 os << "\\begin{minipage}[" << s_pos << "]{"
260 << params_.width.asLatexString() << "}%\n";
262 int i = inset.latex(buf, os, runparams);
264 os << "\\end{minipage}%\n";
269 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
271 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
274 return InsetCollapsable::insetAllowed(code);
278 bool InsetMinipage::showInsetDialog(BufferView * bv) const
280 if (!inset.showInsetDialog(bv)) {
281 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
282 InsetMinipageMailer mailer(*tmp);
283 mailer.showDialog(bv);
290 int InsetMinipage::latexTextWidth(BufferView * bv) const
292 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
296 InsetMinipage::Params::Params()
298 inner_pos(inner_center),
299 width(100, LyXLength::PCW)
303 string const InsetMinipageMailer:: name_("minipage");
305 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
310 string const InsetMinipageMailer::inset2string(Buffer const &) const
312 return params2string(inset_.params());
316 void InsetMinipageMailer::string2params(string const & in,
317 InsetMinipage::Params & params)
319 params = InsetMinipage::Params();
324 istringstream data(in);
330 string const token = lex.getString();
331 if (token != "minipage")
335 // This is part of the inset proper that is usually swallowed
336 // by Buffer::readInset
339 string const token = lex.getString();
340 if (token != "Minipage")
351 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
354 data << name_ << ' ';