]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
remove Inset::update()
[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 InsetBase * InsetMinipage::clone() const
97 {
98         return new InsetMinipage(*this);
99 }
100
101
102 InsetMinipage::~InsetMinipage()
103 {
104         InsetMinipageMailer mailer(*this);
105         mailer.hideDialog();
106 }
107
108
109 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
110 {
111         switch (cmd.action) {
112         case LFUN_INSET_MODIFY: {
113                 InsetMinipage::Params params;
114                 InsetMinipageMailer::string2params(cmd.argument, params);
115
116                 params_.pos   = params.pos;
117                 params_.width = params.width;
118
119                 /* FIXME: I refuse to believe we have to live
120                  * with ugliness like this ... */
121                 inset.getLyXText(cmd.view())->fullRebreak();
122                 cmd.view()->updateInset(this);
123                 return DISPATCHED;
124         }
125
126         case LFUN_INSET_DIALOG_UPDATE:
127                 InsetMinipageMailer(*this).updateDialog(cmd.view());
128                 return DISPATCHED;
129
130         default:
131                 return InsetCollapsable::localDispatch(cmd);
132         }
133 }
134
135
136 void InsetMinipage::Params::write(ostream & os) const
137 {
138         os << "Minipage" << '\n'
139            << "position " << pos << '\n'
140            << "inner_position " << inner_pos << '\n'
141            << "height \"" << height.asString() << "\"\n"
142            << "width \"" << width.asString() << "\"\n";
143 }
144
145
146 void InsetMinipage::Params::read(LyXLex & lex)
147 {
148         if (lex.isOK()) {
149                 lex.next();
150                 string const token = lex.getString();
151                 if (token == "position") {
152                         lex.next();
153                         pos = static_cast<Position>(lex.getInteger());
154                 } else {
155                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
156                                    << endl;
157                         // take countermeasures
158                         lex.pushToken(token);
159                 }
160         }
161         if (lex.isOK()) {
162                 lex.next();
163                 string const token = lex.getString();
164                 if (token == "inner_position") {
165                         lex.next();
166                         inner_pos = static_cast<InnerPosition>(lex.getInteger());
167                 } else {
168                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
169                                    << endl;
170                         // take countermeasures
171                         lex.pushToken(token);
172                 }
173         }
174         if (lex.isOK()) {
175                 lex.next();
176                 string const token = lex.getString();
177                 if (token == "height") {
178                         lex.next();
179                         height = LyXLength(lex.getString());
180                 } else {
181                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
182                                    << endl;
183                         // take countermeasures
184                         lex.pushToken(token);
185                 }
186         }
187         if (lex.isOK()) {
188                 lex.next();
189                 string const token = lex.getString();
190                 if (token == "width") {
191                         lex.next();
192                         width = LyXLength(lex.getString());
193                 } else {
194                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
195                                    << endl;
196                         // take countermeasures
197                         lex.pushToken(token);
198                 }
199         }
200 }
201
202
203 void InsetMinipage::write(Buffer const * buf, ostream & os) const
204 {
205         params_.write(os);
206         InsetCollapsable::write(buf, os);
207 }
208
209
210 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
211 {
212         params_.read(lex);
213         InsetCollapsable::read(buf, lex);
214 }
215
216
217 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
218 {
219         if (collapsed_)
220                 dimension_collapsed(dim);
221         else {
222                 Dimension d;
223                 InsetCollapsable::metrics(mi, d);
224                 switch (params_.pos) {
225                 case top:
226                         dim.asc = d.asc;
227                         dim.des = d.des;
228                         break;
229                 case center:
230                         dim.asc = d.ascent() + d.descent() / 2;
231                         dim.des = dim.asc;
232                         break;
233                 case bottom:
234                         dim.asc = d.des;
235                         dim.des = d.asc;
236                         break;
237                 }
238                 dim.wid = d.wid;
239         }
240 }
241
242
243 string const InsetMinipage::editMessage() const
244 {
245         return _("Opened Minipage Inset");
246 }
247
248
249 int InsetMinipage::latex(Buffer const * buf, ostream & os,
250                          LatexRunParams const & runparams) const
251 {
252         string s_pos;
253         switch (params_.pos) {
254         case top:
255                 s_pos += 't';
256                 break;
257         case center:
258                 s_pos += 'c';
259                 break;
260         case bottom:
261                 s_pos += 'b';
262                 break;
263         }
264         os << "\\begin{minipage}[" << s_pos << "]{"
265            << params_.width.asLatexString() << "}%\n";
266
267         int i = inset.latex(buf, os, runparams);
268
269         os << "\\end{minipage}%\n";
270         return i + 2;
271 }
272
273
274 bool InsetMinipage::insetAllowed(Inset::Code code) const
275 {
276         if (code == Inset::FLOAT_CODE || code == Inset::MARGIN_CODE)
277                 return false;
278
279         return InsetCollapsable::insetAllowed(code);
280 }
281
282
283 bool InsetMinipage::showInsetDialog(BufferView * bv) const
284 {
285         if (!inset.showInsetDialog(bv)) {
286                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
287                 InsetMinipageMailer mailer(*tmp);
288                 mailer.showDialog(bv);
289         }
290
291         return true;
292 }
293
294
295 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
296         const
297 {
298         if (owner() &&
299             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
300                 return -1;
301         }
302         if (!params_.width.zero()) {
303                 int ww1 = latexTextWidth(bv);
304                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
305                 if (ww2 > 0 && ww2 < ww1) {
306                         return ww2;
307                 }
308                 return ww1;
309         }
310         // this should not happen!
311         return InsetCollapsable::getMaxWidth(bv, inset);
312 }
313
314
315 int InsetMinipage::latexTextWidth(BufferView * bv) const
316 {
317         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
318 }
319
320
321 InsetMinipage::Params::Params()
322         : pos(center),
323           inner_pos(inner_center),
324           width(100, LyXLength::PCW)
325 {}
326
327
328 string const InsetMinipageMailer:: name_("minipage");
329
330 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
331         : inset_(inset)
332 {}
333
334
335 string const InsetMinipageMailer::inset2string() const
336 {
337         return params2string(inset_.params());
338 }
339
340
341 void InsetMinipageMailer::string2params(string const & in,
342                                         InsetMinipage::Params & params)
343 {
344         params = InsetMinipage::Params();
345
346         if (in.empty())
347                 return;
348
349         istringstream data(STRCONV(in));
350         LyXLex lex(0,0);
351         lex.setStream(data);
352
353         if (lex.isOK()) {
354                 lex.next();
355                 string const token = lex.getString();
356                 if (token != "minipage")
357                         return;
358         }
359
360         // This is part of the inset proper that is usually swallowed
361         // by Buffer::readInset
362         if (lex.isOK()) {
363                 lex.next();
364                 string const token = lex.getString();
365                 if (token != "Minipage")
366                         return;
367         }
368
369         if (lex.isOK()) {
370                 params.read(lex);
371         }
372 }
373
374
375 string const
376 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
377 {
378         ostringstream data;
379         data << name_ << ' ';
380         params.write(data);
381         return STRCONV(data.str());
382 }