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 "dispatchresult.h"
19 #include "funcrequest.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
26 #include "support/std_sstream.h"
32 using std::istringstream;
34 using std::ostringstream;
37 // Some information about Minipages in LaTeX:
38 // A minipage is a complete miniversion of a page and can contain
39 // its own footnotes, paragraphs, and array, tabular, and multicols
40 // environments. However it cannot contain floats or \marginpar's,
41 // but it can appear inside floats.
43 // The minipage environment is defined like this:
45 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
48 // pos [opt] = is the vertical placement of the box with respect
49 // to the text baseline, [c], [t] and [b].
50 // height [opt] = the height of the box
51 // inner-pos [opt] = the position of the text within the box.
52 // It can be t, c, b or s, if unspecified the value
54 // width = the width of the box
56 // In LyX we should try to support all these parameters, settable in a
58 // In this pop-up diallog it should also be possible to set all margin
59 // values that is usable in the minipage.
60 // With regard to different formats (like DocBook) I guess a minipage
61 // can be used there also. Perhaps not in the latex way, but we do not
62 // have to output "" for minipages.
65 InsetMinipage::InsetMinipage(BufferParams const & bp)
66 : InsetCollapsable(bp)
68 setLabel(_("minipage"));
69 LyXFont font(LyXFont::ALL_SANE);
72 font.setColor(LColor::collapsable);
75 setAutoCollapse(false);
77 inset.setFrameColor(LColor::blue);
78 setInsetName("Minipage");
82 InsetMinipage::InsetMinipage(InsetMinipage const & in)
83 : InsetCollapsable(in), params_(in.params_)
87 auto_ptr<InsetBase> InsetMinipage::clone() const
89 return auto_ptr<InsetBase>(new InsetMinipage(*this));
93 InsetMinipage::~InsetMinipage()
95 InsetMinipageMailer(*this).hideDialog();
100 InsetMinipage::priv_dispatch(FuncRequest const & cmd,
101 idx_type & idx, pos_type & pos)
103 switch (cmd.action) {
104 case LFUN_INSET_MODIFY: {
105 InsetMinipage::Params params;
106 InsetMinipageMailer::string2params(cmd.argument, params);
107 params_.pos = params.pos;
108 params_.width = params.width;
109 cmd.view()->update();
110 return DispatchResult(true, true);
113 case LFUN_INSET_DIALOG_UPDATE:
114 InsetMinipageMailer(*this).updateDialog(cmd.view());
115 return DispatchResult(true, true);
118 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
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 OutputParams 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(*tmp).showDialog(bv);
284 InsetMinipage::Params::Params()
285 : pos(center), inner_pos(inner_center), width(100, LyXLength::PCW)
289 string const InsetMinipageMailer:: name_("minipage");
291 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
296 string const InsetMinipageMailer::inset2string(Buffer const &) const
298 return params2string(inset_.params());
302 void InsetMinipageMailer::string2params(string const & in,
303 InsetMinipage::Params & params)
305 params = InsetMinipage::Params();
309 istringstream data(in);
315 if (!lex || name != name_)
316 return print_mailer_error("InsetMinipageMailer", in, 1, name_);
318 // This is part of the inset proper that is usually swallowed
319 // by LyXText::readInset
322 if (!lex || id != "Minipage")
323 return print_mailer_error("InsetBoxMailer", in, 2, "Minipage");
330 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
333 data << name_ << ' ';