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