]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
partial inset toggling ; insetAllowed stuff
[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         setAutoCollapse(false);
111         floatType_ = type;
112         setInsetName(type);
113 }
114
115
116 void InsetFloat::write(Buffer const * buf, ostream & os) const
117 {
118         os << "Float " // getInsetName()
119            << floatType_ << '\n';
120
121         if (floatPlacement_.empty()) {
122                 os << "placement "
123                    << floatList.getType(floatType_).placement() << "\n";
124         } else {
125                 os << "placement " << floatPlacement_ << "\n";
126         }
127         if (wide_) {
128                 os << "wide true\n";
129         } else {
130                 os << "wide false\n";
131         }
132         
133         InsetCollapsable::write(buf, os);
134 }
135
136
137 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
138 {
139         if (lex.IsOK()) {
140                 lex.next();
141                 string token = lex.GetString();
142                 if (token == "placement") {
143                         lex.next();
144                         floatPlacement_ = lex.GetString();
145                 } else {
146                         lyxerr << "InsetFloat::Read: Missing placement!"
147                                << endl;
148                 }
149                 lex.next();
150                 token = lex.GetString();
151                 if (token == "wide") {
152                         lex.next();
153                         string const tmptoken = lex.GetString();
154                         if (tmptoken == "true")
155                                 wide(true);
156                         else
157                                 wide(false);
158                 } else {
159                         lyxerr << "InsetFloat::Read:: Missing wide!"
160                                << endl;
161                 }
162         }
163         InsetCollapsable::read(buf, lex);
164 }
165
166
167 void InsetFloat::validate(LaTeXFeatures & features) const
168 {
169         features.usedFloats.insert(floatType_);
170         InsetCollapsable::validate(features);
171 }
172
173
174 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
175 {
176         InsetFloat * result = new InsetFloat(floatType_);
177         result->inset.init(&inset, same_id);
178
179         result->collapsed_ = collapsed_;
180         if (same_id)
181                 result->id_ = id_;
182         return result;
183 }
184
185
186 string const InsetFloat::editMessage() const
187 {
188         return _("Opened Float Inset");
189 }
190
191
192 int InsetFloat::latex(Buffer const * buf,
193                       ostream & os, bool fragile, bool fp) const
194 {
195         string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
196         // Figure out the float placement to use.
197         // From lowest to highest:
198         // - float default placement
199         // - document wide default placement
200         // - specific float placement
201         string placement;
202         string const buf_placement = buf->params.float_placement;
203         string const def_placement = floatList.defaultPlacement(floatType_);
204         if (!floatPlacement_.empty()
205             && floatPlacement_ != def_placement) {
206                 placement = floatPlacement_;
207         } else if (!buf_placement.empty()
208                    && buf_placement != def_placement) {
209                 placement = buf_placement;
210         }
211         
212         os << "\\begin{" << tmptype << "}";
213         // We only output placement if different from the def_placement.
214         if (!placement.empty()) {
215                 os << "[" << placement << "]";
216         }
217         
218         os << "%\n";
219     
220         int const i = inset.latex(buf, os, fragile, fp);
221         os << "\\end{" << tmptype << "}%\n";
222         
223         return i + 2;
224 }
225
226
227 int InsetFloat::docBook(Buffer const * buf, ostream & os) const
228 {
229         os << "<" << floatType_ << ">";
230         int const i = inset.docBook(buf, os);
231         os << "</" << floatType_ << ">";
232
233         return i;
234 }
235
236
237 bool InsetFloat::insetAllowed(Inset::Code code) const
238 {
239         if (code == Inset::FLOAT_CODE)
240                 return false;
241         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
242                 return inset.insetAllowed(code);
243         if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
244                 return false;
245         return true;
246 }
247
248
249 void InsetFloat::insetButtonRelease(BufferView * bv, int x, int y, int button)
250 {
251         if (x >= top_x
252             && x < button_length
253             && y >= button_top_y
254             && y < button_bottom_y
255             && button == 3) {
256                 // This obviously need to change.
257                 lyxerr << "InsetFloat: Let's edit this floats parameters!"
258                        << endl;
259                 //bv->owner()->getDialogs()->showFloat(this);
260         } else {
261                 InsetCollapsable::insetButtonRelease(bv, x, y, button);
262         }
263 }
264
265
266 string const & InsetFloat::type() const 
267 {
268         return floatType_;
269 }
270
271
272 void InsetFloat::placement(string const & p)
273 {
274         // Here we should only allow the placement to be set
275         // if a valid value.
276 #ifdef WITH_WARNINGS
277 #warning FIX!
278 #endif
279         floatPlacement_ = p;
280 }
281
282
283 string const & InsetFloat::placement() const
284 {
285         return floatPlacement_;
286 }
287
288
289 void InsetFloat::wide(bool w)
290 {
291         wide_ = w;
292         if (wide_) {
293                 string lab(_("float:"));
294                 lab += floatType_;
295                 lab += "*";
296                 setLabel(lab);
297         } else {
298                 string lab(_("float:"));
299                 lab += floatType_;
300                 setLabel(lab);
301         }
302 }
303
304
305 bool InsetFloat::wide() const
306 {
307         return wide_;
308 }