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