]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
255085306b486b4a0d88c5af31371a45f9cc181a
[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 string const InsetMinipage::EditMessage() const
181 {
182         return _("Opened Minipage Inset");
183 }
184
185
186 int InsetMinipage::Latex(Buffer const * buf,
187                          ostream & os, bool fragile, bool fp) const
188 {
189         string s_pos;
190         switch (pos_) {
191         case top:
192                 s_pos += "t";
193                 break;
194         case center:
195                 s_pos += "c";
196                 break;
197         case bottom:
198                 s_pos += "b";
199                 break;
200         }
201         
202         if (width_.empty()) {
203             os << "\\begin{minipage}[" << s_pos << "]{."
204                << widthp_ << "\\columnwidth}%\n";
205         } else {
206             os << "\\begin{minipage}[" << s_pos << "]{"
207                << width_ << "}%\n";
208         }
209         
210         int i = inset->Latex(buf, os, fragile, fp);
211         os << "\\end{minipage}%\n";
212         
213         return i + 2;
214 }
215
216
217 bool InsetMinipage::InsertInsetAllowed(Inset * in) const
218 {
219         if ((in->LyxCode() == Inset::FLOAT_CODE) ||
220             (in->LyxCode() == Inset::MARGIN_CODE)) {
221                 return false;
222         }
223         return true;
224 }
225
226
227 InsetMinipage::Position InsetMinipage::pos() const 
228 {
229         return pos_;
230 }
231
232
233 void InsetMinipage::pos(InsetMinipage::Position p)
234 {
235         pos_ = p;
236 }
237
238
239 InsetMinipage::InnerPosition InsetMinipage::innerPos() const
240 {
241         return inner_pos_;
242 }
243
244
245 void InsetMinipage::innerPos(InsetMinipage::InnerPosition ip)
246 {
247         inner_pos_ = ip;
248 }
249
250
251 string const & InsetMinipage::height() const
252 {
253         return height_;
254 }
255
256
257 void InsetMinipage::height(string const & ll)
258 {
259         height_ = ll;
260 }
261
262
263 string const & InsetMinipage::width() const
264 {
265         return width_;
266 }
267
268
269 void InsetMinipage::width(string const & ll)
270 {
271         width_ = ll;
272 }
273
274 int InsetMinipage::widthp() const
275 {
276         return widthp_;
277 }
278
279
280 void InsetMinipage::widthp(int ll)
281 {
282         widthp_ = ll;
283 }
284
285
286 void InsetMinipage::widthp(string const & ll)
287 {
288         widthp_ = strToInt(ll);
289 }
290
291
292 void InsetMinipage::InsetButtonRelease(BufferView * bv, int x, int y,
293                                        int button)
294 {
295     if (button == 3) {
296 #if 0
297 // we have to check first if we have a locking inset and if this locking inset
298 // has a popup menu with the 3rd button
299         if (the_locking_inset) {
300             UpdatableInset * i;
301             if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
302                 i->InsetButtonRelease(bv, x, y, button);
303                 return;
304             }
305         }
306 #endif
307         bv->owner()->getDialogs()->showMinipage(this);
308         return;
309     }
310     InsetCollapsable::InsetButtonRelease(bv, x, y, button);
311 }
312
313 int InsetMinipage::getMaxWidth(Painter & pain, UpdatableInset const * inset)
314     const
315 {
316     if (!width_.empty())
317         return VSpace(width_).inPixels(0, 0);
318     return InsetCollapsable::getMaxWidth(pain, inset) / 100 * widthp_;
319 }