]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
Final touch 'inset display()'; fix 'is a bit silly' bug
[lyx.git] / src / insets / insetminipage.C
1 /**
2  * \file insetminipage.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "insetminipage.h"
15
16 #include "BufferView.h"
17 #include "debug.h"
18 #include "funcrequest.h"
19 #include "gettext.h"
20 #include "LColor.h"
21 #include "lyxlex.h"
22 #include "metricsinfo.h"
23 #include "paragraph.h"
24
25 #include "support/std_sstream.h"
26
27
28 using std::string;
29 using std::endl;
30 using std::auto_ptr;
31 using std::istringstream;
32 using std::ostream;
33 using std::ostringstream;
34
35
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.
41 //
42 // The minipage environment is defined like this:
43 //
44 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
45 //
46 // Where:
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
52 //                 of pos is used.
53 //     width = the width of the box
54 //
55 // In LyX we should try to support all these parameters, settable in a
56 // pop-up dialog.
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.
62 // (Lgb)
63
64 InsetMinipage::InsetMinipage(BufferParams const & bp)
65         : InsetCollapsable(bp)
66 {
67         setLabel(_("minipage"));
68         LyXFont font(LyXFont::ALL_SANE);
69         font.decSize();
70         font.decSize();
71         font.setColor(LColor::collapsable);
72         setLabelFont(font);
73 #if 0
74         setAutoCollapse(false);
75 #endif
76         inset.setFrameColor(LColor::blue);
77         setInsetName("Minipage");
78 }
79
80
81 InsetMinipage::InsetMinipage(InsetMinipage const & in)
82         : InsetCollapsable(in), params_(in.params_)
83 {}
84
85
86 auto_ptr<InsetBase> InsetMinipage::clone() const
87 {
88         return auto_ptr<InsetBase>(new InsetMinipage(*this));
89 }
90
91
92 InsetMinipage::~InsetMinipage()
93 {
94         InsetMinipageMailer(*this).hideDialog();
95 }
96
97
98 dispatch_result
99 InsetMinipage::priv_dispatch(FuncRequest const & cmd,
100                              idx_type & idx, pos_type & pos)
101 {
102         switch (cmd.action) {
103         case LFUN_INSET_MODIFY: {
104                 InsetMinipage::Params params;
105                 InsetMinipageMailer::string2params(cmd.argument, params);
106
107                 params_.pos   = params.pos;
108                 params_.width = params.width;
109
110                 /* FIXME: I refuse to believe we have to live
111                  * with ugliness like this ... */
112                 inset.getLyXText(cmd.view())->fullRebreak();
113                 cmd.view()->updateInset(this);
114                 return DISPATCHED;
115         }
116
117         case LFUN_INSET_DIALOG_UPDATE:
118                 InsetMinipageMailer(*this).updateDialog(cmd.view());
119                 return DISPATCHED;
120
121         default:
122                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
123         }
124 }
125
126
127 void InsetMinipage::Params::write(ostream & os) const
128 {
129         os << "Minipage" << '\n'
130            << "position " << pos << '\n'
131            << "inner_position " << inner_pos << '\n'
132            << "height \"" << height.asString() << "\"\n"
133            << "width \"" << width.asString() << "\"\n";
134 }
135
136
137 void InsetMinipage::Params::read(LyXLex & lex)
138 {
139         if (lex.isOK()) {
140                 lex.next();
141                 string const token = lex.getString();
142                 if (token == "position") {
143                         lex.next();
144                         pos = static_cast<Position>(lex.getInteger());
145                 } else {
146                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
147                                    << endl;
148                         // take countermeasures
149                         lex.pushToken(token);
150                 }
151         }
152         if (lex.isOK()) {
153                 lex.next();
154                 string const token = lex.getString();
155                 if (token == "inner_position") {
156                         lex.next();
157                         inner_pos = static_cast<InnerPosition>(lex.getInteger());
158                 } else {
159                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
160                                    << endl;
161                         // take countermeasures
162                         lex.pushToken(token);
163                 }
164         }
165         if (lex.isOK()) {
166                 lex.next();
167                 string const token = lex.getString();
168                 if (token == "height") {
169                         lex.next();
170                         height = LyXLength(lex.getString());
171                 } else {
172                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
173                                    << endl;
174                         // take countermeasures
175                         lex.pushToken(token);
176                 }
177         }
178         if (lex.isOK()) {
179                 lex.next();
180                 string const token = lex.getString();
181                 if (token == "width") {
182                         lex.next();
183                         width = LyXLength(lex.getString());
184                 } else {
185                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
186                                    << endl;
187                         // take countermeasures
188                         lex.pushToken(token);
189                 }
190         }
191 }
192
193
194 void InsetMinipage::write(Buffer const & buf, ostream & os) const
195 {
196         params_.write(os);
197         InsetCollapsable::write(buf, os);
198 }
199
200
201 void InsetMinipage::read(Buffer const & buf, LyXLex & lex)
202 {
203         params_.read(lex);
204         InsetCollapsable::read(buf, lex);
205 }
206
207
208 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
209 {
210         if (isOpen()) {
211                 Dimension d;
212                 MetricsInfo m = mi;
213                 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
214                 InsetCollapsable::metrics(m, d);
215                 switch (params_.pos) {
216                 case top:
217                         dim.asc = d.asc;
218                         dim.des = d.des;
219                         break;
220                 case center:
221                         dim.asc = d.ascent() + d.descent() / 2;
222                         dim.des = dim.asc;
223                         break;
224                 case bottom:
225                         dim.asc = d.des;
226                         dim.des = d.asc;
227                         break;
228                 }
229                 dim.wid = d.wid;
230         } else
231                 dimension_collapsed(dim);
232
233         dim_ = dim;
234 }
235
236
237 string const InsetMinipage::editMessage() const
238 {
239         return _("Opened Minipage Inset");
240 }
241
242
243 int InsetMinipage::latex(Buffer const & buf, ostream & os,
244                          LatexRunParams const & runparams) const
245 {
246         string s_pos;
247         switch (params_.pos) {
248         case top:
249                 s_pos += 't';
250                 break;
251         case center:
252                 s_pos += 'c';
253                 break;
254         case bottom:
255                 s_pos += 'b';
256                 break;
257         }
258         os << "\\begin{minipage}[" << s_pos << "]{"
259            << params_.width.asLatexString() << "}%\n";
260
261         int i = inset.latex(buf, os, runparams);
262
263         os << "\\end{minipage}%\n";
264         return i + 2;
265 }
266
267
268 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
269 {
270         if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
271                 return false;
272
273         return InsetCollapsable::insetAllowed(code);
274 }
275
276
277 bool InsetMinipage::showInsetDialog(BufferView * bv) const
278 {
279         if (!inset.showInsetDialog(bv)) {
280                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
281                 InsetMinipageMailer mailer(*tmp);
282                 mailer.showDialog(bv);
283         }
284
285         return true;
286 }
287
288
289 int InsetMinipage::latexTextWidth(BufferView * bv) const
290 {
291         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
292 }
293
294
295 InsetMinipage::Params::Params()
296         : pos(center),
297           inner_pos(inner_center),
298           width(100, LyXLength::PCW)
299 {}
300
301
302 string const InsetMinipageMailer:: name_("minipage");
303
304 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
305         : inset_(inset)
306 {}
307
308
309 string const InsetMinipageMailer::inset2string(Buffer const &) const
310 {
311         return params2string(inset_.params());
312 }
313
314
315 void InsetMinipageMailer::string2params(string const & in,
316                                         InsetMinipage::Params & params)
317 {
318         params = InsetMinipage::Params();
319
320         if (in.empty())
321                 return;
322
323         istringstream data(in);
324         LyXLex lex(0, 0);
325         lex.setStream(data);
326
327         if (lex.isOK()) {
328                 lex.next();
329                 string const token = lex.getString();
330                 if (token != "minipage")
331                         return;
332         }
333
334         // This is part of the inset proper that is usually swallowed
335         // by Buffer::readInset
336         if (lex.isOK()) {
337                 lex.next();
338                 string const token = lex.getString();
339                 if (token != "Minipage")
340                         return;
341         }
342
343         if (lex.isOK()) {
344                 params.read(lex);
345         }
346 }
347
348
349 string const
350 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
351 {
352         ostringstream data;
353         data << name_ << ' ';
354         params.write(data);
355         return data.str();
356 }