]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
the dispatch patch
[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 "Floating.h"
21 #include "FloatList.h"
22 #include "funcrequest.h"
23 #include "gettext.h"
24 #include "iterators.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "lyxlex.h"
28 #include "paragraph.h"
29
30 #include "support/lstrings.h"
31 #include "support/tostr.h"
32
33 #include "support/std_sstream.h"
34
35 using lyx::support::contains;
36
37 using std::endl;
38 using std::string;
39 using std::auto_ptr;
40 using std::istringstream;
41 using std::ostream;
42 using std::ostringstream;
43
44
45 // With this inset it will be possible to support the latex package
46 // float.sty, and I am sure that with this and some additional support
47 // classes we can support similar functionality in other formats
48 // (read DocBook).
49 // By using float.sty we will have the same handling for all floats, both
50 // for those already in existance (table and figure) and all user created
51 // ones¹. So suddenly we give the users the possibility of creating new
52 // kinds of floats on the fly. (and with a uniform look)
53 //
54 // API to float.sty:
55 //   \newfloat{type}{placement}{ext}[within]
56 //     type      - The "type" of the new class of floats, like program or
57 //                 algorithm. After the appropriate \newfloat, commands
58 //                 such as \begin{program} or \end{algorithm*} will be
59 //                 available.
60 //     placement - The default placement for the given class of floats.
61 //                 They are like in standard LaTeX: t, b, p and h for top,
62 //                 bottom, page, and here, respectively. On top of that
63 //                 there is a new type, H, which does not really correspond
64 //                 to a float, since it means: put it "here" and nowhere else.
65 //                 Note, however that the H specifier is special and, because
66 //                 of implementation details cannot be used in the second
67 //                 argument of \newfloat.
68 //     ext       - The file name extension of an auxiliary file for the list
69 //                 of figures (or whatever). LaTeX writes the captions to
70 //                 this file.
71 //     within    - This (optional) argument determines whether floats of this
72 //                 class will be numbered within some sectional unit of the
73 //                 document. For example, if within is equal to chapter, the
74 //                 floats will be numbered within chapters.
75 //   \floatstyle{style}
76 //     style -  plain, boxed, ruled
77 //   \floatname{float}{floatname}
78 //     float     -
79 //     floatname -
80 //   \floatplacement{float}{placement}
81 //     float     -
82 //     placement -
83 //   \restylefloat{float}
84 //     float -
85 //   \listof{type}{title}
86 //     title -
87
88 // ¹ the algorithm float is defined using the float.sty package. Like this
89 //   \floatstyle{ruled}
90 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
91 //   \floatname{algorithm}{Algorithm}
92 //
93 // The intention is that floats should be definable from two places:
94 //          - layout files
95 //          - the "gui" (i.e. by the user)
96 //
97 // From layout files.
98 // This should only be done for floats defined in a documentclass and that
99 // does not need any additional packages. The two most known floats in this
100 // category is "table" and "figure". Floats defined in layout files are only
101 // stored in lyx files if the user modifies them.
102 //
103 // By the user.
104 // There should be a gui dialog (and also a collection of lyxfuncs) where
105 // the user can modify existing floats and/or create new ones.
106 //
107 // The individual floats will also have some settable
108 // variables: wide and placement.
109 //
110 // Lgb
111
112 namespace {
113
114 // this should not be hardcoded, but be part of the definition
115 // of the float (JMarc)
116 string const caplayout("Caption");
117
118 string floatname(string const & type, BufferParams const & bp)
119 {
120         FloatList const & floats = bp.getLyXTextClass().floats();
121         FloatList::const_iterator it = floats[type];
122         if (it == floats.end())
123                 return type;
124
125         return _(it->second.name());
126 }
127
128 } // namespace anon
129
130
131 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
132         : InsetCollapsable(bp)
133 {
134         string lab(_("float: "));
135         lab += floatname(type, bp);
136         setLabel(lab);
137         LyXFont font(LyXFont::ALL_SANE);
138         font.decSize();
139         font.decSize();
140         font.setColor(LColor::collapsable);
141         setLabelFont(font);
142         params_.type = type;
143         setInsetName(type);
144         LyXTextClass const & tclass = bp.getLyXTextClass();
145         if (tclass.hasLayout(caplayout))
146                 inset.paragraphs.begin()->layout(tclass[caplayout]);
147 }
148
149
150 InsetFloat::InsetFloat(InsetFloat const & in)
151         : InsetCollapsable(in), params_(in.params_)
152 {}
153
154
155 InsetFloat::~InsetFloat()
156 {
157         InsetFloatMailer mailer(*this);
158         mailer.hideDialog();
159 }
160
161
162 dispatch_result
163 InsetFloat::priv_dispatch(FuncRequest const & cmd,
164                           idx_type & idx, pos_type & pos)
165 {
166         switch (cmd.action) {
167
168         case LFUN_INSET_MODIFY: {
169                 InsetFloatParams params;
170                 InsetFloatMailer::string2params(cmd.argument, params);
171
172                 params_.placement = params.placement;
173                 params_.wide      = params.wide;
174
175                 wide(params_.wide, cmd.view()->buffer()->params());
176                 cmd.view()->updateInset(this);
177                 return DISPATCHED;
178         }
179
180         case LFUN_INSET_DIALOG_UPDATE: {
181                 InsetFloatMailer(*this).updateDialog(cmd.view());
182                 return DISPATCHED;
183         }
184
185         default:
186                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
187         }
188 }
189
190
191 void InsetFloatParams::write(ostream & os) const
192 {
193         os << "Float " // getInsetName()
194            << type << '\n';
195
196         if (!placement.empty())
197                 os << "placement " << placement << "\n";
198
199         if (wide)
200                 os << "wide true\n";
201         else
202                 os << "wide false\n";
203 }
204
205
206 void InsetFloatParams::read(LyXLex & lex)
207 {
208         if (lex.isOK()) {
209                 lex.next();
210                 string token = lex.getString();
211                 if (token == "placement") {
212                         lex.next();
213                         placement = lex.getString();
214                 } else {
215                         // take countermeasures
216                         lex.pushToken(token);
217                 }
218                 lex.next();
219                 token = lex.getString();
220                 if (token == "wide") {
221                         lex.next();
222                         string const tmptoken = lex.getString();
223                         wide = (tmptoken == "true");
224                 } else {
225                         lyxerr << "InsetFloat::Read:: Missing wide!"
226                                << endl;
227                         // take countermeasures
228                         lex.pushToken(token);
229                 }
230         }
231 }
232
233
234 void InsetFloat::write(Buffer const & buf, ostream & os) const
235 {
236         params_.write(os);
237         InsetCollapsable::write(buf, os);
238 }
239
240
241 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
242 {
243         params_.read(lex);
244         wide(params_.wide, buf.params());
245         InsetCollapsable::read(buf, lex);
246 }
247
248
249 void InsetFloat::validate(LaTeXFeatures & features) const
250 {
251         if (contains(params_.placement, "H")) {
252                 features.require("float");
253         }
254
255         features.useFloat(params_.type);
256         InsetCollapsable::validate(features);
257 }
258
259
260 auto_ptr<InsetBase> InsetFloat::clone() const
261 {
262         return auto_ptr<InsetBase>(new InsetFloat(*this));
263 }
264
265
266 string const InsetFloat::editMessage() const
267 {
268         return _("Opened Float Inset");
269 }
270
271
272 int InsetFloat::latex(Buffer const & buf, ostream & os,
273                       LatexRunParams const & runparams) const
274 {
275         FloatList const & floats = buf.params().getLyXTextClass().floats();
276         string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
277         // Figure out the float placement to use.
278         // From lowest to highest:
279         // - float default placement
280         // - document wide default placement
281         // - specific float placement
282         string placement;
283         string const buf_placement = buf.params().float_placement;
284         string const def_placement = floats.defaultPlacement(params_.type);
285         if (!params_.placement.empty()
286             && params_.placement != def_placement) {
287                 placement = params_.placement;
288         } else if (params_.placement.empty()
289                    && !buf_placement.empty()
290                    && buf_placement != def_placement) {
291                 placement = buf_placement;
292         }
293
294         // The \n is used to force \begin{<floatname>} to appear in a new line.
295         // The % is needed to prevent two consecutive \n chars in the case
296         // when the current output line is empty.
297         os << "%\n\\begin{" << tmptype << '}';
298         // We only output placement if different from the def_placement.
299         if (!placement.empty()) {
300                 os << '[' << placement << ']';
301         }
302         os << '\n';
303
304         int const i = inset.latex(buf, os, runparams);
305
306         // The \n is used to force \end{<floatname>} to appear in a new line.
307         // In this case, we do not case if the current output line is empty.
308         os << "\n\\end{" << tmptype << "}\n";
309
310         return i + 4;
311 }
312
313
314 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os) const
315 {
316         FloatList const & floats = buf.params().getLyXTextClass().floats();
317         string const tmptype =  params_.type;
318         // Figure out the float placement to use.
319         // From lowest to highest:
320         // - float default placement
321         // - document wide default placement
322         // - specific float placement
323         // This is the same as latex, as linuxdoc is modeled after latex.
324
325         string placement;
326         string const buf_placement = buf.params().float_placement;
327         string const def_placement = floats.defaultPlacement(params_.type);
328         if (!params_.placement.empty()
329             && params_.placement != def_placement) {
330                 placement = params_.placement;
331         } else if (params_.placement.empty()
332                    && !buf_placement.empty()
333                    && buf_placement != def_placement) {
334                 placement = buf_placement;
335         }
336
337         os << "\n<" << tmptype ;
338         // We only output placement if different from the def_placement.
339         if (!placement.empty()) {
340                 os << " loc=\"" << placement << '"';
341         }
342         os << ">";
343
344         int const i = inset.linuxdoc(buf, os);
345         os << "</" << tmptype << ">\n";
346
347         return i;
348 }
349
350
351 int InsetFloat::docbook(Buffer const & buf, ostream & os, bool mixcont) const
352 {
353         os << '<' << params_.type << '>';
354         int const i = inset.docbook(buf, os, mixcont);
355         os << "</" << params_.type << '>';
356
357         return i;
358 }
359
360
361 bool InsetFloat::insetAllowed(InsetOld::Code code) const
362 {
363         if (code == InsetOld::FLOAT_CODE)
364                 return false;
365         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
366                 return inset.insetAllowed(code);
367         if ((code == InsetOld::FOOT_CODE) || (code == InsetOld::MARGIN_CODE))
368                 return false;
369         return true;
370 }
371
372
373 bool InsetFloat::showInsetDialog(BufferView * bv) const
374 {
375         if (!inset.showInsetDialog(bv)) {
376                 InsetFloat * tmp = const_cast<InsetFloat *>(this);
377                 InsetFloatMailer mailer(*tmp);
378                 mailer.showDialog(bv);
379         }
380         return true;
381 }
382
383
384 void InsetFloat::wide(bool w, BufferParams const & bp)
385 {
386         params_.wide = w;
387
388         string lab(_("float: "));
389         lab += floatname(params_.type, bp);
390
391         if (params_.wide)
392                 lab += '*';
393
394         setLabel(lab);
395 }
396
397
398 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
399 {
400         ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
401         ParIterator end(inset.paragraphs.end(), inset.paragraphs);
402
403         // Find a caption layout in one of the (child inset's) pars
404         for (; pit != end; ++pit) {
405                 if (pit->layout()->name() == caplayout) {
406                         string const name = floatname(params_.type, buf.params());
407                         string const str =
408                                 tostr(toclist[name].size() + 1)
409                                 + ". " + pit->asString(buf, false);
410                         lyx::toc::TocItem const item(pit->id(), 0 , str);
411                         toclist[name].push_back(item);
412                 }
413         }
414 }
415
416
417 string const InsetFloatMailer::name_("float");
418
419 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
420         : inset_(inset)
421 {}
422
423
424 string const InsetFloatMailer::inset2string(Buffer const &) const
425 {
426         return params2string(inset_.params());
427 }
428
429
430 void InsetFloatMailer::string2params(string const & in,
431                                      InsetFloatParams & params)
432 {
433         params = InsetFloatParams();
434
435         if (in.empty())
436                 return;
437
438         istringstream data(in);
439         LyXLex lex(0,0);
440         lex.setStream(data);
441
442         if (lex.isOK()) {
443                 lex.next();
444                 string const token = lex.getString();
445                 if (token != name_)
446                         return;
447         }
448
449         // This is part of the inset proper that is usually swallowed
450         // by Buffer::readInset
451         if (lex.isOK()) {
452                 lex.next();
453                 string const token = lex.getString();
454                 if (token != "Float" || !lex.eatLine())
455                         return;
456         }
457
458         if (lex.isOK()) {
459                 params.read(lex);
460         }
461 }
462
463
464 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
465 {
466         ostringstream data;
467         data << name_ << ' ';
468         params.write(data);
469         return data.str();
470 }