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"
22 #include "paragraph.h"
24 #include "support/std_sstream.h"
28 using std::istringstream;
30 using std::ostringstream;
33 // Some information about Minipages in LaTeX:
34 // A minipage is a complete miniversion of a page and can contain
35 // its own footnotes, paragraphs, and array, tabular, and multicols
36 // environments. However it cannot contain floats or \marginpar's,
37 // but it can appear inside floats.
39 // The minipage environment is defined like this:
41 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
44 // pos [opt] = is the vertical placement of the box with respect
45 // to the text baseline, [c], [t] and [b].
46 // height [opt] = the height of the box
47 // inner-pos [opt] = the position of the text within the box.
48 // It can be t, c, b or s, if unspecified the value
50 // width = the width of the box
52 // In LyX we should try to support all these parameters, settable in a
54 // In this pop-up diallog it should also be possible to set all margin
55 // values that is usable in the minipage.
56 // With regard to different formats (like DocBook) I guess a minipage
57 // can be used there also. Perhaps not in the latex way, but we do not
58 // have to output "" for minipages.
61 InsetMinipage::InsetMinipage(BufferParams const & bp)
62 : InsetCollapsable(bp)
64 setLabel(_("minipage"));
65 LyXFont font(LyXFont::ALL_SANE);
68 font.setColor(LColor::collapsable);
71 setAutoCollapse(false);
73 inset.setFrameColor(LColor::blue);
74 setInsetName("Minipage");
78 InsetMinipage::InsetMinipage(InsetMinipage const & in)
79 : InsetCollapsable(in), params_(in.params_)
83 auto_ptr<InsetBase> InsetMinipage::clone() const
85 return auto_ptr<InsetBase>(new InsetMinipage(*this));
89 InsetMinipage::~InsetMinipage()
91 InsetMinipageMailer(*this).hideDialog();
95 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
98 case LFUN_INSET_MODIFY: {
99 InsetMinipage::Params params;
100 InsetMinipageMailer::string2params(cmd.argument, params);
102 params_.pos = params.pos;
103 params_.width = params.width;
105 /* FIXME: I refuse to believe we have to live
106 * with ugliness like this ... */
107 inset.getLyXText(cmd.view())->fullRebreak();
108 cmd.view()->updateInset(this);
112 case LFUN_INSET_DIALOG_UPDATE:
113 InsetMinipageMailer(*this).updateDialog(cmd.view());
117 return InsetCollapsable::localDispatch(cmd);
122 void InsetMinipage::Params::write(ostream & os) const
124 os << "Minipage" << '\n'
125 << "position " << pos << '\n'
126 << "inner_position " << inner_pos << '\n'
127 << "height \"" << height.asString() << "\"\n"
128 << "width \"" << width.asString() << "\"\n";
132 void InsetMinipage::Params::read(LyXLex & lex)
136 string const token = lex.getString();
137 if (token == "position") {
139 pos = static_cast<Position>(lex.getInteger());
141 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
143 // take countermeasures
144 lex.pushToken(token);
149 string const token = lex.getString();
150 if (token == "inner_position") {
152 inner_pos = static_cast<InnerPosition>(lex.getInteger());
154 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
156 // take countermeasures
157 lex.pushToken(token);
162 string const token = lex.getString();
163 if (token == "height") {
165 height = LyXLength(lex.getString());
167 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
169 // take countermeasures
170 lex.pushToken(token);
175 string const token = lex.getString();
176 if (token == "width") {
178 width = LyXLength(lex.getString());
180 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
182 // take countermeasures
183 lex.pushToken(token);
189 void InsetMinipage::write(Buffer const & buf, ostream & os) const
192 InsetCollapsable::write(buf, os);
196 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
199 InsetCollapsable::read(buf, lex);
203 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
206 dimension_collapsed(dim);
210 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
211 InsetCollapsable::metrics(m, d);
212 switch (params_.pos) {
218 dim.asc = d.ascent() + d.descent() / 2;
232 string const InsetMinipage::editMessage() const
234 return _("Opened Minipage Inset");
238 int InsetMinipage::latex(Buffer const & buf, ostream & os,
239 LatexRunParams const & runparams) const
242 switch (params_.pos) {
253 os << "\\begin{minipage}[" << s_pos << "]{"
254 << params_.width.asLatexString() << "}%\n";
256 int i = inset.latex(buf, os, runparams);
258 os << "\\end{minipage}%\n";
263 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
265 if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
268 return InsetCollapsable::insetAllowed(code);
272 bool InsetMinipage::showInsetDialog(BufferView * bv) const
274 if (!inset.showInsetDialog(bv)) {
275 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
276 InsetMinipageMailer mailer(*tmp);
277 mailer.showDialog(bv);
284 int InsetMinipage::latexTextWidth(BufferView * bv) const
286 return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
290 InsetMinipage::Params::Params()
292 inner_pos(inner_center),
293 width(100, LyXLength::PCW)
297 string const InsetMinipageMailer:: name_("minipage");
299 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
304 string const InsetMinipageMailer::inset2string(Buffer const &) const
306 return params2string(inset_.params());
310 void InsetMinipageMailer::string2params(string const & in,
311 InsetMinipage::Params & params)
313 params = InsetMinipage::Params();
318 istringstream data(STRCONV(in));
324 string const token = lex.getString();
325 if (token != "minipage")
329 // This is part of the inset proper that is usually swallowed
330 // by Buffer::readInset
333 string const token = lex.getString();
334 if (token != "Minipage")
345 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
348 data << name_ << ' ';
350 return STRCONV(data.str());