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