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