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