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