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