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