]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
10ac8dcb28a071e2bcf5a938ae92b9ce9c55ddc4
[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 &) const
178 {
179         InsetMinipage * result = new InsetMinipage;
180         result->inset.init(&inset);
181         
182         result->collapsed = collapsed;
183         result->pos_ = pos_;
184         result->inner_pos_ = inner_pos_;
185         result->height_ = height_;
186         result->width_ = width_;
187         return result;
188 }
189
190
191 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
192 {
193         if (collapsed)
194                 return ascent_collapsed(bv->painter(), font);
195         else {
196                 // Take placement into account.
197                 int i = 0;
198                 switch (pos_) {
199                 case top:
200                         i = InsetCollapsable::ascent(bv, font);
201                         break;
202                 case center:
203                         i = (InsetCollapsable::ascent(bv, font)
204                              + InsetCollapsable::descent(bv, font)) / 2;
205                         break;
206                 case bottom:
207                         i = InsetCollapsable::descent(bv, font);
208                         break;
209                 }
210                 return i;
211         }
212 }
213
214
215 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
216 {
217         if (collapsed)
218                 return descent_collapsed(bv->painter(), font);
219         else {
220                 // Take placement into account.
221                 int i = 0;
222                 switch (pos_) {
223                 case top:
224                         i = InsetCollapsable::descent(bv, font);
225                         break;
226                 case center:
227                         i = (InsetCollapsable::ascent(bv, font)
228                              + InsetCollapsable::descent(bv, font)) / 2;
229                         break;
230                 case bottom:
231                         i = InsetCollapsable::ascent(bv, font);
232                         break;
233                 }
234                 return i;
235         }
236 }
237
238
239 string const InsetMinipage::editMessage() const
240 {
241         return _("Opened Minipage Inset");
242 }
243
244
245 int InsetMinipage::latex(Buffer const * buf,
246                          ostream & os, bool fragile, bool fp) const
247 {
248         string s_pos;
249         switch (pos_) {
250         case top:
251                 s_pos += "t";
252                 break;
253         case center:
254                 s_pos += "c";
255                 break;
256         case bottom:
257                 s_pos += "b";
258                 break;
259         }
260         os << "\\begin{minipage}[" << s_pos << "]{"
261            << LyXLength(width_).asLatexString() << "}%\n";
262         
263         int i = inset.latex(buf, os, fragile, fp);
264
265         os << "\\end{minipage}%\n";
266         return i + 2;
267 }
268
269
270 bool InsetMinipage::insertInsetAllowed(Inset * in) const
271 {
272         if ((in->lyxCode() == Inset::FLOAT_CODE) ||
273             (in->lyxCode() == Inset::MARGIN_CODE)) {
274                 return false;
275         }
276         return true;
277 }
278
279
280 InsetMinipage::Position InsetMinipage::pos() const 
281 {
282         return pos_;
283 }
284
285
286 void InsetMinipage::pos(InsetMinipage::Position p)
287 {
288         pos_ = p;
289 }
290
291
292 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
293 {
294         return inner_pos_;
295 }
296
297
298 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
299 {
300         inner_pos_ = ip;
301 }
302
303
304 string const & InsetMinipage::height() const
305 {
306         return height_;
307 }
308
309
310 void InsetMinipage::height(string const & ll)
311 {
312         height_ = ll;
313 }
314
315
316 string const & InsetMinipage::width() const
317 {
318         return width_;
319 }
320
321
322 void InsetMinipage::width(string const & ll)
323 {
324         width_ = ll;
325 }
326
327
328 bool InsetMinipage::showInsetDialog(BufferView * bv) const
329 {
330         if (!inset.showInsetDialog(bv))
331         bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
332         return true;
333 }
334
335
336 void InsetMinipage::insetButtonRelease(BufferView * bv, int x, int y,
337                                        int button)
338 {
339         if (button == 3) {
340         showInsetDialog(bv);
341         return;
342         }
343         InsetCollapsable::insetButtonRelease(bv, x, y, button);
344 }
345
346
347 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
348         const
349 {
350         if (!width_.empty())
351         return VSpace(width_).inPixels(bv);
352         // this should not happen!
353         return InsetCollapsable::getMaxWidth(bv, inset);
354 }