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"
33 #include "support/tostr.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/Dialogs.h"
41 // With this inset it will be possible to support the latex package
42 // float.sty, and I am sure that with this and some additional support
43 // classes we can support similar functionality in other formats
45 // By using float.sty we will have the same handling for all floats, both
46 // for those already in existance (table and figure) and all user created
47 // ones¹. So suddenly we give the users the possibility of creating new
48 // kinds of floats on the fly. (and with a uniform look)
51 // \newfloat{type}{placement}{ext}[within]
52 // type - The "type" of the new class of floats, like program or
53 // algorithm. After the appropriate \newfloat, commands
54 // such as \begin{program} or \end{algorithm*} will be
56 // placement - The default placement for the given class of floats.
57 // They are like in standard LaTeX: t, b, p and h for top,
58 // bottom, page, and here, respectively. On top of that
59 // there is a new type, H, which does not really correspond
60 // to a float, since it means: put it "here" and nowhere else.
61 // Note, however that the H specifier is special and, because
62 // of implementation details cannot be used in the second
63 // argument of \newfloat.
64 // ext - The file name extension of an auxiliary file for the list
65 // of figures (or whatever). LaTeX writes the captions to
67 // within - This (optional) argument determines whether floats of this
68 // class will be numbered within some sectional unit of the
69 // document. For example, if within is equal to chapter, the
70 // floats will be numbered within chapters.
72 // style - plain, boxed, ruled
73 // \floatname{float}{floatname}
76 // \floatplacement{float}{placement}
79 // \restylefloat{float}
81 // \listof{type}{title}
84 // ¹ the algorithm float is defined using the float.sty package. Like this
86 // \newfloat{algorithm}{htbp}{loa}[<sect>]
87 // \floatname{algorithm}{Algorithm}
89 // The intention is that floats should be definable from two places:
91 // - the "gui" (i.e. by the user)
94 // This should only be done for floats defined in a documentclass and that
95 // does not need any additional packages. The two most known floats in this
96 // category is "table" and "figure". Floats defined in layout files are only
97 // stored in lyx files if the user modifies them.
100 // There should be a gui dialog (and also a collection of lyxfuncs) where
101 // the user can modify existing floats and/or create new ones.
103 // The individual floats will also have some settable
104 // variables: wide and placement.
110 // this should not be hardcoded, but be part of the definition
111 // of the float (JMarc)
112 string const caplayout("Caption");
114 string floatname(string const & type, BufferParams const & bp)
116 FloatList const & floats = bp.getLyXTextClass().floats();
117 FloatList::const_iterator it = floats[type];
118 if (it == floats.end())
121 return _(it->second.name());
127 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
128 : InsetCollapsable(bp)
130 string lab(_("float: "));
131 lab += floatname(type, bp);
133 LyXFont font(LyXFont::ALL_SANE);
136 font.setColor(LColor::collapsable);
140 LyXTextClass const & tclass = bp.getLyXTextClass();
141 if (tclass.hasLayout(caplayout))
142 inset.paragraphs.begin()->layout(tclass[caplayout]);
146 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
147 : InsetCollapsable(in, same_id), params_(in.params_)
151 InsetFloat::~InsetFloat()
153 InsetFloatMailer mailer(*this);
158 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
160 Inset::RESULT result = UNDISPATCHED;
162 switch (cmd.action) {
163 case LFUN_INSET_MODIFY: {
164 InsetFloatParams params;
165 InsetFloatMailer::string2params(cmd.argument, params);
167 params_.placement = params.placement;
168 params_.wide = params.wide;
170 wide(params_.wide, cmd.view()->buffer()->params);
171 cmd.view()->updateInset(this);
176 case LFUN_INSET_DIALOG_UPDATE: {
177 InsetFloatMailer mailer(*this);
178 mailer.updateDialog(cmd.view());
183 result = InsetCollapsable::localDispatch(cmd);
190 void InsetFloatParams::write(ostream & os) const
192 os << "Float " // getInsetName()
195 if (!placement.empty()) {
196 os << "placement " << placement << "\n";
201 os << "wide false\n";
206 void InsetFloatParams::read(LyXLex & lex)
210 string token = lex.getString();
211 if (token == "placement") {
213 placement = lex.getString();
215 // take countermeasures
216 lex.pushToken(token);
219 token = lex.getString();
220 if (token == "wide") {
222 string const tmptoken = lex.getString();
223 wide = (tmptoken == "true");
225 lyxerr << "InsetFloat::Read:: Missing wide!"
227 // take countermeasures
228 lex.pushToken(token);
234 void InsetFloat::write(Buffer const * buf, ostream & os) const
237 InsetCollapsable::write(buf, os);
241 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
244 wide(params_.wide, buf->params);
245 InsetCollapsable::read(buf, lex);
249 void InsetFloat::validate(LaTeXFeatures & features) const
251 if (contains(params_.placement, "H")) {
252 features.require("float");
255 features.useFloat(params_.type);
256 InsetCollapsable::validate(features);
260 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
262 return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
266 string const InsetFloat::editMessage() const
268 return _("Opened Float Inset");
272 int InsetFloat::latex(Buffer const * buf,
273 ostream & os, bool fragile, bool fp) const
275 FloatList const & floats = buf->params.getLyXTextClass().floats();
276 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
277 // Figure out the float placement to use.
278 // From lowest to highest:
279 // - float default placement
280 // - document wide default placement
281 // - specific float placement
283 string const buf_placement = buf->params.float_placement;
284 string const def_placement = floats.defaultPlacement(params_.type);
285 if (!params_.placement.empty()
286 && params_.placement != def_placement) {
287 placement = params_.placement;
288 } else if (params_.placement.empty()
289 && !buf_placement.empty()
290 && buf_placement != def_placement) {
291 placement = buf_placement;
294 // The \n is used to force \begin{<floatname>} to appear in a new line.
295 // The % is needed to prevent two consecutive \n chars in the case
296 // when the current output line is empty.
297 os << "%\n\\begin{" << tmptype << '}';
298 // We only output placement if different from the def_placement.
299 if (!placement.empty()) {
300 os << '[' << placement << ']';
304 int const i = inset.latex(buf, os, fragile, fp);
306 // The \n is used to force \end{<floatname>} to appear in a new line.
307 // In this case, we do not case if the current output line is empty.
308 os << "\n\\end{" << tmptype << "}\n";
314 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
316 os << '<' << params_.type << '>';
317 int const i = inset.docbook(buf, os, mixcont);
318 os << "</" << params_.type << '>';
324 bool InsetFloat::insetAllowed(Inset::Code code) const
326 if (code == Inset::FLOAT_CODE)
328 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
329 return inset.insetAllowed(code);
330 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
336 bool InsetFloat::showInsetDialog(BufferView * bv) const
338 if (!inset.showInsetDialog(bv)) {
339 InsetFloat * tmp = const_cast<InsetFloat *>(this);
340 InsetFloatMailer mailer(*tmp);
341 mailer.showDialog(bv);
347 void InsetFloat::wide(bool w, BufferParams const & bp)
351 string lab(_("float:"));
352 lab += floatname(params_.type, bp);
361 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
363 ParIterator pit(&*inset.paragraphs.begin());
366 // Find a caption layout in one of the (child inset's) pars
367 for (; pit != end; ++pit) {
368 Paragraph * tmp = *pit;
370 if (tmp->layout()->name() == caplayout) {
371 string const name = floatname(params_.type, buf->params);
373 tostr(toclist[name].size() + 1)
374 + ". " + tmp->asString(buf, false);
375 toc::TocItem const item(tmp->id(), 0 , str);
376 toclist[name].push_back(item);
382 string const InsetFloatMailer::name_("float");
384 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
389 string const InsetFloatMailer::inset2string() const
391 return params2string(inset_.params());
395 void InsetFloatMailer::string2params(string const & in,
396 InsetFloatParams & params)
398 params = InsetFloatParams();
403 istringstream data(in);
409 string const token = lex.getString();
414 // This is part of the inset proper that is usually swallowed
415 // by Buffer::readInset
418 string const token = lex.getString();
419 if (token != "Float" || !lex.eatLine())
430 InsetFloatMailer::params2string(InsetFloatParams const & params)
433 data << name_ << ' ';