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"
29 #include "frontends/LyXView.h"
30 #include "frontends/Dialogs.h"
36 // With this inset it will be possible to support the latex package
37 // float.sty, and I am sure that with this and some additional support
38 // classes we can support similar functionality in other formats
40 // By using float.sty we will have the same handling for all floats, both
41 // for those already in existance (table and figure) and all user created
42 // ones¹. So suddenly we give the users the possibility of creating new
43 // kinds of floats on the fly. (and with a uniform look)
46 // \newfloat{type}{placement}{ext}[within]
47 // type - The "type" of the new class of floats, like program or
48 // algorithm. After the appropriate \newfloat, commands
49 // such as \begin{program} or \end{algorithm*} will be
51 // placement - The default placement for the given class of floats.
52 // They are like in standard LaTeX: t, b, p and h for top,
53 // bottom, page, and here, respectively. On top of that
54 // there is a new type, H, which does not really correspond
55 // to a float, since it means: put it "here" and nowhere else.
56 // Note, however that the H specifier is special and, because
57 // of implementation details cannot be used in the second
58 // argument of \newfloat.
59 // ext - The file name extension of an auxiliary file for the list
60 // of figures (or whatever). LaTeX writes the captions to
62 // within - This (optional) argument determines whether floats of this
63 // class will be numbered within some sectional unit of the
64 // document. For example, if within is equal to chapter, the
65 // floats will be numbered within chapters.
67 // style - plain, boxed, ruled
68 // \floatname{float}{floatname}
71 // \floatplacement{float}{placement}
74 // \restylefloat{float}
76 // \listof{type}{title}
79 // ¹ the algorithm float is defined using the float.sty package. Like this
81 // \newfloat{algorithm}{htbp}{loa}[<sect>]
82 // \floatname{algorithm}{Algorithm}
84 // The intention is that floats should be definable from two places:
86 // - the "gui" (i.e. by the user)
89 // This should only be done for floats defined in a documentclass and that
90 // does not need any additional packages. The two most known floats in this
91 // category is "table" and "figure". Floats defined in layout files are only
92 // stored in lyx files if the user modifies them.
95 // There should be a gui dialog (and also a collection of lyxfuncs) where
96 // the user can modify existing floats and/or create new ones.
98 // The individual floats will also have some settable
99 // variables: wide and placement.
105 // this should not be hardcoded, but be part of the definition
106 // of the float (JMarc)
107 string const caplayout("Caption");
109 string floatname(string const & type)
111 FloatList::const_iterator it = floatList[type];
112 if (it == floatList.end())
115 return _(it->second.name());
121 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
122 : InsetCollapsable(bp), wide_(false)
124 string lab(_("float: "));
125 lab += floatname(type);
127 LyXFont font(LyXFont::ALL_SANE);
130 font.setColor(LColor::collapsable);
134 LyXTextClass const & tclass = bp.getLyXTextClass();
135 if (tclass.hasLayout(caplayout))
136 inset.paragraph()->layout(tclass[caplayout]);
140 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
141 : InsetCollapsable(in, same_id), floatType_(in.floatType_),
142 floatPlacement_(in.floatPlacement_), wide_(in.wide_)
146 InsetFloat::~InsetFloat()
152 void InsetFloat::write(Buffer const * buf, ostream & os) const
154 os << "Float " // getInsetName()
155 << floatType_ << '\n';
157 if (!floatPlacement_.empty()) {
158 os << "placement " << floatPlacement_ << "\n";
163 os << "wide false\n";
166 InsetCollapsable::write(buf, os);
170 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
174 string token = lex.getString();
175 if (token == "placement") {
177 floatPlacement_ = lex.getString();
179 // take countermeasures
180 lex.pushToken(token);
183 token = lex.getString();
184 if (token == "wide") {
186 string const tmptoken = lex.getString();
187 if (tmptoken == "true")
192 lyxerr << "InsetFloat::Read:: Missing wide!"
194 // take countermeasures
195 lex.pushToken(token);
198 InsetCollapsable::read(buf, lex);
202 void InsetFloat::validate(LaTeXFeatures & features) const
204 if (contains(placement(), "H")) {
205 features.require("float");
208 features.useFloat(floatType_);
209 InsetCollapsable::validate(features);
213 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
215 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
219 string const InsetFloat::editMessage() const
221 return _("Opened Float Inset");
225 int InsetFloat::latex(Buffer const * buf,
226 ostream & os, bool fragile, bool fp) const
228 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
229 // Figure out the float placement to use.
230 // From lowest to highest:
231 // - float default placement
232 // - document wide default placement
233 // - specific float placement
235 string const buf_placement = buf->params.float_placement;
236 string const def_placement = floatList.defaultPlacement(floatType_);
237 if (!floatPlacement_.empty()
238 && floatPlacement_ != def_placement) {
239 placement = floatPlacement_;
240 } else if (!buf_placement.empty()
241 && buf_placement != def_placement) {
242 placement = buf_placement;
245 // The \n is used to force \begin{<floatname>} to appear in a new line.
246 // The % is needed to prevent two consecutive \n chars in the case
247 // when the current output line is empty.
248 os << "%\n\\begin{" << tmptype << "}";
249 // We only output placement if different from the def_placement.
250 if (!placement.empty()) {
251 os << "[" << placement << "]";
255 int const i = inset.latex(buf, os, fragile, fp);
257 // The \n is used to force \end{<floatname>} to appear in a new line.
258 // In this case, we do not case if the current output line is empty.
259 os << "\n\\end{" << tmptype << "}\n";
265 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
267 os << "<" << floatType_ << ">";
268 int const i = inset.docbook(buf, os, mixcont);
269 os << "</" << floatType_ << ">";
275 bool InsetFloat::insetAllowed(Inset::Code code) const
277 if (code == Inset::FLOAT_CODE)
279 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
280 return inset.insetAllowed(code);
281 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
287 bool InsetFloat::showInsetDialog(BufferView * bv) const
289 if (!inset.showInsetDialog(bv)) {
290 bv->owner()->getDialogs().showFloat(const_cast<InsetFloat *>(this));
296 string const & InsetFloat::type() const
302 void InsetFloat::placement(string const & p)
304 // FIX: Here we should only allow the placement to be set
310 string const & InsetFloat::placement() const
312 return floatPlacement_;
316 void InsetFloat::wide(bool w)
320 string lab(_("float:"));
321 lab += floatname(floatType_);
330 bool InsetFloat::wide() const
336 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
338 // Now find the caption in the float...
339 // We now tranverse the paragraphs of
341 Paragraph * tmp = inset.paragraph();
343 if (tmp->layout()->name() == caplayout) {
345 tostr(toclist[type()].size() + 1)
346 + ". " + tmp->asString(buf, false);
347 toc::TocItem const item(tmp, 0 , str);
348 toclist[type()].push_back(item);