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"
27 using std::istringstream;
29 using std::ostringstream;
32 // Some information about Minipages in LaTeX:
33 // A minipage is a complete miniversion of a page and can contain
34 // its own footnotes, paragraphs, and array, tabular, and multicols
35 // environments. However it cannot contain floats or \marginpar's,
36 // but it can appear inside floats.
38 // The minipage environment is defined like this:
40 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
43 // pos [opt] = is the vertical placement of the box with respect
44 // to the text baseline, [c], [t] and [b].
45 // height [opt] = the height of the box
46 // inner-pos [opt] = the position of the text within the box.
47 // It can be t, c, b or s, if unspecified the value
49 // width = the width of the box
51 // In LyX we should try to support all these parameters, settable in a
53 // In this pop-up diallog it should also be possible to set all margin
54 // values that is usable in the minipage.
55 // With regard to different formats (like DocBook) I guess a minipage
56 // can be used there also. Perhaps not in the latex way, but we do not
57 // have to output "" for minipages.
60 InsetMinipage::InsetMinipage(BufferParams const & bp)
61 : InsetCollapsable(bp)
63 setLabel(_("minipage"));
64 LyXFont font(LyXFont::ALL_SANE);
67 font.setColor(LColor::collapsable);
70 setAutoCollapse(false);
72 inset.setFrameColor(LColor::blue);
73 setInsetName("Minipage");
77 InsetMinipage::InsetMinipage(InsetMinipage const & in)
78 : InsetCollapsable(in), params_(in.params_)
82 auto_ptr<InsetBase> InsetMinipage::clone() const
84 return auto_ptr<InsetBase>(new InsetMinipage(*this));
88 InsetMinipage::~InsetMinipage()
90 InsetMinipageMailer(*this).hideDialog();
94 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
97 case LFUN_INSET_MODIFY: {
98 InsetMinipage::Params params;
99 InsetMinipageMailer::string2params(cmd.argument, params);
101 params_.pos = params.pos;
102 params_.width = params.width;
104 /* FIXME: I refuse to believe we have to live
105 * with ugliness like this ... */
106 inset.getLyXText(cmd.view())->fullRebreak();
107 cmd.view()->updateInset(this);
111 case LFUN_INSET_DIALOG_UPDATE:
112 InsetMinipageMailer(*this).updateDialog(cmd.view());
116 return InsetCollapsable::localDispatch(cmd);
121 void InsetMinipage::Params::write(ostream & os) const
123 os << "Minipage" << '\n'
124 << "position " << pos << '\n'
125 << "inner_position " << inner_pos << '\n'
126 << "height \"" << height.asString() << "\"\n"
127 << "width \"" << width.asString() << "\"\n";
131 void InsetMinipage::Params::read(LyXLex & lex)
135 string const token = lex.getString();
136 if (token == "position") {
138 pos = static_cast<Position>(lex.getInteger());
140 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
142 // take countermeasures
143 lex.pushToken(token);
148 string const token = lex.getString();
149 if (token == "inner_position") {
151 inner_pos = static_cast<InnerPosition>(lex.getInteger());
153 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
155 // take countermeasures
156 lex.pushToken(token);
161 string const token = lex.getString();
162 if (token == "height") {
164 height = LyXLength(lex.getString());
166 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
168 // take countermeasures
169 lex.pushToken(token);
174 string const token = lex.getString();
175 if (token == "width") {
177 width = LyXLength(lex.getString());
179 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
181 // take countermeasures
182 lex.pushToken(token);
188 void InsetMinipage::write(Buffer const & buf, ostream & os) const
191 InsetCollapsable::write(buf, os);
195 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
198 InsetCollapsable::read(buf, lex);
202 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
205 dimension_collapsed(dim);
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;
231 string const InsetMinipage::editMessage() const
233 return _("Opened Minipage Inset");
237 int InsetMinipage::latex(Buffer const & buf, ostream & os,
238 LatexRunParams const & runparams) const
241 switch (params_.pos) {
252 os << "\\begin{minipage}[" << s_pos << "]{"
253 << params_.width.asLatexString() << "}%\n";
255 int i = inset.latex(buf, os, runparams);
257 os << "\\end{minipage}%\n";
262 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
264 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
267 return InsetCollapsable::insetAllowed(code);
271 bool InsetMinipage::showInsetDialog(BufferView * bv) const
273 if (!inset.showInsetDialog(bv)) {
274 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
275 InsetMinipageMailer mailer(*tmp);
276 mailer.showDialog(bv);
283 int InsetMinipage::latexTextWidth(BufferView * bv) const
285 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
289 InsetMinipage::Params::Params()
291 inner_pos(inner_center),
292 width(100, LyXLength::PCW)
296 string const InsetMinipageMailer:: name_("minipage");
298 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
303 string const InsetMinipageMailer::inset2string(Buffer const &) const
305 return params2string(inset_.params());
309 void InsetMinipageMailer::string2params(string const & in,
310 InsetMinipage::Params & params)
312 params = InsetMinipage::Params();
317 istringstream data(STRCONV(in));
323 string const token = lex.getString();
324 if (token != "minipage")
328 // This is part of the inset proper that is usually swallowed
329 // by Buffer::readInset
332 string const token = lex.getString();
333 if (token != "Minipage")
344 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
347 data << name_ << ' ';
349 return STRCONV(data.str());