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()) {
159 << floatList.getType(floatType_).placement() << "\n";
161 os << "placement " << floatPlacement_ << "\n";
166 os << "wide false\n";
169 InsetCollapsable::write(buf, os);
173 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
177 string token = lex.getString();
178 if (token == "placement") {
180 floatPlacement_ = lex.getString();
182 lyxerr << "InsetFloat::Read: Missing placement!"
184 // take countermeasures
185 lex.pushToken(token);
188 token = lex.getString();
189 if (token == "wide") {
191 string const tmptoken = lex.getString();
192 if (tmptoken == "true")
197 lyxerr << "InsetFloat::Read:: Missing wide!"
199 // take countermeasures
200 lex.pushToken(token);
203 InsetCollapsable::read(buf, lex);
207 void InsetFloat::validate(LaTeXFeatures & features) const
209 if (contains(placement(), "H")) {
210 features.require("float");
213 features.useFloat(floatType_);
214 InsetCollapsable::validate(features);
218 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
220 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
224 string const InsetFloat::editMessage() const
226 return _("Opened Float Inset");
230 int InsetFloat::latex(Buffer const * buf,
231 ostream & os, bool fragile, bool fp) const
233 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
234 // Figure out the float placement to use.
235 // From lowest to highest:
236 // - float default placement
237 // - document wide default placement
238 // - specific float placement
240 string const buf_placement = buf->params.float_placement;
241 string const def_placement = floatList.defaultPlacement(floatType_);
242 if (!floatPlacement_.empty()
243 && floatPlacement_ != def_placement) {
244 placement = floatPlacement_;
245 } else if (!buf_placement.empty()
246 && buf_placement != def_placement) {
247 placement = buf_placement;
250 // The \n is used to force \begin{<floatname>} to appear in a new line.
251 // The % is needed to prevent two consecutive \n chars in the case
252 // when the current output line is empty.
253 os << "%\n\\begin{" << tmptype << "}";
254 // We only output placement if different from the def_placement.
255 if (!placement.empty()) {
256 os << "[" << placement << "]";
260 int const i = inset.latex(buf, os, fragile, fp);
262 // The \n is used to force \end{<floatname>} to appear in a new line.
263 // In this case, we do not case if the current output line is empty.
264 os << "\n\\end{" << tmptype << "}\n";
270 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
272 os << "<" << floatType_ << ">";
273 int const i = inset.docbook(buf, os, mixcont);
274 os << "</" << floatType_ << ">";
280 bool InsetFloat::insetAllowed(Inset::Code code) const
282 if (code == Inset::FLOAT_CODE)
284 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
285 return inset.insetAllowed(code);
286 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
292 bool InsetFloat::showInsetDialog(BufferView * bv) const
294 if (!inset.showInsetDialog(bv)) {
295 bv->owner()->getDialogs()->showFloat(const_cast<InsetFloat *>(this));
301 string const & InsetFloat::type() const
307 void InsetFloat::placement(string const & p)
309 // FIX: Here we should only allow the placement to be set
315 string const & InsetFloat::placement() const
317 return floatPlacement_;
321 void InsetFloat::wide(bool w)
325 string lab(_("float:"));
326 lab += floatname(floatType_);
335 bool InsetFloat::wide() const
341 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
343 // Now find the caption in the float...
344 // We now tranverse the paragraphs of
346 Paragraph * tmp = inset.paragraph();
348 if (tmp->layout()->name() == caplayout) {
350 tostr(toclist[type()].size() + 1)
351 + ". " + tmp->asString(buf, false);
352 toc::TocItem const item(tmp, 0 , str);
353 toclist[type()].push_back(item);