]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
Added copy constructor to inset.h and used it in most insets which permit
[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::collapsable);
70         setLabelFont(font);
71         setAutoCollapse(false);
72         // just for experimentation :)
73         setBackgroundColor(LColor::red);
74         setInsetName("Minipage");
75         width_ = "100%"; // set default to 100% of column_width
76 }
77
78
79 InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
80         : InsetCollapsable(in, same_id),
81           pos_(in.pos_), inner_pos_(in.inner_pos_),
82           height_(in.height_), width_(in.width_)
83 {}
84
85
86 Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
87 {
88         return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
89 }
90
91
92 InsetMinipage::~InsetMinipage()
93 {
94         hideDialog();
95 }
96
97
98 void InsetMinipage::write(Buffer const * buf, ostream & os) const 
99 {
100         os << getInsetName() << "\n"
101            << "position " << pos_ << "\n"
102            << "inner_position " << inner_pos_ << "\n"
103            << "height \"" << height_ << "\"\n"
104            << "width \"" << width_ << "\"\n";
105         InsetCollapsable::write(buf, os);
106 }
107
108
109 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
110 {
111         string token;
112
113         if (lex.IsOK()) {
114                 lex.next();
115                 token = lex.GetString();
116                 if (token == "position") {
117                         lex.next();
118                         pos_ = static_cast<Position>(lex.GetInteger());
119                         token = string();
120                 } else {
121                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
122                                    << endl;
123                 }
124         }
125         if (lex.IsOK()) {
126                 if (token.empty()) {
127                         lex.next();
128                         token = lex.GetString();
129                 }
130                 if (token == "inner_position") {
131                         lex.next();
132                         inner_pos_ = static_cast<InnerPosition>(lex.GetInteger());
133                         token = string();
134                 } else {
135                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
136                                    << endl;
137                 }
138         }
139         if (lex.IsOK()) {
140                 if (token.empty()) {
141                         lex.next();
142                         token = lex.GetString();
143                 }
144                 if (token == "height") {
145                         lex.next();
146                         height_ = lex.GetString();
147                         token = string();
148                 } else {
149                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
150                                    << endl;
151                 }
152         }
153         if (lex.IsOK()) {
154                 if (token.empty()) {
155                         lex.next();
156                         token = lex.GetString();
157                 }
158                 if (token == "width") {
159                         lex.next();
160                         width_ = lex.GetString();
161                         token = string();
162                 } else {
163                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
164                                    << endl;
165                 }
166         }
167         if (!token.empty())
168                 lex.pushToken(token);
169         InsetCollapsable::read(buf, lex);
170 }
171
172
173 int InsetMinipage::ascent(BufferView * bv, LyXFont const & font) const
174 {
175         if (collapsed_)
176                 return ascent_collapsed(bv->painter());
177         else {
178                 // Take placement into account.
179                 int i = 0;
180                 switch (pos_) {
181                 case top:
182                         i = InsetCollapsable::ascent(bv, font);
183                         break;
184                 case center:
185                         i = (InsetCollapsable::ascent(bv, font)
186                              + InsetCollapsable::descent(bv, font)) / 2;
187                         break;
188                 case bottom:
189                         i = InsetCollapsable::descent(bv, font);
190                         break;
191                 }
192                 return i;
193         }
194 }
195
196
197 int InsetMinipage::descent(BufferView * bv, LyXFont const & font) const
198 {
199         if (collapsed_)
200                 return descent_collapsed(bv->painter());
201         else {
202                 // Take placement into account.
203                 int i = 0;
204                 switch (pos_) {
205                 case top:
206                         i = InsetCollapsable::descent(bv, font);
207                         break;
208                 case center:
209                         i = (InsetCollapsable::ascent(bv, font)
210                              + InsetCollapsable::descent(bv, font)) / 2;
211                         break;
212                 case bottom:
213                         i = InsetCollapsable::ascent(bv, font);
214                         break;
215                 }
216                 return i;
217         }
218 }
219
220
221 string const InsetMinipage::editMessage() const
222 {
223         return _("Opened Minipage Inset");
224 }
225
226
227 int InsetMinipage::latex(Buffer const * buf,
228                          ostream & os, bool fragile, bool fp) const
229 {
230         string s_pos;
231         switch (pos_) {
232         case top:
233                 s_pos += "t";
234                 break;
235         case center:
236                 s_pos += "c";
237                 break;
238         case bottom:
239                 s_pos += "b";
240                 break;
241         }
242         os << "\\begin{minipage}[" << s_pos << "]{"
243            << LyXLength(width_).asLatexString() << "}%\n";
244         
245         int i = inset.latex(buf, os, fragile, fp);
246
247         os << "\\end{minipage}%\n";
248         return i + 2;
249 }
250
251
252 bool InsetMinipage::insetAllowed(Inset::Code code) const
253 {
254         if ((code == Inset::FLOAT_CODE) || (code == Inset::MARGIN_CODE))
255                 return false;
256
257         return InsetCollapsable::insetAllowed(code);
258 }
259
260
261 InsetMinipage::Position InsetMinipage::pos() const 
262 {
263         return pos_;
264 }
265
266
267 void InsetMinipage::pos(InsetMinipage::Position p)
268 {
269         pos_ = p;
270 }
271
272
273 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
274 {
275         return inner_pos_;
276 }
277
278
279 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
280 {
281         inner_pos_ = ip;
282 }
283
284
285 string const & InsetMinipage::height() const
286 {
287         return height_;
288 }
289
290
291 void InsetMinipage::height(string const & ll)
292 {
293         height_ = ll;
294 }
295
296
297 string const & InsetMinipage::width() const
298 {
299         return width_;
300 }
301
302
303 void InsetMinipage::width(string const & ll)
304 {
305         width_ = ll;
306 }
307
308
309 bool InsetMinipage::showInsetDialog(BufferView * bv) const
310 {
311         if (!inset.showInsetDialog(bv))
312                 bv->owner()->getDialogs()->showMinipage(const_cast<InsetMinipage *>(this));
313         return true;
314 }
315
316
317 void InsetMinipage::insetButtonRelease(BufferView * bv, int x, int y,
318                                        int button)
319 {
320         if (button == 3) {
321                 showInsetDialog(bv);
322                 return;
323         }
324         InsetCollapsable::insetButtonRelease(bv, x, y, button);
325 }
326
327
328 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
329         const
330 {
331         if (!width_.empty())
332                 return VSpace(width_).inPixels(bv);
333         // this should not happen!
334         return InsetCollapsable::getMaxWidth(bv, inset);
335 }