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