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 #pragma implementation
17 #include "insetfloat.h"
20 #include "BufferView.h"
22 #include "insets/insettext.h"
23 #include "support/LOstream.h"
24 #include "support/lstrings.h"
25 #include "FloatList.h"
26 #include "LaTeXFeatures.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/Dialogs.h"
37 // With this inset it will be possible to support the latex package
38 // float.sty, and I am sure that with this and some additional support
39 // classes we can support similar functionality in other formats
41 // By using float.sty we will have the same handling for all floats, both
42 // for those already in existance (table and figure) and all user created
43 // ones¹. So suddenly we give the users the possibility of creating new
44 // kinds of floats on the fly. (and with a uniform look)
47 // \newfloat{type}{placement}{ext}[within]
48 // type - The "type" of the new class of floats, like program or
49 // algorithm. After the appropriate \newfloat, commands
50 // such as \begin{program} or \end{algorithm*} will be
52 // placement - The default placement for the given class of floats.
53 // They are like in standard LaTeX: t, b, p and h for top,
54 // bottom, page, and here, respectively. On top of that
55 // there is a new type, H, which does not really correspond
56 // to a float, since it means: put it "here" and nowhere else.
57 // Note, however that the H specifier is special and, because
58 // of implementation details cannot be used in the second
59 // argument of \newfloat.
60 // ext - The file name extension of an auxiliary file for the list
61 // of figures (or whatever). LaTeX writes the captions to
63 // within - This (optional) argument determines whether floats of this
64 // class will be numbered within some sectional unit of the
65 // document. For example, if within is equal to chapter, the
66 // floats will be numbered within chapters.
68 // style - plain, boxed, ruled
69 // \floatname{float}{floatname}
72 // \floatplacement{float}{placement}
75 // \restylefloat{float}
77 // \listof{type}{title}
80 // ¹ the algorithm float is defined using the float.sty package. Like this
82 // \newfloat{algorithm}{htbp}{loa}[<sect>]
83 // \floatname{algorithm}{Algorithm}
85 // The intention is that floats should be definable from two places:
87 // - the "gui" (i.e. by the user)
90 // This should only be done for floats defined in a documentclass and that
91 // does not need any additional packages. The two most known floats in this
92 // category is "table" and "figure". Floats defined in layout files are only
93 // stored in lyx files if the user modifies them.
96 // There should be a gui dialog (and also a collection of lyxfuncs) where
97 // the user can modify existing floats and/or create new ones.
99 // The individual floats will also have some settable
100 // variables: wide and placement.
106 // this should not be hardcoded, but be part of the definition
107 // of the float (JMarc)
108 string const caplayout("Caption");
110 string floatname(string const & type, BufferParams const & bp)
112 FloatList const & floats = bp.getLyXTextClass().floats();
113 FloatList::const_iterator it = floats[type];
114 if (it == floats.end())
117 return _(it->second.name());
123 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
124 : InsetCollapsable(bp), wide_(false)
126 string lab(_("float: "));
127 lab += floatname(type, bp);
129 LyXFont font(LyXFont::ALL_SANE);
132 font.setColor(LColor::collapsable);
136 LyXTextClass const & tclass = bp.getLyXTextClass();
137 if (tclass.hasLayout(caplayout))
138 inset.paragraph()->layout(tclass[caplayout]);
142 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
143 : InsetCollapsable(in, same_id), floatType_(in.floatType_),
144 floatPlacement_(in.floatPlacement_), wide_(in.wide_)
148 InsetFloat::~InsetFloat()
154 void InsetFloat::write(Buffer const * buf, ostream & os) const
156 os << "Float " // getInsetName()
157 << floatType_ << '\n';
159 if (!floatPlacement_.empty()) {
160 os << "placement " << floatPlacement_ << "\n";
165 os << "wide false\n";
168 InsetCollapsable::write(buf, os);
172 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
176 string token = lex.getString();
177 if (token == "placement") {
179 floatPlacement_ = lex.getString();
181 // take countermeasures
182 lex.pushToken(token);
185 token = lex.getString();
186 if (token == "wide") {
188 string const tmptoken = lex.getString();
189 if (tmptoken == "true")
190 wide(true, buf->params);
192 wide(false, buf->params);
194 lyxerr << "InsetFloat::Read:: Missing wide!"
196 // take countermeasures
197 lex.pushToken(token);
200 InsetCollapsable::read(buf, lex);
204 void InsetFloat::validate(LaTeXFeatures & features) const
206 if (contains(placement(), "H")) {
207 features.require("float");
210 features.useFloat(floatType_);
211 InsetCollapsable::validate(features);
215 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
217 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
221 string const InsetFloat::editMessage() const
223 return _("Opened Float Inset");
227 int InsetFloat::latex(Buffer const * buf,
228 ostream & os, bool fragile, bool fp) const
230 FloatList const & floats = buf->params.getLyXTextClass().floats();
231 string const tmptype = (wide_ ? floatType_ + "*" : floatType_);
232 // Figure out the float placement to use.
233 // From lowest to highest:
234 // - float default placement
235 // - document wide default placement
236 // - specific float placement
238 string const buf_placement = buf->params.float_placement;
239 string const def_placement = floats.defaultPlacement(floatType_);
240 if (!floatPlacement_.empty()
241 && floatPlacement_ != def_placement) {
242 placement = floatPlacement_;
243 } else if (floatPlacement_.empty()
244 && !buf_placement.empty()
245 && buf_placement != def_placement) {
246 placement = buf_placement;
249 // The \n is used to force \begin{<floatname>} to appear in a new line.
250 // The % is needed to prevent two consecutive \n chars in the case
251 // when the current output line is empty.
252 os << "%\n\\begin{" << tmptype << "}";
253 // We only output placement if different from the def_placement.
254 if (!placement.empty()) {
255 os << "[" << placement << "]";
259 int const i = inset.latex(buf, os, fragile, fp);
261 // The \n is used to force \end{<floatname>} to appear in a new line.
262 // In this case, we do not case if the current output line is empty.
263 os << "\n\\end{" << tmptype << "}\n";
269 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
271 os << "<" << floatType_ << ">";
272 int const i = inset.docbook(buf, os, mixcont);
273 os << "</" << floatType_ << ">";
279 bool InsetFloat::insetAllowed(Inset::Code code) const
281 if (code == Inset::FLOAT_CODE)
283 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
284 return inset.insetAllowed(code);
285 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
291 bool InsetFloat::showInsetDialog(BufferView * bv) const
293 if (!inset.showInsetDialog(bv)) {
294 bv->owner()->getDialogs().showFloat(const_cast<InsetFloat *>(this));
300 string const & InsetFloat::type() const
306 void InsetFloat::placement(string const & p)
308 // FIX: Here we should only allow the placement to be set
314 string const & InsetFloat::placement() const
316 return floatPlacement_;
320 void InsetFloat::wide(bool w, BufferParams const & bp)
324 string lab(_("float:"));
325 lab += floatname(floatType_, bp);
334 bool InsetFloat::wide() const
340 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
342 // Now find the caption in the float...
343 // We now tranverse the paragraphs of
345 Paragraph * tmp = inset.paragraph();
347 if (tmp->layout()->name() == caplayout) {
349 tostr(toclist[type()].size() + 1)
350 + ". " + tmp->asString(buf, false);
351 toc::TocItem const item(tmp, 0 , str);
352 toclist[type()].push_back(item);