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