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