]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloat.cpp
271090f1614778b41983d678d8214c6b59a22fb6
[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 docstring InsetFloat::xhtml(odocstream & os, OutputParams const & rp) const
288 {
289         FloatList const & floats = buffer().params().documentClass().floats();
290         Floating const & ftype = floats.getType(params_.type);
291         string const htmltype = ftype.htmlType().empty() ? 
292                         "div" : ftype.htmlType();
293         string const htmlclass = ftype.htmlClass().empty() ?
294                         "float-" + params_.type : ftype.htmlClass();
295         docstring const otag = 
296                         from_ascii("<" + htmltype + " class='float " + htmlclass + "'>\n");
297         docstring const ctag = from_ascii("</" + htmltype + ">\n");
298
299         odocstringstream out;
300
301         docstring caption = getCaptionHTML(rp);
302         out << otag;
303         if (!caption.empty())
304                 out << "<div class='float-caption'>" << caption << "</div>\n";
305
306         docstring def = InsetText::xhtml(out, rp);
307         out << ctag;
308
309         if (rp.inFloat == OutputParams::NONFLOAT)
310                 // In this case, this float needs to be deferred, but we'll put it
311                 // before anything the text itself deferred.
312                 def = out.str() + '\n' + def;
313         else 
314                 // In this case, the whole thing is already being deferred, so
315                 // we can write to the stream.
316                 os << out.str();
317         return def;
318 }
319
320
321 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
322 {
323         if (runparams_in.inFloat != OutputParams::NONFLOAT) {
324                 if (runparams_in.moving_arg)
325                         os << "\\protect";
326                 os << "\\subfloat";
327         
328                 OutputParams rp = runparams_in;
329                 docstring const caption = getCaption(rp);
330                 if (!caption.empty()) {
331                         os << caption;
332                 }
333                 os << '{';
334                 rp.inFloat = OutputParams::SUBFLOAT;
335                 int const i = InsetText::latex(os, rp);
336                 os << "}";
337         
338                 return i + 1;
339         }
340         OutputParams runparams(runparams_in);
341         runparams.inFloat = OutputParams::MAINFLOAT;
342
343         FloatList const & floats = buffer().params().documentClass().floats();
344         string tmptype = params_.type;
345         if (params_.sideways)
346                 tmptype = "sideways" + params_.type;
347         if (params_.wide && (!params_.sideways ||
348                              params_.type == "figure" ||
349                              params_.type == "table"))
350                 tmptype += "*";
351         // Figure out the float placement to use.
352         // From lowest to highest:
353         // - float default placement
354         // - document wide default placement
355         // - specific float placement
356         string placement;
357         string const buf_placement = buffer().params().float_placement;
358         string const def_placement = floats.defaultPlacement(params_.type);
359         if (!params_.placement.empty()
360             && params_.placement != def_placement) {
361                 placement = params_.placement;
362         } else if (params_.placement.empty()
363                    && !buf_placement.empty()
364                    && buf_placement != def_placement) {
365                 placement = buf_placement;
366         }
367
368         // The \n is used to force \begin{<floatname>} to appear in a new line.
369         // The % is needed to prevent two consecutive \n chars in the case
370         // when the current output line is empty.
371         os << "%\n\\begin{" << from_ascii(tmptype) << '}';
372         // We only output placement if different from the def_placement.
373         // sidewaysfloats always use their own page
374         if (!placement.empty() && !params_.sideways) {
375                 os << '[' << from_ascii(placement) << ']';
376         }
377         os << '\n';
378
379         int const i = InsetText::latex(os, runparams);
380
381         // The \n is used to force \end{<floatname>} to appear in a new line.
382         // In this case, we do not case if the current output line is empty.
383         os << "\n\\end{" << from_ascii(tmptype) << "}\n";
384
385         return i + 4;
386 }
387
388
389 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
390 {
391         os << '[' << buffer().B_("float") << ' '
392                 << floatName(params_.type, buffer().params()) << ":\n";
393         InsetText::plaintext(os, runparams);
394         os << "\n]";
395
396         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
397 }
398
399
400 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
401 {
402         // FIXME Implement subfloat!
403         // FIXME UNICODE
404         os << '<' << from_ascii(params_.type) << '>';
405         int const i = InsetText::docbook(os, runparams);
406         os << "</" << from_ascii(params_.type) << '>';
407
408         return i;
409 }
410
411
412 bool InsetFloat::insetAllowed(InsetCode code) const
413 {
414         return code != FOOT_CODE
415             && code != MARGIN_CODE
416             && (code != FLOAT_CODE || !params_.subfloat);
417 }
418
419
420 bool InsetFloat::showInsetDialog(BufferView * bv) const
421 {
422         if (!InsetText::showInsetDialog(bv))
423                 bv->showDialog("float", params2string(params()),
424                         const_cast<InsetFloat *>(this));
425         return true;
426 }
427
428
429 void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
430 {
431         params_.wide = w;
432         if (update_label)
433                 setNewLabel(bp);
434 }
435
436
437 void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
438 {
439         params_.sideways = s;
440         if (update_label)
441                 setNewLabel(bp);
442 }
443
444
445 void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
446 {
447         params_.subfloat = s;
448         if (update_label)
449                 setNewLabel(bp);
450 }
451
452
453 void InsetFloat::setNewLabel(BufferParams const & bp)
454 {
455         docstring lab = _("float: ");
456
457         if (params_.subfloat)
458                 lab = _("subfloat: ");
459
460         lab += floatName(params_.type, bp);
461
462         if (params_.wide)
463                 lab += '*';
464
465         if (params_.sideways)
466                 lab += _(" (sideways)");
467
468         setLabel(lab);
469 }
470
471
472 InsetCaption const * InsetFloat::getCaptionInset() const
473 {
474         ParagraphList::const_iterator pit = paragraphs().begin();
475         for (; pit != paragraphs().end(); ++pit) {
476                 InsetList::const_iterator it = pit->insetList().begin();
477                 for (; it != pit->insetList().end(); ++it) {
478                         Inset & inset = *it->inset;
479                         if (inset.lyxCode() == CAPTION_CODE) {
480                                 InsetCaption const * ins =
481                                         static_cast<InsetCaption const *>(it->inset);
482                                 return ins;
483                         }
484                 }
485         }
486         return 0;
487 }
488
489
490 docstring InsetFloat::getCaption(OutputParams const & runparams) const
491 {
492         if (paragraphs().empty())
493                 return docstring();
494
495         InsetCaption const * ins = getCaptionInset();
496         if (ins == 0)
497                 return docstring();
498
499         odocstringstream ods;
500         ins->getOptArg(ods, runparams);
501         ods << '[';
502         ins->getArgument(ods, runparams);
503         ods << ']';
504         return ods.str();
505 }
506
507
508 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
509 {
510         if (paragraphs().empty())
511                 return docstring();
512
513         InsetCaption const * ins = getCaptionInset();
514         if (ins == 0)
515                 return docstring();
516
517         odocstringstream ods;
518         ins->getCaptionText(ods, runparams);
519         return ods.str();
520 }
521
522
523 docstring InsetFloat::getCaptionHTML(OutputParams const & runparams) const
524 {
525         if (paragraphs().empty())
526                 return docstring();
527
528         InsetCaption const * ins = getCaptionInset();
529         if (ins == 0)
530                 return docstring();
531
532         odocstringstream ods;
533         docstring def = ins->getCaptionHTML(ods, runparams);
534         if (!def.empty())
535                 ods << def << '\n';
536         return ods.str();
537 }
538
539
540 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
541 {
542         params = InsetFloatParams();
543         if (in.empty())
544                 return;
545
546         istringstream data(in);
547         Lexer lex;
548         lex.setStream(data);
549         lex.setContext("InsetFloat::string2params");
550         lex >> "float" >> "Float";
551         lex >> params.type; // We have to read the type here!
552         params.read(lex);
553 }
554
555
556 string InsetFloat::params2string(InsetFloatParams const & params)
557 {
558         ostringstream data;
559         data << "float" << ' ';
560         params.write(data);
561         return data.str();
562 }
563
564
565 } // namespace lyx