]> git.lyx.org Git - lyx.git/blob - src/insets/insetminipage.C
"Inter-word Space"
[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(Buffer const &) 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::dimension(BufferView * bv, LyXFont const & font,
222         Dimension & dim) const
223 {
224         if (collapsed_)
225                 dimension_collapsed(dim);
226         else {
227                 Dimension d;
228                 InsetCollapsable::dimension(bv, font, d);
229                 switch (params_.pos) {
230                 case top:
231                         dim.asc = d.asc;
232                         dim.des = d.des;
233                         break;
234                 case center:
235                         dim.asc = d.ascent() + d.descent() / 2;
236                         dim.des = dim.asc;
237                         break;
238                 case bottom:
239                         dim.asc = d.des;
240                         dim.des = d.asc;
241                         break;
242                 }
243                 dim.wid = d.wid;
244         }
245 }
246
247
248 string const InsetMinipage::editMessage() const
249 {
250         return _("Opened Minipage Inset");
251 }
252
253
254 int InsetMinipage::latex(Buffer const * buf, ostream & os,
255                          LatexRunParams const & runparams) const
256 {
257         string s_pos;
258         switch (params_.pos) {
259         case top:
260                 s_pos += 't';
261                 break;
262         case center:
263                 s_pos += 'c';
264                 break;
265         case bottom:
266                 s_pos += 'b';
267                 break;
268         }
269         os << "\\begin{minipage}[" << s_pos << "]{"
270            << params_.width.asLatexString() << "}%\n";
271
272         int i = inset.latex(buf, os, runparams);
273
274         os << "\\end{minipage}%\n";
275         return i + 2;
276 }
277
278
279 bool InsetMinipage::insetAllowed(Inset::Code code) const
280 {
281         if (code == Inset::FLOAT_CODE || code == Inset::MARGIN_CODE)
282                 return false;
283
284         return InsetCollapsable::insetAllowed(code);
285 }
286
287
288 bool InsetMinipage::showInsetDialog(BufferView * bv) const
289 {
290         if (!inset.showInsetDialog(bv)) {
291                 InsetMinipage * tmp = const_cast<InsetMinipage *>(this);
292                 InsetMinipageMailer mailer(*tmp);
293                 mailer.showDialog(bv);
294         }
295
296         return true;
297 }
298
299
300 int InsetMinipage::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
301         const
302 {
303         if (owner() &&
304             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
305                 return -1;
306         }
307         if (!params_.width.zero()) {
308                 int ww1 = latexTextWidth(bv);
309                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
310                 if (ww2 > 0 && ww2 < ww1) {
311                         return ww2;
312                 }
313                 return ww1;
314         }
315         // this should not happen!
316         return InsetCollapsable::getMaxWidth(bv, inset);
317 }
318
319
320 int InsetMinipage::latexTextWidth(BufferView * bv) const
321 {
322         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
323 }
324
325
326 InsetMinipage::Params::Params()
327         : pos(center),
328           inner_pos(inner_center),
329           width(100, LyXLength::PCW)
330 {}
331
332
333 string const InsetMinipageMailer:: name_("minipage");
334
335 InsetMinipageMailer::InsetMinipageMailer(InsetMinipage & inset)
336         : inset_(inset)
337 {}
338
339
340 string const InsetMinipageMailer::inset2string() const
341 {
342         return params2string(inset_.params());
343 }
344
345
346 void InsetMinipageMailer::string2params(string const & in,
347                                         InsetMinipage::Params & params)
348 {
349         params = InsetMinipage::Params();
350
351         if (in.empty())
352                 return;
353
354         istringstream data(STRCONV(in));
355         LyXLex lex(0,0);
356         lex.setStream(data);
357
358         if (lex.isOK()) {
359                 lex.next();
360                 string const token = lex.getString();
361                 if (token != "minipage")
362                         return;
363         }
364
365         // This is part of the inset proper that is usually swallowed
366         // by Buffer::readInset
367         if (lex.isOK()) {
368                 lex.next();
369                 string const token = lex.getString();
370                 if (token != "Minipage")
371                         return;
372         }
373
374         if (lex.isOK()) {
375                 params.read(lex);
376         }
377 }
378
379
380 string const
381 InsetMinipageMailer::params2string(InsetMinipage::Params const & params)
382 {
383         ostringstream data;
384         data << name_ << ' ';
385         params.write(data);
386         return STRCONV(data.str());
387 }