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