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