]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloat.cpp
a595b2e0321f5560530f58b5abef6d6fd352c9b4
[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/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.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                              || it.innerInsetOfType(WRAP_CODE));
200         // floats can only embed subfloats of their own kind
201         if (subflt)
202                 params_.type = saveflt;
203         setSubfloat(subflt, buffer().params());
204
205         // Tell to captions what the current float is
206         cnts.current_float(params().type);
207         cnts.isSubfloat(subflt);
208
209         InsetCollapsable::updateLabels(it);
210
211         //reset afterwards
212         cnts.current_float(saveflt);
213         cnts.isSubfloat(savesubflt);
214 }
215
216
217 void InsetFloatParams::write(ostream & os) const
218 {
219         os << "Float " << type << '\n';
220
221         if (!placement.empty())
222                 os << "placement " << placement << "\n";
223
224         if (wide)
225                 os << "wide true\n";
226         else
227                 os << "wide false\n";
228
229         if (sideways)
230                 os << "sideways true\n";
231         else
232                 os << "sideways false\n";
233 }
234
235
236 void InsetFloatParams::read(Lexer & lex)
237 {
238         lex.setContext("InsetFloatParams::read");
239         if (lex.checkFor("placement"))
240                 lex >> placement;
241         lex >> "wide" >> wide;
242         lex >> "sideways" >> sideways;
243 }
244
245
246 void InsetFloat::write(ostream & os) const
247 {
248         params_.write(os);
249         InsetCollapsable::write(os);
250 }
251
252
253 void InsetFloat::read(Lexer & lex)
254 {
255         params_.read(lex);
256         setWide(params_.wide, buffer().params());
257         setSideways(params_.sideways, buffer().params());
258         setSubfloat(params_.subfloat, buffer().params());
259         InsetCollapsable::read(lex);
260 }
261
262
263 void InsetFloat::validate(LaTeXFeatures & features) const
264 {
265         if (support::contains(params_.placement, 'H'))
266                 features.require("float");
267
268         if (params_.sideways)
269                 features.require("rotfloat");
270
271         if (params_.subfloat)
272                 features.require("subfig");
273
274         features.useFloat(params_.type, params_.subfloat);
275         InsetCollapsable::validate(features);
276 }
277
278
279 docstring InsetFloat::editMessage() const
280 {
281         return _("Opened Float Inset");
282 }
283
284
285 int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
286 {
287         if (params_.subfloat) {
288                 if (runparams.moving_arg)
289                         os << "\\protect";
290                 os << "\\subfloat";
291         
292                 OutputParams rp = runparams;
293                 docstring const caption = getCaption(rp);
294                 if (!caption.empty()) {
295                         os << caption;
296                 }
297                 os << '{';
298                 int const i = InsetText::latex(os, runparams);
299                 os << "}";
300         
301                 return i + 1;
302         }
303
304         FloatList const & floats = buffer().params().documentClass().floats();
305         string tmptype = params_.type;
306         if (params_.sideways)
307                 tmptype = "sideways" + params_.type;
308         if (params_.wide && (!params_.sideways ||
309                              params_.type == "figure" ||
310                              params_.type == "table"))
311                 tmptype += "*";
312         // Figure out the float placement to use.
313         // From lowest to highest:
314         // - float default placement
315         // - document wide default placement
316         // - specific float placement
317         string placement;
318         string const buf_placement = buffer().params().float_placement;
319         string const def_placement = floats.defaultPlacement(params_.type);
320         if (!params_.placement.empty()
321             && params_.placement != def_placement) {
322                 placement = params_.placement;
323         } else if (params_.placement.empty()
324                    && !buf_placement.empty()
325                    && buf_placement != def_placement) {
326                 placement = buf_placement;
327         }
328
329         // The \n is used to force \begin{<floatname>} to appear in a new line.
330         // The % is needed to prevent two consecutive \n chars in the case
331         // when the current output line is empty.
332         os << "%\n\\begin{" << from_ascii(tmptype) << '}';
333         // We only output placement if different from the def_placement.
334         // sidewaysfloats always use their own page
335         if (!placement.empty() && !params_.sideways) {
336                 os << '[' << from_ascii(placement) << ']';
337         }
338         os << '\n';
339
340         int const i = InsetText::latex(os, runparams);
341
342         // The \n is used to force \end{<floatname>} to appear in a new line.
343         // In this case, we do not case if the current output line is empty.
344         os << "\n\\end{" << from_ascii(tmptype) << "}\n";
345
346         return i + 4;
347 }
348
349
350 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
351 {
352         os << '[' << buffer().B_("float") << ' '
353                 << floatName(params_.type, buffer().params()) << ":\n";
354         InsetText::plaintext(os, runparams);
355         os << "\n]";
356
357         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
358 }
359
360
361 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
362 {
363         // FIXME Implement subfloat!
364         // FIXME UNICODE
365         os << '<' << from_ascii(params_.type) << '>';
366         int const i = InsetText::docbook(os, runparams);
367         os << "</" << from_ascii(params_.type) << '>';
368
369         return i;
370 }
371
372
373 bool InsetFloat::insetAllowed(InsetCode code) const
374 {
375         return code != FOOT_CODE
376             && code != MARGIN_CODE
377             && (code != FLOAT_CODE || !params_.subfloat);
378 }
379
380
381 bool InsetFloat::showInsetDialog(BufferView * bv) const
382 {
383         if (!InsetText::showInsetDialog(bv))
384                 bv->showDialog("float", params2string(params()),
385                         const_cast<InsetFloat *>(this));
386         return true;
387 }
388
389
390 void InsetFloat::setWide(bool w, BufferParams const & bp)
391 {
392         params_.wide = w;
393         docstring lab = _("float: ") + floatName(params_.type, bp);
394         if (params_.wide)
395                 lab += '*';
396         setLabel(lab);
397 }
398
399
400 void InsetFloat::setSideways(bool s, BufferParams const & bp)
401 {
402         params_.sideways = s;
403         docstring lab = _("float: ") + floatName(params_.type, bp);
404         if (params_.sideways)
405                 lab += _(" (sideways)");
406         setLabel(lab);
407 }
408
409
410 void InsetFloat::setSubfloat(bool s, BufferParams const & bp)
411 {
412         params_.subfloat = s;
413         docstring lab = _("float: ") + floatName(params_.type, bp);
414         if (s)
415                 lab = _("subfloat: ") + floatName(params_.type, bp);
416         setLabel(lab);
417 }
418
419
420 docstring InsetFloat::getCaption(OutputParams const & runparams) const
421 {
422         if (paragraphs().empty())
423                 return docstring();
424
425         ParagraphList::const_iterator pit = paragraphs().begin();
426         for (; pit != paragraphs().end(); ++pit) {
427                 InsetList::const_iterator it = pit->insetList().begin();
428                 for (; it != pit->insetList().end(); ++it) {
429                         Inset & inset = *it->inset;
430                         if (inset.lyxCode() == CAPTION_CODE) {
431                                 odocstringstream ods;
432                                 InsetCaption * ins =
433                                         static_cast<InsetCaption *>(it->inset);
434                                 ins->getOptArg(ods, runparams);
435                                 ods << '[';
436                                 ins->getArgument(ods, runparams);
437                                 ods << ']';
438                                 return ods.str();
439                         }
440                 }
441         }
442         return docstring();
443 }
444
445
446 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
447 {
448         if (paragraphs().empty())
449                 return docstring();
450
451         ParagraphList::const_iterator pit = paragraphs().begin();
452         for (; pit != paragraphs().end(); ++pit) {
453                 InsetList::const_iterator it = pit->insetList().begin();
454                 for (; it != pit->insetList().end(); ++it) {
455                         Inset & inset = *it->inset;
456                         if (inset.lyxCode() == CAPTION_CODE) {
457                                 odocstringstream ods;
458                                 InsetCaption * ins =
459                                         static_cast<InsetCaption *>(it->inset);
460                                 ins->getCaptionText(ods, runparams);
461                                 return ods.str();
462                         }
463                 }
464         }
465         return docstring();
466 }
467
468
469 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
470 {
471         params = InsetFloatParams();
472         if (in.empty())
473                 return;
474
475         istringstream data(in);
476         Lexer lex;
477         lex.setStream(data);
478         lex.setContext("InsetFloat::string2params");
479         lex >> "float" >> "Float";
480         lex >> params.type; // We have to read the type here!
481         params.read(lex);
482 }
483
484
485 string InsetFloat::params2string(InsetFloatParams const & params)
486 {
487         ostringstream data;
488         data << "float" << ' ';
489         params.write(data);
490         return data.str();
491 }
492
493
494 } // namespace lyx