]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloat.cpp
Fix bug http://bugzilla.lyx.org/show_bug.cgi?id=4910 by creating InsetText::addToToc().
[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 "InsetList.h"
29 #include "LaTeXFeatures.h"
30 #include "Lexer.h"
31 #include "OutputParams.h"
32 #include "ParIterator.h"
33 #include "TextClass.h"
34
35 #include "support/convert.h"
36 #include "support/debug.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/docstream.h"
40
41 #include "frontends/Application.h"
42
43 using namespace std;
44
45
46 namespace lyx {
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(Buffer const & buf, string const & type)
117         : InsetCollapsable(buf), name_(from_utf8(type))
118 {
119         setLabel(_("float: ") + floatName(type, buf.params()));
120         params_.type = type;
121 }
122
123
124 InsetFloat::~InsetFloat()
125 {
126         hideDialogs("float", this);
127 }
128
129
130 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
131 {
132         if (InsetCollapsable::toolTip(bv, x, y).empty())
133                 return docstring();
134
135         docstring default_tip;
136         if (isOpen())
137                 default_tip = _("Left-click to collapse the inset");
138         else
139                 default_tip = _("Left-click to open the inset");
140         OutputParams rp(&buffer().params().encoding());
141         docstring caption_tip = getCaptionText(rp);
142         if (!isOpen() && !caption_tip.empty())
143                 return caption_tip + '\n' + default_tip;
144         return default_tip;
145 }
146
147
148 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
149 {
150         switch (cmd.action) {
151
152         case LFUN_INSET_MODIFY: {
153                 InsetFloatParams params;
154                 string2params(to_utf8(cmd.argument()), params);
155                 params_.placement = params.placement;
156                 params_.wide      = params.wide;
157                 params_.sideways  = params.sideways;
158                 params_.subfloat  = params.subfloat;
159                 setWide(params_.wide, cur.buffer().params());
160                 setSideways(params_.sideways, 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 = buffer().masterBuffer()->params().documentClass().counters();
195         string const saveflt = cnts.current_float();
196         bool const savesubflt = cnts.isSubfloat();
197
198         bool const subflt = it.innerInsetOfType(FLOAT_CODE);
199         // floats can only embed subfloats of their own kind
200         if (subflt)
201                 params_.type = saveflt;
202         setSubfloat(subflt, buffer().params());
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         setWide(params_.wide, buffer().params());
256         setSideways(params_.sideways, buffer().params());
257         setSubfloat(params_.subfloat, buffer().params());
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 (params_.subfloat)
271                 features.require("subfig");
272
273         features.useFloat(params_.type, params_.subfloat);
274         InsetCollapsable::validate(features);
275 }
276
277
278 docstring InsetFloat::editMessage() const
279 {
280         return _("Opened Float Inset");
281 }
282
283
284 int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
285 {
286         if (params_.subfloat) {
287                 if (runparams.moving_arg)
288                         os << "\\protect";
289                 os << "\\subfloat";
290         
291                 OutputParams rp = runparams;
292                 docstring const caption = getCaption(rp);
293                 if (!caption.empty()) {
294                         os << caption;
295                 }
296                 os << '{';
297                 int const i = InsetText::latex(os, runparams);
298                 os << "}";
299         
300                 return i + 1;
301         }
302
303         FloatList const & floats = buffer().params().documentClass().floats();
304         string tmptype = params_.type;
305         if (params_.sideways)
306                 tmptype = "sideways" + params_.type;
307         if (params_.wide && (!params_.sideways ||
308                              params_.type == "figure" ||
309                              params_.type == "table"))
310                 tmptype += "*";
311         // Figure out the float placement to use.
312         // From lowest to highest:
313         // - float default placement
314         // - document wide default placement
315         // - specific float placement
316         string placement;
317         string const buf_placement = buffer().params().float_placement;
318         string const def_placement = floats.defaultPlacement(params_.type);
319         if (!params_.placement.empty()
320             && params_.placement != def_placement) {
321                 placement = params_.placement;
322         } else if (params_.placement.empty()
323                    && !buf_placement.empty()
324                    && buf_placement != def_placement) {
325                 placement = buf_placement;
326         }
327
328         // The \n is used to force \begin{<floatname>} to appear in a new line.
329         // The % is needed to prevent two consecutive \n chars in the case
330         // when the current output line is empty.
331         os << "%\n\\begin{" << from_ascii(tmptype) << '}';
332         // We only output placement if different from the def_placement.
333         // sidewaysfloats always use their own page
334         if (!placement.empty() && !params_.sideways) {
335                 os << '[' << from_ascii(placement) << ']';
336         }
337         os << '\n';
338
339         int const i = InsetText::latex(os, runparams);
340
341         // The \n is used to force \end{<floatname>} to appear in a new line.
342         // In this case, we do not case if the current output line is empty.
343         os << "\n\\end{" << from_ascii(tmptype) << "}\n";
344
345         return i + 4;
346 }
347
348
349 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
350 {
351         os << '[' << buffer().B_("float") << ' '
352                 << floatName(params_.type, buffer().params()) << ":\n";
353         InsetText::plaintext(os, runparams);
354         os << "\n]";
355
356         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
357 }
358
359
360 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
361 {
362         // FIXME Implement subfloat!
363         // FIXME UNICODE
364         os << '<' << from_ascii(params_.type) << '>';
365         int const i = InsetText::docbook(os, runparams);
366         os << "</" << from_ascii(params_.type) << '>';
367
368         return i;
369 }
370
371
372 bool InsetFloat::insetAllowed(InsetCode code) const
373 {
374         return code != FOOT_CODE
375             && code != MARGIN_CODE
376             && (code != FLOAT_CODE || !params_.subfloat);
377 }
378
379
380 bool InsetFloat::showInsetDialog(BufferView * bv) const
381 {
382         if (!InsetText::showInsetDialog(bv))
383                 bv->showDialog("float", params2string(params()),
384                         const_cast<InsetFloat *>(this));
385         return true;
386 }
387
388
389 void InsetFloat::setWide(bool w, BufferParams const & bp)
390 {
391         params_.wide = w;
392         docstring lab = _("float: ") + floatName(params_.type, bp);
393         if (params_.wide)
394                 lab += '*';
395         setLabel(lab);
396 }
397
398
399 void InsetFloat::setSideways(bool s, BufferParams const & bp)
400 {
401         params_.sideways = s;
402         docstring lab = _("float: ") + floatName(params_.type, bp);
403         if (params_.sideways)
404                 lab += _(" (sideways)");
405         setLabel(lab);
406 }
407
408
409 void InsetFloat::setSubfloat(bool s, BufferParams const & bp)
410 {
411         params_.subfloat = s;
412         docstring lab = _("float: ") + floatName(params_.type, bp);
413         if (s)
414                 lab = _("subfloat: ") + floatName(params_.type, bp);
415         setLabel(lab);
416 }
417
418
419 docstring InsetFloat::getCaption(OutputParams const & runparams) const
420 {
421         if (paragraphs().empty())
422                 return docstring();
423
424         ParagraphList::const_iterator pit = paragraphs().begin();
425         for (; pit != paragraphs().end(); ++pit) {
426                 InsetList::const_iterator it = pit->insetList().begin();
427                 for (; it != pit->insetList().end(); ++it) {
428                         Inset & inset = *it->inset;
429                         if (inset.lyxCode() == CAPTION_CODE) {
430                                 odocstringstream ods;
431                                 InsetCaption * ins =
432                                         static_cast<InsetCaption *>(it->inset);
433                                 ins->getOptArg(ods, runparams);
434                                 ods << '[';
435                                 ins->getArgument(ods, runparams);
436                                 ods << ']';
437                                 return ods.str();
438                         }
439                 }
440         }
441         return docstring();
442 }
443
444
445 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
446 {
447         if (paragraphs().empty())
448                 return docstring();
449
450         ParagraphList::const_iterator pit = paragraphs().begin();
451         for (; pit != paragraphs().end(); ++pit) {
452                 InsetList::const_iterator it = pit->insetList().begin();
453                 for (; it != pit->insetList().end(); ++it) {
454                         Inset & inset = *it->inset;
455                         if (inset.lyxCode() == CAPTION_CODE) {
456                                 odocstringstream ods;
457                                 InsetCaption * ins =
458                                         static_cast<InsetCaption *>(it->inset);
459                                 ins->getCaptionText(ods, runparams);
460                                 return ods.str();
461                         }
462                 }
463         }
464         return docstring();
465 }
466
467
468 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
469 {
470         params = InsetFloatParams();
471         if (in.empty())
472                 return;
473
474         istringstream data(in);
475         Lexer lex;
476         lex.setStream(data);
477         lex.setContext("InsetFloat::string2params");
478         lex >> "float" >> "Float";
479         lex >> params.type; // We have to read the type here!
480         params.read(lex);
481 }
482
483
484 string InsetFloat::params2string(InsetFloatParams const & params)
485 {
486         ostringstream data;
487         data << "float" << ' ';
488         params.write(data);
489         return data.str();
490 }
491
492
493 } // namespace lyx