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