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