]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
c827af9d8c0087272f82a4bc9a2c46ecc847850b
[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         setInsetName("Minipage");
73         width_ = "100%"; // set default to 100% of column_width
74 }
75
76
77 InsetMinipage::~InsetMinipage()
78 {
79         hideDialog();
80 }
81
82
83 void InsetMinipage::write(Buffer const * buf, ostream & os) const 
84 {
85         os << getInsetName() << "\n"
86            << "position " << pos_ << "\n"
87            << "inner_position " << inner_pos_ << "\n"
88            << "height \"" << height_ << "\"\n"
89            << "width \"" << width_ << "\"\n";
90         InsetCollapsable::write(buf, os);
91 }
92
93
94 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
95 {
96         string token;
97
98         if (lex.IsOK()) {
99                 lex.next();
100                 token = lex.GetString();
101                 if (token == "position") {
102                         lex.next();
103                         pos_ = static_cast<Position>(lex.GetInteger());
104                         token = string();
105                 } else {
106                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
107                                    << endl;
108                 }
109         }
110         if (lex.IsOK()) {
111                 if (token.empty()) {
112                         lex.next();
113                         token = lex.GetString();
114                 }
115                 if (token == "inner_position") {
116                         lex.next();
117                         inner_pos_ = static_cast<InnerPosition>(lex.GetInteger());
118                         token = string();
119                 } else {
120                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
121                                    << endl;
122                 }
123         }
124         if (lex.IsOK()) {
125                 if (token.empty()) {
126                         lex.next();
127                         token = lex.GetString();
128                 }
129                 if (token == "height") {
130                         lex.next();
131                         height_ = lex.GetString();
132                         token = string();
133                 } else {
134                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
135                                    << endl;
136                 }
137         }
138         if (lex.IsOK()) {
139                 if (token.empty()) {
140                         lex.next();
141                         token = lex.GetString();
142                 }
143                 if (token == "width") {
144                         lex.next();
145                         width_ = lex.GetString();
146                         token = string();
147                 } else {
148                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
149                                    << endl;
150                 }
151         }
152 #ifdef WITH_WARNINGS
153 #warning Remove me before final 1.2.0 (Jug)
154 #warning Can we please remove this as soon as possible? (Lgb)
155 #endif
156         // this is only for compatibility to the intermediate format and should
157         // vanish till the final 1.2.0!
158         if (lex.IsOK()) {
159                 if (token.empty()) {
160                         lex.next();
161                         token = lex.GetString();
162                 }
163                 if (token == "widthp") {
164                         lex.next();
165                         // only do this if the width_-string was not already set!
166                         if (width_.empty())
167                                 width_ = lex.GetString() + "%";
168                         token = string();
169                 }
170         }
171         if (!token.empty())
172                 lex.pushToken(token);
173         InsetCollapsable::read(buf, lex);
174 }
175
176
177 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
178 {
179         InsetMinipage * result = new InsetMinipage;
180         result->inset.init(&inset, same_id);
181         
182         result->collapsed_ = collapsed_;
183         result->pos_ = pos_;
184         result->inner_pos_ = inner_pos_;
185         result->height_ = height_;
186         result->width_ = width_;
187         if (same_id)
188                 result->id_ = id_;
189         return result;
190 }
191
192
193 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
194 {
195         if (collapsed_)
196                 return ascent_collapsed(bv->painter(), font);
197         else {
198                 // Take placement into account.
199                 int i = 0;
200                 switch (pos_) {
201                 case top:
202                         i = InsetCollapsable::ascent(bv, font);
203                         break;
204                 case center:
205                         i = (InsetCollapsable::ascent(bv, font)
206                              + InsetCollapsable::descent(bv, font)) / 2;
207                         break;
208                 case bottom:
209                         i = InsetCollapsable::descent(bv, font);
210                         break;
211                 }
212                 return i;
213         }
214 }
215
216
217 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
218 {
219         if (collapsed_)
220                 return descent_collapsed(bv->painter(), font);
221         else {
222                 // Take placement into account.
223                 int i = 0;
224                 switch (pos_) {
225                 case top:
226                         i = InsetCollapsable::descent(bv, font);
227                         break;
228                 case center:
229                         i = (InsetCollapsable::ascent(bv, font)
230                              + InsetCollapsable::descent(bv, font)) / 2;
231                         break;
232                 case bottom:
233                         i = InsetCollapsable::ascent(bv, font);
234                         break;
235                 }
236                 return i;
237         }
238 }
239
240
241 string const InsetMinipage::editMessage() const
242 {
243         return _("Opened Minipage Inset");
244 }
245
246
247 int InsetMinipage::latex(Buffer const * buf,
248                          ostream & os, bool fragile, bool fp) const
249 {
250         string s_pos;
251         switch (pos_) {
252         case top:
253                 s_pos += "t";
254                 break;
255         case center:
256                 s_pos += "c";
257                 break;
258         case bottom:
259                 s_pos += "b";
260                 break;
261         }
262         os << "\\begin{minipage}[" << s_pos << "]{"
263            << LyXLength(width_).asLatexString() << "}%\n";
264         
265         int i = inset.latex(buf, os, fragile, fp);
266
267         os << "\\end{minipage}%\n";
268         return i + 2;
269 }
270
271
272 bool InsetMinipage::insetAllowed(Inset::Code code) const
273 {
274         if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
275                 return false;
276
277         return InsetCollapsable::insetAllowed(code);
278 }
279
280
281 InsetMinipage::Position InsetMinipage::pos() const 
282 {
283         return pos_;
284 }
285
286
287 void InsetMinipage::pos(InsetMinipage::Position p)
288 {
289         pos_ = p;
290 }
291
292
293 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
294 {
295         return inner_pos_;
296 }
297
298
299 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
300 {
301         inner_pos_ = ip;
302 }
303
304
305 string const & InsetMinipage::height() const
306 {
307         return height_;
308 }
309
310
311 void InsetMinipage::height(string const & ll)
312 {
313         height_ = ll;
314 }
315
316
317 string const & InsetMinipage::width() const
318 {
319         return width_;
320 }
321
322
323 void InsetMinipage::width(string const & ll)
324 {
325         width_ = ll;
326 }
327
328
329 bool InsetMinipage::showInsetDialog(BufferView * bv) const
330 {
331         if (!inset.showInsetDialog(bv))
332                 bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
333         return true;
334 }
335
336
337 void InsetMinipage::insetButtonRelease(BufferView * bv, int x, int y,
338                                        int button)
339 {
340         if (button == 3) {
341                 showInsetDialog(bv);
342                 return;
343         }
344         InsetCollapsable::insetButtonRelease(bv, x, y, button);
345 }
346
347
348 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
349         const
350 {
351         if (!width_.empty())
352                 return VSpace(width_).inPixels(bv);
353         // this should not happen!
354         return InsetCollapsable::getMaxWidth(bv, inset);
355 }