]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloat.cpp
Get InsetCollapsable working, at least to some extent.
[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/debug.h"
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39
40 #include "frontends/Application.h"
41
42 using namespace std;
43
44
45 namespace lyx {
46
47 // With this inset it will be possible to support the latex package
48 // float.sty, and I am sure that with this and some additional support
49 // classes we can support similar functionality in other formats
50 // (read DocBook).
51 // By using float.sty we will have the same handling for all floats, both
52 // for those already in existance (table and figure) and all user created
53 // ones¹. So suddenly we give the users the possibility of creating new
54 // kinds of floats on the fly. (and with a uniform look)
55 //
56 // API to float.sty:
57 //   \newfloat{type}{placement}{ext}[within]
58 //     type      - The "type" of the new class of floats, like program or
59 //                 algorithm. After the appropriate \newfloat, commands
60 //                 such as \begin{program} or \end{algorithm*} will be
61 //                 available.
62 //     placement - The default placement for the given class of floats.
63 //                 They are like in standard LaTeX: t, b, p and h for top,
64 //                 bottom, page, and here, respectively. On top of that
65 //                 there is a new type, H, which does not really correspond
66 //                 to a float, since it means: put it "here" and nowhere else.
67 //                 Note, however that the H specifier is special and, because
68 //                 of implementation details cannot be used in the second
69 //                 argument of \newfloat.
70 //     ext       - The file name extension of an auxiliary file for the list
71 //                 of figures (or whatever). LaTeX writes the captions to
72 //                 this file.
73 //     within    - This (optional) argument determines whether floats of this
74 //                 class will be numbered within some sectional unit of the
75 //                 document. For example, if within is equal to chapter, the
76 //                 floats will be numbered within chapters.
77 //   \floatstyle{style}
78 //     style -  plain, boxed, ruled
79 //   \floatname{float}{floatname}
80 //     float     -
81 //     floatname -
82 //   \floatplacement{float}{placement}
83 //     float     -
84 //     placement -
85 //   \restylefloat{float}
86 //     float -
87 //   \listof{type}{title}
88 //     title -
89
90 // ¹ the algorithm float is defined using the float.sty package. Like this
91 //   \floatstyle{ruled}
92 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
93 //   \floatname{algorithm}{Algorithm}
94 //
95 // The intention is that floats should be definable from two places:
96 //          - layout files
97 //          - the "gui" (i.e. by the user)
98 //
99 // From layout files.
100 // This should only be done for floats defined in a documentclass and that
101 // does not need any additional packages. The two most known floats in this
102 // category is "table" and "figure". Floats defined in layout files are only
103 // stored in lyx files if the user modifies them.
104 //
105 // By the user.
106 // There should be a gui dialog (and also a collection of lyxfuncs) where
107 // the user can modify existing floats and/or create new ones.
108 //
109 // The individual floats will also have some settable
110 // variables: wide and placement.
111 //
112 // Lgb
113
114
115 InsetFloat::InsetFloat(Buffer const & buf, string const & type)
116         : InsetCollapsable(buf), name_(from_utf8(type))
117 {
118         setLabel(_("float: ") + floatName(type, buf.params()));
119         params_.type = type;
120 }
121
122
123 InsetFloat::~InsetFloat()
124 {
125         hideDialogs("float", this);
126 }
127
128
129 docstring InsetFloat::name() const 
130
131         return "Float:" + name_; 
132 }
133
134
135 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
136 {
137         if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
138                 return docstring();
139
140         OutputParams rp(&buffer().params().encoding());
141         return getCaptionText(rp);
142 }
143
144
145 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
146 {
147         switch (cmd.action) {
148
149         case LFUN_INSET_MODIFY: {
150                 InsetFloatParams params;
151                 string2params(to_utf8(cmd.argument()), params);
152
153                 // placement, wide and sideways are not used for subfloats
154                 if (!params_.subfloat) {
155                         params_.placement = params.placement;
156                         params_.wide      = params.wide;
157                         params_.sideways  = params.sideways;
158                         setWide(params_.wide, cur.buffer()->params(), false);
159                         setSideways(params_.sideways, cur.buffer()->params(), false);
160                 }
161
162                 setNewLabel(cur.buffer()->params());
163                 break;
164         }
165
166         case LFUN_INSET_DIALOG_UPDATE: {
167                 cur.bv().updateDialog("float", params2string(params()));
168                 break;
169         }
170
171         default:
172                 InsetCollapsable::doDispatch(cur, cmd);
173                 break;
174         }
175 }
176
177
178 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
179                 FuncStatus & flag) const
180 {
181         switch (cmd.action) {
182
183         case LFUN_INSET_MODIFY:
184         case LFUN_INSET_DIALOG_UPDATE:
185                 flag.setEnabled(true);
186                 return true;
187
188         default:
189                 return InsetCollapsable::getStatus(cur, cmd, flag);
190         }
191 }
192
193
194 void InsetFloat::updateLabels(ParIterator const & it)
195 {
196         Counters & cnts =
197                 buffer().masterBuffer()->params().documentClass().counters();
198         string const saveflt = cnts.current_float();
199         bool const savesubflt = cnts.isSubfloat();
200
201         bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
202                              || it.innerInsetOfType(WRAP_CODE));
203         // floats can only embed subfloats of their own kind
204         if (subflt)
205                 params_.type = saveflt;
206         setSubfloat(subflt, buffer().params());
207
208         // Tell to captions what the current float is
209         cnts.current_float(params().type);
210         cnts.isSubfloat(subflt);
211
212         InsetCollapsable::updateLabels(it);
213
214         //reset afterwards
215         cnts.current_float(saveflt);
216         cnts.isSubfloat(savesubflt);
217 }
218
219
220 void InsetFloatParams::write(ostream & os) const
221 {
222         os << "Float " << type << '\n';
223
224         if (!placement.empty())
225                 os << "placement " << placement << "\n";
226
227         if (wide)
228                 os << "wide true\n";
229         else
230                 os << "wide false\n";
231
232         if (sideways)
233                 os << "sideways true\n";
234         else
235                 os << "sideways false\n";
236 }
237
238
239 void InsetFloatParams::read(Lexer & lex)
240 {
241         lex.setContext("InsetFloatParams::read");
242         if (lex.checkFor("placement"))
243                 lex >> placement;
244         lex >> "wide" >> wide;
245         lex >> "sideways" >> sideways;
246 }
247
248
249 void InsetFloat::write(ostream & os) const
250 {
251         params_.write(os);
252         InsetCollapsable::write(os);
253 }
254
255
256 void InsetFloat::read(Lexer & lex)
257 {
258         params_.read(lex);
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 (features.inFloat())
272                 features.require("subfig");
273
274         features.useFloat(params_.type, features.inFloat());
275         features.inFloat(true);
276         InsetCollapsable::validate(features);
277         features.inFloat(false);
278 }
279
280
281 docstring InsetFloat::editMessage() const
282 {
283         return _("Opened Float Inset");
284 }
285
286
287 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
288 {
289         if (runparams_in.inFloat != OutputParams::NONFLOAT) {
290                 if (runparams_in.moving_arg)
291                         os << "\\protect";
292                 os << "\\subfloat";
293         
294                 OutputParams rp = runparams_in;
295                 docstring const caption = getCaption(rp);
296                 if (!caption.empty()) {
297                         os << caption;
298                 }
299                 os << '{';
300                 rp.inFloat = OutputParams::SUBFLOAT;
301                 int const i = InsetText::latex(os, rp);
302                 os << "}";
303         
304                 return i + 1;
305         }
306         OutputParams runparams(runparams_in);
307         runparams.inFloat = OutputParams::MAINFLOAT;
308
309         FloatList const & floats = buffer().params().documentClass().floats();
310         string tmptype = params_.type;
311         if (params_.sideways)
312                 tmptype = "sideways" + params_.type;
313         if (params_.wide && (!params_.sideways ||
314                              params_.type == "figure" ||
315                              params_.type == "table"))
316                 tmptype += "*";
317         // Figure out the float placement to use.
318         // From lowest to highest:
319         // - float default placement
320         // - document wide default placement
321         // - specific float placement
322         string placement;
323         string const buf_placement = buffer().params().float_placement;
324         string const def_placement = floats.defaultPlacement(params_.type);
325         if (!params_.placement.empty()
326             && params_.placement != def_placement) {
327                 placement = params_.placement;
328         } else if (params_.placement.empty()
329                    && !buf_placement.empty()
330                    && buf_placement != def_placement) {
331                 placement = buf_placement;
332         }
333
334         // The \n is used to force \begin{<floatname>} to appear in a new line.
335         // The % is needed to prevent two consecutive \n chars in the case
336         // when the current output line is empty.
337         os << "%\n\\begin{" << from_ascii(tmptype) << '}';
338         // We only output placement if different from the def_placement.
339         // sidewaysfloats always use their own page
340         if (!placement.empty() && !params_.sideways) {
341                 os << '[' << from_ascii(placement) << ']';
342         }
343         os << '\n';
344
345         int const i = InsetText::latex(os, runparams);
346
347         // The \n is used to force \end{<floatname>} to appear in a new line.
348         // In this case, we do not case if the current output line is empty.
349         os << "\n\\end{" << from_ascii(tmptype) << "}\n";
350
351         return i + 4;
352 }
353
354
355 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
356 {
357         os << '[' << buffer().B_("float") << ' '
358                 << floatName(params_.type, buffer().params()) << ":\n";
359         InsetText::plaintext(os, runparams);
360         os << "\n]";
361
362         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
363 }
364
365
366 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
367 {
368         // FIXME Implement subfloat!
369         // FIXME UNICODE
370         os << '<' << from_ascii(params_.type) << '>';
371         int const i = InsetText::docbook(os, runparams);
372         os << "</" << from_ascii(params_.type) << '>';
373
374         return i;
375 }
376
377
378 bool InsetFloat::insetAllowed(InsetCode code) const
379 {
380         return code != FOOT_CODE
381             && code != MARGIN_CODE
382             && (code != FLOAT_CODE || !params_.subfloat);
383 }
384
385
386 bool InsetFloat::showInsetDialog(BufferView * bv) const
387 {
388         if (!InsetText::showInsetDialog(bv))
389                 bv->showDialog("float", params2string(params()),
390                         const_cast<InsetFloat *>(this));
391         return true;
392 }
393
394
395 void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
396 {
397         params_.wide = w;
398         if (update_label)
399                 setNewLabel(bp);
400 }
401
402
403 void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
404 {
405         params_.sideways = s;
406         if (update_label)
407                 setNewLabel(bp);
408 }
409
410
411 void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
412 {
413         params_.subfloat = s;
414         if (update_label)
415                 setNewLabel(bp);
416 }
417
418
419 void InsetFloat::setNewLabel(BufferParams const & bp)
420 {
421         docstring lab = _("float: ");
422
423         if (params_.subfloat)
424                 lab = _("subfloat: ");
425
426         lab += floatName(params_.type, bp);
427
428         if (params_.wide)
429                 lab += '*';
430
431         if (params_.sideways)
432                 lab += _(" (sideways)");
433
434         setLabel(lab);
435 }
436
437
438 docstring InsetFloat::getCaption(OutputParams const & runparams) const
439 {
440         if (paragraphs().empty())
441                 return docstring();
442
443         ParagraphList::const_iterator pit = paragraphs().begin();
444         for (; pit != paragraphs().end(); ++pit) {
445                 InsetList::const_iterator it = pit->insetList().begin();
446                 for (; it != pit->insetList().end(); ++it) {
447                         Inset & inset = *it->inset;
448                         if (inset.lyxCode() == CAPTION_CODE) {
449                                 odocstringstream ods;
450                                 InsetCaption * ins =
451                                         static_cast<InsetCaption *>(it->inset);
452                                 ins->getOptArg(ods, runparams);
453                                 ods << '[';
454                                 ins->getArgument(ods, runparams);
455                                 ods << ']';
456                                 return ods.str();
457                         }
458                 }
459         }
460         return docstring();
461 }
462
463
464 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
465 {
466         if (paragraphs().empty())
467                 return docstring();
468
469         ParagraphList::const_iterator pit = paragraphs().begin();
470         for (; pit != paragraphs().end(); ++pit) {
471                 InsetList::const_iterator it = pit->insetList().begin();
472                 for (; it != pit->insetList().end(); ++it) {
473                         Inset & inset = *it->inset;
474                         if (inset.lyxCode() == CAPTION_CODE) {
475                                 odocstringstream ods;
476                                 InsetCaption * ins =
477                                         static_cast<InsetCaption *>(it->inset);
478                                 ins->getCaptionText(ods, runparams);
479                                 return ods.str();
480                         }
481                 }
482         }
483         return docstring();
484 }
485
486
487 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
488 {
489         params = InsetFloatParams();
490         if (in.empty())
491                 return;
492
493         istringstream data(in);
494         Lexer lex;
495         lex.setStream(data);
496         lex.setContext("InsetFloat::string2params");
497         lex >> "float" >> "Float";
498         lex >> params.type; // We have to read the type here!
499         params.read(lex);
500 }
501
502
503 string InsetFloat::params2string(InsetFloatParams const & params)
504 {
505         ostringstream data;
506         data << "float" << ' ';
507         params.write(data);
508         return data.str();
509 }
510
511
512 } // namespace lyx