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