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