3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS
14 #include "insetfloat.h"
17 #include "BufferView.h"
19 #include "insets/insettext.h"
20 #include "support/LOstream.h"
21 #include "support/lstrings.h"
22 #include "FloatList.h"
23 #include "LaTeXFeatures.h"
27 #include "frontends/LyXView.h"
28 #include "frontends/Dialogs.h"
30 #include "iterators.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.
104 // this should not be hardcoded, but be part of the definition
105 // of the float (JMarc)
106 string const caplayout("Caption");
108 string floatname(string const & type, BufferParams const & bp)
110 FloatList const & floats = bp.getLyXTextClass().floats();
111 FloatList::const_iterator it = floats[type];
112 if (it == floats.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, bp);
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")
188 wide(true, buf->params);
190 wide(false, buf->params);
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 FloatList const & floats = buf->params.getLyXTextClass().floats();
229 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
230 // Figure out the float placement to use.
231 // From lowest to highest:
232 // - float default placement
233 // - document wide default placement
234 // - specific float placement
236 string const buf_placement = buf->params.float_placement;
237 string const def_placement = floats.defaultPlacement(floatType_);
238 if (!floatPlacement_.empty()
239 && floatPlacement_ != def_placement) {
240 placement = floatPlacement_;
241 } else if (floatPlacement_.empty()
242 && !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 ParIterator pit(inset.paragraph());
343 // Find a caption layout in one of the (child inset's) pars
344 for (; pit != end; ++pit) {
345 Paragraph * tmp = *pit;
347 if (tmp->layout()->name() == caplayout) {
348 string const name = floatname(type(), buf->params);
350 tostr(toclist[name].size() + 1)
351 + ". " + tmp->asString(buf, false);
352 toc::TocItem const item(tmp->id(), 0 , str);
353 toclist[name].push_back(item);