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