]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
reformatting and remove using delc
[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
28 using std::ostream;
29 using std::endl;
30
31
32 // Some information about Minipages in LaTeX:
33 // A minipage is a complete miniversion of a page and can contain
34 // its own footnotes, paragraphs, and array, tabular, and multicols
35 // environments. However it cannot contain floats or \marginpar's,
36 // but it can appear inside floats.
37 //
38 // The minipage environment is defined like this:
39 //
40 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
41 //
42 // Where:
43 //     pos [opt] = is the vertical placement of the box with respect
44 //                 to the text baseline, [c], [t] and [b].
45 //     height [opt] = the height of the box
46 //     inner-pos [opt] = the position of the text within the box.
47 //                 It can be t, c, b or s, if unspecified the value
48 //                 of pos is used.
49 //     width = the width of the box
50 //
51 // In LyX we should try to support all these parameters, settable in a
52 // pop-up dialog.
53 // In this pop-up diallog it should also be possible to set all margin
54 // values that is usable in the minipage.
55 // With regard to different formats (like DocBook) I guess a minipage
56 // can be used there also. Perhaps not in the latex way, but we do not
57 // have to output "" for minipages.
58 // (Lgb)
59
60 InsetMinipage::InsetMinipage()
61         : InsetCollapsable(), pos_(center),
62           inner_pos_(inner_center)
63 {
64         setLabel(_("minipage"));
65         LyXFont font(LyXFont::ALL_SANE);
66         font.decSize();
67         font.decSize();
68         font.setColor(LColor::footnote);
69         setLabelFont(font);
70         setAutoCollapse(false);
71         setInsetName("Minipage");
72         width_ = "100%"; // set default to 100% of column_width
73 }
74
75
76 InsetMinipage::~InsetMinipage()
77 {
78         hideDialog();
79 }
80
81
82 void InsetMinipage::Write(Buffer const * buf, ostream & os) const 
83 {
84         os << getInsetName() << "\n"
85            << "position " << pos_ << "\n"
86            << "inner_position " << inner_pos_ << "\n"
87            << "height \"" << height_ << "\"\n"
88            << "width \"" << width_ << "\"\n";
89         InsetCollapsable::Write(buf, os);
90 }
91
92
93 void InsetMinipage::Read(Buffer const * buf, LyXLex & lex)
94 {
95     string token;
96
97     if (lex.IsOK()) {
98         lex.next();
99         token = lex.GetString();
100         if (token == "position") {
101             lex.next();
102             pos_ = static_cast<Position>(lex.GetInteger());
103             token = string();
104         } else {
105                 lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
106                        << endl;
107         }
108     }
109     if (lex.IsOK()) {
110         if (token.empty()) {
111             lex.next();
112             token = lex.GetString();
113         }
114         if (token == "inner_position") {
115             lex.next();
116             inner_pos_ = static_cast<InnerPosition>(lex.GetInteger());
117             token = string();
118         } else {
119                 lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
120                        << endl;
121         }
122     }
123     if (lex.IsOK()) {
124         if (token.empty()) {
125             lex.next();
126             token = lex.GetString();
127         }
128         if (token == "height") {
129             lex.next();
130             height_ = lex.GetString();
131             token = string();
132         } else {
133                 lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
134                        << endl;
135         }
136     }
137     if (lex.IsOK()) {
138         if (token.empty()) {
139             lex.next();
140             token = lex.GetString();
141         }
142         if (token == "width") {
143             lex.next();
144             width_ = lex.GetString();
145             token = string();
146         } else {
147                 lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
148                        << endl;
149         }
150     }
151 #warning Remove me before final 1.2.0 (Jug)
152     // this is only for compatibility to the intermediate format and should
153     // vanish till the final 1.2.0!
154     if (lex.IsOK()) {
155         if (token.empty()) {
156             lex.next();
157             token = lex.GetString();
158         }
159         if (token == "widthp") {
160             lex.next();
161             // only do this if the width_-string was not already set!
162             if (width_.empty())
163                 width_ = lex.GetString() + "%";
164             token = string();
165         } else {
166                 lyxerr << "InsetMinipage::Read: Missing 'widthp_'-tag!"
167                        << endl;
168         }
169     }
170     if (!token.empty())
171         lex.pushToken(token);
172     InsetCollapsable::Read(buf, lex);
173 }
174
175
176 Inset * InsetMinipage::Clone(Buffer const &) const
177 {
178         InsetMinipage * result = new InsetMinipage;
179         result->inset.init(&inset);
180         
181         result->collapsed = collapsed;
182         result->pos_ = pos_;
183         result->inner_pos_ = inner_pos_;
184         result->height_ = height_;
185         result->width_ = width_;
186         return result;
187 }
188
189
190 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
191 {
192         if (collapsed)
193                 return ascent_collapsed(bv->painter(), font);
194         else {
195                 // Take placement into account.
196                 int i = 0;
197                 switch (pos_) {
198                 case top:
199                         i = InsetCollapsable::ascent(bv, font);
200                         break;
201                 case center:
202                         i = (InsetCollapsable::ascent(bv, font)
203                              + InsetCollapsable::descent(bv, font)) / 2;
204                         break;
205                 case bottom:
206                         i = InsetCollapsable::descent(bv, font);
207                         break;
208                 }
209                 return i;
210         }
211 }
212
213
214 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
215 {
216         if (collapsed)
217                 return descent_collapsed(bv->painter(), font);
218         else {
219                 // Take placement into account.
220                 int i = 0;
221                 switch (pos_) {
222                 case top:
223                         i = InsetCollapsable::descent(bv, font);
224                         break;
225                 case center:
226                         i = (InsetCollapsable::ascent(bv, font)
227                              + InsetCollapsable::descent(bv, font)) / 2;
228                         break;
229                 case bottom:
230                         i = InsetCollapsable::ascent(bv, font);
231                         break;
232                 }
233                 return i;
234         }
235 }
236
237
238 string const InsetMinipage::EditMessage() const
239 {
240         return _("Opened Minipage Inset");
241 }
242
243
244 int InsetMinipage::Latex(Buffer const * buf,
245                          ostream & os, bool fragile, bool fp) const
246 {
247         string s_pos;
248         switch (pos_) {
249         case top:
250                 s_pos += "t";
251                 break;
252         case center:
253                 s_pos += "c";
254                 break;
255         case bottom:
256                 s_pos += "b";
257                 break;
258         }
259         os << "\\begin{minipage}[" << s_pos << "]{"
260            << LyXLength(width_).asLatexString() << "}%\n";
261         
262         int i = inset.Latex(buf, os, fragile, fp);
263
264         os << "\\end{minipage}%\n";
265         return i + 2;
266 }
267
268
269 bool InsetMinipage::InsertInsetAllowed(Inset * in) const
270 {
271         if ((in->LyxCode() == Inset::FLOAT_CODE) ||
272             (in->LyxCode() == Inset::MARGIN_CODE)) {
273                 return false;
274         }
275         return true;
276 }
277
278
279 InsetMinipage::Position InsetMinipage::pos() const 
280 {
281         return pos_;
282 }
283
284
285 void InsetMinipage::pos(InsetMinipage::Position p)
286 {
287         pos_ = p;
288 }
289
290
291 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
292 {
293         return inner_pos_;
294 }
295
296
297 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
298 {
299         inner_pos_ = ip;
300 }
301
302
303 string const & InsetMinipage::height() const
304 {
305         return height_;
306 }
307
308
309 void InsetMinipage::height(string const & ll)
310 {
311         height_ = ll;
312 }
313
314
315 string const & InsetMinipage::width() const
316 {
317         return width_;
318 }
319
320
321 void InsetMinipage::width(string const & ll)
322 {
323         width_ = ll;
324 }
325
326
327 bool InsetMinipage::ShowInsetDialog(BufferView * bv) const
328 {
329     if (!inset.ShowInsetDialog(bv))
330         bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
331     return true;
332 }
333
334
335 void InsetMinipage::InsetButtonRelease(BufferView * bv, int x, int y,
336                                        int button)
337 {
338     if (button == 3) {
339         ShowInsetDialog(bv);
340         return;
341     }
342     InsetCollapsable::InsetButtonRelease(bv, x, y, button);
343 }
344
345
346 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
347     const
348 {
349     if (!width_.empty())
350         return VSpace(width_).inPixels(bv);
351     // this should not happen!
352     return InsetCollapsable::getMaxWidth(bv, inset);
353 }