]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
fix bug 175 (minipage in minipage); fix some warnings; apply external.diff from Herbert
[lyx.git] / src / insets / insetminipage.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1998 The LyX Team.
7  *
8  *======================================================*/
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "insetminipage.h"
17 #include "gettext.h"
18 #include "lyxfont.h"
19 #include "BufferView.h"
20 #include "LyXView.h"
21 #include "frontends/Dialogs.h"
22 #include "lyxtext.h"
23 #include "insets/insettext.h"
24 #include "support/LOstream.h"
25 #include "support/lstrings.h"
26 #include "debug.h"
27 #include "gettext.h"
28 #include "lyxlex.h"
29
30 using std::ostream;
31 using std::endl;
32
33
34 // Some information about Minipages in LaTeX:
35 // A minipage is a complete miniversion of a page and can contain
36 // its own footnotes, paragraphs, and array, tabular, and multicols
37 // environments. However it cannot contain floats or \marginpar's,
38 // but it can appear inside floats.
39 //
40 // The minipage environment is defined like this:
41 //
42 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
43 //
44 // Where:
45 //     pos [opt] = is the vertical placement of the box with respect
46 //                 to the text baseline, [c], [t] and [b].
47 //     height [opt] = the height of the box
48 //     inner-pos [opt] = the position of the text within the box.
49 //                 It can be t, c, b or s, if unspecified the value
50 //                 of pos is used.
51 //     width = the width of the box
52 //
53 // In LyX we should try to support all these parameters, settable in a
54 // pop-up dialog.
55 // In this pop-up diallog it should also be possible to set all margin
56 // values that is usable in the minipage.
57 // With regard to different formats (like DocBook) I guess a minipage
58 // can be used there also. Perhaps not in the latex way, but we do not
59 // have to output "" for minipages.
60 // (Lgb)
61
62 InsetMinipage::InsetMinipage()
63         : InsetCollapsable(), pos_(center),
64           inner_pos_(inner_center), width_(100, LyXLength::PW)
65 {
66         setLabel(_("minipage"));
67         LyXFont font(LyXFont::ALL_SANE);
68         font.decSize();
69         font.decSize();
70         font.setColor(LColor::collapsable);
71         setLabelFont(font);
72 #if 0
73         setAutoCollapse(false);
74 #endif
75 #ifdef WITH_WARNINGS
76 #warning Remove this color definitions before 1.2.0 final!
77 #endif
78         // just for experimentation :)
79         setBackgroundColor(LColor::green);
80         inset.setFrameColor(0, LColor::blue);
81         setInsetName("Minipage");
82 }
83
84
85 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
86         : InsetCollapsable(in, same_id),
87           pos_(in.pos_), inner_pos_(in.inner_pos_),
88           height_(in.height_), width_(in.width_)
89 {}
90
91
92 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
93 {
94         return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
95 }
96
97
98 InsetMinipage::~InsetMinipage()
99 {
100         hideDialog();
101 }
102
103
104 void InsetMinipage::write(Buffer const * buf, ostream & os) const 
105 {
106         os << getInsetName() << "\n"
107            << "position " << pos_ << "\n"
108            << "inner_position " << inner_pos_ << "\n"
109            << "height \"" << height_.asString() << "\"\n"
110            << "width \"" << width_.asString() << "\"\n";
111         InsetCollapsable::write(buf, os);
112 }
113
114
115 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
116 {
117         if (lex.isOK()) {
118                 lex.next();
119                 string const token = lex.getString();
120                 if (token == "position") {
121                         lex.next();
122                         pos_ = static_cast<Position>(lex.getInteger());
123                 } else {
124                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
125                                    << endl;
126                         // take countermeasures
127                         lex.pushToken(token);
128                 }
129         }
130         if (lex.isOK()) {
131                 lex.next();
132                 string const token = lex.getString();
133                 if (token == "inner_position") {
134                         lex.next();
135                         inner_pos_ = static_cast<InnerPosition>(lex.getInteger());
136                 } else {
137                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
138                                    << endl;
139                         // take countermeasures
140                         lex.pushToken(token);
141                 }
142         }
143         if (lex.isOK()) {
144                 lex.next();
145                 string const token = lex.getString();
146                 if (token == "height") {
147                         lex.next();
148                         height_ = LyXLength(lex.getString());
149                 } else {
150                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
151                                    << endl;
152                         // take countermeasures
153                         lex.pushToken(token);
154                 }
155         }
156         if (lex.isOK()) {
157                 lex.next();
158                 string const token = lex.getString();
159                 if (token == "width") {
160                         lex.next();
161                         width_ = LyXLength(lex.getString());
162                 } else {
163                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
164                                    << endl;
165                         // take countermeasures
166                         lex.pushToken(token);
167                 }
168         }
169         InsetCollapsable::read(buf, lex);
170 }
171
172
173 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
174 {
175         if (collapsed_)
176                 return ascent_collapsed();
177         else {
178                 // Take placement into account.
179                 int i = 0;
180                 switch (pos_) {
181                 case top:
182                         i = InsetCollapsable::ascent(bv, font);
183                         break;
184                 case center:
185                         i = (InsetCollapsable::ascent(bv, font)
186                              + InsetCollapsable::descent(bv, font)) / 2;
187                         break;
188                 case bottom:
189                         i = InsetCollapsable::descent(bv, font);
190                         break;
191                 }
192                 return i;
193         }
194 }
195
196
197 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
198 {
199         if (collapsed_)
200                 return descent_collapsed();
201         else {
202                 // Take placement into account.
203                 int i = 0;
204                 switch (pos_) {
205                 case top:
206                         i = InsetCollapsable::descent(bv, font);
207                         break;
208                 case center:
209                         i = (InsetCollapsable::ascent(bv, font)
210                              + InsetCollapsable::descent(bv, font)) / 2;
211                         break;
212                 case bottom:
213                         i = InsetCollapsable::ascent(bv, font);
214                         break;
215                 }
216                 return i;
217         }
218 }
219
220
221 string const InsetMinipage::editMessage() const
222 {
223         return _("Opened Minipage Inset");
224 }
225
226
227 int InsetMinipage::latex(Buffer const * buf,
228                          ostream & os, bool fragile, bool fp) const
229 {
230         string s_pos;
231         switch (pos_) {
232         case top:
233                 s_pos += "t";
234                 break;
235         case center:
236                 s_pos += "c";
237                 break;
238         case bottom:
239                 s_pos += "b";
240                 break;
241         }
242         os << "\\begin{minipage}[" << s_pos << "]{"
243            << width_.asLatexString() << "}%\n";
244         
245         int i = inset.latex(buf, os, fragile, fp);
246
247         os << "\\end{minipage}%\n";
248         return i + 2;
249 }
250
251
252 bool InsetMinipage::insetAllowed(Inset::Code code) const
253 {
254         if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
255                 return false;
256
257         return InsetCollapsable::insetAllowed(code);
258 }
259
260
261 InsetMinipage::Position InsetMinipage::pos() const 
262 {
263         return pos_;
264 }
265
266
267 void InsetMinipage::pos(InsetMinipage::Position p)
268 {
269         if (pos_ != p) {
270                 pos_ = p;
271                 need_update = FULL;
272         }
273 }
274
275
276 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
277 {
278         return inner_pos_;
279 }
280
281
282 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
283 {
284         inner_pos_ = ip;
285 }
286
287
288 LyXLength const & InsetMinipage::pageHeight() const
289 {
290         return height_;
291 }
292
293
294 void InsetMinipage::pageHeight(LyXLength const & ll)
295 {
296         if (height_ != ll) {
297                 height_ = ll;
298                 need_update = FULL;
299         }
300 }
301
302
303 LyXLength const & InsetMinipage::pageWidth() const
304 {
305         return width_;
306 }
307
308
309 void InsetMinipage::pageWidth(LyXLength const & ll)
310 {
311         if (ll != width_) {
312                 width_ = ll;
313                 need_update = FULL;
314         }
315 }
316
317
318 bool InsetMinipage::showInsetDialog(BufferView * bv) const
319 {
320         if (!inset.showInsetDialog(bv))
321                 bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
322         return true;
323 }
324
325
326 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
327         const
328 {
329         if (owner() &&
330             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
331                 return -1;
332         }
333         if (!width_.zero()) {
334                 int ww1 = latexTextWidth(bv);
335                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
336                 if (ww2 > 0 && ww2 < ww1) {
337                         return ww2;
338                 }
339                 return ww1;
340         }
341         // this should not happen!
342         return InsetCollapsable::getMaxWidth(bv, inset);
343 }
344
345
346 int InsetMinipage::latexTextWidth(BufferView * bv) const
347 {
348         return width_.inPixels(InsetCollapsable::latexTextWidth(bv),
349                                bv->text->defaultHeight());
350 }