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();
98 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
100 switch (cmd.action) {
101 case LFUN_INSET_MODIFY: {
102 InsetMinipage::Params params;
103 InsetMinipageMailer::string2params(cmd.argument, params);
105 params_.pos = params.pos;
106 params_.width = params.width;
108 /* FIXME: I refuse to believe we have to live
109 * with ugliness like this ... */
110 inset.getLyXText(cmd.view())->fullRebreak();
111 cmd.view()->updateInset(this);
115 case LFUN_INSET_DIALOG_UPDATE:
116 InsetMinipageMailer(*this).updateDialog(cmd.view());
120 return InsetCollapsable::localDispatch(cmd);
125 void InsetMinipage::Params::write(ostream & os) const
127 os << "Minipage" << '\n'
128 << "position " << pos << '\n'
129 << "inner_position " << inner_pos << '\n'
130 << "height \"" << height.asString() << "\"\n"
131 << "width \"" << width.asString() << "\"\n";
135 void InsetMinipage::Params::read(LyXLex & lex)
139 string const token = lex.getString();
140 if (token == "position") {
142 pos = static_cast<Position>(lex.getInteger());
144 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
146 // take countermeasures
147 lex.pushToken(token);
152 string const token = lex.getString();
153 if (token == "inner_position") {
155 inner_pos = static_cast<InnerPosition>(lex.getInteger());
157 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
159 // take countermeasures
160 lex.pushToken(token);
165 string const token = lex.getString();
166 if (token == "height") {
168 height = LyXLength(lex.getString());
170 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
172 // take countermeasures
173 lex.pushToken(token);
178 string const token = lex.getString();
179 if (token == "width") {
181 width = LyXLength(lex.getString());
183 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
185 // take countermeasures
186 lex.pushToken(token);
192 void InsetMinipage::write(Buffer const & buf, ostream & os) const
195 InsetCollapsable::write(buf, os);
199 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
202 InsetCollapsable::read(buf, lex);
206 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
211 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
212 InsetCollapsable::metrics(m, d);
213 switch (params_.pos) {
219 dim.asc = d.ascent() + d.descent() / 2;
229 dimension_collapsed(dim);
235 string const InsetMinipage::editMessage() const
237 return _("Opened Minipage Inset");
241 int InsetMinipage::latex(Buffer const & buf, ostream & os,
242 LatexRunParams const & runparams) const
245 switch (params_.pos) {
256 os << "\\begin{minipage}[" << s_pos << "]{"
257 << params_.width.asLatexString() << "}%\n";
259 int i = inset.latex(buf, os, runparams);
261 os << "\\end{minipage}%\n";
266 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
268 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
271 return InsetCollapsable::insetAllowed(code);
275 bool InsetMinipage::showInsetDialog(BufferView * bv) const
277 if (!inset.showInsetDialog(bv)) {
278 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
279 InsetMinipageMailer mailer(*tmp);
280 mailer.showDialog(bv);
287 int InsetMinipage::latexTextWidth(BufferView * bv) const
289 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
293 InsetMinipage::Params::Params()
295 inner_pos(inner_center),
296 width(100, LyXLength::PCW)
300 string const InsetMinipageMailer:: name_("minipage");
302 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
307 string const InsetMinipageMailer::inset2string(Buffer const &) const
309 return params2string(inset_.params());
313 void InsetMinipageMailer::string2params(string const & in,
314 InsetMinipage::Params & params)
316 params = InsetMinipage::Params();
321 istringstream data(in);
327 string const token = lex.getString();
328 if (token != "minipage")
332 // This is part of the inset proper that is usually swallowed
333 // by Buffer::readInset
336 string const token = lex.getString();
337 if (token != "Minipage")
348 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
351 data << name_ << ' ';