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"
32 #include "support/LOstream.h"
33 #include "support/lstrings.h"
34 #include "support/tostr.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/Dialogs.h"
43 // With this inset it will be possible to support the latex package
44 // float.sty, and I am sure that with this and some additional support
45 // classes we can support similar functionality in other formats
47 // By using float.sty we will have the same handling for all floats, both
48 // for those already in existance (table and figure) and all user created
49 // ones¹. So suddenly we give the users the possibility of creating new
50 // kinds of floats on the fly. (and with a uniform look)
53 // \newfloat{type}{placement}{ext}[within]
54 // type - The "type" of the new class of floats, like program or
55 // algorithm. After the appropriate \newfloat, commands
56 // such as \begin{program} or \end{algorithm*} will be
58 // placement - The default placement for the given class of floats.
59 // They are like in standard LaTeX: t, b, p and h for top,
60 // bottom, page, and here, respectively. On top of that
61 // there is a new type, H, which does not really correspond
62 // to a float, since it means: put it "here" and nowhere else.
63 // Note, however that the H specifier is special and, because
64 // of implementation details cannot be used in the second
65 // argument of \newfloat.
66 // ext - The file name extension of an auxiliary file for the list
67 // of figures (or whatever). LaTeX writes the captions to
69 // within - This (optional) argument determines whether floats of this
70 // class will be numbered within some sectional unit of the
71 // document. For example, if within is equal to chapter, the
72 // floats will be numbered within chapters.
74 // style - plain, boxed, ruled
75 // \floatname{float}{floatname}
78 // \floatplacement{float}{placement}
81 // \restylefloat{float}
83 // \listof{type}{title}
86 // ¹ the algorithm float is defined using the float.sty package. Like this
88 // \newfloat{algorithm}{htbp}{loa}[<sect>]
89 // \floatname{algorithm}{Algorithm}
91 // The intention is that floats should be definable from two places:
93 // - the "gui" (i.e. by the user)
96 // This should only be done for floats defined in a documentclass and that
97 // does not need any additional packages. The two most known floats in this
98 // category is "table" and "figure". Floats defined in layout files are only
99 // stored in lyx files if the user modifies them.
102 // There should be a gui dialog (and also a collection of lyxfuncs) where
103 // the user can modify existing floats and/or create new ones.
105 // The individual floats will also have some settable
106 // variables: wide and placement.
112 // this should not be hardcoded, but be part of the definition
113 // of the float (JMarc)
114 string const caplayout("Caption");
116 string floatname(string const & type, BufferParams const & bp)
118 FloatList const & floats = bp.getLyXTextClass().floats();
119 FloatList::const_iterator it = floats[type];
120 if (it == floats.end())
123 return _(it->second.name());
129 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
130 : InsetCollapsable(bp)
132 string lab(_("float: "));
133 lab += floatname(type, bp);
135 LyXFont font(LyXFont::ALL_SANE);
138 font.setColor(LColor::collapsable);
142 LyXTextClass const & tclass = bp.getLyXTextClass();
143 if (tclass.hasLayout(caplayout))
144 inset.paragraphs.begin()->layout(tclass[caplayout]);
148 InsetFloat::InsetFloat(InsetFloat const & in)
149 : InsetCollapsable(in), params_(in.params_)
153 // InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
154 // : InsetCollapsable(in, same_id), params_(in.params_)
158 InsetFloat::~InsetFloat()
160 InsetFloatMailer mailer(*this);
165 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
167 Inset::RESULT result = UNDISPATCHED;
169 switch (cmd.action) {
170 case LFUN_INSET_MODIFY: {
171 InsetFloatParams params;
172 InsetFloatMailer::string2params(cmd.argument, params);
174 params_.placement = params.placement;
175 params_.wide = params.wide;
177 wide(params_.wide, cmd.view()->buffer()->params);
178 cmd.view()->updateInset(this);
183 case LFUN_INSET_DIALOG_UPDATE: {
184 InsetFloatMailer mailer(*this);
185 mailer.updateDialog(cmd.view());
190 result = InsetCollapsable::localDispatch(cmd);
197 void InsetFloatParams::write(ostream & os) const
199 os << "Float " // getInsetName()
202 if (!placement.empty()) {
203 os << "placement " << placement << "\n";
208 os << "wide false\n";
213 void InsetFloatParams::read(LyXLex & lex)
217 string token = lex.getString();
218 if (token == "placement") {
220 placement = lex.getString();
222 // take countermeasures
223 lex.pushToken(token);
226 token = lex.getString();
227 if (token == "wide") {
229 string const tmptoken = lex.getString();
230 wide = (tmptoken == "true");
232 lyxerr << "InsetFloat::Read:: Missing wide!"
234 // take countermeasures
235 lex.pushToken(token);
241 void InsetFloat::write(Buffer const * buf, ostream & os) const
244 InsetCollapsable::write(buf, os);
248 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
251 wide(params_.wide, buf->params);
252 InsetCollapsable::read(buf, lex);
256 void InsetFloat::validate(LaTeXFeatures & features) const
258 if (contains(params_.placement, "H")) {
259 features.require("float");
262 features.useFloat(params_.type);
263 InsetCollapsable::validate(features);
267 Inset * InsetFloat::clone(Buffer const &) const
269 return new InsetFloat(*const_cast<InsetFloat *>(this));
273 // Inset * InsetFloat::clone(Buffer const &, bool same_id) const
275 // return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
279 string const InsetFloat::editMessage() const
281 return _("Opened Float Inset");
285 int InsetFloat::latex(Buffer const * buf, ostream & os,
286 LatexRunParams const & runparams) const
288 FloatList const & floats = buf->params.getLyXTextClass().floats();
289 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
290 // Figure out the float placement to use.
291 // From lowest to highest:
292 // - float default placement
293 // - document wide default placement
294 // - specific float placement
296 string const buf_placement = buf->params.float_placement;
297 string const def_placement = floats.defaultPlacement(params_.type);
298 if (!params_.placement.empty()
299 && params_.placement != def_placement) {
300 placement = params_.placement;
301 } else if (params_.placement.empty()
302 && !buf_placement.empty()
303 && buf_placement != def_placement) {
304 placement = buf_placement;
307 // The \n is used to force \begin{<floatname>} to appear in a new line.
308 // The % is needed to prevent two consecutive \n chars in the case
309 // when the current output line is empty.
310 os << "%\n\\begin{" << tmptype << '}';
311 // We only output placement if different from the def_placement.
312 if (!placement.empty()) {
313 os << '[' << placement << ']';
317 int const i = inset.latex(buf, os, runparams);
319 // The \n is used to force \end{<floatname>} to appear in a new line.
320 // In this case, we do not case if the current output line is empty.
321 os << "\n\\end{" << tmptype << "}\n";
327 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
329 os << '<' << params_.type << '>';
330 int const i = inset.docbook(buf, os, mixcont);
331 os << "</" << params_.type << '>';
337 bool InsetFloat::insetAllowed(Inset::Code code) const
339 if (code == Inset::FLOAT_CODE)
341 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
342 return inset.insetAllowed(code);
343 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
349 bool InsetFloat::showInsetDialog(BufferView * bv) const
351 if (!inset.showInsetDialog(bv)) {
352 InsetFloat * tmp = const_cast<InsetFloat *>(this);
353 InsetFloatMailer mailer(*tmp);
354 mailer.showDialog(bv);
360 void InsetFloat::wide(bool w, BufferParams const & bp)
364 string lab(_("float:"));
365 lab += floatname(params_.type, bp);
374 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
376 ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
377 ParIterator end(inset.paragraphs.end(), inset.paragraphs);
379 // Find a caption layout in one of the (child inset's) pars
380 for (; pit != end; ++pit) {
381 if (pit->layout()->name() == caplayout) {
382 string const name = floatname(params_.type, buf->params);
384 tostr(toclist[name].size() + 1)
385 + ". " + pit->asString(buf, false);
386 toc::TocItem const item(pit->id(), 0 , str);
387 toclist[name].push_back(item);
393 string const InsetFloatMailer::name_("float");
395 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
400 string const InsetFloatMailer::inset2string() const
402 return params2string(inset_.params());
406 void InsetFloatMailer::string2params(string const & in,
407 InsetFloatParams & params)
409 params = InsetFloatParams();
414 istringstream data(STRCONV(in));
420 string const token = lex.getString();
425 // This is part of the inset proper that is usually swallowed
426 // by Buffer::readInset
429 string const token = lex.getString();
430 if (token != "Float" || !lex.eatLine())
440 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
443 data << name_ << ' ';
445 return STRCONV(data.str());