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