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