1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ====================================================== */
13 #pragma implementation
16 #include "insetfloat.h"
19 #include "BufferView.h"
21 #include "insets/insettext.h"
22 #include "support/LOstream.h"
23 #include "FloatList.h"
24 #include "LaTeXFeatures.h"
32 // With this inset it will be possible to support the latex package
33 // float.sty, and I am sure that with this and some additional support
34 // classes we can support similar functionality in other formats
36 // By using float.sty we will have the same handling for all floats, both
37 // for those already in existance (table and figure) and all user created
38 // ones¹. So suddenly we give the users the possibility of creating new
39 // kinds of floats on the fly. (and with a uniform look)
42 // \newfloat{type}{placement}{ext}[within]
43 // type - The "type" of the new class of floats, like program or
44 // algorithm. After the appropriate \newfloat, commands
45 // such as \begin{program} or \end{algorithm*} will be
47 // placement - The default placement for the given class of floats.
48 // They are like in standard LaTeX: t, b, p and h for top,
49 // bottom, page, and here, respectively. On top of that
50 // there is a new type, H, which does not really correspond
51 // to a float, since it means: put it "here" and nowhere else.
52 // Note, however that the H specifier is special and, because
53 // of implementation details cannot be used in the second
54 // argument of \newfloat.
55 // ext - The file name extension of an auxiliary file for the list
56 // of figures (or whatever). LaTeX writes the captions to
58 // within - This (optional) argument determines whether floats of this
59 // class will be numbered within some sectional unit of the
60 // document. For example, if within is equal to chapter, the
61 // floats will be numbered within chapters.
63 // style - plain, boxed, ruled
64 // \floatname{float}{floatname}
67 // \floatplacement{float}{placement}
70 // \restylefloat{float}
72 // \listof{type}{title}
75 // ¹ the algorithm float is defined using the float.sty package. Like this
77 // \newfloat{algorithm}{htbp}{loa}[<sect>]
78 // \floatname{algorithm}{Algorithm}
80 // The intention is that floats should be definable from two places:
82 // - the "gui" (i.e. by the user)
85 // This should only be done for floats defined in a documentclass and that
86 // does not need any additional packages. The two most known floats in this
87 // category is "table" and "figure". Floats defined in layout files are only
88 // stored in lyx files if the user modifies them.
91 // There should be a gui dialog (and also a collection of lyxfuncs) where
92 // the user can modify existing floats and/or create new ones.
94 // The individual floats will also have some settable
95 // variables: wide and placement.
99 InsetFloat::InsetFloat(string const & type)
100 : InsetCollapsable(), wide_(false)
102 string lab(_("float:"));
105 LyXFont font(LyXFont::ALL_SANE);
108 font.setColor(LColor::collapsable);
115 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
116 : InsetCollapsable(in, same_id), floatType_(in.floatType_),
117 floatPlacement_(in.floatPlacement_), wide_(in.wide_)
121 void InsetFloat::write(Buffer const * buf, ostream & os) const
123 os << "Float " // getInsetName()
124 << floatType_ << '\n';
126 if (floatPlacement_.empty()) {
128 << floatList.getType(floatType_).placement() << "\n";
130 os << "placement " << floatPlacement_ << "\n";
135 os << "wide false\n";
138 InsetCollapsable::write(buf, os);
142 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
146 string token = lex.GetString();
147 if (token == "placement") {
149 floatPlacement_ = lex.GetString();
151 lyxerr << "InsetFloat::Read: Missing placement!"
153 // take countermeasures
154 lex.pushToken(token);
157 token = lex.GetString();
158 if (token == "wide") {
160 string const tmptoken = lex.GetString();
161 if (tmptoken == "true")
166 lyxerr << "InsetFloat::Read:: Missing wide!"
168 // take countermeasures
169 lex.pushToken(token);
172 InsetCollapsable::read(buf, lex);
176 void InsetFloat::validate(LaTeXFeatures & features) const
178 features.usedFloats.insert(floatType_);
179 InsetCollapsable::validate(features);
183 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
185 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
189 string const InsetFloat::editMessage() const
191 return _("Opened Float Inset");
195 int InsetFloat::latex(Buffer const * buf,
196 ostream & os, bool fragile, bool fp) const
198 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
199 // Figure out the float placement to use.
200 // From lowest to highest:
201 // - float default placement
202 // - document wide default placement
203 // - specific float placement
205 string const buf_placement = buf->params.float_placement;
206 string const def_placement = floatList.defaultPlacement(floatType_);
207 if (!floatPlacement_.empty()
208 && floatPlacement_ != def_placement) {
209 placement = floatPlacement_;
210 } else if (!buf_placement.empty()
211 && buf_placement != def_placement) {
212 placement = buf_placement;
215 os << "\\begin{" << tmptype << "}";
216 // We only output placement if different from the def_placement.
217 if (!placement.empty()) {
218 os << "[" << placement << "]";
223 int const i = inset.latex(buf, os, fragile, fp);
224 os << "\\end{" << tmptype << "}%\n";
230 int InsetFloat::docBook(Buffer const * buf, ostream & os) const
232 os << "<" << floatType_ << ">";
233 int const i = inset.docBook(buf, os);
234 os << "</" << floatType_ << ">";
240 bool InsetFloat::insetAllowed(Inset::Code code) const
242 if (code == Inset::FLOAT_CODE)
244 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
245 return inset.insetAllowed(code);
246 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
252 void InsetFloat::insetButtonRelease(BufferView * bv, int x, int y, int button)
257 && y < button_bottom_y
259 // This obviously need to change.
260 lyxerr << "InsetFloat: Let's edit this floats parameters!"
262 //bv->owner()->getDialogs()->showFloat(this);
264 InsetCollapsable::insetButtonRelease(bv, x, y, button);
269 string const & InsetFloat::type() const
275 void InsetFloat::placement(string const & p)
277 // Here we should only allow the placement to be set
286 string const & InsetFloat::placement() const
288 return floatPlacement_;
292 void InsetFloat::wide(bool w)
296 string lab(_("float:"));
301 string lab(_("float:"));
308 bool InsetFloat::wide() const