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 case LFUN_INSET_DIALOG_UPDATE: {
174 InsetFloatMailer mailer(*this);
175 mailer.updateDialog(cmd.view());
180 result = InsetCollapsable::localDispatch(cmd);
187 void InsetFloatParams::write(ostream & os) const
189 os << "Float " // getInsetName()
192 if (!placement.empty()) {
193 os << "placement " << placement << "\n";
198 os << "wide false\n";
203 void InsetFloatParams::read(LyXLex & lex)
207 string token = lex.getString();
208 if (token == "placement") {
210 placement = lex.getString();
212 // take countermeasures
213 lex.pushToken(token);
216 token = lex.getString();
217 if (token == "wide") {
219 string const tmptoken = lex.getString();
220 wide = (tmptoken == "true");
222 lyxerr << "InsetFloat::Read:: Missing wide!"
224 // take countermeasures
225 lex.pushToken(token);
231 void InsetFloat::write(Buffer const * buf, ostream & os) const
234 InsetCollapsable::write(buf, os);
238 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
241 wide(params_.wide, buf->params);
242 InsetCollapsable::read(buf, lex);
246 void InsetFloat::validate(LaTeXFeatures & features) const
248 if (contains(placement(), "H")) {
249 features.require("float");
252 features.useFloat(params_.type);
253 InsetCollapsable::validate(features);
257 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
259 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
263 string const InsetFloat::editMessage() const
265 return _("Opened Float Inset");
269 int InsetFloat::latex(Buffer const * buf,
270 ostream & os, bool fragile, bool fp) const
272 FloatList const & floats = buf->params.getLyXTextClass().floats();
273 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
274 // Figure out the float placement to use.
275 // From lowest to highest:
276 // - float default placement
277 // - document wide default placement
278 // - specific float placement
280 string const buf_placement = buf->params.float_placement;
281 string const def_placement = floats.defaultPlacement(params_.type);
282 if (!params_.placement.empty()
283 && params_.placement != def_placement) {
284 placement = params_.placement;
285 } else if (params_.placement.empty()
286 && !buf_placement.empty()
287 && buf_placement != def_placement) {
288 placement = buf_placement;
291 // The \n is used to force \begin{<floatname>} to appear in a new line.
292 // The % is needed to prevent two consecutive \n chars in the case
293 // when the current output line is empty.
294 os << "%\n\\begin{" << tmptype << '}';
295 // We only output placement if different from the def_placement.
296 if (!placement.empty()) {
297 os << '[' << placement << ']';
301 int const i = inset.latex(buf, os, fragile, fp);
303 // The \n is used to force \end{<floatname>} to appear in a new line.
304 // In this case, we do not case if the current output line is empty.
305 os << "\n\\end{" << tmptype << "}\n";
311 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
313 os << '<' << params_.type << '>';
314 int const i = inset.docbook(buf, os, mixcont);
315 os << "</" << params_.type << '>';
321 bool InsetFloat::insetAllowed(Inset::Code code) const
323 if (code == Inset::FLOAT_CODE)
325 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
326 return inset.insetAllowed(code);
327 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
333 bool InsetFloat::showInsetDialog(BufferView * bv) const
335 if (!inset.showInsetDialog(bv)) {
336 InsetFloat * tmp = const_cast<InsetFloat *>(this);
337 InsetFloatMailer mailer(*tmp);
338 mailer.showDialog(bv);
344 string const & InsetFloat::type() const
350 void InsetFloat::placement(string const & p)
352 // FIX: Here we should only allow the placement to be set
354 params_.placement = p;
358 string const & InsetFloat::placement() const
360 return params_.placement;
364 void InsetFloat::wide(bool w, BufferParams const & bp)
368 string lab(_("float:"));
369 lab += floatname(params_.type, bp);
378 bool InsetFloat::wide() const
384 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
386 ParIterator pit(inset.paragraph());
389 // Find a caption layout in one of the (child inset's) pars
390 for (; pit != end; ++pit) {
391 Paragraph * tmp = *pit;
393 if (tmp->layout()->name() == caplayout) {
394 string const name = floatname(type(), buf->params);
396 tostr(toclist[name].size() + 1)
397 + ". " + tmp->asString(buf, false);
398 toc::TocItem const item(tmp->id(), 0 , str);
399 toclist[name].push_back(item);
405 string const InsetFloatMailer::name_("float");
407 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
412 string const InsetFloatMailer::inset2string() const
414 return params2string(inset_.params());
418 void InsetFloatMailer::string2params(string const & in,
419 InsetFloatParams & params)
421 params = InsetFloatParams();
423 istringstream data(in);
429 string const token = lex.getString();
434 // This is part of the inset proper that is usually swallowed
435 // by Buffer::readInset
438 string const token = lex.getString();
439 if (token != "Float" || !lex.eatLine())
450 InsetFloatMailer::params2string(InsetFloatParams const & params)
453 data << name_ << ' ';