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