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"
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
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)
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
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
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.
62 // style - plain, boxed, ruled
63 // \floatname{float}{floatname}
66 // \floatplacement{float}{placement}
69 // \restylefloat{float}
71 // \listof{type}{title}
74 // ¹ the algorithm float is defined using the float.sty package. Like this
76 // \newfloat{algorithm}{htbp}{loa}[<sect>]
77 // \floatname{algorithm}{Algorithm}
79 // The intention is that floats should be definable from two places:
81 // - the "gui" (i.e. by the user)
84 // This should only be done for floats defined in a documentclass and that
85 // does not need any additional packages. The two most known floats in this
86 // category is "table" and "figure". Floats defined in layout files are only
87 // stored in lyx files if the user modifies them.
90 // There should be a gui dialog (and also a collection of lyxfuncs) where
91 // the user can modify existing floats and/or create new ones.
93 // The individual floats will also have some settable
94 // variables: wide and placement.
98 InsetFloat::InsetFloat(string const & type)
99 : InsetCollapsable(), wide_(false)
101 string lab(_("float:"));
104 LyXFont font(LyXFont::ALL_SANE);
107 font.setColor(LColor::collapsable);
109 setAutoCollapse(false);
115 void InsetFloat::Write(Buffer const * buf, ostream & os) const
117 os << "Float " // getInsetName()
118 << floatType_ << '\n';
120 if (floatPlacement_.empty()) {
122 << floatList.getType(floatType_).placement() << "\n";
124 os << "placement " << floatPlacement_ << "\n";
129 os << "wide false\n";
132 InsetCollapsable::Write(buf, os);
136 void InsetFloat::Read(Buffer const * buf, LyXLex & lex)
140 string token = lex.GetString();
141 if (token == "placement") {
143 floatPlacement_ = lex.GetString();
145 lyxerr << "InsetFloat::Read: Missing placement!"
149 token = lex.GetString();
150 if (token == "wide") {
152 string const tmptoken = lex.GetString();
153 if (tmptoken == "true")
158 lyxerr << "InsetFloat::Read:: Missing wide!"
162 InsetCollapsable::Read(buf, lex);
166 void InsetFloat::Validate(LaTeXFeatures & features) const
168 features.usedFloats.insert(floatType_);
172 Inset * InsetFloat::Clone(Buffer const &) const
174 InsetFloat * result = new InsetFloat(floatType_);
175 result->inset.init(&inset);
177 result->collapsed = collapsed;
182 string const InsetFloat::EditMessage() const
184 return _("Opened Float Inset");
188 int InsetFloat::Latex(Buffer const * buf,
189 ostream & os, bool fragile, bool fp) const
191 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
193 os << "\\begin{" << tmptype << "}";
194 if (!floatPlacement_.empty()
195 && floatPlacement_ != floatList.defaultPlacement(floatType_))
196 os << "[" << floatPlacement_ << "]";
199 int const i = inset.Latex(buf, os, fragile, fp);
200 os << "\\end{" << tmptype << "}%\n";
206 int InsetFloat::DocBook(Buffer const * buf, ostream & os) const
208 os << "<" << floatType_ << ">";
209 int const i = inset.DocBook(buf, os);
210 os << "</" << floatType_ << ">";
216 bool InsetFloat::InsertInsetAllowed(Inset * in) const
218 if ((in->LyxCode() == Inset::FOOT_CODE) ||
219 (in->LyxCode() == Inset::MARGIN_CODE)) {
226 void InsetFloat::InsetButtonRelease(BufferView * bv, int x, int y, int button)
231 && y < button_bottom_y
233 // This obviously need to change.
234 lyxerr << "InsetFloat: Let's edit this floats parameters!"
236 //bv->owner()->getDialogs()->showFloat(this);
238 InsetCollapsable::InsetButtonRelease(bv, x, y, button);
243 string const & InsetFloat::type() const
249 void InsetFloat::placement(string const & p)
251 // Here we should only allow the placement to be set
260 string const & InsetFloat::placement() const
262 return floatPlacement_;
266 void InsetFloat::wide(bool w)
270 string lab(_("float:"));
275 string lab(_("float:"));
282 bool InsetFloat::wide() const