]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
Fix linking issue with MacOSX.
[lyx.git] / src / insets / insetfloat.C
1 /**
2  * \file insetfloat.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 "insetfloat.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "dispatchresult.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "LaTeXFeatures.h"
28 #include "LColor.h"
29 #include "lyxlex.h"
30 #include "outputparams.h"
31 #include "paragraph.h"
32 #include "pariterator.h"
33
34 #include "support/lstrings.h"
35 #include "support/convert.h"
36
37 #include <sstream>
38
39 using lyx::docstring;
40 using lyx::support::contains;
41
42 using std::endl;
43 using std::string;
44 using std::auto_ptr;
45 using std::istringstream;
46 using std::ostream;
47 using std::ostringstream;
48
49
50 // With this inset it will be possible to support the latex package
51 // float.sty, and I am sure that with this and some additional support
52 // classes we can support similar functionality in other formats
53 // (read DocBook).
54 // By using float.sty we will have the same handling for all floats, both
55 // for those already in existance (table and figure) and all user created
56 // ones¹. So suddenly we give the users the possibility of creating new
57 // kinds of floats on the fly. (and with a uniform look)
58 //
59 // API to float.sty:
60 //   \newfloat{type}{placement}{ext}[within]
61 //     type      - The "type" of the new class of floats, like program or
62 //                 algorithm. After the appropriate \newfloat, commands
63 //                 such as \begin{program} or \end{algorithm*} will be
64 //                 available.
65 //     placement - The default placement for the given class of floats.
66 //                 They are like in standard LaTeX: t, b, p and h for top,
67 //                 bottom, page, and here, respectively. On top of that
68 //                 there is a new type, H, which does not really correspond
69 //                 to a float, since it means: put it "here" and nowhere else.
70 //                 Note, however that the H specifier is special and, because
71 //                 of implementation details cannot be used in the second
72 //                 argument of \newfloat.
73 //     ext       - The file name extension of an auxiliary file for the list
74 //                 of figures (or whatever). LaTeX writes the captions to
75 //                 this file.
76 //     within    - This (optional) argument determines whether floats of this
77 //                 class will be numbered within some sectional unit of the
78 //                 document. For example, if within is equal to chapter, the
79 //                 floats will be numbered within chapters.
80 //   \floatstyle{style}
81 //     style -  plain, boxed, ruled
82 //   \floatname{float}{floatname}
83 //     float     -
84 //     floatname -
85 //   \floatplacement{float}{placement}
86 //     float     -
87 //     placement -
88 //   \restylefloat{float}
89 //     float -
90 //   \listof{type}{title}
91 //     title -
92
93 // ¹ the algorithm float is defined using the float.sty package. Like this
94 //   \floatstyle{ruled}
95 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
96 //   \floatname{algorithm}{Algorithm}
97 //
98 // The intention is that floats should be definable from two places:
99 //          - layout files
100 //          - the "gui" (i.e. by the user)
101 //
102 // From layout files.
103 // This should only be done for floats defined in a documentclass and that
104 // does not need any additional packages. The two most known floats in this
105 // category is "table" and "figure". Floats defined in layout files are only
106 // stored in lyx files if the user modifies them.
107 //
108 // By the user.
109 // There should be a gui dialog (and also a collection of lyxfuncs) where
110 // the user can modify existing floats and/or create new ones.
111 //
112 // The individual floats will also have some settable
113 // variables: wide and placement.
114 //
115 // Lgb
116
117
118 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
119         : InsetCollapsable(bp)
120 {
121         // FIXME UNICODE
122         setLabel(lyx::to_utf8(_("float: ")) + floatName(type, bp));
123         LyXFont font(LyXFont::ALL_SANE);
124         font.decSize();
125         font.decSize();
126         font.setColor(LColor::collapsable);
127         setLabelFont(font);
128         params_.type = type;
129         setInsetName(type);
130 }
131
132
133 InsetFloat::~InsetFloat()
134 {
135         InsetFloatMailer(*this).hideDialog();
136 }
137
138
139 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
140 {
141         switch (cmd.action) {
142
143         case LFUN_INSET_MODIFY: {
144                 InsetFloatParams params;
145                 InsetFloatMailer::string2params(lyx::to_utf8(cmd.argument()), params);
146                 params_.placement = params.placement;
147                 params_.wide      = params.wide;
148                 params_.sideways  = params.sideways;
149                 wide(params_.wide, cur.buffer().params());
150                 sideways(params_.sideways, cur.buffer().params());
151                 break;
152         }
153
154         case LFUN_INSET_DIALOG_UPDATE: {
155                 InsetFloatMailer(*this).updateDialog(&cur.bv());
156                 break;
157         }
158
159         case LFUN_MOUSE_RELEASE: {
160                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
161                         InsetFloatMailer(*this).showDialog(&cur.bv());
162                         break;
163                 }
164                 InsetCollapsable::doDispatch(cur, cmd);
165                 break;
166         }
167
168         default:
169                 InsetCollapsable::doDispatch(cur, cmd);
170                 break;
171         }
172 }
173
174
175 bool InsetFloat::getStatus(LCursor & cur, FuncRequest const & cmd,
176                 FuncStatus & flag) const
177 {
178         switch (cmd.action) {
179
180         case LFUN_INSET_MODIFY:
181         case LFUN_INSET_DIALOG_UPDATE:
182                 flag.enabled(true);
183                 return true;
184
185         default:
186                 return InsetCollapsable::getStatus(cur, cmd, flag);
187         }
188 }
189
190
191 void InsetFloatParams::write(ostream & os) const
192 {
193         os << "Float " << type << '\n';
194
195         if (!placement.empty())
196                 os << "placement " << placement << "\n";
197
198         if (wide)
199                 os << "wide true\n";
200         else
201                 os << "wide false\n";
202
203         if (sideways)
204                 os << "sideways true\n";
205         else
206                 os << "sideways false\n";
207 }
208
209
210 void InsetFloatParams::read(LyXLex & lex)
211 {
212         string token;
213         lex >> token;
214         if (token == "placement") {
215                 lex >> placement;
216         } else {
217                 // take countermeasures
218                 lex.pushToken(token);
219         }
220         lex >> token;
221         if (token == "wide") {
222                 lex >> wide;
223         } else {
224                 lyxerr << "InsetFloat::Read:: Missing wide!"
225                 << endl;
226                 // take countermeasures
227                 lex.pushToken(token);
228         }
229         lex >> token;
230         if (token == "sideways") {
231                 lex >> sideways;
232         } else {
233                 lyxerr << "InsetFloat::Read:: Missing sideways!"
234                 << endl;
235                 // take countermeasures
236                 lex.pushToken(token);
237         }
238 }
239
240
241 void InsetFloat::write(Buffer const & buf, ostream & os) const
242 {
243         params_.write(os);
244         InsetCollapsable::write(buf, os);
245 }
246
247
248 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
249 {
250         params_.read(lex);
251         wide(params_.wide, buf.params());
252         sideways(params_.sideways, buf.params());
253         InsetCollapsable::read(buf, lex);
254 }
255
256
257 void InsetFloat::validate(LaTeXFeatures & features) const
258 {
259         if (contains(params_.placement, 'H')) {
260                 features.require("float");
261         }
262
263         if (params_.sideways)
264                 features.require("rotating");
265
266         features.useFloat(params_.type);
267         InsetCollapsable::validate(features);
268 }
269
270
271 auto_ptr<InsetBase> InsetFloat::doClone() const
272 {
273         return auto_ptr<InsetBase>(new InsetFloat(*this));
274 }
275
276
277 docstring const InsetFloat::editMessage() const
278 {
279         return _("Opened Float Inset");
280 }
281
282
283 int InsetFloat::latex(Buffer const & buf, ostream & os,
284                       OutputParams const & runparams) const
285 {
286         FloatList const & floats = buf.params().getLyXTextClass().floats();
287         string tmptype = (params_.wide ? params_.type + "*" : params_.type);
288         if (params_.sideways) {
289                 if (params_.type == "table")
290                         tmptype = "sidewaystable";
291                 else if (params_.type == "figure")
292                         tmptype = "sidewaysfigure";
293         }
294         // Figure out the float placement to use.
295         // From lowest to highest:
296         // - float default placement
297         // - document wide default placement
298         // - specific float placement
299         string placement;
300         string const buf_placement = buf.params().float_placement;
301         string const def_placement = floats.defaultPlacement(params_.type);
302         if (!params_.placement.empty()
303             && params_.placement != def_placement) {
304                 placement = params_.placement;
305         } else if (params_.placement.empty()
306                    && !buf_placement.empty()
307                    && buf_placement != def_placement) {
308                 placement = buf_placement;
309         }
310
311         // The \n is used to force \begin{<floatname>} to appear in a new line.
312         // The % is needed to prevent two consecutive \n chars in the case
313         // when the current output line is empty.
314         os << "%\n\\begin{" << tmptype << '}';
315         // We only output placement if different from the def_placement.
316         // sidewaysfloats always use their own page
317         if (!placement.empty() && !params_.sideways) {
318                 os << '[' << placement << ']';
319         }
320         os << '\n';
321
322         int const i = InsetText::latex(buf, os, runparams);
323
324         // The \n is used to force \end{<floatname>} to appear in a new line.
325         // In this case, we do not case if the current output line is empty.
326         os << "\n\\end{" << tmptype << "}\n";
327
328         return i + 4;
329 }
330
331
332 int InsetFloat::docbook(Buffer const & buf, ostream & os,
333                         OutputParams const & runparams) const
334 {
335         os << '<' << params_.type << '>';
336         int const i = InsetText::docbook(buf, os, runparams);
337         os << "</" << params_.type << '>';
338
339         return i;
340 }
341
342
343 bool InsetFloat::insetAllowed(InsetBase::Code code) const
344 {
345         return code != InsetBase::FLOAT_CODE
346             && code != InsetBase::FOOT_CODE
347             && code != InsetBase::MARGIN_CODE;
348 }
349
350
351 bool InsetFloat::showInsetDialog(BufferView * bv) const
352 {
353         if (!InsetText::showInsetDialog(bv))
354                 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
355         return true;
356 }
357
358
359 void InsetFloat::wide(bool w, BufferParams const & bp)
360 {
361         params_.wide = w;
362         // FIXME UNICODE
363         string lab = lyx::to_utf8(_("float: ")) + floatName(params_.type, bp);
364         if (params_.wide)
365                 lab += '*';
366         setLabel(lab);
367 }
368
369
370 void InsetFloat::sideways(bool s, BufferParams const & bp)
371 {
372         params_.sideways = s;
373         // FIXME UNICODE
374         string lab = lyx::to_utf8(_("float: ")) + floatName(params_.type, bp);
375         if (params_.sideways)
376                 lab += lyx::to_utf8(_(" (sideways)"));
377         setLabel(lab);
378 }
379
380
381 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
382 {
383         ParConstIterator pit = par_const_iterator_begin(*this);
384         ParConstIterator end = par_const_iterator_end(*this);
385
386         // Find a caption layout in one of the (child inset's) pars
387         for (; pit != end; ++pit) {
388                 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
389                         string const type = params_.type;
390                         string const str =
391                                 convert<string>(toclist[type].size() + 1)
392                                 + ". " + pit->asString(buf, false);
393                         lyx::toc::TocItem const item(pit, 0 , str);
394                         toclist[type].push_back(item);
395                 }
396         }
397 }
398
399
400 string const InsetFloatMailer::name_("float");
401
402 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
403         : inset_(inset)
404 {}
405
406
407 string const InsetFloatMailer::inset2string(Buffer const &) const
408 {
409         return params2string(inset_.params());
410 }
411
412
413 void InsetFloatMailer::string2params(string const & in,
414                                      InsetFloatParams & params)
415 {
416         params = InsetFloatParams();
417         if (in.empty())
418                 return;
419
420         istringstream data(in);
421         LyXLex lex(0,0);
422         lex.setStream(data);
423
424         string name;
425         lex >> name;
426         if (!lex || name != name_)
427                 return print_mailer_error("InsetFloatMailer", in, 1, name_);
428
429         // This is part of the inset proper that is usually swallowed
430         // by LyXText::readInset
431         string id;
432         lex >> id;
433         if (!lex || id != "Float")
434                 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
435
436         // We have to read the type here!
437         lex >> params.type;
438         params.read(lex);
439 }
440
441
442 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
443 {
444         ostringstream data;
445         data << name_ << ' ';
446         params.write(data);
447         return data.str();
448 }