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)
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), params_(in.params_)
145 InsetFloat::~InsetFloat()
151 void InsetFloat::writeParams(ostream & os) const
153 os << "Float " // getInsetName()
154 << params_.type << '\n';
156 if (!params_.placement.empty()) {
157 os << "placement " << params_.placement << "\n";
162 os << "wide false\n";
167 void InsetFloat::write(Buffer const * buf, ostream & os) const
170 InsetCollapsable::write(buf, os);
174 void InsetFloat::readParams(Buffer const * buf, LyXLex & lex)
178 string token = lex.getString();
179 if (token == "placement") {
181 params_.placement = lex.getString();
183 // take countermeasures
184 lex.pushToken(token);
187 token = lex.getString();
188 if (token == "wide") {
190 string const tmptoken = lex.getString();
191 if (tmptoken == "true")
192 wide(true, buf->params);
194 wide(false, buf->params);
196 lyxerr << "InsetFloat::Read:: Missing wide!"
198 // take countermeasures
199 lex.pushToken(token);
205 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
207 readParams(buf, lex);
208 InsetCollapsable::read(buf, lex);
212 void InsetFloat::validate(LaTeXFeatures & features) const
214 if (contains(placement(), "H")) {
215 features.require("float");
218 features.useFloat(params_.type);
219 InsetCollapsable::validate(features);
223 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
225 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
229 string const InsetFloat::editMessage() const
231 return _("Opened Float Inset");
235 int InsetFloat::latex(Buffer const * buf,
236 ostream & os, bool fragile, bool fp) const
238 FloatList const & floats = buf->params.getLyXTextClass().floats();
239 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
240 // Figure out the float placement to use.
241 // From lowest to highest:
242 // - float default placement
243 // - document wide default placement
244 // - specific float placement
246 string const buf_placement = buf->params.float_placement;
247 string const def_placement = floats.defaultPlacement(params_.type);
248 if (!params_.placement.empty()
249 && params_.placement != def_placement) {
250 placement = params_.placement;
251 } else if (params_.placement.empty()
252 && !buf_placement.empty()
253 && buf_placement != def_placement) {
254 placement = buf_placement;
257 // The \n is used to force \begin{<floatname>} to appear in a new line.
258 // The % is needed to prevent two consecutive \n chars in the case
259 // when the current output line is empty.
260 os << "%\n\\begin{" << tmptype << '}';
261 // We only output placement if different from the def_placement.
262 if (!placement.empty()) {
263 os << '[' << placement << ']';
267 int const i = inset.latex(buf, os, fragile, fp);
269 // The \n is used to force \end{<floatname>} to appear in a new line.
270 // In this case, we do not case if the current output line is empty.
271 os << "\n\\end{" << tmptype << "}\n";
277 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
279 os << '<' << params_.type << '>';
280 int const i = inset.docbook(buf, os, mixcont);
281 os << "</" << params_.type << '>';
287 bool InsetFloat::insetAllowed(Inset::Code code) const
289 if (code == Inset::FLOAT_CODE)
291 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
292 return inset.insetAllowed(code);
293 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
299 bool InsetFloat::showInsetDialog(BufferView * bv) const
301 if (!inset.showInsetDialog(bv)) {
302 bv->owner()->getDialogs().showFloat(const_cast<InsetFloat *>(this));
308 string const & InsetFloat::type() const
314 void InsetFloat::placement(string const & p)
316 // FIX: Here we should only allow the placement to be set
318 params_.placement = p;
322 string const & InsetFloat::placement() const
324 return params_.placement;
328 void InsetFloat::wide(bool w, BufferParams const & bp)
332 string lab(_("float:"));
333 lab += floatname(params_.type, bp);
342 bool InsetFloat::wide() const
348 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
350 ParIterator pit(inset.paragraph());
353 // Find a caption layout in one of the (child inset's) pars
354 for (; pit != end; ++pit) {
355 Paragraph * tmp = *pit;
357 if (tmp->layout()->name() == caplayout) {
358 string const name = floatname(type(), buf->params);
360 tostr(toclist[name].size() + 1)
361 + ". " + tmp->asString(buf, false);
362 toc::TocItem const item(tmp->id(), 0 , str);
363 toclist[name].push_back(item);