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