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