]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
prepare for 1.1.6pre2
[lyx.git] / src / insets / insetfloat.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1998 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 // Lgb
80
81 InsetFloat::InsetFloat(string const & type)
82         : InsetCollapsable()
83 {
84         string lab(_("float:"));
85         lab += type;
86         setLabel(lab);
87         LyXFont font(LyXFont::ALL_SANE);
88         font.decSize();
89         font.decSize();
90         font.setColor(LColor::footnote);
91         setLabelFont(font);
92         setAutoCollapse(false);
93         floatType = type;
94         setInsetName(type);
95         //floatPlacement = "H";
96 }
97
98
99 void InsetFloat::Write(Buffer const * buf, ostream & os) const
100 {
101         os << "Float " // getInsetName()
102            << floatType << '\n';
103
104         if (floatPlacement.empty()) {
105                 os << "placement "
106                    << floatList.getType(floatType).placement << "\n";
107         } else {
108                 os << "placement " << floatPlacement << "\n";
109         }
110         
111         InsetCollapsable::Write(buf, os);
112 }
113
114
115 void InsetFloat::Read(Buffer const * buf, LyXLex & lex)
116 {
117         if (lex.IsOK()) {
118                 lex.next();
119                 string token = lex.GetString();
120                 if (token == "placement") {
121                         lex.next();
122                         floatPlacement = lex.GetString();
123                 } else {
124                         lyxerr << "InsetFloat::Read: Missing placement!"
125                                << endl;
126                 }
127         }
128         InsetCollapsable::Read(buf, lex);
129 }
130
131
132 void InsetFloat::Validate(LaTeXFeatures & features) const
133 {
134         features.usedFloats.insert(floatType);
135 }
136
137
138 Inset * InsetFloat::Clone(Buffer const &) const
139 {
140         InsetFloat * result = new InsetFloat(floatType);
141         result->inset->init(inset);
142
143         result->collapsed = collapsed;
144         return result;
145 }
146
147
148 string const InsetFloat::EditMessage() const
149 {
150         return _("Opened Float Inset");
151 }
152
153
154 int InsetFloat::Latex(Buffer const * buf,
155                       ostream & os, bool fragile, bool fp) const
156 {
157         os << "\\begin{" << floatType << "}";
158         if (!floatPlacement.empty()
159             && floatPlacement != floatList.defaultPlacement(floatType))
160                 os << "[" << floatPlacement << "]";
161         os << "%\n";
162     
163         int i = inset->Latex(buf, os, fragile, fp);
164         os << "\\end{" << floatType << "}%\n";
165         
166         return i + 2;
167 }
168
169
170 bool InsetFloat::InsertInsetAllowed(Inset * in) const
171 {
172         if ((in->LyxCode() == Inset::FOOT_CODE) ||
173             (in->LyxCode() == Inset::MARGIN_CODE)) {
174                 return false;
175         }
176         return true;
177 }
178
179
180 void InsetFloat::InsetButtonRelease(BufferView * bv, int x, int y, int button)
181 {
182         if (x >= 0
183             && x < button_length
184             && y >= button_top_y
185             && y < button_bottom_y
186             && button == 3) {
187                 // This obviously need to change.
188                 lyxerr << "InsetFloat: Let's edit this floats parameters!"
189                        << endl;
190         } else {
191                 InsetCollapsable::InsetButtonRelease(bv, x, y, button);
192         }
193 }
194
195
196 string const & InsetFloat::type() const 
197 {
198         return floatType;
199 }
200
201
202 void InsetFloat::wide(bool w)
203 {
204         wide_ = w;
205         if (wide_) {
206                 string lab(_("float:"));
207                 lab += floatType;
208                 lab += "*";
209                 setLabel(lab);
210         } else {
211                 string lab(_("float:"));
212                 lab += floatType;
213                 setLabel(lab);
214         }
215 }
216
217
218 bool InsetFloat::wide() const
219 {
220         return wide_;
221 }