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