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