]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
prevent crash when inserting minipage in table cell,
[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 #include "insettext.h"
16
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "dimension.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "Lsstream.h"
23 #include "lyxfont.h"
24 #include "lyxlex.h"
25 #include "lyxtext.h"
26 #include "Lsstream.h"
27 #include "metricsinfo.h"
28
29 #include "frontends/LyXView.h"
30 #include "frontends/Dialogs.h"
31
32 #include "support/LOstream.h"
33
34 using std::ostream;
35 using std::endl;
36 using std::auto_ptr;
37
38
39 // Some information about Minipages in LaTeX:
40 // A minipage is a complete miniversion of a page and can contain
41 // its own footnotes, paragraphs, and array, tabular, and multicols
42 // environments. However it cannot contain floats or \marginpar's,
43 // but it can appear inside floats.
44 //
45 // The minipage environment is defined like this:
46 //
47 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
48 //
49 // Where:
50 //     pos [opt] = is the vertical placement of the box with respect
51 //                 to the text baseline, [c], [t] and [b].
52 //     height [opt] = the height of the box
53 //     inner-pos [opt] = the position of the text within the box.
54 //                 It can be t, c, b or s, if unspecified the value
55 //                 of pos is used.
56 //     width = the width of the box
57 //
58 // In LyX we should try to support all these parameters, settable in a
59 // pop-up dialog.
60 // In this pop-up diallog it should also be possible to set all margin
61 // values that is usable in the minipage.
62 // With regard to different formats (like DocBook) I guess a minipage
63 // can be used there also. Perhaps not in the latex way, but we do not
64 // have to output "" for minipages.
65 // (Lgb)
66
67 InsetMinipage::InsetMinipage(BufferParams const & bp)
68         : InsetCollapsable(bp)
69 {
70         setLabel(_("minipage"));
71         LyXFont font(LyXFont::ALL_SANE);
72         font.decSize();
73         font.decSize();
74         font.setColor(LColor::collapsable);
75         setLabelFont(font);
76 #if 0
77         setAutoCollapse(false);
78 #endif
79
80 #if 0
81 #ifdef WITH_WARNINGS
82 #warning Remove this color definitions before 1.2.0 final!
83 #endif
84         // just for experimentation :)
85         setBackgroundColor(LColor::green);
86 #endif
87
88         inset.setFrameColor(0, LColor::blue);
89         setInsetName("Minipage");
90 }
91
92
93 InsetMinipage::InsetMinipage(InsetMinipage const & in)
94         : InsetCollapsable(in), params_(in.params_)
95 {}
96
97
98 auto_ptr<InsetBase> InsetMinipage::clone() const
99 {
100         return auto_ptr<InsetBase>(new InsetMinipage(*this));
101 }
102
103
104 InsetMinipage::~InsetMinipage()
105 {
106         InsetMinipageMailer mailer(*this);
107         mailer.hideDialog();
108 }
109
110
111 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
112 {
113         switch (cmd.action) {
114         case LFUN_INSET_MODIFY: {
115                 InsetMinipage::Params params;
116                 InsetMinipageMailer::string2params(cmd.argument, params);
117
118                 params_.pos   = params.pos;
119                 params_.width = params.width;
120
121                 /* FIXME: I refuse to believe we have to live
122                  * with ugliness like this ... */
123                 inset.getLyXText(cmd.view())->fullRebreak();
124                 cmd.view()->updateInset(this);
125                 return DISPATCHED;
126         }
127
128         case LFUN_INSET_DIALOG_UPDATE:
129                 InsetMinipageMailer(*this).updateDialog(cmd.view());
130                 return DISPATCHED;
131
132         default:
133                 return InsetCollapsable::localDispatch(cmd);
134         }
135 }
136
137
138 void InsetMinipage::Params::write(ostream & os) const
139 {
140         os << "Minipage" << '\n'
141            << "position " << pos << '\n'
142            << "inner_position " << inner_pos << '\n'
143            << "height \"" << height.asString() << "\"\n"
144            << "width \"" << width.asString() << "\"\n";
145 }
146
147
148 void InsetMinipage::Params::read(LyXLex & lex)
149 {
150         if (lex.isOK()) {
151                 lex.next();
152                 string const token = lex.getString();
153                 if (token == "position") {
154                         lex.next();
155                         pos = static_cast<Position>(lex.getInteger());
156                 } else {
157                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
158                                    << endl;
159                         // take countermeasures
160                         lex.pushToken(token);
161                 }
162         }
163         if (lex.isOK()) {
164                 lex.next();
165                 string const token = lex.getString();
166                 if (token == "inner_position") {
167                         lex.next();
168                         inner_pos = static_cast<InnerPosition>(lex.getInteger());
169                 } else {
170                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
171                                    << endl;
172                         // take countermeasures
173                         lex.pushToken(token);
174                 }
175         }
176         if (lex.isOK()) {
177                 lex.next();
178                 string const token = lex.getString();
179                 if (token == "height") {
180                         lex.next();
181                         height = LyXLength(lex.getString());
182                 } else {
183                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
184                                    << endl;
185                         // take countermeasures
186                         lex.pushToken(token);
187                 }
188         }
189         if (lex.isOK()) {
190                 lex.next();
191                 string const token = lex.getString();
192                 if (token == "width") {
193                         lex.next();
194                         width = LyXLength(lex.getString());
195                 } else {
196                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
197                                    << endl;
198                         // take countermeasures
199                         lex.pushToken(token);
200                 }
201         }
202 }
203
204
205 void InsetMinipage::write(Buffer const * buf, ostream & os) const
206 {
207         params_.write(os);
208         InsetCollapsable::write(buf, os);
209 }
210
211
212 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
213 {
214         params_.read(lex);
215         InsetCollapsable::read(buf, lex);
216 }
217
218
219 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
220 {
221         if (collapsed_)
222                 dimension_collapsed(dim);
223         else {
224                 Dimension d;
225                 MetricsInfo m = mi;
226                 m.base.textwidth = params_.width.inPixels(mi.base.textwidth);
227                 InsetCollapsable::metrics(m, d);
228                 switch (params_.pos) {
229                 case top:
230                         dim.asc = d.asc;
231                         dim.des = d.des;
232                         break;
233                 case center:
234                         dim.asc = d.ascent() + d.descent() / 2;
235                         dim.des = dim.asc;
236                         break;
237                 case bottom:
238                         dim.asc = d.des;
239                         dim.des = d.asc;
240                         break;
241                 }
242                 dim.wid = d.wid;
243         }
244         dim_ = dim;
245 }
246
247
248 string const InsetMinipage::editMessage() const
249 {
250         return _("Opened Minipage Inset");
251 }
252
253
254 int InsetMinipage::latex(Buffer const * buf, ostream & os,
255                          LatexRunParams const & runparams) const
256 {
257         string s_pos;
258         switch (params_.pos) {
259         case top:
260                 s_pos += 't';
261                 break;
262         case center:
263                 s_pos += 'c';
264                 break;
265         case bottom:
266                 s_pos += 'b';
267                 break;
268         }
269         os << "\\begin{minipage}[" << s_pos << "]{"
270            << params_.width.asLatexString() << "}%\n";
271
272         int i = inset.latex(buf, os, runparams);
273
274         os << "\\end{minipage}%\n";
275         return i + 2;
276 }
277
278
279 bool InsetMinipage::insetAllowed(InsetOld::Code code) const
280 {
281         if (code == InsetOld::FLOAT_CODE || code == InsetOld::MARGIN_CODE)
282                 return false;
283
284         return InsetCollapsable::insetAllowed(code);
285 }
286
287
288 bool InsetMinipage::showInsetDialog(BufferView * bv) const
289 {
290         if (!inset.showInsetDialog(bv)) {
291                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
292                 InsetMinipageMailer mailer(*tmp);
293                 mailer.showDialog(bv);
294         }
295
296         return true;
297 }
298
299
300 int InsetMinipage::latexTextWidth(BufferView * bv) const
301 {
302         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
303 }
304
305
306 InsetMinipage::Params::Params()
307         : pos(center),
308           inner_pos(inner_center),
309           width(100, LyXLength::PCW)
310 {}
311
312
313 string const InsetMinipageMailer:: name_("minipage");
314
315 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
316         : inset_(inset)
317 {}
318
319
320 string const InsetMinipageMailer::inset2string(Buffer const &) const
321 {
322         return params2string(inset_.params());
323 }
324
325
326 void InsetMinipageMailer::string2params(string const & in,
327                                         InsetMinipage::Params & params)
328 {
329         params = InsetMinipage::Params();
330
331         if (in.empty())
332                 return;
333
334         istringstream data(STRCONV(in));
335         LyXLex lex(0, 0);
336         lex.setStream(data);
337
338         if (lex.isOK()) {
339                 lex.next();
340                 string const token = lex.getString();
341                 if (token != "minipage")
342                         return;
343         }
344
345         // This is part of the inset proper that is usually swallowed
346         // by Buffer::readInset
347         if (lex.isOK()) {
348                 lex.next();
349                 string const token = lex.getString();
350                 if (token != "Minipage")
351                         return;
352         }
353
354         if (lex.isOK()) {
355                 params.read(lex);
356         }
357 }
358
359
360 string const
361 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
362 {
363         ostringstream data;
364         data << name_ << ' ';
365         params.write(data);
366         return STRCONV(data.str());
367 }