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