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