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