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