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"
29 using std::istringstream;
31 using std::ostringstream;
34 // Some information about Minipages in LaTeX:
35 // A minipage is a complete miniversion of a page and can contain
36 // its own footnotes, paragraphs, and array, tabular, and multicols
37 // environments. However it cannot contain floats or \marginpar's,
38 // but it can appear inside floats.
40 // The minipage environment is defined like this:
42 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
45 // pos [opt] = is the vertical placement of the box with respect
46 // to the text baseline, [c], [t] and [b].
47 // height [opt] = the height of the box
48 // inner-pos [opt] = the position of the text within the box.
49 // It can be t, c, b or s, if unspecified the value
51 // width = the width of the box
53 // In LyX we should try to support all these parameters, settable in a
55 // In this pop-up diallog it should also be possible to set all margin
56 // values that is usable in the minipage.
57 // With regard to different formats (like DocBook) I guess a minipage
58 // can be used there also. Perhaps not in the latex way, but we do not
59 // have to output "" for minipages.
62 InsetMinipage::InsetMinipage(BufferParams const & bp)
63 : InsetCollapsable(bp)
65 setLabel(_("minipage"));
66 LyXFont font(LyXFont::ALL_SANE);
69 font.setColor(LColor::collapsable);
72 setAutoCollapse(false);
74 inset.setFrameColor(LColor::blue);
75 setInsetName("Minipage");
79 InsetMinipage::InsetMinipage(InsetMinipage const & in)
80 : InsetCollapsable(in), params_(in.params_)
84 auto_ptr<InsetBase> InsetMinipage::clone() const
86 return auto_ptr<InsetBase>(new InsetMinipage(*this));
90 InsetMinipage::~InsetMinipage()
92 InsetMinipageMailer(*this).hideDialog();
96 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
99 case LFUN_INSET_MODIFY: {
100 InsetMinipage::Params params;
101 InsetMinipageMailer::string2params(cmd.argument, params);
103 params_.pos = params.pos;
104 params_.width = params.width;
106 /* FIXME: I refuse to believe we have to live
107 * with ugliness like this ... */
108 inset.getLyXText(cmd.view())->fullRebreak();
109 cmd.view()->updateInset(this);
113 case LFUN_INSET_DIALOG_UPDATE:
114 InsetMinipageMailer(*this).updateDialog(cmd.view());
118 return InsetCollapsable::localDispatch(cmd);
123 void InsetMinipage::Params::write(ostream & os) const
125 os << "Minipage" << '\n'
126 << "position " << pos << '\n'
127 << "inner_position " << inner_pos << '\n'
128 << "height \"" << height.asString() << "\"\n"
129 << "width \"" << width.asString() << "\"\n";
133 void InsetMinipage::Params::read(LyXLex & lex)
137 string const token = lex.getString();
138 if (token == "position") {
140 pos = static_cast<Position>(lex.getInteger());
142 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
144 // take countermeasures
145 lex.pushToken(token);
150 string const token = lex.getString();
151 if (token == "inner_position") {
153 inner_pos = static_cast<InnerPosition>(lex.getInteger());
155 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
157 // take countermeasures
158 lex.pushToken(token);
163 string const token = lex.getString();
164 if (token == "height") {
166 height = LyXLength(lex.getString());
168 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
170 // take countermeasures
171 lex.pushToken(token);
176 string const token = lex.getString();
177 if (token == "width") {
179 width = LyXLength(lex.getString());
181 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
183 // take countermeasures
184 lex.pushToken(token);
190 void InsetMinipage::write(Buffer const & buf, ostream & os) const
193 InsetCollapsable::write(buf, os);
197 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
200 InsetCollapsable::read(buf, lex);
204 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
209 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
210 InsetCollapsable::metrics(m, d);
211 switch (params_.pos) {
217 dim.asc = d.ascent() + d.descent() / 2;
227 dimension_collapsed(dim);
233 string const InsetMinipage::editMessage() const
235 return _("Opened Minipage Inset");
239 int InsetMinipage::latex(Buffer const & buf, ostream & os,
240 LatexRunParams const & runparams) const
243 switch (params_.pos) {
254 os << "\\begin{minipage}[" << s_pos << "]{"
255 << params_.width.asLatexString() << "}%\n";
257 int i = inset.latex(buf, os, runparams);
259 os << "\\end{minipage}%\n";
264 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
266 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
269 return InsetCollapsable::insetAllowed(code);
273 bool InsetMinipage::showInsetDialog(BufferView * bv) const
275 if (!inset.showInsetDialog(bv)) {
276 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
277 InsetMinipageMailer mailer(*tmp);
278 mailer.showDialog(bv);
285 int InsetMinipage::latexTextWidth(BufferView * bv) const
287 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
291 InsetMinipage::Params::Params()
293 inner_pos(inner_center),
294 width(100, LyXLength::PCW)
298 string const InsetMinipageMailer:: name_("minipage");
300 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
305 string const InsetMinipageMailer::inset2string(Buffer const &) const
307 return params2string(inset_.params());
311 void InsetMinipageMailer::string2params(string const & in,
312 InsetMinipage::Params & params)
314 params = InsetMinipage::Params();
319 istringstream data(in);
325 string const token = lex.getString();
326 if (token != "minipage")
330 // This is part of the inset proper that is usually swallowed
331 // by Buffer::readInset
334 string const token = lex.getString();
335 if (token != "Minipage")
346 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
349 data << name_ << ' ';