]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
*** empty log message ***
[lyx.git] / src / insets / insetfloat.C
1 /**
2  * \file insetfloat.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS
10  */
11 #include <config.h>
12
13
14 #include "insetfloat.h"
15 #include "gettext.h"
16 #include "lyxfont.h"
17 #include "BufferView.h"
18 #include "lyxtext.h"
19 #include "insets/insettext.h"
20 #include "support/LOstream.h"
21 #include "support/lstrings.h"
22 #include "FloatList.h"
23 #include "LaTeXFeatures.h"
24 #include "debug.h"
25 #include "Floating.h"
26 #include "buffer.h"
27 #include "frontends/LyXView.h"
28 #include "frontends/Dialogs.h"
29 #include "lyxlex.h"
30 #include "iterators.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 // this should not be hardcoded, but be part of the definition
105 // of the float (JMarc)
106 string const caplayout("Caption");
107
108 string floatname(string const & type, BufferParams const & bp)
109 {
110         FloatList const & floats = bp.getLyXTextClass().floats();
111         FloatList::const_iterator it = floats[type];
112         if (it == floats.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, bp);
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, buf->params);
189                         else
190                                 wide(false, buf->params);
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         FloatList const & floats = buf->params.getLyXTextClass().floats();
229         string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
230         // Figure out the float placement to use.
231         // From lowest to highest:
232         // - float default placement
233         // - document wide default placement
234         // - specific float placement
235         string placement;
236         string const buf_placement = buf->params.float_placement;
237         string const def_placement = floats.defaultPlacement(floatType_);
238         if (!floatPlacement_.empty()
239             && floatPlacement_ != def_placement) {
240                 placement = floatPlacement_;
241         } else if (floatPlacement_.empty()
242                    && !buf_placement.empty()
243                    && buf_placement != def_placement) {
244                 placement = buf_placement;
245         }
246
247         // The \n is used to force \begin{<floatname>} to appear in a new line.
248         // The % is needed to prevent two consecutive \n chars in the case
249         // when the current output line is empty.
250         os << "%\n\\begin{" << tmptype << '}';
251         // We only output placement if different from the def_placement.
252         if (!placement.empty()) {
253                 os << '[' << placement << ']';
254         }
255         os << '\n';
256
257         int const i = inset.latex(buf, os, fragile, fp);
258
259         // The \n is used to force \end{<floatname>} to appear in a new line.
260         // In this case, we do not case if the current output line is empty.
261         os << "\n\\end{" << tmptype << "}\n";
262
263         return i + 4;
264 }
265
266
267 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
268 {
269         os << '<' << floatType_ << '>';
270         int const i = inset.docbook(buf, os, mixcont);
271         os << "</" << floatType_ << '>';
272
273         return i;
274 }
275
276
277 bool InsetFloat::insetAllowed(Inset::Code code) const
278 {
279         if (code == Inset::FLOAT_CODE)
280                 return false;
281         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
282                 return inset.insetAllowed(code);
283         if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
284                 return false;
285         return true;
286 }
287
288
289 bool InsetFloat::showInsetDialog(BufferView * bv) const
290 {
291         if (!inset.showInsetDialog(bv)) {
292                 bv->owner()->getDialogs().showFloat(const_cast<InsetFloat *>(this));
293         }
294         return true;
295 }
296
297
298 string const & InsetFloat::type() const
299 {
300         return floatType_;
301 }
302
303
304 void InsetFloat::placement(string const & p)
305 {
306         // FIX: Here we should only allow the placement to be set
307         // if a valid value.
308         floatPlacement_ = p;
309 }
310
311
312 string const & InsetFloat::placement() const
313 {
314         return floatPlacement_;
315 }
316
317
318 void InsetFloat::wide(bool w, BufferParams const & bp)
319 {
320         wide_ = w;
321
322         string lab(_("float:"));
323         lab += floatname(floatType_, bp);
324
325         if (wide_)
326                 lab += '*';
327
328         setLabel(lab);
329 }
330
331
332 bool InsetFloat::wide() const
333 {
334         return wide_;
335 }
336
337
338 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
339 {
340         ParIterator pit(inset.paragraph());
341         ParIterator end;
342
343         // Find a caption layout in one of the (child inset's) pars
344         for (; pit != end; ++pit) {
345                 Paragraph * tmp = *pit;
346
347                 if (tmp->layout()->name() == caplayout) {
348                         string const name = floatname(type(), buf->params);
349                         string const str =
350                                 tostr(toclist[name].size() + 1)
351                                 + ". " + tmp->asString(buf, false);
352                         toc::TocItem const item(tmp->id(), 0 , str);
353                         toclist[name].push_back(item);
354                 }
355         }
356 }