]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
reformatting and remove using delc
[lyx.git] / src / insets / insetfloat.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1998-2000 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         
127         InsetCollapsable::Write(buf, os);
128 }
129
130
131 void InsetFloat::Read(Buffer const * buf, LyXLex & lex)
132 {
133         if (lex.IsOK()) {
134                 lex.next();
135                 string const token = lex.GetString();
136                 if (token == "placement") {
137                         lex.next();
138                         floatPlacement_ = lex.GetString();
139                 } else {
140                         lyxerr << "InsetFloat::Read: Missing placement!"
141                                << endl;
142                 }
143         }
144         InsetCollapsable::Read(buf, lex);
145 }
146
147
148 void InsetFloat::Validate(LaTeXFeatures & features) const
149 {
150         features.usedFloats.insert(floatType_);
151 }
152
153
154 Inset * InsetFloat::Clone(Buffer const &) const
155 {
156         InsetFloat * result = new InsetFloat(floatType_);
157         result->inset.init(&inset);
158
159         result->collapsed = collapsed;
160         return result;
161 }
162
163
164 string const InsetFloat::EditMessage() const
165 {
166         return _("Opened Float Inset");
167 }
168
169
170 int InsetFloat::Latex(Buffer const * buf,
171                       ostream & os, bool fragile, bool fp) const
172 {
173         os << "\\begin{" << floatType_ << "}";
174         if (!floatPlacement_.empty()
175             && floatPlacement_ != floatList.defaultPlacement(floatType_))
176                 os << "[" << floatPlacement_ << "]";
177         os << "%\n";
178     
179         int const i = inset.Latex(buf, os, fragile, fp);
180         os << "\\end{" << floatType_ << "}%\n";
181         
182         return i + 2;
183 }
184
185
186 int InsetFloat::DocBook(Buffer const * buf, ostream & os) const
187 {
188         os << "<" << floatType_ << ">";
189         int const i = inset.DocBook(buf, os);
190         os << "</" << floatType_ << ">";
191
192         return i;
193 }
194
195
196 bool InsetFloat::InsertInsetAllowed(Inset * in) const
197 {
198         if ((in->LyxCode() == Inset::FOOT_CODE) ||
199             (in->LyxCode() == Inset::MARGIN_CODE)) {
200                 return false;
201         }
202         return true;
203 }
204
205
206 void InsetFloat::InsetButtonRelease(BufferView * bv, int x, int y, int button)
207 {
208         if (x >= top_x
209             && x < button_length
210             && y >= button_top_y
211             && y < button_bottom_y
212             && button == 3) {
213                 // This obviously need to change.
214                 lyxerr << "InsetFloat: Let's edit this floats parameters!"
215                        << endl;
216                 //bv->owner()->getDialogs()->showFloat(this);
217         } else {
218                 InsetCollapsable::InsetButtonRelease(bv, x, y, button);
219         }
220 }
221
222
223 string const & InsetFloat::type() const 
224 {
225         return floatType_;
226 }
227
228
229 void InsetFloat::placement(string const & p)
230 {
231         // Here we should only allow the placement to be set
232         // if a valid value.
233 #warning FIX!
234         floatPlacement_ = p;
235 }
236
237
238 string const & InsetFloat::placement() const
239 {
240         return floatPlacement_;
241 }
242
243
244 void InsetFloat::wide(bool w)
245 {
246         wide_ = w;
247         if (wide_) {
248                 string lab(_("float:"));
249                 lab += floatType_;
250                 lab += "*";
251                 setLabel(lab);
252         } else {
253                 string lab(_("float:"));
254                 lab += floatType_;
255                 setLabel(lab);
256         }
257 }
258
259
260 bool InsetFloat::wide() const
261 {
262         return wide_;
263 }