]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
fix #832
[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 "funcrequest.h"
20 #include "gettext.h"
21 #include "lyxfont.h"
22 #include "lyxlex.h"
23 #include "lyxtext.h"
24 #include "Lsstream.h"
25
26 #include "frontends/LyXView.h"
27 #include "frontends/Dialogs.h"
28
29 #include "support/LOstream.h"
30 #include "support/lstrings.h"
31
32 using std::ostream;
33 using std::endl;
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
77 #if 0
78 #ifdef WITH_WARNINGS
79 #warning Remove this color definitions before 1.2.0 final!
80 #endif
81         // just for experimentation :)
82         setBackgroundColor(LColor::green);
83 #endif
84
85         inset.setFrameColor(0, LColor::blue);
86         setInsetName("Minipage");
87 }
88
89
90 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
91         : InsetCollapsable(in, same_id), params_(in.params_)
92 {}
93
94
95 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
96 {
97         return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
98 }
99
100
101 InsetMinipage::~InsetMinipage()
102 {
103         InsetMinipageMailer mailer(*this);
104         mailer.hideDialog();
105 }
106
107
108 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
109 {
110         switch (cmd.action) {
111         case LFUN_INSET_MODIFY: {
112                 InsetMinipage::Params params;
113                 InsetMinipageMailer::string2params(cmd.argument, params);
114
115                 params_.pos   = params.pos;
116                 params_.width = params.width;
117
118                 /* FIXME: I refuse to believe we have to live
119                  * with ugliness like this ... */
120                 LyXText * t = inset.getLyXText(cmd.view());
121                 t->need_break_row = t->rows().begin();
122                 t->fullRebreak();
123                 inset.update(cmd.view(), true);
124                 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
125                 cmd.view()->updateInset(this);
126                 return DISPATCHED;
127         }
128
129         case LFUN_INSET_DIALOG_UPDATE: 
130                 InsetMinipageMailer(*this).updateDialog(cmd.view());
131                 return DISPATCHED;
132
133         default:
134                 return InsetCollapsable::localDispatch(cmd);
135         }
136 }
137
138
139 void InsetMinipage::Params::write(ostream & os) const
140 {
141         os << "Minipage" << '\n'
142            << "position " << pos << '\n'
143            << "inner_position " << inner_pos << '\n'
144            << "height \"" << height.asString() << "\"\n"
145            << "width \"" << width.asString() << "\"\n";
146 }
147
148
149 void InsetMinipage::Params::read(LyXLex & lex)
150 {
151         if (lex.isOK()) {
152                 lex.next();
153                 string const token = lex.getString();
154                 if (token == "position") {
155                         lex.next();
156                         pos = static_cast<Position>(lex.getInteger());
157                 } else {
158                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
159                                    << endl;
160                         // take countermeasures
161                         lex.pushToken(token);
162                 }
163         }
164         if (lex.isOK()) {
165                 lex.next();
166                 string const token = lex.getString();
167                 if (token == "inner_position") {
168                         lex.next();
169                         inner_pos = static_cast<InnerPosition>(lex.getInteger());
170                 } else {
171                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
172                                    << endl;
173                         // take countermeasures
174                         lex.pushToken(token);
175                 }
176         }
177         if (lex.isOK()) {
178                 lex.next();
179                 string const token = lex.getString();
180                 if (token == "height") {
181                         lex.next();
182                         height = LyXLength(lex.getString());
183                 } else {
184                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
185                                    << endl;
186                         // take countermeasures
187                         lex.pushToken(token);
188                 }
189         }
190         if (lex.isOK()) {
191                 lex.next();
192                 string const token = lex.getString();
193                 if (token == "width") {
194                         lex.next();
195                         width = LyXLength(lex.getString());
196                 } else {
197                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
198                                    << endl;
199                         // take countermeasures
200                         lex.pushToken(token);
201                 }
202         }
203 }
204
205
206 void InsetMinipage::write(Buffer const * buf, ostream & os) const
207 {
208         params_.write(os);
209         InsetCollapsable::write(buf, os);
210 }
211
212
213 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
214 {
215         params_.read(lex);
216         InsetCollapsable::read(buf, lex);
217 }
218
219
220 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
221 {
222         if (collapsed_)
223                 return ascent_collapsed();
224         else {
225                 // Take placement into account.
226                 int i = 0;
227                 switch (params_.pos) {
228                 case top:
229                         i = InsetCollapsable::ascent(bv, font);
230                         break;
231                 case center:
232                         i = (InsetCollapsable::ascent(bv, font)
233                              + InsetCollapsable::descent(bv, font)) / 2;
234                         break;
235                 case bottom:
236                         i = InsetCollapsable::descent(bv, font);
237                         break;
238                 }
239                 return i;
240         }
241 }
242
243
244 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
245 {
246         if (collapsed_)
247                 return descent_collapsed();
248         else {
249                 // Take placement into account.
250                 int i = 0;
251                 switch (params_.pos) {
252                 case top:
253                         i = InsetCollapsable::descent(bv, font);
254                         break;
255                 case center:
256                         i = (InsetCollapsable::ascent(bv, font)
257                              + InsetCollapsable::descent(bv, font)) / 2;
258                         break;
259                 case bottom:
260                         i = InsetCollapsable::ascent(bv, font);
261                         break;
262                 }
263                 return i;
264         }
265 }
266
267
268 string const InsetMinipage::editMessage() const
269 {
270         return _("Opened Minipage Inset");
271 }
272
273
274 int InsetMinipage::latex(Buffer const * buf,
275                          ostream & os, bool fragile, bool fp) const
276 {
277         string s_pos;
278         switch (params_.pos) {
279         case top:
280                 s_pos += 't';
281                 break;
282         case center:
283                 s_pos += 'c';
284                 break;
285         case bottom:
286                 s_pos += 'b';
287                 break;
288         }
289         os << "\\begin{minipage}[" << s_pos << "]{"
290            << params_.width.asLatexString() << "}%\n";
291
292         int i = inset.latex(buf, os, fragile, fp);
293
294         os << "\\end{minipage}%\n";
295         return i + 2;
296 }
297
298
299 bool InsetMinipage::insetAllowed(Inset::Code code) const
300 {
301         if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
302                 return false;
303
304         return InsetCollapsable::insetAllowed(code);
305 }
306
307
308 bool InsetMinipage::showInsetDialog(BufferView * bv) const
309 {
310         if (!inset.showInsetDialog(bv)) {
311                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
312                 InsetMinipageMailer mailer(*tmp);
313                 mailer.showDialog(bv);
314         }
315
316         return true;
317 }
318
319
320 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
321         const
322 {
323         if (owner() &&
324             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
325                 return -1;
326         }
327         if (!params_.width.zero()) {
328                 int ww1 = latexTextWidth(bv);
329                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
330                 if (ww2 > 0 && ww2 < ww1) {
331                         return ww2;
332                 }
333                 return ww1;
334         }
335         // this should not happen!
336         return InsetCollapsable::getMaxWidth(bv, inset);
337 }
338
339
340 int InsetMinipage::latexTextWidth(BufferView * bv) const
341 {
342         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
343 }
344
345
346 InsetMinipage::Params::Params()
347         : pos(center),
348           inner_pos(inner_center),
349           width(100, LyXLength::PCW)
350 {}
351
352
353 string const InsetMinipageMailer:: name_("minipage");
354
355 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
356         : inset_(inset)
357 {}
358
359
360 string const InsetMinipageMailer::inset2string() const
361 {
362         return params2string(inset_.params());
363 }
364
365
366 void InsetMinipageMailer::string2params(string const & in,
367                                         InsetMinipage::Params & params)
368 {
369         params = InsetMinipage::Params();
370
371         if (in.empty())
372                 return;
373         
374         istringstream data(STRCONV(in));
375         LyXLex lex(0,0);
376         lex.setStream(data);
377
378         if (lex.isOK()) {
379                 lex.next();
380                 string const token = lex.getString();
381                 if (token != "minipage")
382                         return;
383         }
384
385         // This is part of the inset proper that is usually swallowed
386         // by Buffer::readInset
387         if (lex.isOK()) {
388                 lex.next();
389                 string const token = lex.getString();
390                 if (token != "Minipage")
391                         return;
392         }
393
394         if (lex.isOK()) {
395                 params.read(lex);
396         }
397 }
398
399
400 string const
401 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
402 {
403         ostringstream data;
404         data << name_ << ' ';
405         params.write(data);
406         return STRCONV(data.str());
407 }