]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
Clean up InsetGraphics::Cache and rename as GraphicsInset.
[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
28 #include "frontends/LyXView.h"
29 #include "frontends/Dialogs.h"
30
31 #include "support/LOstream.h"
32
33 using std::ostream;
34 using std::endl;
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
78 #if 0
79 #ifdef WITH_WARNINGS
80 #warning Remove this color definitions before 1.2.0 final!
81 #endif
82         // just for experimentation :)
83         setBackgroundColor(LColor::green);
84 #endif
85
86         inset.setFrameColor(0, LColor::blue);
87         setInsetName("Minipage");
88 }
89
90
91 InsetMinipage::InsetMinipage(InsetMinipage const & in)
92         : InsetCollapsable(in), params_(in.params_)
93 {}
94
95
96 Inset * InsetMinipage::clone() const
97 {
98         return new InsetMinipage(*this);
99 }
100
101
102 InsetMinipage::~InsetMinipage()
103 {
104         InsetMinipageMailer mailer(*this);
105         mailer.hideDialog();
106 }
107
108
109 dispatch_result InsetMinipage::localDispatch(FuncRequest const & cmd)
110 {
111         switch (cmd.action) {
112         case LFUN_INSET_MODIFY: {
113                 InsetMinipage::Params params;
114                 InsetMinipageMailer::string2params(cmd.argument, params);
115
116                 params_.pos   = params.pos;
117                 params_.width = params.width;
118
119                 /* FIXME: I refuse to believe we have to live
120                  * with ugliness like this ... */
121                 LyXText * t = inset.getLyXText(cmd.view());
122                 t->need_break_row = t->rows().begin();
123                 t->fullRebreak();
124                 inset.update(cmd.view(), true);
125                 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
126                 cmd.view()->updateInset(this);
127                 return DISPATCHED;
128         }
129
130         case LFUN_INSET_DIALOG_UPDATE:
131                 InsetMinipageMailer(*this).updateDialog(cmd.view());
132                 return DISPATCHED;
133
134         default:
135                 return InsetCollapsable::localDispatch(cmd);
136         }
137 }
138
139
140 void InsetMinipage::Params::write(ostream & os) const
141 {
142         os << "Minipage" << '\n'
143            << "position " << pos << '\n'
144            << "inner_position " << inner_pos << '\n'
145            << "height \"" << height.asString() << "\"\n"
146            << "width \"" << width.asString() << "\"\n";
147 }
148
149
150 void InsetMinipage::Params::read(LyXLex & lex)
151 {
152         if (lex.isOK()) {
153                 lex.next();
154                 string const token = lex.getString();
155                 if (token == "position") {
156                         lex.next();
157                         pos = static_cast<Position>(lex.getInteger());
158                 } else {
159                         lyxerr << "InsetMinipage::Read: Missing 'position'-tag!"
160                                    << endl;
161                         // take countermeasures
162                         lex.pushToken(token);
163                 }
164         }
165         if (lex.isOK()) {
166                 lex.next();
167                 string const token = lex.getString();
168                 if (token == "inner_position") {
169                         lex.next();
170                         inner_pos = static_cast<InnerPosition>(lex.getInteger());
171                 } else {
172                         lyxerr << "InsetMinipage::Read: Missing 'inner_position'-tag!"
173                                    << endl;
174                         // take countermeasures
175                         lex.pushToken(token);
176                 }
177         }
178         if (lex.isOK()) {
179                 lex.next();
180                 string const token = lex.getString();
181                 if (token == "height") {
182                         lex.next();
183                         height = LyXLength(lex.getString());
184                 } else {
185                         lyxerr << "InsetMinipage::Read: Missing 'height'-tag!"
186                                    << endl;
187                         // take countermeasures
188                         lex.pushToken(token);
189                 }
190         }
191         if (lex.isOK()) {
192                 lex.next();
193                 string const token = lex.getString();
194                 if (token == "width") {
195                         lex.next();
196                         width = LyXLength(lex.getString());
197                 } else {
198                         lyxerr << "InsetMinipage::Read: Missing 'width'-tag!"
199                                    << endl;
200                         // take countermeasures
201                         lex.pushToken(token);
202                 }
203         }
204 }
205
206
207 void InsetMinipage::write(Buffer const * buf, ostream & os) const
208 {
209         params_.write(os);
210         InsetCollapsable::write(buf, os);
211 }
212
213
214 void InsetMinipage::read(Buffer const * buf, LyXLex & lex)
215 {
216         params_.read(lex);
217         InsetCollapsable::read(buf, lex);
218 }
219
220
221 void InsetMinipage::metrics(MetricsInfo & mi, Dimension & dim) const
222 {
223         if (collapsed_)
224                 dimension_collapsed(dim);
225         else {
226                 Dimension d;
227                 InsetCollapsable::metrics(mi, d);
228                 switch (params_.pos) {
229                 case top:
230                         dim.asc = d.asc;
231                         dim.des = d.des;
232                         break;
233                 case center:
234                         dim.asc = d.ascent() + d.descent() / 2;
235                         dim.des = dim.asc;
236                         break;
237                 case bottom:
238                         dim.asc = d.des;
239                         dim.des = d.asc;
240                         break;
241                 }
242                 dim.wid = d.wid;
243         }
244 }
245
246
247 string const InsetMinipage::editMessage() const
248 {
249         return _("Opened Minipage Inset");
250 }
251
252
253 int InsetMinipage::latex(Buffer const * buf, ostream & os,
254                          LatexRunParams const & runparams) const
255 {
256         string s_pos;
257         switch (params_.pos) {
258         case top:
259                 s_pos += 't';
260                 break;
261         case center:
262                 s_pos += 'c';
263                 break;
264         case bottom:
265                 s_pos += 'b';
266                 break;
267         }
268         os << "\\begin{minipage}[" << s_pos << "]{"
269            << params_.width.asLatexString() << "}%\n";
270
271         int i = inset.latex(buf, os, runparams);
272
273         os << "\\end{minipage}%\n";
274         return i + 2;
275 }
276
277
278 bool InsetMinipage::insetAllowed(Inset::Code code) const
279 {
280         if (code == Inset::FLOAT_CODE || code == Inset::MARGIN_CODE)
281                 return false;
282
283         return InsetCollapsable::insetAllowed(code);
284 }
285
286
287 bool InsetMinipage::showInsetDialog(BufferView * bv) const
288 {
289         if (!inset.showInsetDialog(bv)) {
290                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
291                 InsetMinipageMailer mailer(*tmp);
292                 mailer.showDialog(bv);
293         }
294
295         return true;
296 }
297
298
299 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
300         const
301 {
302         if (owner() &&
303             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
304                 return -1;
305         }
306         if (!params_.width.zero()) {
307                 int ww1 = latexTextWidth(bv);
308                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
309                 if (ww2 > 0 && ww2 < ww1) {
310                         return ww2;
311                 }
312                 return ww1;
313         }
314         // this should not happen!
315         return InsetCollapsable::getMaxWidth(bv, inset);
316 }
317
318
319 int InsetMinipage::latexTextWidth(BufferView * bv) const
320 {
321         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
322 }
323
324
325 InsetMinipage::Params::Params()
326         : pos(center),
327           inner_pos(inner_center),
328           width(100, LyXLength::PCW)
329 {}
330
331
332 string const InsetMinipageMailer:: name_("minipage");
333
334 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
335         : inset_(inset)
336 {}
337
338
339 string const InsetMinipageMailer::inset2string() const
340 {
341         return params2string(inset_.params());
342 }
343
344
345 void InsetMinipageMailer::string2params(string const & in,
346                                         InsetMinipage::Params & params)
347 {
348         params = InsetMinipage::Params();
349
350         if (in.empty())
351                 return;
352
353         istringstream data(STRCONV(in));
354         LyXLex lex(0,0);
355         lex.setStream(data);
356
357         if (lex.isOK()) {
358                 lex.next();
359                 string const token = lex.getString();
360                 if (token != "minipage")
361                         return;
362         }
363
364         // This is part of the inset proper that is usually swallowed
365         // by Buffer::readInset
366         if (lex.isOK()) {
367                 lex.next();
368                 string const token = lex.getString();
369                 if (token != "Minipage")
370                         return;
371         }
372
373         if (lex.isOK()) {
374                 params.read(lex);
375         }
376 }
377
378
379 string const
380 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
381 {
382         ostringstream data;
383         data << name_ << ' ';
384         params.write(data);
385         return STRCONV(data.str());
386 }