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"
39 using namespace lyx::support;
46 // With this inset it will be possible to support the latex package
47 // float.sty, and I am sure that with this and some additional support
48 // classes we can support similar functionality in other formats
50 // By using float.sty we will have the same handling for all floats, both
51 // for those already in existance (table and figure) and all user created
52 // ones¹. So suddenly we give the users the possibility of creating new
53 // kinds of floats on the fly. (and with a uniform look)
56 // \newfloat{type}{placement}{ext}[within]
57 // type - The "type" of the new class of floats, like program or
58 // algorithm. After the appropriate \newfloat, commands
59 // such as \begin{program} or \end{algorithm*} will be
61 // placement - The default placement for the given class of floats.
62 // They are like in standard LaTeX: t, b, p and h for top,
63 // bottom, page, and here, respectively. On top of that
64 // there is a new type, H, which does not really correspond
65 // to a float, since it means: put it "here" and nowhere else.
66 // Note, however that the H specifier is special and, because
67 // of implementation details cannot be used in the second
68 // argument of \newfloat.
69 // ext - The file name extension of an auxiliary file for the list
70 // of figures (or whatever). LaTeX writes the captions to
72 // within - This (optional) argument determines whether floats of this
73 // class will be numbered within some sectional unit of the
74 // document. For example, if within is equal to chapter, the
75 // floats will be numbered within chapters.
77 // style - plain, boxed, ruled
78 // \floatname{float}{floatname}
81 // \floatplacement{float}{placement}
84 // \restylefloat{float}
86 // \listof{type}{title}
89 // ¹ the algorithm float is defined using the float.sty package. Like this
91 // \newfloat{algorithm}{htbp}{loa}[<sect>]
92 // \floatname{algorithm}{Algorithm}
94 // The intention is that floats should be definable from two places:
96 // - the "gui" (i.e. by the user)
99 // This should only be done for floats defined in a documentclass and that
100 // does not need any additional packages. The two most known floats in this
101 // category is "table" and "figure". Floats defined in layout files are only
102 // stored in lyx files if the user modifies them.
105 // There should be a gui dialog (and also a collection of lyxfuncs) where
106 // the user can modify existing floats and/or create new ones.
108 // The individual floats will also have some settable
109 // variables: wide and placement.
115 // this should not be hardcoded, but be part of the definition
116 // of the float (JMarc)
117 string const caplayout("Caption");
119 string floatname(string const & type, BufferParams const & bp)
121 FloatList const & floats = bp.getLyXTextClass().floats();
122 FloatList::const_iterator it = floats[type];
123 if (it == floats.end())
126 return _(it->second.name());
132 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
133 : InsetCollapsable(bp)
135 string lab(_("float: "));
136 lab += floatname(type, bp);
138 LyXFont font(LyXFont::ALL_SANE);
141 font.setColor(LColor::collapsable);
145 LyXTextClass const & tclass = bp.getLyXTextClass();
146 if (tclass.hasLayout(caplayout))
147 inset.paragraphs.begin()->layout(tclass[caplayout]);
151 InsetFloat::InsetFloat(InsetFloat const & in)
152 : InsetCollapsable(in), params_(in.params_)
156 InsetFloat::~InsetFloat()
158 InsetFloatMailer mailer(*this);
163 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
165 Inset::RESULT result = UNDISPATCHED;
167 switch (cmd.action) {
168 case LFUN_INSET_MODIFY: {
169 InsetFloatParams params;
170 InsetFloatMailer::string2params(cmd.argument, params);
172 params_.placement = params.placement;
173 params_.wide = params.wide;
175 wide(params_.wide, cmd.view()->buffer()->params);
176 cmd.view()->updateInset(this);
181 case LFUN_INSET_DIALOG_UPDATE: {
182 InsetFloatMailer mailer(*this);
183 mailer.updateDialog(cmd.view());
188 result = InsetCollapsable::localDispatch(cmd);
195 void InsetFloatParams::write(ostream & os) const
197 os << "Float " // getInsetName()
200 if (!placement.empty()) {
201 os << "placement " << placement << "\n";
206 os << "wide false\n";
211 void InsetFloatParams::read(LyXLex & lex)
215 string token = lex.getString();
216 if (token == "placement") {
218 placement = lex.getString();
220 // take countermeasures
221 lex.pushToken(token);
224 token = lex.getString();
225 if (token == "wide") {
227 string const tmptoken = lex.getString();
228 wide = (tmptoken == "true");
230 lyxerr << "InsetFloat::Read:: Missing wide!"
232 // take countermeasures
233 lex.pushToken(token);
239 void InsetFloat::write(Buffer const * buf, ostream & os) const
242 InsetCollapsable::write(buf, os);
246 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
249 wide(params_.wide, buf->params);
250 InsetCollapsable::read(buf, lex);
254 void InsetFloat::validate(LaTeXFeatures & features) const
256 if (contains(params_.placement, "H")) {
257 features.require("float");
260 features.useFloat(params_.type);
261 InsetCollapsable::validate(features);
265 auto_ptr<InsetBase> InsetFloat::clone() const
267 return auto_ptr<InsetBase>(new InsetFloat(*this));
271 string const InsetFloat::editMessage() const
273 return _("Opened Float Inset");
277 int InsetFloat::latex(Buffer const * buf, ostream & os,
278 LatexRunParams const & runparams) const
280 FloatList const & floats = buf->params.getLyXTextClass().floats();
281 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
282 // Figure out the float placement to use.
283 // From lowest to highest:
284 // - float default placement
285 // - document wide default placement
286 // - specific float placement
288 string const buf_placement = buf->params.float_placement;
289 string const def_placement = floats.defaultPlacement(params_.type);
290 if (!params_.placement.empty()
291 && params_.placement != def_placement) {
292 placement = params_.placement;
293 } else if (params_.placement.empty()
294 && !buf_placement.empty()
295 && buf_placement != def_placement) {
296 placement = buf_placement;
299 // The \n is used to force \begin{<floatname>} to appear in a new line.
300 // The % is needed to prevent two consecutive \n chars in the case
301 // when the current output line is empty.
302 os << "%\n\\begin{" << tmptype << '}';
303 // We only output placement if different from the def_placement.
304 if (!placement.empty()) {
305 os << '[' << placement << ']';
309 int const i = inset.latex(buf, os, runparams);
311 // The \n is used to force \end{<floatname>} to appear in a new line.
312 // In this case, we do not case if the current output line is empty.
313 os << "\n\\end{" << tmptype << "}\n";
319 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
321 os << '<' << params_.type << '>';
322 int const i = inset.docbook(buf, os, mixcont);
323 os << "</" << params_.type << '>';
329 bool InsetFloat::insetAllowed(Inset::Code code) const
331 if (code == Inset::FLOAT_CODE)
333 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
334 return inset.insetAllowed(code);
335 if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
341 bool InsetFloat::showInsetDialog(BufferView * bv) const
343 if (!inset.showInsetDialog(bv)) {
344 InsetFloat * tmp = const_cast<InsetFloat *>(this);
345 InsetFloatMailer mailer(*tmp);
346 mailer.showDialog(bv);
352 void InsetFloat::wide(bool w, BufferParams const & bp)
356 string lab(_("float:"));
357 lab += floatname(params_.type, bp);
366 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
368 ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
369 ParIterator end(inset.paragraphs.end(), inset.paragraphs);
371 // Find a caption layout in one of the (child inset's) pars
372 for (; pit != end; ++pit) {
373 if (pit->layout()->name() == caplayout) {
374 string const name = floatname(params_.type, buf->params);
376 tostr(toclist[name].size() + 1)
377 + ". " + pit->asString(buf, false);
378 toc::TocItem const item(pit->id(), 0 , str);
379 toclist[name].push_back(item);
385 string const InsetFloatMailer::name_("float");
387 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
392 string const InsetFloatMailer::inset2string(Buffer const &) const
394 return params2string(inset_.params());
398 void InsetFloatMailer::string2params(string const & in,
399 InsetFloatParams & params)
401 params = InsetFloatParams();
406 istringstream data(STRCONV(in));
412 string const token = lex.getString();
417 // This is part of the inset proper that is usually swallowed
418 // by Buffer::readInset
421 string const token = lex.getString();
422 if (token != "Float" || !lex.eatLine())
432 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
435 data << name_ << ' ';
437 return STRCONV(data.str());