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