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