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