]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
fix bug 1055, bug 1143, bug 1144
[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, d);
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.ascent() + d.descent() / 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                 dim.wid = d.wid;
255         }
256 }
257
258
259 string const InsetMinipage::editMessage() const
260 {
261         return _("Opened Minipage Inset");
262 }
263
264
265 int InsetMinipage::latex(Buffer const * buf, ostream & os,
266                          LatexRunParams const & runparams) const
267 {
268         string s_pos;
269         switch (params_.pos) {
270         case top:
271                 s_pos += 't';
272                 break;
273         case center:
274                 s_pos += 'c';
275                 break;
276         case bottom:
277                 s_pos += 'b';
278                 break;
279         }
280         os << "\\begin{minipage}[" << s_pos << "]{"
281            << params_.width.asLatexString() << "}%\n";
282
283         int i = inset.latex(buf, os, runparams);
284
285         os << "\\end{minipage}%\n";
286         return i + 2;
287 }
288
289
290 bool InsetMinipage::insetAllowed(Inset::Code code) const
291 {
292         if (code == Inset::FLOAT_CODE || code == Inset::MARGIN_CODE)
293                 return false;
294
295         return InsetCollapsable::insetAllowed(code);
296 }
297
298
299 bool InsetMinipage::showInsetDialog(BufferView * bv) const
300 {
301         if (!inset.showInsetDialog(bv)) {
302                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
303                 InsetMinipageMailer mailer(*tmp);
304                 mailer.showDialog(bv);
305         }
306
307         return true;
308 }
309
310
311 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
312         const
313 {
314         if (owner() &&
315             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
316                 return -1;
317         }
318         if (!params_.width.zero()) {
319                 int ww1 = latexTextWidth(bv);
320                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
321                 if (ww2 > 0 && ww2 < ww1) {
322                         return ww2;
323                 }
324                 return ww1;
325         }
326         // this should not happen!
327         return InsetCollapsable::getMaxWidth(bv, inset);
328 }
329
330
331 int InsetMinipage::latexTextWidth(BufferView * bv) const
332 {
333         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
334 }
335
336
337 InsetMinipage::Params::Params()
338         : pos(center),
339           inner_pos(inner_center),
340           width(100, LyXLength::PCW)
341 {}
342
343
344 string const InsetMinipageMailer:: name_("minipage");
345
346 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
347         : inset_(inset)
348 {}
349
350
351 string const InsetMinipageMailer::inset2string() const
352 {
353         return params2string(inset_.params());
354 }
355
356
357 void InsetMinipageMailer::string2params(string const & in,
358                                         InsetMinipage::Params & params)
359 {
360         params = InsetMinipage::Params();
361
362         if (in.empty())
363                 return;
364
365         istringstream data(STRCONV(in));
366         LyXLex lex(0,0);
367         lex.setStream(data);
368
369         if (lex.isOK()) {
370                 lex.next();
371                 string const token = lex.getString();
372                 if (token != "minipage")
373                         return;
374         }
375
376         // This is part of the inset proper that is usually swallowed
377         // by Buffer::readInset
378         if (lex.isOK()) {
379                 lex.next();
380                 string const token = lex.getString();
381                 if (token != "Minipage")
382                         return;
383         }
384
385         if (lex.isOK()) {
386                 params.read(lex);
387         }
388 }
389
390
391 string const
392 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
393 {
394         ostringstream data;
395         data << name_ << ' ';
396         params.write(data);
397         return STRCONV(data.str());
398 }