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