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"
17 #include "BufferView.h"
20 #include "FloatList.h"
21 #include "funcrequest.h"
23 #include "iterators.h"
24 #include "LaTeXFeatures.h"
27 #include "support/lstrings.h"
28 #include "support/tostr.h"
32 using namespace lyx::support;
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.paragraphs.begin()->layout(tclass[caplayout]);
144 InsetFloat::InsetFloat(InsetFloat const & in)
145 : InsetCollapsable(in), params_(in.params_)
149 InsetFloat::~InsetFloat()
151 InsetFloatMailer mailer(*this);
156 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
158 switch (cmd.action) {
160 case LFUN_INSET_MODIFY: {
161 InsetFloatParams params;
162 InsetFloatMailer::string2params(cmd.argument, params);
164 params_.placement = params.placement;
165 params_.wide = params.wide;
167 wide(params_.wide, cmd.view()->buffer()->params);
168 cmd.view()->updateInset(this);
172 case LFUN_INSET_DIALOG_UPDATE: {
173 InsetFloatMailer(*this).updateDialog(cmd.view());
178 return InsetCollapsable::localDispatch(cmd);
183 void InsetFloatParams::write(ostream & os) const
185 os << "Float " // getInsetName()
188 if (!placement.empty())
189 os << "placement " << placement << "\n";
194 os << "wide false\n";
198 void InsetFloatParams::read(LyXLex & lex)
202 string token = lex.getString();
203 if (token == "placement") {
205 placement = lex.getString();
207 // take countermeasures
208 lex.pushToken(token);
211 token = lex.getString();
212 if (token == "wide") {
214 string const tmptoken = lex.getString();
215 wide = (tmptoken == "true");
217 lyxerr << "InsetFloat::Read:: Missing wide!"
219 // take countermeasures
220 lex.pushToken(token);
226 void InsetFloat::write(Buffer const & buf, ostream & os) const
229 InsetCollapsable::write(buf, os);
233 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
236 wide(params_.wide, buf.params);
237 InsetCollapsable::read(buf, lex);
241 void InsetFloat::validate(LaTeXFeatures & features) const
243 if (contains(params_.placement, "H")) {
244 features.require("float");
247 features.useFloat(params_.type);
248 InsetCollapsable::validate(features);
252 auto_ptr<InsetBase> InsetFloat::clone() const
254 return auto_ptr<InsetBase>(new InsetFloat(*this));
258 string const InsetFloat::editMessage() const
260 return _("Opened Float Inset");
264 int InsetFloat::latex(Buffer const & buf, ostream & os,
265 LatexRunParams const & runparams) const
267 FloatList const & floats = buf.params.getLyXTextClass().floats();
268 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
269 // Figure out the float placement to use.
270 // From lowest to highest:
271 // - float default placement
272 // - document wide default placement
273 // - specific float placement
275 string const buf_placement = buf.params.float_placement;
276 string const def_placement = floats.defaultPlacement(params_.type);
277 if (!params_.placement.empty()
278 && params_.placement != def_placement) {
279 placement = params_.placement;
280 } else if (params_.placement.empty()
281 && !buf_placement.empty()
282 && buf_placement != def_placement) {
283 placement = buf_placement;
286 // The \n is used to force \begin{<floatname>} to appear in a new line.
287 // The % is needed to prevent two consecutive \n chars in the case
288 // when the current output line is empty.
289 os << "%\n\\begin{" << tmptype << '}';
290 // We only output placement if different from the def_placement.
291 if (!placement.empty()) {
292 os << '[' << placement << ']';
296 int const i = inset.latex(buf, os, runparams);
298 // The \n is used to force \end{<floatname>} to appear in a new line.
299 // In this case, we do not case if the current output line is empty.
300 os << "\n\\end{" << tmptype << "}\n";
306 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os) const
308 FloatList const & floats = buf.params.getLyXTextClass().floats();
309 string const tmptype = params_.type;
310 // Figure out the float placement to use.
311 // From lowest to highest:
312 // - float default placement
313 // - document wide default placement
314 // - specific float placement
315 // This is the same as latex, as linuxdoc is modeled after latex.
318 string const buf_placement = buf.params.float_placement;
319 string const def_placement = floats.defaultPlacement(params_.type);
320 if (!params_.placement.empty()
321 && params_.placement != def_placement) {
322 placement = params_.placement;
323 } else if (params_.placement.empty()
324 && !buf_placement.empty()
325 && buf_placement != def_placement) {
326 placement = buf_placement;
329 os << "\n<" << tmptype ;
330 // We only output placement if different from the def_placement.
331 if (!placement.empty()) {
332 os << " loc=\"" << placement << '"';
336 int const i = inset.linuxdoc(buf, os);
337 os << "</" << tmptype << ">\n";
343 int InsetFloat::docbook(Buffer const & buf, ostream & os, bool mixcont) const
345 os << '<' << params_.type << '>';
346 int const i = inset.docbook(buf, os, mixcont);
347 os << "</" << params_.type << '>';
353 bool InsetFloat::insetAllowed(InsetOld::Code code) const
355 if (code == InsetOld::FLOAT_CODE)
357 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
358 return inset.insetAllowed(code);
359 if ((code == InsetOld::FOOT_CODE) || (code == InsetOld::MARGIN_CODE))
365 bool InsetFloat::showInsetDialog(BufferView * bv) const
367 if (!inset.showInsetDialog(bv)) {
368 InsetFloat * tmp = const_cast<InsetFloat *>(this);
369 InsetFloatMailer mailer(*tmp);
370 mailer.showDialog(bv);
376 void InsetFloat::wide(bool w, BufferParams const & bp)
380 string lab(_("float: "));
381 lab += floatname(params_.type, bp);
390 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
392 ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
393 ParIterator end(inset.paragraphs.end(), inset.paragraphs);
395 // Find a caption layout in one of the (child inset's) pars
396 for (; pit != end; ++pit) {
397 if (pit->layout()->name() == caplayout) {
398 string const name = floatname(params_.type, buf.params);
400 tostr(toclist[name].size() + 1)
401 + ". " + pit->asString(buf, false);
402 lyx::toc::TocItem const item(pit->id(), 0 , str);
403 toclist[name].push_back(item);
409 string const InsetFloatMailer::name_("float");
411 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
416 string const InsetFloatMailer::inset2string(Buffer const &) const
418 return params2string(inset_.params());
422 void InsetFloatMailer::string2params(string const & in,
423 InsetFloatParams & params)
425 params = InsetFloatParams();
430 istringstream data(STRCONV(in));
436 string const token = lex.getString();
441 // This is part of the inset proper that is usually swallowed
442 // by Buffer::readInset
445 string const token = lex.getString();
446 if (token != "Float" || !lex.eatLine())
456 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
459 data << name_ << ' ';
461 return STRCONV(data.str());