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