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