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