]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
Remove a whole heap of redundant functions from classes derived from
[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 "dispatchresult.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
25
26 #include "support/std_sstream.h"
27
28
29 using std::string;
30 using std::endl;
31 using std::auto_ptr;
32 using std::istringstream;
33 using std::ostream;
34 using std::ostringstream;
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         inset.setFrameColor(LColor::blue);
78         setInsetName("Minipage");
79 }
80
81
82 InsetMinipage::InsetMinipage(InsetMinipage const & in)
83         : InsetCollapsable(in), params_(in.params_)
84 {}
85
86
87 auto_ptr<InsetBase> InsetMinipage::clone() const
88 {
89         return auto_ptr<InsetBase>(new InsetMinipage(*this));
90 }
91
92
93 InsetMinipage::~InsetMinipage()
94 {
95         InsetMinipageMailer(*this).hideDialog();
96 }
97
98
99 DispatchResult
100 InsetMinipage::priv_dispatch(FuncRequest const & cmd,
101                              idx_type & idx, pos_type & pos)
102 {
103         switch (cmd.action) {
104         case LFUN_INSET_MODIFY: {
105                 InsetMinipage::Params params;
106                 InsetMinipageMailer::string2params(cmd.argument, params);
107                 params_.pos   = params.pos;
108                 params_.width = params.width;
109                 cmd.view()->update();
110                 return DispatchResult(true, true);
111         }
112
113         case LFUN_INSET_DIALOG_UPDATE:
114                 InsetMinipageMailer(*this).updateDialog(cmd.view());
115                 return DispatchResult(true, true);
116
117         default:
118                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
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                          OutputParams 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(*tmp).showDialog(bv);
278         }
279
280         return true;
281 }
282
283
284 InsetMinipage::Params::Params()
285         : pos(center), inner_pos(inner_center), width(100, LyXLength::PCW)
286 {}
287
288
289 string const InsetMinipageMailer:: name_("minipage");
290
291 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
292         : inset_(inset)
293 {}
294
295
296 string const InsetMinipageMailer::inset2string(Buffer const &) const
297 {
298         return params2string(inset_.params());
299 }
300
301
302 void InsetMinipageMailer::string2params(string const & in,
303                                         InsetMinipage::Params & params)
304 {
305         params = InsetMinipage::Params();
306         if (in.empty())
307                 return;
308
309         istringstream data(in);
310         LyXLex lex(0, 0);
311         lex.setStream(data);
312
313         string name;
314         lex >> name;
315         if (!lex || name != name_)
316                 return print_mailer_error("InsetMinipageMailer", in, 1, name_);
317
318         // This is part of the inset proper that is usually swallowed
319         // by LyXText::readInset
320         string id;
321         lex >> id;
322         if (!lex || id != "Minipage")
323                 return print_mailer_error("InsetBoxMailer", in, 2, "Minipage");
324
325         params.read(lex);
326 }
327
328
329 string const
330 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
331 {
332         ostringstream data;
333         data << name_ << ' ';
334         params.write(data);
335         return data.str();
336 }