]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloat.cpp
39f7870987b2a500416b25721442c3849d2f6851
[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  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetFloat.h"
16 #include "InsetCaption.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Counters.h"
22 #include "Cursor.h"
23 #include "DispatchResult.h"
24 #include "Floating.h"
25 #include "FloatList.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "OutputParams.h"
31 #include "ParIterator.h"
32 #include "TextClass.h"
33
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38
39 #include "frontends/Application.h"
40
41 using namespace std;
42
43
44 namespace lyx {
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(Buffer const & buf, string const & type)
115         : InsetCollapsable(buf), name_(from_utf8(type))
116 {
117         setLabel(_("float: ") + floatName(type, buf.params()));
118         params_.type = type;
119 }
120
121
122 InsetFloat::~InsetFloat()
123 {
124         hideDialogs("float", this);
125 }
126
127
128 docstring InsetFloat::name() const 
129
130         return "Float:" + name_; 
131 }
132
133
134 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
135 {
136         if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
137                 return docstring();
138
139         OutputParams rp(&buffer().params().encoding());
140         return getCaptionText(rp);
141 }
142
143
144 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
145 {
146         switch (cmd.action) {
147
148         case LFUN_INSET_MODIFY: {
149                 InsetFloatParams params;
150                 string2params(to_utf8(cmd.argument()), params);
151
152                 // placement, wide and sideways are not used for subfloats
153                 if (!params_.subfloat) {
154                         params_.placement = params.placement;
155                         params_.wide      = params.wide;
156                         params_.sideways  = params.sideways;
157                         setWide(params_.wide, cur.buffer()->params(), false);
158                         setSideways(params_.sideways, cur.buffer()->params(), false);
159                 }
160
161                 setNewLabel(cur.buffer()->params());
162                 break;
163         }
164
165         case LFUN_INSET_DIALOG_UPDATE: {
166                 cur.bv().updateDialog("float", params2string(params()));
167                 break;
168         }
169
170         default:
171                 InsetCollapsable::doDispatch(cur, cmd);
172                 break;
173         }
174 }
175
176
177 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
178                 FuncStatus & flag) const
179 {
180         switch (cmd.action) {
181
182         case LFUN_INSET_MODIFY:
183         case LFUN_INSET_DIALOG_UPDATE:
184                 flag.setEnabled(true);
185                 return true;
186
187         default:
188                 return InsetCollapsable::getStatus(cur, cmd, flag);
189         }
190 }
191
192
193 void InsetFloat::updateLabels(ParIterator const & it)
194 {
195         Counters & cnts =
196                 buffer().masterBuffer()->params().documentClass().counters();
197         string const saveflt = cnts.current_float();
198         bool const savesubflt = cnts.isSubfloat();
199
200         bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
201                              || it.innerInsetOfType(WRAP_CODE));
202         // floats can only embed subfloats of their own kind
203         if (subflt)
204                 params_.type = saveflt;
205         setSubfloat(subflt, buffer().params());
206
207         // Tell to captions what the current float is
208         cnts.current_float(params().type);
209         cnts.isSubfloat(subflt);
210
211         InsetCollapsable::updateLabels(it);
212
213         //reset afterwards
214         cnts.current_float(saveflt);
215         cnts.isSubfloat(savesubflt);
216 }
217
218
219 void InsetFloatParams::write(ostream & os) const
220 {
221         os << "Float " << type << '\n';
222
223         if (!placement.empty())
224                 os << "placement " << placement << "\n";
225
226         if (wide)
227                 os << "wide true\n";
228         else
229                 os << "wide false\n";
230
231         if (sideways)
232                 os << "sideways true\n";
233         else
234                 os << "sideways false\n";
235 }
236
237
238 void InsetFloatParams::read(Lexer & lex)
239 {
240         lex.setContext("InsetFloatParams::read");
241         if (lex.checkFor("placement"))
242                 lex >> placement;
243         lex >> "wide" >> wide;
244         lex >> "sideways" >> sideways;
245 }
246
247
248 void InsetFloat::write(ostream & os) const
249 {
250         params_.write(os);
251         InsetCollapsable::write(os);
252 }
253
254
255 void InsetFloat::read(Lexer & lex)
256 {
257         params_.read(lex);
258         InsetCollapsable::read(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         if (params_.sideways)
268                 features.require("rotfloat");
269
270         if (features.inFloat())
271                 features.require("subfig");
272
273         features.useFloat(params_.type, features.inFloat());
274         features.inFloat(true);
275         InsetCollapsable::validate(features);
276         features.inFloat(false);
277 }
278
279
280 docstring InsetFloat::editMessage() const
281 {
282         return _("Opened Float Inset");
283 }
284
285
286 docstring InsetFloat::xhtml(odocstream & os, OutputParams const & rp) const
287 {
288         FloatList const & floats = buffer().params().documentClass().floats();
289         Floating const & ftype = floats.getType(params_.type);
290         string const htmltype = ftype.htmlType().empty() ? 
291                         "div" : ftype.htmlType();
292         string const htmlclass = ftype.htmlClass().empty() ?
293                         "float-" + params_.type : ftype.htmlClass();
294         docstring const otag = 
295                         from_ascii("<" + htmltype + " class='float " + htmlclass + "'>\n");
296         docstring const ctag = from_ascii("</" + htmltype + ">\n");
297
298         odocstringstream out;
299
300         out << otag;
301         docstring def = InsetText::xhtml(out, rp);
302         out << ctag;
303
304         if (rp.inFloat == OutputParams::NONFLOAT)
305                 // In this case, this float needs to be deferred, but we'll put it
306                 // before anything the text itself deferred.
307                 def = out.str() + '\n' + def;
308         else 
309                 // In this case, the whole thing is already being deferred, so
310                 // we can write to the stream.
311                 os << out.str();
312         return def;
313 }
314
315
316 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
317 {
318         if (runparams_in.inFloat != OutputParams::NONFLOAT) {
319                 if (runparams_in.moving_arg)
320                         os << "\\protect";
321                 os << "\\subfloat";
322         
323                 OutputParams rp = runparams_in;
324                 docstring const caption = getCaption(rp);
325                 if (!caption.empty()) {
326                         os << caption;
327                 }
328                 os << '{';
329                 rp.inFloat = OutputParams::SUBFLOAT;
330                 int const i = InsetText::latex(os, rp);
331                 os << "}";
332         
333                 return i + 1;
334         }
335         OutputParams runparams(runparams_in);
336         runparams.inFloat = OutputParams::MAINFLOAT;
337
338         FloatList const & floats = buffer().params().documentClass().floats();
339         string tmptype = params_.type;
340         if (params_.sideways)
341                 tmptype = "sideways" + params_.type;
342         if (params_.wide && (!params_.sideways ||
343                              params_.type == "figure" ||
344                              params_.type == "table"))
345                 tmptype += "*";
346         // Figure out the float placement to use.
347         // From lowest to highest:
348         // - float default placement
349         // - document wide default placement
350         // - specific float placement
351         string placement;
352         string const buf_placement = buffer().params().float_placement;
353         string const def_placement = floats.defaultPlacement(params_.type);
354         if (!params_.placement.empty()
355             && params_.placement != def_placement) {
356                 placement = params_.placement;
357         } else if (params_.placement.empty()
358                    && !buf_placement.empty()
359                    && buf_placement != def_placement) {
360                 placement = buf_placement;
361         }
362
363         // The \n is used to force \begin{<floatname>} to appear in a new line.
364         // The % is needed to prevent two consecutive \n chars in the case
365         // when the current output line is empty.
366         os << "%\n\\begin{" << from_ascii(tmptype) << '}';
367         // We only output placement if different from the def_placement.
368         // sidewaysfloats always use their own page
369         if (!placement.empty() && !params_.sideways) {
370                 os << '[' << from_ascii(placement) << ']';
371         }
372         os << '\n';
373
374         int const i = InsetText::latex(os, runparams);
375
376         // The \n is used to force \end{<floatname>} to appear in a new line.
377         // In this case, we do not case if the current output line is empty.
378         os << "\n\\end{" << from_ascii(tmptype) << "}\n";
379
380         return i + 4;
381 }
382
383
384 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
385 {
386         os << '[' << buffer().B_("float") << ' '
387                 << floatName(params_.type, buffer().params()) << ":\n";
388         InsetText::plaintext(os, runparams);
389         os << "\n]";
390
391         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
392 }
393
394
395 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
396 {
397         // FIXME Implement subfloat!
398         // FIXME UNICODE
399         os << '<' << from_ascii(params_.type) << '>';
400         int const i = InsetText::docbook(os, runparams);
401         os << "</" << from_ascii(params_.type) << '>';
402
403         return i;
404 }
405
406
407 bool InsetFloat::insetAllowed(InsetCode code) const
408 {
409         return code != FOOT_CODE
410             && code != MARGIN_CODE
411             && (code != FLOAT_CODE || !params_.subfloat);
412 }
413
414
415 bool InsetFloat::showInsetDialog(BufferView * bv) const
416 {
417         if (!InsetText::showInsetDialog(bv))
418                 bv->showDialog("float", params2string(params()),
419                         const_cast<InsetFloat *>(this));
420         return true;
421 }
422
423
424 void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
425 {
426         params_.wide = w;
427         if (update_label)
428                 setNewLabel(bp);
429 }
430
431
432 void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
433 {
434         params_.sideways = s;
435         if (update_label)
436                 setNewLabel(bp);
437 }
438
439
440 void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
441 {
442         params_.subfloat = s;
443         if (update_label)
444                 setNewLabel(bp);
445 }
446
447
448 void InsetFloat::setNewLabel(BufferParams const & bp)
449 {
450         docstring lab = _("float: ");
451
452         if (params_.subfloat)
453                 lab = _("subfloat: ");
454
455         lab += floatName(params_.type, bp);
456
457         if (params_.wide)
458                 lab += '*';
459
460         if (params_.sideways)
461                 lab += _(" (sideways)");
462
463         setLabel(lab);
464 }
465
466
467 docstring InsetFloat::getCaption(OutputParams const & runparams) const
468 {
469         if (paragraphs().empty())
470                 return docstring();
471
472         InsetCaption const * ins = getCaptionInset();
473         if (ins == 0)
474                 return docstring();
475
476         odocstringstream ods;
477         ins->getOptArg(ods, runparams);
478         ods << '[';
479         ins->getArgument(ods, runparams);
480         ods << ']';
481         return ods.str();
482 }
483
484
485 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
486 {
487         params = InsetFloatParams();
488         if (in.empty())
489                 return;
490
491         istringstream data(in);
492         Lexer lex;
493         lex.setStream(data);
494         lex.setContext("InsetFloat::string2params");
495         lex >> "float" >> "Float";
496         lex >> params.type; // We have to read the type here!
497         params.read(lex);
498 }
499
500
501 string InsetFloat::params2string(InsetFloatParams const & params)
502 {
503         ostringstream data;
504         data << "float" << ' ';
505         params.write(data);
506         return data.str();
507 }
508
509
510 } // namespace lyx