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 "support/lstrings.h"
24 #include "FloatList.h"
25 #include "LaTeXFeatures.h"
30 #include "frontends/Dialogs.h"
35 // With this inset it will be possible to support the latex package
36 // float.sty, and I am sure that with this and some additional support
37 // classes we can support similar functionality in other formats
39 // By using float.sty we will have the same handling for all floats, both
40 // for those already in existance (table and figure) and all user created
41 // ones¹. So suddenly we give the users the possibility of creating new
42 // kinds of floats on the fly. (and with a uniform look)
45 // \newfloat{type}{placement}{ext}[within]
46 // type - The "type" of the new class of floats, like program or
47 // algorithm. After the appropriate \newfloat, commands
48 // such as \begin{program} or \end{algorithm*} will be
50 // placement - The default placement for the given class of floats.
51 // They are like in standard LaTeX: t, b, p and h for top,
52 // bottom, page, and here, respectively. On top of that
53 // there is a new type, H, which does not really correspond
54 // to a float, since it means: put it "here" and nowhere else.
55 // Note, however that the H specifier is special and, because
56 // of implementation details cannot be used in the second
57 // argument of \newfloat.
58 // ext - The file name extension of an auxiliary file for the list
59 // of figures (or whatever). LaTeX writes the captions to
61 // within - This (optional) argument determines whether floats of this
62 // class will be numbered within some sectional unit of the
63 // document. For example, if within is equal to chapter, the
64 // floats will be numbered within chapters.
66 // style - plain, boxed, ruled
67 // \floatname{float}{floatname}
70 // \floatplacement{float}{placement}
73 // \restylefloat{float}
75 // \listof{type}{title}
78 // ¹ the algorithm float is defined using the float.sty package. Like this
80 // \newfloat{algorithm}{htbp}{loa}[<sect>]
81 // \floatname{algorithm}{Algorithm}
83 // The intention is that floats should be definable from two places:
85 // - the "gui" (i.e. by the user)
88 // This should only be done for floats defined in a documentclass and that
89 // does not need any additional packages. The two most known floats in this
90 // category is "table" and "figure". Floats defined in layout files are only
91 // stored in lyx files if the user modifies them.
94 // There should be a gui dialog (and also a collection of lyxfuncs) where
95 // the user can modify existing floats and/or create new ones.
97 // The individual floats will also have some settable
98 // variables: wide and placement.
102 InsetFloat::InsetFloat(string const & type)
103 : InsetCollapsable(), wide_(false)
105 string lab(_("float:"));
108 LyXFont font(LyXFont::ALL_SANE);
111 font.setColor(LColor::collapsable);
118 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
119 : InsetCollapsable(in, same_id), floatType_(in.floatType_),
120 floatPlacement_(in.floatPlacement_), wide_(in.wide_)
124 InsetFloat::~InsetFloat()
130 void InsetFloat::write(Buffer const * buf, ostream & os) const
132 os << "Float " // getInsetName()
133 << floatType_ << '\n';
135 if (floatPlacement_.empty()) {
137 << floatList.getType(floatType_).placement() << "\n";
139 os << "placement " << floatPlacement_ << "\n";
144 os << "wide false\n";
147 InsetCollapsable::write(buf, os);
151 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
155 string token = lex.getString();
156 if (token == "placement") {
158 floatPlacement_ = lex.getString();
160 lyxerr << "InsetFloat::Read: Missing placement!"
162 // take countermeasures
163 lex.pushToken(token);
166 token = lex.getString();
167 if (token == "wide") {
169 string const tmptoken = lex.getString();
170 if (tmptoken == "true")
175 lyxerr << "InsetFloat::Read:: Missing wide!"
177 // take countermeasures
178 lex.pushToken(token);
181 InsetCollapsable::read(buf, lex);
185 void InsetFloat::validate(LaTeXFeatures & features) const
187 if (contains(placement(), "H")) {
188 features.floats = true;
191 features.usedFloats.insert(floatType_);
192 InsetCollapsable::validate(features);
196 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
198 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
202 string const InsetFloat::editMessage() const
204 return _("Opened Float Inset");
208 int InsetFloat::latex(Buffer const * buf,
209 ostream & os, bool fragile, bool fp) const
211 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
212 // Figure out the float placement to use.
213 // From lowest to highest:
214 // - float default placement
215 // - document wide default placement
216 // - specific float placement
218 string const buf_placement = buf->params.float_placement;
219 string const def_placement = floatList.defaultPlacement(floatType_);
220 if (!floatPlacement_.empty()
221 && floatPlacement_ != def_placement) {
222 placement = floatPlacement_;
223 } else if (!buf_placement.empty()
224 && buf_placement != def_placement) {
225 placement = buf_placement;
228 os << "\\begin{" << tmptype << "}";
229 // We only output placement if different from the def_placement.
230 if (!placement.empty()) {
231 os << "[" << placement << "]";
236 int const i = inset.latex(buf, os, fragile, fp);
237 os << "\\end{" << tmptype << "}%\n";
243 int InsetFloat::docBook(Buffer const * buf, ostream & os) const
245 os << "<" << floatType_ << ">";
246 int const i = inset.docBook(buf, os);
247 os << "</" << floatType_ << ">";
253 bool InsetFloat::insetAllowed(Inset::Code code) const
255 if (code == Inset::FLOAT_CODE)
257 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
258 return inset.insetAllowed(code);
259 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
265 bool InsetFloat::showInsetDialog(BufferView * bv) const
267 if (!inset.showInsetDialog(bv)) {
268 bv->owner()->getDialogs()->showFloat(const_cast<InsetFloat *>(this));
274 void InsetFloat::insetButtonRelease(BufferView * bv, int x, int y, int button)
277 if ((x >= 0) && (x < button_length) &&
278 (y >= button_top_y) && (y <= button_bottom_y) &&
290 InsetCollapsable::insetButtonRelease(bv, x, y, button);
294 string const & InsetFloat::type() const
300 void InsetFloat::placement(string const & p)
302 // Here we should only allow the placement to be set
311 string const & InsetFloat::placement() const
313 return floatPlacement_;
317 void InsetFloat::wide(bool w)
321 string lab(_("float:"));
326 string lab(_("float:"));
333 bool InsetFloat::wide() const