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