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