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