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