]> git.lyx.org Git - features.git/blob - src/insets/InsetFloat.cpp
Sanitize the way we use abort() and exit(). With this commit, we should never crash...
[features.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 "support/debug.h"
22 #include "DispatchResult.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "OutputParams.h"
31 #include "TextClass.h"
32
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         BOOST_ASSERT(false);
114         setLabel(_("float: ") + floatName(type, bp));
115         params_.type = type;
116 }
117
118
119 InsetFloat::~InsetFloat()
120 {
121         InsetFloatMailer(*this).hideDialog();
122 }
123
124
125 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
126 {
127         switch (cmd.action) {
128
129         case LFUN_INSET_MODIFY: {
130                 InsetFloatParams params;
131                 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
132                 params_.placement = params.placement;
133                 params_.wide      = params.wide;
134                 params_.sideways  = params.sideways;
135                 wide(params_.wide, cur.buffer().params());
136                 sideways(params_.sideways, cur.buffer().params());
137                 break;
138         }
139
140         case LFUN_INSET_DIALOG_UPDATE: {
141                 InsetFloatMailer(*this).updateDialog(&cur.bv());
142                 break;
143         }
144
145         case LFUN_MOUSE_RELEASE: {
146                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
147                         InsetFloatMailer(*this).showDialog(&cur.bv());
148                         break;
149                 }
150                 InsetCollapsable::doDispatch(cur, cmd);
151                 break;
152         }
153
154         default:
155                 InsetCollapsable::doDispatch(cur, cmd);
156                 break;
157         }
158 }
159
160
161 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
162                 FuncStatus & flag) const
163 {
164         switch (cmd.action) {
165
166         case LFUN_INSET_MODIFY:
167         case LFUN_INSET_DIALOG_UPDATE:
168                 flag.enabled(true);
169                 return true;
170
171         default:
172                 return InsetCollapsable::getStatus(cur, cmd, flag);
173         }
174 }
175
176
177 void InsetFloat::updateLabels(Buffer const & buf, ParIterator const & it)
178 {
179         Counters & cnts = buf.params().getTextClass().counters();
180         string const saveflt = cnts.current_float();
181
182         // Tell to captions what the current float is
183         cnts.current_float(params().type);
184
185         InsetCollapsable::updateLabels(buf, it);
186
187         //reset afterwards
188         cnts.current_float(saveflt);
189 }
190
191
192 void InsetFloatParams::write(ostream & os) const
193 {
194         os << "Float " << type << '\n';
195
196         if (!placement.empty())
197                 os << "placement " << placement << "\n";
198
199         if (wide)
200                 os << "wide true\n";
201         else
202                 os << "wide false\n";
203
204         if (sideways)
205                 os << "sideways true\n";
206         else
207                 os << "sideways false\n";
208 }
209
210
211 void InsetFloatParams::read(Lexer & lex)
212 {
213         string token;
214         lex >> token;
215         if (token == "placement") {
216                 lex >> placement;
217         } else {
218                 // take countermeasures
219                 lex.pushToken(token);
220         }
221         lex >> token;
222         if (token == "wide") {
223                 lex >> wide;
224         } else {
225                 lyxerr << "InsetFloat::Read:: Missing wide!"
226                 << endl;
227                 // take countermeasures
228                 lex.pushToken(token);
229         }
230         lex >> token;
231         if (token == "sideways") {
232                 lex >> sideways;
233         } else {
234                 lyxerr << "InsetFloat::Read:: Missing sideways!"
235                 << endl;
236                 // take countermeasures
237                 lex.pushToken(token);
238         }
239 }
240
241
242 void InsetFloat::write(Buffer const & buf, ostream & os) const
243 {
244         params_.write(os);
245         InsetCollapsable::write(buf, os);
246 }
247
248
249 void InsetFloat::read(Buffer const & buf, Lexer & lex)
250 {
251         params_.read(lex);
252         wide(params_.wide, buf.params());
253         sideways(params_.sideways, buf.params());
254         InsetCollapsable::read(buf, lex);
255 }
256
257
258 void InsetFloat::validate(LaTeXFeatures & features) const
259 {
260         if (support::contains(params_.placement, 'H')) {
261                 features.require("float");
262         }
263
264         if (params_.sideways)
265                 features.require("rotating");
266
267         features.useFloat(params_.type);
268         InsetCollapsable::validate(features);
269 }
270
271
272 Inset * InsetFloat::clone() const
273 {
274         return new InsetFloat(*this);
275 }
276
277
278 docstring const InsetFloat::editMessage() const
279 {
280         return _("Opened Float Inset");
281 }
282
283
284 int InsetFloat::latex(Buffer const & buf, odocstream & os,
285                       OutputParams const & runparams) const
286 {
287         FloatList const & floats = buf.params().getTextClass().floats();
288         string tmptype = (params_.wide ? params_.type + "*" : params_.type);
289         if (params_.sideways) {
290                 if (params_.type == "table")
291                         tmptype = "sidewaystable";
292                 else if (params_.type == "figure")
293                         tmptype = "sidewaysfigure";
294         }
295         // Figure out the float placement to use.
296         // From lowest to highest:
297         // - float default placement
298         // - document wide default placement
299         // - specific float placement
300         string placement;
301         string const buf_placement = buf.params().float_placement;
302         string const def_placement = floats.defaultPlacement(params_.type);
303         if (!params_.placement.empty()
304             && params_.placement != def_placement) {
305                 placement = params_.placement;
306         } else if (params_.placement.empty()
307                    && !buf_placement.empty()
308                    && buf_placement != def_placement) {
309                 placement = buf_placement;
310         }
311
312         // The \n is used to force \begin{<floatname>} to appear in a new line.
313         // The % is needed to prevent two consecutive \n chars in the case
314         // when the current output line is empty.
315         os << "%\n\\begin{" << from_ascii(tmptype) << '}';
316         // We only output placement if different from the def_placement.
317         // sidewaysfloats always use their own page
318         if (!placement.empty() && !params_.sideways) {
319                 os << '[' << from_ascii(placement) << ']';
320         }
321         os << '\n';
322
323         int const i = InsetText::latex(buf, os, runparams);
324
325         // The \n is used to force \end{<floatname>} to appear in a new line.
326         // In this case, we do not case if the current output line is empty.
327         os << "\n\\end{" << from_ascii(tmptype) << "}\n";
328
329         return i + 4;
330 }
331
332
333 int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
334                           OutputParams const & runparams) const
335 {
336         os << '[' << buf.B_("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
337         InsetText::plaintext(buf, os, runparams);
338         os << "\n]";
339
340         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
341 }
342
343
344 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
345                         OutputParams const & runparams) const
346 {
347         // FIXME UNICODE
348         os << '<' << from_ascii(params_.type) << '>';
349         int const i = InsetText::docbook(buf, os, runparams);
350         os << "</" << from_ascii(params_.type) << '>';
351
352         return i;
353 }
354
355
356 bool InsetFloat::insetAllowed(InsetCode code) const
357 {
358         return code != FLOAT_CODE
359             && code != FOOT_CODE
360             && code != MARGIN_CODE;
361 }
362
363
364 bool InsetFloat::showInsetDialog(BufferView * bv) const
365 {
366         if (!InsetText::showInsetDialog(bv))
367                 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
368         return true;
369 }
370
371
372 void InsetFloat::wide(bool w, BufferParams const & bp)
373 {
374         params_.wide = w;
375         docstring lab = _("float: ") + floatName(params_.type, bp);
376         if (params_.wide)
377                 lab += '*';
378         setLabel(lab);
379 }
380
381
382 void InsetFloat::sideways(bool s, BufferParams const & bp)
383 {
384         params_.sideways = s;
385         docstring lab = _("float: ") + floatName(params_.type, bp);
386         if (params_.sideways)
387                 lab += _(" (sideways)");
388         setLabel(lab);
389 }
390
391
392 string const InsetFloatMailer::name_("float");
393
394 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
395         : inset_(inset)
396 {}
397
398
399 string const InsetFloatMailer::inset2string(Buffer const &) const
400 {
401         return params2string(inset_.params());
402 }
403
404
405 void InsetFloatMailer::string2params(string const & in,
406                                      InsetFloatParams & params)
407 {
408         params = InsetFloatParams();
409         if (in.empty())
410                 return;
411
412         istringstream data(in);
413         Lexer lex(0,0);
414         lex.setStream(data);
415
416         string name;
417         lex >> name;
418         if (!lex || name != name_)
419                 return print_mailer_error("InsetFloatMailer", in, 1, name_);
420
421         // This is part of the inset proper that is usually swallowed
422         // by Text::readInset
423         string id;
424         lex >> id;
425         if (!lex || id != "Float")
426                 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
427
428         // We have to read the type here!
429         lex >> params.type;
430         params.read(lex);
431 }
432
433
434 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
435 {
436         ostringstream data;
437         data << name_ << ' ';
438         params.write(data);
439         return data.str();
440 }
441
442
443 } // namespace lyx