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