]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
Translate labels for float:algorithm, float:figure and float:table.
[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 namespace {
103
104 string floatname(string const & type)
105 {
106         FloatList::const_iterator it = floatList[type];
107         if (it == floatList.end())
108                 return type;
109
110         return _(it->second.name());
111 }
112  
113 } // namespace anon
114
115
116 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
117         : InsetCollapsable(bp), wide_(false)
118 {
119         string lab(_("float: "));
120         lab += floatname(type);
121         setLabel(lab);
122         LyXFont font(LyXFont::ALL_SANE);
123         font.decSize();
124         font.decSize();
125         font.setColor(LColor::collapsable);
126         setLabelFont(font);
127         floatType_ = type;
128         setInsetName(type);
129 }
130
131
132 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
133         : InsetCollapsable(in, same_id), floatType_(in.floatType_),
134           floatPlacement_(in.floatPlacement_), wide_(in.wide_)
135 {}
136
137
138 InsetFloat::~InsetFloat()
139 {
140         hideDialog();
141 }
142
143
144 void InsetFloat::write(Buffer const * buf, ostream & os) const
145 {
146         os << "Float " // getInsetName()
147            << floatType_ << '\n';
148
149         if (floatPlacement_.empty()) {
150                 os << "placement "
151                    << floatList.getType(floatType_).placement() << "\n";
152         } else {
153                 os << "placement " << floatPlacement_ << "\n";
154         }
155         if (wide_) {
156                 os << "wide true\n";
157         } else {
158                 os << "wide false\n";
159         }
160
161         InsetCollapsable::write(buf, os);
162 }
163
164
165 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
166 {
167         if (lex.isOK()) {
168                 lex.next();
169                 string token = lex.getString();
170                 if (token == "placement") {
171                         lex.next();
172                         floatPlacement_ = lex.getString();
173                 } else {
174                         lyxerr << "InsetFloat::Read: Missing placement!"
175                                << endl;
176                         // take countermeasures
177                         lex.pushToken(token);
178                 }
179                 lex.next();
180                 token = lex.getString();
181                 if (token == "wide") {
182                         lex.next();
183                         string const tmptoken = lex.getString();
184                         if (tmptoken == "true")
185                                 wide(true);
186                         else
187                                 wide(false);
188                 } else {
189                         lyxerr << "InsetFloat::Read:: Missing wide!"
190                                << endl;
191                         // take countermeasures
192                         lex.pushToken(token);
193                 }
194         }
195         InsetCollapsable::read(buf, lex);
196 }
197
198
199 void InsetFloat::validate(LaTeXFeatures & features) const
200 {
201         if (contains(placement(), "H")) {
202                 features.require("float");
203         }
204
205         features.useFloat(floatType_);
206         InsetCollapsable::validate(features);
207 }
208
209
210 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
211 {
212         return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
213 }
214
215
216 string const InsetFloat::editMessage() const
217 {
218         return _("Opened Float Inset");
219 }
220
221
222 int InsetFloat::latex(Buffer const * buf,
223                       ostream & os, bool fragile, bool fp) const
224 {
225         string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
226         // Figure out the float placement to use.
227         // From lowest to highest:
228         // - float default placement
229         // - document wide default placement
230         // - specific float placement
231         string placement;
232         string const buf_placement = buf->params.float_placement;
233         string const def_placement = floatList.defaultPlacement(floatType_);
234         if (!floatPlacement_.empty()
235             && floatPlacement_ != def_placement) {
236                 placement = floatPlacement_;
237         } else if (!buf_placement.empty()
238                    && buf_placement != def_placement) {
239                 placement = buf_placement;
240         }
241
242         os << "\\begin{" << tmptype << "}";
243         // We only output placement if different from the def_placement.
244         if (!placement.empty()) {
245                 os << "[" << placement << "]";
246         }
247
248         os << "%\n";
249
250         int const i = inset.latex(buf, os, fragile, fp);
251         os << "\\end{" << tmptype << "}%\n";
252
253         return i + 2;
254 }
255
256
257 int InsetFloat::docbook(Buffer const * buf, ostream & os) const
258 {
259         os << "<" << floatType_ << ">";
260         int const i = inset.docbook(buf, os);
261         os << "</" << floatType_ << ">";
262
263         return i;
264 }
265
266
267 bool InsetFloat::insetAllowed(Inset::Code code) const
268 {
269         if (code == Inset::FLOAT_CODE)
270                 return false;
271         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
272                 return inset.insetAllowed(code);
273         if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
274                 return false;
275         return true;
276 }
277
278
279 bool InsetFloat::showInsetDialog(BufferView * bv) const
280 {
281         if (!inset.showInsetDialog(bv)) {
282                 bv->owner()->getDialogs()->showFloat(const_cast<InsetFloat *>(this));
283         }
284         return true;
285 }
286
287
288 string const & InsetFloat::type() const
289 {
290         return floatType_;
291 }
292
293
294 void InsetFloat::placement(string const & p)
295 {
296         // FIX: Here we should only allow the placement to be set
297         // if a valid value.
298         floatPlacement_ = p;
299 }
300
301
302 string const & InsetFloat::placement() const
303 {
304         return floatPlacement_;
305 }
306
307
308 void InsetFloat::wide(bool w)
309 {
310         wide_ = w;
311
312         string lab(_("float:"));
313         lab += floatname(floatType_);
314
315         if (wide_)
316                 lab += "*";
317
318         setLabel(lab);
319 }
320
321
322 bool InsetFloat::wide() const
323 {
324         return wide_;
325 }