]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
8d8c8c6a2f795671be1ed086c10f040d1691e110
[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 "Painter.h"
21 #include "lyxtext.h"
22 #include "support/LOstream.h"
23 #include "FloatList.h"
24 #include "LaTeXFeatures.h"
25 #include "debug.h"
26
27 using std::ostream;
28 using std::endl;
29
30 // With this inset it will be possible to support the latex package
31 // float.sty, and I am sure that with this and some additional support
32 // classes we can support similar functionality in other formats
33 // (read DocBook).
34 // By using float.sty we will have the same handling for all floats, both
35 // for those already in existance (table and figure) and all user created
36 // ones¹. So suddenly we give the users the possibility of creating new
37 // kinds of floats on the fly. (and with a uniform look)
38 //
39 // API to float.sty:
40 //   \newfloat{type}{placement}{ext}[within]
41 //     type      - The "type" of the new class of floats, like program or
42 //                 algorithm. After the appropriate \newfloat, commands
43 //                 such as \begin{program} or \end{algorithm*} will be
44 //                 available.
45 //     placement - The default placement for the given class of floats.
46 //                 They are like in standard LaTeX: t, b, p and h for top,
47 //                 bottom, page, and here, respectively. On top of that
48 //                 there is a new type, H, which does not really correspond
49 //                 to a float, since it means: put it "here" and nowhere else.
50 //                 Note, however that the H specifier is special and, because
51 //                 of implementation details cannot be used in the second
52 //                 argument of \newfloat.
53 //     ext       - The file name extension of an auxiliary file for the list
54 //                 of figures (or whatever). LaTeX writes the captions to
55 //                 this file.
56 //     within    - This (optional) argument determines whether floats of this
57 //                 class will be numbered within some sectional unit of the
58 //                 document. For example, if within is equal to chapter, the
59 //                 floats will be numbered within chapters. 
60 //   \floatstyle{style}
61 //     style -  plain, boxed, ruled
62 //   \floatname{float}{floatname}
63 //     float     -
64 //     floatname -
65 //   \floatplacement{float}{placement}
66 //     float     -
67 //     placement -
68 //   \restylefloat{float}
69 //     float -
70 //   \listof{type}{title}
71 //     title -
72
73 // ¹ the algorithm float is defined using the float.sty package. Like this
74 //   \floatstyle{ruled}
75 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
76 //   \floatname{algorithm}{Algorithm}
77 //
78 // Lgb
79
80 InsetFloat::InsetFloat() : InsetCollapsable()
81 {
82     setLabel(_("float"));
83     LyXFont font(LyXFont::ALL_SANE);
84     font.decSize();
85     font.decSize();
86     font.setColor(LColor::footnote);
87     setLabelFont(font);
88     setAutoCollapse(false);
89     setInsetName("Float");
90     floatType = "table";
91     floatPlacement = "H";
92 }
93
94
95 void InsetFloat::Write(Buffer const * buf, ostream & os) const
96 {
97         os << getInsetName()
98            << "\ntype " << floatType
99            << "\nplacement " << floatPlacement << "\n";
100         InsetCollapsable::Write(buf, os);
101 }
102
103
104 void InsetFloat::Read(Buffer const * buf, LyXLex & lex)
105 {
106         if (lex.IsOK()) {
107                 lex.next();
108                 string token = lex.GetString();
109                 if (token == "type") {
110                         lex.next();
111                         floatType = lex.GetString();
112                 }
113                 lex.next();
114                 token = lex.GetString();
115                 if (token == "placement") {
116                         lex.next();
117                         floatPlacement = lex.GetString();
118                 }
119         }
120         InsetCollapsable::Read(buf, lex);
121 }
122
123
124 void InsetFloat::Validate(LaTeXFeatures & features) const
125 {
126         features.usedFloats.insert(floatType);
127 }
128
129
130 Inset * InsetFloat::Clone() const
131 {
132     InsetFloat * result = new InsetFloat;
133     result->init(this);
134
135     result->collapsed = collapsed;
136     return result;
137 }
138
139
140 char const * InsetFloat::EditMessage() const
141 {
142     return _("Opened Float Inset");
143 }
144
145
146 int InsetFloat::Latex(Buffer const * buf,
147                       ostream & os, bool fragile, bool fp) const
148 {
149         os << "\\begin{" << floatType << "}";
150         if (!floatPlacement.empty()
151             && floatPlacement != floatList.defaultPlacement(floatType))
152                 os << "[" << floatPlacement << "]";
153         os << "%\n";
154     
155     int i = InsetText::Latex(buf, os, fragile, fp);
156     os << "\\end{" << floatType << "}%\n";
157     
158     return i + 2;
159 }
160
161
162 bool InsetFloat::InsertInset(BufferView * bv, Inset * inset)
163 {
164     if (!InsertInsetAllowed(inset))
165         return false;
166
167     return InsetText::InsertInset(bv, inset);
168 }
169
170
171 bool InsetFloat::InsertInsetAllowed(Inset * inset) const
172 {
173     if ((inset->LyxCode() == Inset::FOOT_CODE) ||
174         (inset->LyxCode() == Inset::MARGIN_CODE)) {
175         return false;
176     }
177     return true;
178 }
179
180
181 LyXFont InsetFloat::GetDrawFont(BufferView * bv,
182                                 LyXParagraph * p, int pos) const
183 {
184     LyXFont fn = getLyXText(bv)->GetFont(bv->buffer(), p, pos);
185     fn.decSize().decSize();
186     return fn;
187 }
188
189
190 void InsetFloat::InsetButtonRelease(BufferView * bv, int x, int y, int button)
191 {
192         if (x >= 0
193             && x < button_length
194             && y >= button_top_y
195             && y < button_bottom_y
196             && button == 3) {
197                 // This obviously need to change.
198                 lyxerr << "InsetFloat: Let's edit this floats parameters!"
199                        << endl;
200         } else {
201                 InsetCollapsable::InsetButtonRelease(bv, x, y, button);
202         }
203 }