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"
31 #include "support/LOstream.h"
32 #include "support/lstrings.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/Dialogs.h"
40 // With this inset it will be possible to support the latex package
41 // float.sty, and I am sure that with this and some additional support
42 // classes we can support similar functionality in other formats
44 // By using float.sty we will have the same handling for all floats, both
45 // for those already in existance (table and figure) and all user created
46 // ones¹. So suddenly we give the users the possibility of creating new
47 // kinds of floats on the fly. (and with a uniform look)
50 // \newfloat{type}{placement}{ext}[within]
51 // type - The "type" of the new class of floats, like program or
52 // algorithm. After the appropriate \newfloat, commands
53 // such as \begin{program} or \end{algorithm*} will be
55 // placement - The default placement for the given class of floats.
56 // They are like in standard LaTeX: t, b, p and h for top,
57 // bottom, page, and here, respectively. On top of that
58 // there is a new type, H, which does not really correspond
59 // to a float, since it means: put it "here" and nowhere else.
60 // Note, however that the H specifier is special and, because
61 // of implementation details cannot be used in the second
62 // argument of \newfloat.
63 // ext - The file name extension of an auxiliary file for the list
64 // of figures (or whatever). LaTeX writes the captions to
66 // within - This (optional) argument determines whether floats of this
67 // class will be numbered within some sectional unit of the
68 // document. For example, if within is equal to chapter, the
69 // floats will be numbered within chapters.
71 // style - plain, boxed, ruled
72 // \floatname{float}{floatname}
75 // \floatplacement{float}{placement}
78 // \restylefloat{float}
80 // \listof{type}{title}
83 // ¹ the algorithm float is defined using the float.sty package. Like this
85 // \newfloat{algorithm}{htbp}{loa}[<sect>]
86 // \floatname{algorithm}{Algorithm}
88 // The intention is that floats should be definable from two places:
90 // - the "gui" (i.e. by the user)
93 // This should only be done for floats defined in a documentclass and that
94 // does not need any additional packages. The two most known floats in this
95 // category is "table" and "figure". Floats defined in layout files are only
96 // stored in lyx files if the user modifies them.
99 // There should be a gui dialog (and also a collection of lyxfuncs) where
100 // the user can modify existing floats and/or create new ones.
102 // The individual floats will also have some settable
103 // variables: wide and placement.
109 // this should not be hardcoded, but be part of the definition
110 // of the float (JMarc)
111 string const caplayout("Caption");
113 string floatname(string const & type, BufferParams const & bp)
115 FloatList const & floats = bp.getLyXTextClass().floats();
116 FloatList::const_iterator it = floats[type];
117 if (it == floats.end())
120 return _(it->second.name());
126 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
127 : InsetCollapsable(bp)
129 string lab(_("float: "));
130 lab += floatname(type, bp);
132 LyXFont font(LyXFont::ALL_SANE);
135 font.setColor(LColor::collapsable);
139 LyXTextClass const & tclass = bp.getLyXTextClass();
140 if (tclass.hasLayout(caplayout))
141 inset.paragraphs.begin()->layout(tclass[caplayout]);
145 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
146 : InsetCollapsable(in, same_id), params_(in.params_)
150 InsetFloat::~InsetFloat()
152 InsetFloatMailer mailer(*this);
157 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
159 Inset::RESULT result = UNDISPATCHED;
161 switch (cmd.action) {
162 case LFUN_INSET_MODIFY: {
163 InsetFloatParams params;
164 InsetFloatMailer::string2params(cmd.argument, params);
166 params_.placement = params.placement;
167 params_.wide = params.wide;
169 wide(params_.wide, cmd.view()->buffer()->params);
170 cmd.view()->updateInset(this);
175 case LFUN_INSET_DIALOG_UPDATE: {
176 InsetFloatMailer mailer(*this);
177 mailer.updateDialog(cmd.view());
182 result = InsetCollapsable::localDispatch(cmd);
189 void InsetFloatParams::write(ostream & os) const
191 os << "Float " // getInsetName()
194 if (!placement.empty()) {
195 os << "placement " << placement << "\n";
200 os << "wide false\n";
205 void InsetFloatParams::read(LyXLex & lex)
209 string token = lex.getString();
210 if (token == "placement") {
212 placement = lex.getString();
214 // take countermeasures
215 lex.pushToken(token);
218 token = lex.getString();
219 if (token == "wide") {
221 string const tmptoken = lex.getString();
222 wide = (tmptoken == "true");
224 lyxerr << "InsetFloat::Read:: Missing wide!"
226 // take countermeasures
227 lex.pushToken(token);
233 void InsetFloat::write(Buffer const * buf, ostream & os) const
236 InsetCollapsable::write(buf, os);
240 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
243 wide(params_.wide, buf->params);
244 InsetCollapsable::read(buf, lex);
248 void InsetFloat::validate(LaTeXFeatures & features) const
250 if (contains(params_.placement, "H")) {
251 features.require("float");
254 features.useFloat(params_.type);
255 InsetCollapsable::validate(features);
259 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
261 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
265 string const InsetFloat::editMessage() const
267 return _("Opened Float Inset");
271 int InsetFloat::latex(Buffer const * buf,
272 ostream & os, bool fragile, bool fp) const
274 FloatList const & floats = buf->params.getLyXTextClass().floats();
275 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
276 // Figure out the float placement to use.
277 // From lowest to highest:
278 // - float default placement
279 // - document wide default placement
280 // - specific float placement
282 string const buf_placement = buf->params.float_placement;
283 string const def_placement = floats.defaultPlacement(params_.type);
284 if (!params_.placement.empty()
285 && params_.placement != def_placement) {
286 placement = params_.placement;
287 } else if (params_.placement.empty()
288 && !buf_placement.empty()
289 && buf_placement != def_placement) {
290 placement = buf_placement;
293 // The \n is used to force \begin{<floatname>} to appear in a new line.
294 // The % is needed to prevent two consecutive \n chars in the case
295 // when the current output line is empty.
296 os << "%\n\\begin{" << tmptype << '}';
297 // We only output placement if different from the def_placement.
298 if (!placement.empty()) {
299 os << '[' << placement << ']';
303 int const i = inset.latex(buf, os, fragile, fp);
305 // The \n is used to force \end{<floatname>} to appear in a new line.
306 // In this case, we do not case if the current output line is empty.
307 os << "\n\\end{" << tmptype << "}\n";
313 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
315 os << '<' << params_.type << '>';
316 int const i = inset.docbook(buf, os, mixcont);
317 os << "</" << params_.type << '>';
323 bool InsetFloat::insetAllowed(Inset::Code code) const
325 if (code == Inset::FLOAT_CODE)
327 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
328 return inset.insetAllowed(code);
329 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
335 bool InsetFloat::showInsetDialog(BufferView * bv) const
337 if (!inset.showInsetDialog(bv)) {
338 InsetFloat * tmp = const_cast<InsetFloat *>(this);
339 InsetFloatMailer mailer(*tmp);
340 mailer.showDialog(bv);
346 void InsetFloat::wide(bool w, BufferParams const & bp)
350 string lab(_("float:"));
351 lab += floatname(params_.type, bp);
360 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
362 ParIterator pit(&*inset.paragraphs.begin());
365 // Find a caption layout in one of the (child inset's) pars
366 for (; pit != end; ++pit) {
367 Paragraph * tmp = *pit;
369 if (tmp->layout()->name() == caplayout) {
370 string const name = floatname(params_.type, buf->params);
372 tostr(toclist[name].size() + 1)
373 + ". " + tmp->asString(buf, false);
374 toc::TocItem const item(tmp->id(), 0 , str);
375 toclist[name].push_back(item);
381 string const InsetFloatMailer::name_("float");
383 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
388 string const InsetFloatMailer::inset2string() const
390 return params2string(inset_.params());
394 void InsetFloatMailer::string2params(string const & in,
395 InsetFloatParams & params)
397 params = InsetFloatParams();
402 istringstream data(in);
408 string const token = lex.getString();
413 // This is part of the inset proper that is usually swallowed
414 // by Buffer::readInset
417 string const token = lex.getString();
418 if (token != "Float" || !lex.eatLine())
429 InsetFloatMailer::params2string(InsetFloatParams const & params)
432 data << name_ << ' ';