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