]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloat.cpp
This should be the last of the commits refactoring the InsetLayout code.
[lyx.git] / src / insets / InsetFloat.cpp
1 /**
2  * \file InsetFloat.cpp
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 "Counters.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "OutputParams.h"
29 #include "TextClass.h"
30
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/docstream.h"
35 #include "support/convert.h"
36
37 using namespace std;
38
39 namespace lyx {
40
41
42 // With this inset it will be possible to support the latex package
43 // float.sty, and I am sure that with this and some additional support
44 // classes we can support similar functionality in other formats
45 // (read DocBook).
46 // By using float.sty we will have the same handling for all floats, both
47 // for those already in existance (table and figure) and all user created
48 // ones¹. So suddenly we give the users the possibility of creating new
49 // kinds of floats on the fly. (and with a uniform look)
50 //
51 // API to float.sty:
52 //   \newfloat{type}{placement}{ext}[within]
53 //     type      - The "type" of the new class of floats, like program or
54 //                 algorithm. After the appropriate \newfloat, commands
55 //                 such as \begin{program} or \end{algorithm*} will be
56 //                 available.
57 //     placement - The default placement for the given class of floats.
58 //                 They are like in standard LaTeX: t, b, p and h for top,
59 //                 bottom, page, and here, respectively. On top of that
60 //                 there is a new type, H, which does not really correspond
61 //                 to a float, since it means: put it "here" and nowhere else.
62 //                 Note, however that the H specifier is special and, because
63 //                 of implementation details cannot be used in the second
64 //                 argument of \newfloat.
65 //     ext       - The file name extension of an auxiliary file for the list
66 //                 of figures (or whatever). LaTeX writes the captions to
67 //                 this file.
68 //     within    - This (optional) argument determines whether floats of this
69 //                 class will be numbered within some sectional unit of the
70 //                 document. For example, if within is equal to chapter, the
71 //                 floats will be numbered within chapters.
72 //   \floatstyle{style}
73 //     style -  plain, boxed, ruled
74 //   \floatname{float}{floatname}
75 //     float     -
76 //     floatname -
77 //   \floatplacement{float}{placement}
78 //     float     -
79 //     placement -
80 //   \restylefloat{float}
81 //     float -
82 //   \listof{type}{title}
83 //     title -
84
85 // ¹ the algorithm float is defined using the float.sty package. Like this
86 //   \floatstyle{ruled}
87 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
88 //   \floatname{algorithm}{Algorithm}
89 //
90 // The intention is that floats should be definable from two places:
91 //          - layout files
92 //          - the "gui" (i.e. by the user)
93 //
94 // From layout files.
95 // This should only be done for floats defined in a documentclass and that
96 // does not need any additional packages. The two most known floats in this
97 // category is "table" and "figure". Floats defined in layout files are only
98 // stored in lyx files if the user modifies them.
99 //
100 // By the user.
101 // There should be a gui dialog (and also a collection of lyxfuncs) where
102 // the user can modify existing floats and/or create new ones.
103 //
104 // The individual floats will also have some settable
105 // variables: wide and placement.
106 //
107 // Lgb
108
109
110 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
111         : InsetCollapsable(bp), name_(from_utf8(type))
112 {
113         setLabel(_("float: ") + floatName(type, bp));
114         params_.type = type;
115 }
116
117
118 InsetFloat::~InsetFloat()
119 {
120         InsetFloatMailer(*this).hideDialog();
121 }
122
123
124 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
125 {
126         switch (cmd.action) {
127
128         case LFUN_INSET_MODIFY: {
129                 InsetFloatParams params;
130                 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
131                 params_.placement = params.placement;
132                 params_.wide      = params.wide;
133                 params_.sideways  = params.sideways;
134                 wide(params_.wide, cur.buffer().params());
135                 sideways(params_.sideways, cur.buffer().params());
136                 break;
137         }
138
139         case LFUN_INSET_DIALOG_UPDATE: {
140                 InsetFloatMailer(*this).updateDialog(&cur.bv());
141                 break;
142         }
143
144         case LFUN_MOUSE_RELEASE: {
145                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
146                         InsetFloatMailer(*this).showDialog(&cur.bv());
147                         break;
148                 }
149                 InsetCollapsable::doDispatch(cur, cmd);
150                 break;
151         }
152
153         default:
154                 InsetCollapsable::doDispatch(cur, cmd);
155                 break;
156         }
157 }
158
159
160 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
161                 FuncStatus & flag) const
162 {
163         switch (cmd.action) {
164
165         case LFUN_INSET_MODIFY:
166         case LFUN_INSET_DIALOG_UPDATE:
167                 flag.enabled(true);
168                 return true;
169
170         default:
171                 return InsetCollapsable::getStatus(cur, cmd, flag);
172         }
173 }
174
175
176 void InsetFloat::updateLabels(Buffer const & buf, ParIterator const & it)
177 {
178         Counters & cnts = buf.params().getTextClass().counters();
179         string const saveflt = cnts.current_float();
180
181         // Tell to captions what the current float is
182         cnts.current_float(params().type);
183
184         InsetCollapsable::updateLabels(buf, it);
185
186         //reset afterwards
187         cnts.current_float(saveflt);
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(Lexer & 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, Lexer & 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 (support::contains(params_.placement, 'H')) {
260                 features.require("float");
261         }
262
263         if (params_.sideways)
264                 features.require("rotfloat");
265
266         features.useFloat(params_.type);
267         InsetCollapsable::validate(features);
268 }
269
270
271 Inset * InsetFloat::clone() const
272 {
273         return 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, odocstream & os,
284                       OutputParams const & runparams) const
285 {
286         FloatList const & floats = buf.params().getTextClass().floats();
287         string tmptype = params_.type;
288         if (params_.sideways)
289                 tmptype = "sideways" + params_.type;
290         if (params_.wide && (!params_.sideways ||
291                              params_.type == "figure" ||
292                              params_.type == "table"))
293                 tmptype += "*";
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{" << from_ascii(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 << '[' << from_ascii(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{" << from_ascii(tmptype) << "}\n";
327
328         return i + 4;
329 }
330
331
332 int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
333                           OutputParams const & runparams) const
334 {
335         os << '[' << buf.B_("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
336         InsetText::plaintext(buf, os, runparams);
337         os << "\n]";
338
339         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
340 }
341
342
343 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
344                         OutputParams const & runparams) const
345 {
346         // FIXME UNICODE
347         os << '<' << from_ascii(params_.type) << '>';
348         int const i = InsetText::docbook(buf, os, runparams);
349         os << "</" << from_ascii(params_.type) << '>';
350
351         return i;
352 }
353
354
355 bool InsetFloat::insetAllowed(InsetCode code) const
356 {
357         return code != FLOAT_CODE
358             && code != FOOT_CODE
359             && code != MARGIN_CODE;
360 }
361
362
363 bool InsetFloat::showInsetDialog(BufferView * bv) const
364 {
365         if (!InsetText::showInsetDialog(bv))
366                 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
367         return true;
368 }
369
370
371 void InsetFloat::wide(bool w, BufferParams const & bp)
372 {
373         params_.wide = w;
374         docstring lab = _("float: ") + floatName(params_.type, bp);
375         if (params_.wide)
376                 lab += '*';
377         setLabel(lab);
378 }
379
380
381 void InsetFloat::sideways(bool s, BufferParams const & bp)
382 {
383         params_.sideways = s;
384         docstring lab = _("float: ") + floatName(params_.type, bp);
385         if (params_.sideways)
386                 lab += _(" (sideways)");
387         setLabel(lab);
388 }
389
390
391 string const InsetFloatMailer::name_("float");
392
393 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
394         : inset_(inset)
395 {}
396
397
398 string const InsetFloatMailer::inset2string(Buffer const &) const
399 {
400         return params2string(inset_.params());
401 }
402
403
404 void InsetFloatMailer::string2params(string const & in,
405                                      InsetFloatParams & params)
406 {
407         params = InsetFloatParams();
408         if (in.empty())
409                 return;
410
411         istringstream data(in);
412         Lexer lex(0,0);
413         lex.setStream(data);
414
415         string name;
416         lex >> name;
417         if (!lex || name != name_)
418                 return print_mailer_error("InsetFloatMailer", in, 1, name_);
419
420         // This is part of the inset proper that is usually swallowed
421         // by Text::readInset
422         string id;
423         lex >> id;
424         if (!lex || id != "Float")
425                 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
426
427         // We have to read the type here!
428         lex >> params.type;
429         params.read(lex);
430 }
431
432
433 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
434 {
435         ostringstream data;
436         data << name_ << ' ';
437         params.write(data);
438         return data.str();
439 }
440
441
442 } // namespace lyx