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"
15 #include "insettext.h"
18 #include "BufferView.h"
21 #include "FloatList.h"
22 #include "funcrequest.h"
24 #include "iterators.h"
25 #include "LaTeXFeatures.h"
30 #include "support/LOstream.h"
31 #include "support/lstrings.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/Dialogs.h"
39 // With this inset it will be possible to support the latex package
40 // float.sty, and I am sure that with this and some additional support
41 // classes we can support similar functionality in other formats
43 // By using float.sty we will have the same handling for all floats, both
44 // for those already in existance (table and figure) and all user created
45 // ones¹. So suddenly we give the users the possibility of creating new
46 // kinds of floats on the fly. (and with a uniform look)
49 // \newfloat{type}{placement}{ext}[within]
50 // type - The "type" of the new class of floats, like program or
51 // algorithm. After the appropriate \newfloat, commands
52 // such as \begin{program} or \end{algorithm*} will be
54 // placement - The default placement for the given class of floats.
55 // They are like in standard LaTeX: t, b, p and h for top,
56 // bottom, page, and here, respectively. On top of that
57 // there is a new type, H, which does not really correspond
58 // to a float, since it means: put it "here" and nowhere else.
59 // Note, however that the H specifier is special and, because
60 // of implementation details cannot be used in the second
61 // argument of \newfloat.
62 // ext - The file name extension of an auxiliary file for the list
63 // of figures (or whatever). LaTeX writes the captions to
65 // within - This (optional) argument determines whether floats of this
66 // class will be numbered within some sectional unit of the
67 // document. For example, if within is equal to chapter, the
68 // floats will be numbered within chapters.
70 // style - plain, boxed, ruled
71 // \floatname{float}{floatname}
74 // \floatplacement{float}{placement}
77 // \restylefloat{float}
79 // \listof{type}{title}
82 // ¹ the algorithm float is defined using the float.sty package. Like this
84 // \newfloat{algorithm}{htbp}{loa}[<sect>]
85 // \floatname{algorithm}{Algorithm}
87 // The intention is that floats should be definable from two places:
89 // - the "gui" (i.e. by the user)
92 // This should only be done for floats defined in a documentclass and that
93 // does not need any additional packages. The two most known floats in this
94 // category is "table" and "figure". Floats defined in layout files are only
95 // stored in lyx files if the user modifies them.
98 // There should be a gui dialog (and also a collection of lyxfuncs) where
99 // the user can modify existing floats and/or create new ones.
101 // The individual floats will also have some settable
102 // variables: wide and placement.
108 // this should not be hardcoded, but be part of the definition
109 // of the float (JMarc)
110 string const caplayout("Caption");
112 string floatname(string const & type, BufferParams const & bp)
114 FloatList const & floats = bp.getLyXTextClass().floats();
115 FloatList::const_iterator it = floats[type];
116 if (it == floats.end())
119 return _(it->second.name());
125 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
126 : InsetCollapsable(bp)
128 string lab(_("float: "));
129 lab += floatname(type, bp);
131 LyXFont font(LyXFont::ALL_SANE);
134 font.setColor(LColor::collapsable);
138 LyXTextClass const & tclass = bp.getLyXTextClass();
139 if (tclass.hasLayout(caplayout))
140 inset.paragraph()->layout(tclass[caplayout]);
144 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
145 : InsetCollapsable(in, same_id), params_(in.params_)
149 InsetFloat::~InsetFloat()
151 InsetFloatMailer mailer(*this);
156 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
158 Inset::RESULT result = UNDISPATCHED;
160 switch (cmd.action) {
161 case LFUN_INSET_MODIFY: {
162 InsetFloatParams params;
163 InsetFloatMailer::string2params(cmd.argument, params);
165 params_.placement = params.placement;
166 params_.wide = params.wide;
168 cmd.view()->updateInset(this, true);
173 result = InsetCollapsable::localDispatch(cmd);
180 void InsetFloatParams::write(ostream & os) const
182 os << "Float " // getInsetName()
185 if (!placement.empty()) {
186 os << "placement " << placement << "\n";
191 os << "wide false\n";
196 void InsetFloatParams::read(LyXLex & lex)
200 string token = lex.getString();
201 if (token == "placement") {
203 placement = lex.getString();
205 // take countermeasures
206 lex.pushToken(token);
209 token = lex.getString();
210 if (token == "wide") {
212 string const tmptoken = lex.getString();
213 wide = (tmptoken == "true");
215 lyxerr << "InsetFloat::Read:: Missing wide!"
217 // take countermeasures
218 lex.pushToken(token);
224 void InsetFloat::write(Buffer const * buf, ostream & os) const
227 InsetCollapsable::write(buf, os);
231 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
234 wide(params_.wide, buf->params);
235 InsetCollapsable::read(buf, lex);
239 void InsetFloat::validate(LaTeXFeatures & features) const
241 if (contains(placement(), "H")) {
242 features.require("float");
245 features.useFloat(params_.type);
246 InsetCollapsable::validate(features);
250 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
252 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
256 string const InsetFloat::editMessage() const
258 return _("Opened Float Inset");
262 int InsetFloat::latex(Buffer const * buf,
263 ostream & os, bool fragile, bool fp) const
265 FloatList const & floats = buf->params.getLyXTextClass().floats();
266 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
267 // Figure out the float placement to use.
268 // From lowest to highest:
269 // - float default placement
270 // - document wide default placement
271 // - specific float placement
273 string const buf_placement = buf->params.float_placement;
274 string const def_placement = floats.defaultPlacement(params_.type);
275 if (!params_.placement.empty()
276 && params_.placement != def_placement) {
277 placement = params_.placement;
278 } else if (params_.placement.empty()
279 && !buf_placement.empty()
280 && buf_placement != def_placement) {
281 placement = buf_placement;
284 // The \n is used to force \begin{<floatname>} to appear in a new line.
285 // The % is needed to prevent two consecutive \n chars in the case
286 // when the current output line is empty.
287 os << "%\n\\begin{" << tmptype << '}';
288 // We only output placement if different from the def_placement.
289 if (!placement.empty()) {
290 os << '[' << placement << ']';
294 int const i = inset.latex(buf, os, fragile, fp);
296 // The \n is used to force \end{<floatname>} to appear in a new line.
297 // In this case, we do not case if the current output line is empty.
298 os << "\n\\end{" << tmptype << "}\n";
304 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
306 os << '<' << params_.type << '>';
307 int const i = inset.docbook(buf, os, mixcont);
308 os << "</" << params_.type << '>';
314 bool InsetFloat::insetAllowed(Inset::Code code) const
316 if (code == Inset::FLOAT_CODE)
318 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
319 return inset.insetAllowed(code);
320 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
326 bool InsetFloat::showInsetDialog(BufferView * bv) const
328 if (!inset.showInsetDialog(bv)) {
329 InsetFloat * tmp = const_cast<InsetFloat *>(this);
330 InsetFloatMailer mailer(*tmp);
337 string const & InsetFloat::type() const
343 void InsetFloat::placement(string const & p)
345 // FIX: Here we should only allow the placement to be set
347 params_.placement = p;
351 string const & InsetFloat::placement() const
353 return params_.placement;
357 void InsetFloat::wide(bool w, BufferParams const & bp)
361 string lab(_("float:"));
362 lab += floatname(params_.type, bp);
371 bool InsetFloat::wide() const
377 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
379 ParIterator pit(inset.paragraph());
382 // Find a caption layout in one of the (child inset's) pars
383 for (; pit != end; ++pit) {
384 Paragraph * tmp = *pit;
386 if (tmp->layout()->name() == caplayout) {
387 string const name = floatname(type(), buf->params);
389 tostr(toclist[name].size() + 1)
390 + ". " + tmp->asString(buf, false);
391 toc::TocItem const item(tmp->id(), 0 , str);
392 toclist[name].push_back(item);
398 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
399 : name_("float"), inset_(inset)
403 string const InsetFloatMailer::inset2string() const
405 return params2string(name(), inset_.params());
409 void InsetFloatMailer::string2params(string const & in,
410 InsetFloatParams & params)
412 params = InsetFloatParams();
415 string body = split(in, name, ' ');
417 if (name != "float" || body.empty())
420 // This is part of the inset proper that is usually swallowed
421 // by Buffer::readInset
422 body = split(body, name, '\n');
423 if (!prefixIs(name, "Float "))
426 istringstream data(body);
435 InsetFloatMailer::params2string(string const & name,
436 InsetFloatParams const & params)