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