]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
cc893bc70d5fe5381d8803c415bea9a6e6b10e47
[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 "insets/insettext.h"
23 #include "support/LOstream.h"
24 #include "FloatList.h"
25 #include "LaTeXFeatures.h"
26 #include "debug.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() : InsetCollapsable()
82 {
83     setLabel(_("float"));
84     LyXFont font(LyXFont::ALL_SANE);
85     font.decSize();
86     font.decSize();
87     font.setColor(LColor::footnote);
88     setLabelFont(font);
89     setAutoCollapse(false);
90     setInsetName("Float");
91     floatType = "table";
92     floatPlacement = "H";
93 }
94
95
96 void InsetFloat::Write(Buffer const * buf, ostream & os) const
97 {
98         os << getInsetName()
99            << "\ntype " << floatType
100            << "\nplacement " << floatPlacement << "\n";
101         InsetCollapsable::Write(buf, os);
102 }
103
104
105 void InsetFloat::Read(Buffer const * buf, LyXLex & lex)
106 {
107         if (lex.IsOK()) {
108                 lex.next();
109                 string token = lex.GetString();
110                 if (token == "type") {
111                         lex.next();
112                         floatType = lex.GetString();
113                 }
114                 lex.next();
115                 token = lex.GetString();
116                 if (token == "placement") {
117                         lex.next();
118                         floatPlacement = lex.GetString();
119                 }
120         }
121         InsetCollapsable::Read(buf, lex);
122 }
123
124
125 void InsetFloat::Validate(LaTeXFeatures & features) const
126 {
127         features.usedFloats.insert(floatType);
128 }
129
130
131 Inset * InsetFloat::Clone() const
132 {
133     InsetFloat * result = new InsetFloat;
134     result->inset->init(inset);
135
136     result->collapsed = collapsed;
137     return result;
138 }
139
140
141 char const * InsetFloat::EditMessage() const
142 {
143     return _("Opened Float Inset");
144 }
145
146
147 int InsetFloat::Latex(Buffer const * buf,
148                       ostream & os, bool fragile, bool fp) const
149 {
150         os << "\\begin{" << floatType << "}";
151         if (!floatPlacement.empty()
152             && floatPlacement != floatList.defaultPlacement(floatType))
153                 os << "[" << floatPlacement << "]";
154         os << "%\n";
155     
156     int i = inset->Latex(buf, os, fragile, fp);
157     os << "\\end{" << floatType << "}%\n";
158     
159     return i + 2;
160 }
161
162
163 bool InsetFloat::InsertInset(BufferView * bv, Inset * in)
164 {
165     if (!InsertInsetAllowed(in))
166         return false;
167
168     return inset->InsertInset(bv, in);
169 }
170
171
172 bool InsetFloat::InsertInsetAllowed(Inset * in) const
173 {
174     if ((in->LyxCode() == Inset::FOOT_CODE) ||
175         (in->LyxCode() == Inset::MARGIN_CODE)) {
176         return false;
177     }
178     return true;
179 }
180
181 #if 0
182 LyXFont InsetFloat::GetDrawFont(BufferView * bv,
183                                 LyXParagraph * p, int pos) const
184 {
185     LyXFont fn = getLyXText(bv)->GetFont(bv->buffer(), p, pos);
186     fn.decSize().decSize();
187     return fn;
188 }
189 #endif
190
191 void InsetFloat::InsetButtonRelease(BufferView * bv, int x, int y, int button)
192 {
193         if (x >= 0
194             && x < button_length
195             && y >= button_top_y
196             && y < button_bottom_y
197             && button == 3) {
198                 // This obviously need to change.
199                 lyxerr << "InsetFloat: Let's edit this floats parameters!"
200                        << endl;
201         } else {
202                 InsetCollapsable::InsetButtonRelease(bv, x, y, button);
203         }
204 }