]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
rename the members of Dimension
[lyx.git] / src / insets / insetminipage.C
1 /**
2  * \file insetminipage.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include "insetminipage.h"
15 #include "insettext.h"
16
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "dimension.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "Lsstream.h"
23 #include "lyxfont.h"
24 #include "lyxlex.h"
25 #include "lyxtext.h"
26 #include "Lsstream.h"
27
28 #include "frontends/LyXView.h"
29 #include "frontends/Dialogs.h"
30
31 #include "support/LOstream.h"
32
33 using std::ostream;
34 using std::endl;
35
36
37 // Some information about Minipages in LaTeX:
38 // A minipage is a complete miniversion of a page and can contain
39 // its own footnotes, paragraphs, and array, tabular, and multicols
40 // environments. However it cannot contain floats or \marginpar's,
41 // but it can appear inside floats.
42 //
43 // The minipage environment is defined like this:
44 //
45 // \begin{minipage}[pos][height][inner-pos]{width} <text> \end{minipage}
46 //
47 // Where:
48 //     pos [opt] = is the vertical placement of the box with respect
49 //                 to the text baseline, [c], [t] and [b].
50 //     height [opt] = the height of the box
51 //     inner-pos [opt] = the position of the text within the box.
52 //                 It can be t, c, b or s, if unspecified the value
53 //                 of pos is used.
54 //     width = the width of the box
55 //
56 // In LyX we should try to support all these parameters, settable in a
57 // pop-up dialog.
58 // In this pop-up diallog it should also be possible to set all margin
59 // values that is usable in the minipage.
60 // With regard to different formats (like DocBook) I guess a minipage
61 // can be used there also. Perhaps not in the latex way, but we do not
62 // have to output "" for minipages.
63 // (Lgb)
64
65 InsetMinipage::InsetMinipage(BufferParams const & bp)
66         : InsetCollapsable(bp)
67 {
68         setLabel(_("minipage"));
69         LyXFont font(LyXFont::ALL_SANE);
70         font.decSize();
71         font.decSize();
72         font.setColor(LColor::collapsable);
73         setLabelFont(font);
74 #if 0
75         setAutoCollapse(false);
76 #endif
77
78 #if 0
79 #ifdef WITH_WARNINGS
80 #warning Remove this color definitions before 1.2.0 final!
81 #endif
82         // just for experimentation :)
83         setBackgroundColor(LColor::green);
84 #endif
85
86         inset.setFrameColor(0, LColor::blue);
87         setInsetName("Minipage");
88 }
89
90
91 InsetMinipage::InsetMinipage(InsetMinipage const & in)
92         : InsetCollapsable(in), params_(in.params_)
93 {}
94
95
96 // InsetMinipage::InsetMinipage(InsetMinipage const & in, bool same_id)
97 //      : InsetCollapsable(in, same_id), params_(in.params_)
98 // {}
99
100
101 Inset * InsetMinipage::clone(Buffer const &) const
102 {
103         return new InsetMinipage(*const_cast<InsetMinipage *>(this));
104 }
105
106
107 // Inset * InsetMinipage::clone(Buffer const &, bool same_id) const
108 // {
109 //      return new InsetMinipage(*const_cast<InsetMinipage *>(this), same_id);
110 // }
111
112
113 InsetMinipage::~InsetMinipage()
114 {
115         InsetMinipageMailer mailer(*this);
116         mailer.hideDialog();
117 }
118
119
120 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
121 {
122         switch (cmd.action) {
123         case LFUN_INSET_MODIFY: {
124                 InsetMinipage::Params params;
125                 InsetMinipageMailer::string2params(cmd.argument, params);
126
127                 params_.pos   = params.pos;
128                 params_.width = params.width;
129
130                 /* FIXME: I refuse to believe we have to live
131                  * with ugliness like this ... */
132                 LyXText * t = inset.getLyXText(cmd.view());
133                 t->need_break_row = t->rows().begin();
134                 t->fullRebreak();
135                 inset.update(cmd.view(), true);
136                 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
137                 cmd.view()->updateInset(this);
138                 return DISPATCHED;
139         }
140
141         case LFUN_INSET_DIALOG_UPDATE:
142                 InsetMinipageMailer(*this).updateDialog(cmd.view());
143                 return DISPATCHED;
144
145         default:
146                 return InsetCollapsable::localDispatch(cmd);
147         }
148 }
149
150
151 void InsetMinipage::Params::write(ostream & os) const
152 {
153         os << "Minipage" << '\n'
154            << "position " << pos << '\n'
155            << "inner_position " << inner_pos << '\n'
156            << "height \"" << height.asString() << "\"\n"
157            << "width \"" << width.asString() << "\"\n";
158 }
159
160
161 void InsetMinipage::Params::read(LyXLex & lex)
162 {
163         if (lex.isOK()) {
164                 lex.next();
165                 string const token = lex.getString();
166                 if (token == "position") {
167                         lex.next();
168                         pos = static_cast<Position>(lex.getInteger());
169                 } else {
170                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
171                                    << endl;
172                         // take countermeasures
173                         lex.pushToken(token);
174                 }
175         }
176         if (lex.isOK()) {
177                 lex.next();
178                 string const token = lex.getString();
179                 if (token == "inner_position") {
180                         lex.next();
181                         inner_pos = static_cast<InnerPosition>(lex.getInteger());
182                 } else {
183                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
184                                    << endl;
185                         // take countermeasures
186                         lex.pushToken(token);
187                 }
188         }
189         if (lex.isOK()) {
190                 lex.next();
191                 string const token = lex.getString();
192                 if (token == "height") {
193                         lex.next();
194                         height = LyXLength(lex.getString());
195                 } else {
196                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
197                                    << endl;
198                         // take countermeasures
199                         lex.pushToken(token);
200                 }
201         }
202         if (lex.isOK()) {
203                 lex.next();
204                 string const token = lex.getString();
205                 if (token == "width") {
206                         lex.next();
207                         width = LyXLength(lex.getString());
208                 } else {
209                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
210                                    << endl;
211                         // take countermeasures
212                         lex.pushToken(token);
213                 }
214         }
215 }
216
217
218 void InsetMinipage::write(Buffer const * buf, ostream & os) const
219 {
220         params_.write(os);
221         InsetCollapsable::write(buf, os);
222 }
223
224
225 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
226 {
227         params_.read(lex);
228         InsetCollapsable::read(buf, lex);
229 }
230
231
232 void InsetMinipage::dimension(BufferView * bv, LyXFont const & font,
233         Dimension & dim) const
234 {
235         if (collapsed_)
236                 dimension_collapsed(dim);
237         else {
238                 Dimension d;
239                 InsetCollapsable::dimension(bv, font, dim);
240                 switch (params_.pos) {
241                 case top:
242                         dim.asc = d.asc;
243                         dim.des = d.des;
244                         break;
245                 case center:
246                         dim.asc = d.height() / 2;
247                         dim.des = dim.asc;
248                         break;
249                 case bottom:
250                         dim.asc = d.des;
251                         dim.des = d.asc;
252                         break;
253                 }
254         }
255 }
256
257
258 string const InsetMinipage::editMessage() const
259 {
260         return _("Opened Minipage Inset");
261 }
262
263
264 int InsetMinipage::latex(Buffer const * buf, ostream & os,
265                          LatexRunParams const & runparams) const
266 {
267         string s_pos;
268         switch (params_.pos) {
269         case top:
270                 s_pos += 't';
271                 break;
272         case center:
273                 s_pos += 'c';
274                 break;
275         case bottom:
276                 s_pos += 'b';
277                 break;
278         }
279         os << "\\begin{minipage}[" << s_pos << "]{"
280            << params_.width.asLatexString() << "}%\n";
281
282         int i = inset.latex(buf, os, runparams);
283
284         os << "\\end{minipage}%\n";
285         return i + 2;
286 }
287
288
289 bool InsetMinipage::insetAllowed(Inset::Code code) const
290 {
291         if (code == Inset::FLOAT_CODE || code == Inset::MARGIN_CODE)
292                 return false;
293
294         return InsetCollapsable::insetAllowed(code);
295 }
296
297
298 bool InsetMinipage::showInsetDialog(BufferView * bv) const
299 {
300         if (!inset.showInsetDialog(bv)) {
301                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
302                 InsetMinipageMailer mailer(*tmp);
303                 mailer.showDialog(bv);
304         }
305
306         return true;
307 }
308
309
310 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
311         const
312 {
313         if (owner() &&
314             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
315                 return -1;
316         }
317         if (!params_.width.zero()) {
318                 int ww1 = latexTextWidth(bv);
319                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
320                 if (ww2 > 0 && ww2 < ww1) {
321                         return ww2;
322                 }
323                 return ww1;
324         }
325         // this should not happen!
326         return InsetCollapsable::getMaxWidth(bv, inset);
327 }
328
329
330 int InsetMinipage::latexTextWidth(BufferView * bv) const
331 {
332         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
333 }
334
335
336 InsetMinipage::Params::Params()
337         : pos(center),
338           inner_pos(inner_center),
339           width(100, LyXLength::PCW)
340 {}
341
342
343 string const InsetMinipageMailer:: name_("minipage");
344
345 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
346         : inset_(inset)
347 {}
348
349
350 string const InsetMinipageMailer::inset2string() const
351 {
352         return params2string(inset_.params());
353 }
354
355
356 void InsetMinipageMailer::string2params(string const & in,
357                                         InsetMinipage::Params & params)
358 {
359         params = InsetMinipage::Params();
360
361         if (in.empty())
362                 return;
363
364         istringstream data(STRCONV(in));
365         LyXLex lex(0,0);
366         lex.setStream(data);
367
368         if (lex.isOK()) {
369                 lex.next();
370                 string const token = lex.getString();
371                 if (token != "minipage")
372                         return;
373         }
374
375         // This is part of the inset proper that is usually swallowed
376         // by Buffer::readInset
377         if (lex.isOK()) {
378                 lex.next();
379                 string const token = lex.getString();
380                 if (token != "Minipage")
381                         return;
382         }
383
384         if (lex.isOK()) {
385                 params.read(lex);
386         }
387 }
388
389
390 string const
391 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
392 {
393         ostringstream data;
394         data << name_ << ' ';
395         params.write(data);
396         return STRCONV(data.str());
397 }