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