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