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"
22 #include "metricsinfo.h"
23 #include "paragraph.h"
25 #include "support/std_sstream.h"
31 using std::istringstream;
33 using std::ostringstream;
36 // Some information about Minipages in LaTeX:
37 // A minipage is a complete miniversion of a page and can contain
38 // its own footnotes, paragraphs, and array, tabular, and multicols
39 // environments. However it cannot contain floats or \marginpar's,
40 // but it can appear inside floats.
42 // The minipage environment is defined like this:
44 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
47 // pos [opt] = is the vertical placement of the box with respect
48 // to the text baseline, [c], [t] and [b].
49 // height [opt] = the height of the box
50 // inner-pos [opt] = the position of the text within the box.
51 // It can be t, c, b or s, if unspecified the value
53 // width = the width of the box
55 // In LyX we should try to support all these parameters, settable in a
57 // In this pop-up diallog it should also be possible to set all margin
58 // values that is usable in the minipage.
59 // With regard to different formats (like DocBook) I guess a minipage
60 // can be used there also. Perhaps not in the latex way, but we do not
61 // have to output "" for minipages.
64 InsetMinipage::InsetMinipage(BufferParams const & bp)
65 : InsetCollapsable(bp)
67 setLabel(_("minipage"));
68 LyXFont font(LyXFont::ALL_SANE);
71 font.setColor(LColor::collapsable);
74 setAutoCollapse(false);
76 inset.setFrameColor(LColor::blue);
77 setInsetName("Minipage");
81 InsetMinipage::InsetMinipage(InsetMinipage const & in)
82 : InsetCollapsable(in), params_(in.params_)
86 auto_ptr<InsetBase> InsetMinipage::clone() const
88 return auto_ptr<InsetBase>(new InsetMinipage(*this));
92 InsetMinipage::~InsetMinipage()
94 InsetMinipageMailer(*this).hideDialog();
99 InsetMinipage::priv_dispatch(FuncRequest const & cmd,
100 idx_type & idx, pos_type & pos)
102 switch (cmd.action) {
103 case LFUN_INSET_MODIFY: {
104 InsetMinipage::Params params;
105 InsetMinipageMailer::string2params(cmd.argument, params);
107 params_.pos = params.pos;
108 params_.width = params.width;
110 /* FIXME: I refuse to believe we have to live
111 * with ugliness like this ... */
112 inset.getLyXText(cmd.view())->fullRebreak();
113 cmd.view()->updateInset(this);
117 case LFUN_INSET_DIALOG_UPDATE:
118 InsetMinipageMailer(*this).updateDialog(cmd.view());
122 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
127 void InsetMinipage::Params::write(ostream & os) const
129 os << "Minipage" << '\n'
130 << "position " << pos << '\n'
131 << "inner_position " << inner_pos << '\n'
132 << "height \"" << height.asString() << "\"\n"
133 << "width \"" << width.asString() << "\"\n";
137 void InsetMinipage::Params::read(LyXLex & lex)
141 string const token = lex.getString();
142 if (token == "position") {
144 pos = static_cast<Position>(lex.getInteger());
146 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
148 // take countermeasures
149 lex.pushToken(token);
154 string const token = lex.getString();
155 if (token == "inner_position") {
157 inner_pos = static_cast<InnerPosition>(lex.getInteger());
159 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
161 // take countermeasures
162 lex.pushToken(token);
167 string const token = lex.getString();
168 if (token == "height") {
170 height = LyXLength(lex.getString());
172 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
174 // take countermeasures
175 lex.pushToken(token);
180 string const token = lex.getString();
181 if (token == "width") {
183 width = LyXLength(lex.getString());
185 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
187 // take countermeasures
188 lex.pushToken(token);
194 void InsetMinipage::write(Buffer const & buf, ostream & os) const
197 InsetCollapsable::write(buf, os);
201 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
204 InsetCollapsable::read(buf, lex);
208 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
213 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
214 InsetCollapsable::metrics(m, d);
215 switch (params_.pos) {
221 dim.asc = d.ascent() + d.descent() / 2;
231 dimension_collapsed(dim);
237 string const InsetMinipage::editMessage() const
239 return _("Opened Minipage Inset");
243 int InsetMinipage::latex(Buffer const & buf, ostream & os,
244 LatexRunParams const & runparams) const
247 switch (params_.pos) {
258 os << "\\begin{minipage}[" << s_pos << "]{"
259 << params_.width.asLatexString() << "}%\n";
261 int i = inset.latex(buf, os, runparams);
263 os << "\\end{minipage}%\n";
268 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
270 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
273 return InsetCollapsable::insetAllowed(code);
277 bool InsetMinipage::showInsetDialog(BufferView * bv) const
279 if (!inset.showInsetDialog(bv)) {
280 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
281 InsetMinipageMailer mailer(*tmp);
282 mailer.showDialog(bv);
289 int InsetMinipage::latexTextWidth(BufferView * bv) const
291 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
295 InsetMinipage::Params::Params()
297 inner_pos(inner_center),
298 width(100, LyXLength::PCW)
302 string const InsetMinipageMailer:: name_("minipage");
304 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
309 string const InsetMinipageMailer::inset2string(Buffer const &) const
311 return params2string(inset_.params());
315 void InsetMinipageMailer::string2params(string const & in,
316 InsetMinipage::Params & params)
318 params = InsetMinipage::Params();
323 istringstream data(in);
329 string const token = lex.getString();
330 if (token != "minipage")
334 // This is part of the inset proper that is usually swallowed
335 // by Buffer::readInset
338 string const token = lex.getString();
339 if (token != "Minipage")
350 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
353 data << name_ << ' ';