]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
remove harcoding for Caption layout
[lyx.git] / src / insets / insetfloat.C
1 /**
2  * \file insetfloat.C
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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "insetfloat.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "dispatchresult.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "LaTeXFeatures.h"
27 #include "LColor.h"
28 #include "lyxlex.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "pariterator.h"
32
33 #include "support/lstrings.h"
34 #include "support/convert.h"
35
36 #include <sstream>
37
38 using lyx::support::contains;
39
40 using std::endl;
41 using std::string;
42 using std::auto_ptr;
43 using std::istringstream;
44 using std::ostream;
45 using std::ostringstream;
46
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 namespace {
116
117 string floatname(string const & type, BufferParams const & bp)
118 {
119         FloatList const & floats = bp.getLyXTextClass().floats();
120         FloatList::const_iterator it = floats[type];
121         if (it == floats.end())
122                 return type;
123
124         return _(it->second.name());
125 }
126
127 } // namespace anon
128
129
130 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
131         : InsetCollapsable(bp)
132 {
133         setLabel(_("float: ") + floatname(type, bp));
134         LyXFont font(LyXFont::ALL_SANE);
135         font.decSize();
136         font.decSize();
137         font.setColor(LColor::collapsable);
138         setLabelFont(font);
139         params_.type = type;
140         setInsetName(type);
141 }
142
143
144 InsetFloat::~InsetFloat()
145 {
146         InsetFloatMailer(*this).hideDialog();
147 }
148
149
150 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
151 {
152         switch (cmd.action) {
153
154         case LFUN_INSET_MODIFY: {
155                 InsetFloatParams params;
156                 InsetFloatMailer::string2params(cmd.argument, params);
157                 params_.placement = params.placement;
158                 params_.wide      = params.wide;
159                 params_.sideways  = params.sideways;
160                 wide(params_.wide, cur.buffer().params());
161                 sideways(params_.sideways, cur.buffer().params());
162                 cur.bv().update();
163                 break;
164         }
165
166         case LFUN_INSET_DIALOG_UPDATE: {
167                 InsetFloatMailer(*this).updateDialog(&cur.bv());
168                 break;
169         }
170
171         case LFUN_MOUSE_RELEASE: {
172                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
173                         InsetFloatMailer(*this).showDialog(&cur.bv());
174                         break;
175                 }
176                 InsetCollapsable::doDispatch(cur, cmd);
177                 break;
178         }
179
180         default:
181                 InsetCollapsable::doDispatch(cur, cmd);
182                 break;
183         }
184 }
185
186
187 void InsetFloatParams::write(ostream & os) const
188 {
189         os << "Float " << type << '\n';
190
191         if (!placement.empty())
192                 os << "placement " << placement << "\n";
193
194         if (wide)
195                 os << "wide true\n";
196         else
197                 os << "wide false\n";
198
199         if (sideways)
200                 os << "sideways true\n";
201         else
202                 os << "sideways false\n";
203 }
204
205
206 void InsetFloatParams::read(LyXLex & lex)
207 {
208         string token;
209         lex >> token;
210         if (token == "placement") {
211                 lex >> placement;
212         } else {
213                 // take countermeasures
214                 lex.pushToken(token);
215         }
216         lex >> token;
217         if (token == "wide") {
218                 lex >> wide;
219         } else {
220                 lyxerr << "InsetFloat::Read:: Missing wide!"
221                 << endl;
222                 // take countermeasures
223                 lex.pushToken(token);
224         }
225         lex >> token;
226         if (token == "sideways") {
227                 lex >> sideways;
228         } else {
229                 lyxerr << "InsetFloat::Read:: Missing sideways!"
230                 << endl;
231                 // take countermeasures
232                 lex.pushToken(token);
233         }
234 }
235
236
237 void InsetFloat::write(Buffer const & buf, ostream & os) const
238 {
239         params_.write(os);
240         InsetCollapsable::write(buf, os);
241 }
242
243
244 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
245 {
246         params_.read(lex);
247         wide(params_.wide, buf.params());
248         sideways(params_.sideways, buf.params());
249         InsetCollapsable::read(buf, lex);
250 }
251
252
253 void InsetFloat::validate(LaTeXFeatures & features) const
254 {
255         if (contains(params_.placement, 'H')) {
256                 features.require("float");
257         }
258
259         if (params_.sideways)
260                 features.require("rotating");
261
262         features.useFloat(params_.type);
263         InsetCollapsable::validate(features);
264 }
265
266
267 auto_ptr<InsetBase> InsetFloat::doClone() const
268 {
269         return auto_ptr<InsetBase>(new InsetFloat(*this));
270 }
271
272
273 string const InsetFloat::editMessage() const
274 {
275         return _("Opened Float Inset");
276 }
277
278
279 int InsetFloat::latex(Buffer const & buf, ostream & os,
280                       OutputParams const & runparams) const
281 {
282         FloatList const & floats = buf.params().getLyXTextClass().floats();
283         string tmptype = (params_.wide ? params_.type + "*" : params_.type);
284         if (params_.sideways) {
285                 if (params_.type == "table")
286                         tmptype = "sidewaystable";
287                 else if (params_.type == "figure")
288                         tmptype = "sidewaysfigure";
289         }
290         // Figure out the float placement to use.
291         // From lowest to highest:
292         // - float default placement
293         // - document wide default placement
294         // - specific float placement
295         string placement;
296         string const buf_placement = buf.params().float_placement;
297         string const def_placement = floats.defaultPlacement(params_.type);
298         if (!params_.placement.empty()
299             && params_.placement != def_placement) {
300                 placement = params_.placement;
301         } else if (params_.placement.empty()
302                    && !buf_placement.empty()
303                    && buf_placement != def_placement) {
304                 placement = buf_placement;
305         }
306
307         // The \n is used to force \begin{<floatname>} to appear in a new line.
308         // The % is needed to prevent two consecutive \n chars in the case
309         // when the current output line is empty.
310         os << "%\n\\begin{" << tmptype << '}';
311         // We only output placement if different from the def_placement.
312         // sidewaysfloats always use their own page
313         if (!placement.empty() && !params_.sideways) {
314                 os << '[' << placement << ']';
315         }
316         os << '\n';
317
318         int const i = InsetText::latex(buf, os, runparams);
319
320         // The \n is used to force \end{<floatname>} to appear in a new line.
321         // In this case, we do not case if the current output line is empty.
322         os << "\n\\end{" << tmptype << "}\n";
323
324         return i + 4;
325 }
326
327
328 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
329                          OutputParams const & runparams) const
330 {
331         FloatList const & floats = buf.params().getLyXTextClass().floats();
332         string const tmptype =  params_.type;
333         // Figure out the float placement to use.
334         // From lowest to highest:
335         // - float default placement
336         // - document wide default placement
337         // - specific float placement
338         // This is the same as latex, as linuxdoc is modeled after latex.
339
340         string placement;
341         string const buf_placement = buf.params().float_placement;
342         string const def_placement = floats.defaultPlacement(params_.type);
343         if (!params_.placement.empty()
344             && params_.placement != def_placement) {
345                 placement = params_.placement;
346         } else if (params_.placement.empty()
347                    && !buf_placement.empty()
348                    && buf_placement != def_placement) {
349                 placement = buf_placement;
350         }
351
352         os << "\n<" << tmptype ;
353         // We only output placement if different from the def_placement.
354         if (!placement.empty()) {
355                 os << " loc=\"" << placement << '"';
356         }
357         os << ">";
358
359         int const i = InsetText::linuxdoc(buf, os, runparams);
360         os << "</" << tmptype << ">\n";
361
362         return i;
363 }
364
365
366 int InsetFloat::docbook(Buffer const & buf, ostream & os,
367                         OutputParams const & runparams) const
368 {
369         os << '<' << params_.type << '>';
370         int const i = InsetText::docbook(buf, os, runparams);
371         os << "</" << params_.type << '>';
372
373         return i;
374 }
375
376
377 bool InsetFloat::insetAllowed(InsetBase::Code code) const
378 {
379         return code != InsetBase::FLOAT_CODE
380             && code != InsetBase::FOOT_CODE
381             && code != InsetBase::MARGIN_CODE;
382 }
383
384
385 bool InsetFloat::showInsetDialog(BufferView * bv) const
386 {
387         if (!InsetText::showInsetDialog(bv))
388                 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
389         return true;
390 }
391
392
393 void InsetFloat::wide(bool w, BufferParams const & bp)
394 {
395         params_.wide = w;
396         string lab = _("float: ") + floatname(params_.type, bp);
397         if (params_.wide)
398                 lab += '*';
399         setLabel(lab);
400 }
401
402
403 void InsetFloat::sideways(bool s, BufferParams const & bp)
404 {
405         params_.sideways = s;
406         string lab = _("float: ") + floatname(params_.type, bp);
407         if (params_.sideways)
408                 lab += _(" (sideways)");
409         setLabel(lab);
410 }
411
412
413 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
414 {
415         ParConstIterator pit = par_const_iterator_begin(*this);
416         ParConstIterator end = par_const_iterator_end(*this);
417
418         // Find a caption layout in one of the (child inset's) pars
419         for (; pit != end; ++pit) {
420                 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
421                         string const name = floatname(params_.type, buf.params());
422                         string const str =
423                                 convert<string>(toclist[name].size() + 1)
424                                 + ". " + pit->asString(buf, false);
425                         lyx::toc::TocItem const item(pit->id(), 0 , str);
426                         toclist[name].push_back(item);
427                 }
428         }
429 }
430
431
432 string const InsetFloatMailer::name_("float");
433
434 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
435         : inset_(inset)
436 {}
437
438
439 string const InsetFloatMailer::inset2string(Buffer const &) const
440 {
441         return params2string(inset_.params());
442 }
443
444
445 void InsetFloatMailer::string2params(string const & in,
446                                      InsetFloatParams & params)
447 {
448         params = InsetFloatParams();
449         if (in.empty())
450                 return;
451
452         istringstream data(in);
453         LyXLex lex(0,0);
454         lex.setStream(data);
455
456         string name;
457         lex >> name;
458         if (!lex || name != name_)
459                 return print_mailer_error("InsetFloatMailer", in, 1, name_);
460
461         // This is part of the inset proper that is usually swallowed
462         // by LyXText::readInset
463         string id;
464         lex >> id;
465         if (!lex || id != "Float")
466                 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
467
468         // We have to read the type here!
469         lex >> params.type;
470         params.read(lex);
471 }
472
473
474 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
475 {
476         ostringstream data;
477         data << name_ << ' ';
478         params.write(data);
479         return data.str();
480 }