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, BufferParams const & bp)
111 FloatList const & floats = bp.getLyXTextClass().floats();
112 FloatList::const_iterator it = floats[type];
113 if (it == floats.end())
116 return _(it->second.name());
122 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
123 : InsetCollapsable(bp), wide_(false)
125 string lab(_("float: "));
126 lab += floatname(type, bp);
128 LyXFont font(LyXFont::ALL_SANE);
131 font.setColor(LColor::collapsable);
135 LyXTextClass const & tclass = bp.getLyXTextClass();
136 if (tclass.hasLayout(caplayout))
137 inset.paragraph()->layout(tclass[caplayout]);
141 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
142 : InsetCollapsable(in, same_id), floatType_(in.floatType_),
143 floatPlacement_(in.floatPlacement_), wide_(in.wide_)
147 InsetFloat::~InsetFloat()
153 void InsetFloat::write(Buffer const * buf, ostream & os) const
155 os << "Float " // getInsetName()
156 << floatType_ << '\n';
158 if (!floatPlacement_.empty()) {
159 os << "placement " << floatPlacement_ << "\n";
164 os << "wide false\n";
167 InsetCollapsable::write(buf, os);
171 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
175 string token = lex.getString();
176 if (token == "placement") {
178 floatPlacement_ = lex.getString();
180 // take countermeasures
181 lex.pushToken(token);
184 token = lex.getString();
185 if (token == "wide") {
187 string const tmptoken = lex.getString();
188 if (tmptoken == "true")
189 wide(true, buf->params);
191 wide(false, buf->params);
193 lyxerr << "InsetFloat::Read:: Missing wide!"
195 // take countermeasures
196 lex.pushToken(token);
199 InsetCollapsable::read(buf, lex);
203 void InsetFloat::validate(LaTeXFeatures & features) const
205 if (contains(placement(), "H")) {
206 features.require("float");
209 features.useFloat(floatType_);
210 InsetCollapsable::validate(features);
214 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
216 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
220 string const InsetFloat::editMessage() const
222 return _("Opened Float Inset");
226 int InsetFloat::latex(Buffer const * buf,
227 ostream & os, bool fragile, bool fp) const
229 FloatList const & floats = buf->params.getLyXTextClass().floats();
230 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
231 // Figure out the float placement to use.
232 // From lowest to highest:
233 // - float default placement
234 // - document wide default placement
235 // - specific float placement
237 string const buf_placement = buf->params.float_placement;
238 string const def_placement = floats.defaultPlacement(floatType_);
239 if (!floatPlacement_.empty()
240 && floatPlacement_ != def_placement) {
241 placement = floatPlacement_;
242 } else if (!buf_placement.empty()
243 && buf_placement != def_placement) {
244 placement = buf_placement;
247 // The \n is used to force \begin{<floatname>} to appear in a new line.
248 // The % is needed to prevent two consecutive \n chars in the case
249 // when the current output line is empty.
250 os << "%\n\\begin{" << tmptype << "}";
251 // We only output placement if different from the def_placement.
252 if (!placement.empty()) {
253 os << "[" << placement << "]";
257 int const i = inset.latex(buf, os, fragile, fp);
259 // The \n is used to force \end{<floatname>} to appear in a new line.
260 // In this case, we do not case if the current output line is empty.
261 os << "\n\\end{" << tmptype << "}\n";
267 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
269 os << "<" << floatType_ << ">";
270 int const i = inset.docbook(buf, os, mixcont);
271 os << "</" << floatType_ << ">";
277 bool InsetFloat::insetAllowed(Inset::Code code) const
279 if (code == Inset::FLOAT_CODE)
281 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
282 return inset.insetAllowed(code);
283 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
289 bool InsetFloat::showInsetDialog(BufferView * bv) const
291 if (!inset.showInsetDialog(bv)) {
292 bv->owner()->getDialogs().showFloat(const_cast<InsetFloat *>(this));
298 string const & InsetFloat::type() const
304 void InsetFloat::placement(string const & p)
306 // FIX: Here we should only allow the placement to be set
312 string const & InsetFloat::placement() const
314 return floatPlacement_;
318 void InsetFloat::wide(bool w, BufferParams const & bp)
322 string lab(_("float:"));
323 lab += floatname(floatType_, bp);
332 bool InsetFloat::wide() const
338 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
340 // Now find the caption in the float...
341 // We now tranverse the paragraphs of
343 Paragraph * tmp = inset.paragraph();
345 if (tmp->layout()->name() == caplayout) {
347 tostr(toclist[type()].size() + 1)
348 + ". " + tmp->asString(buf, false);
349 toc::TocItem const item(tmp, 0 , str);
350 toclist[type()].push_back(item);