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 "bufferparams.h"
18 #include "BufferView.h"
21 #include "FloatList.h"
22 #include "funcrequest.h"
24 #include "iterators.h"
25 #include "LaTeXFeatures.h"
28 #include "paragraph.h"
30 #include "support/lstrings.h"
31 #include "support/tostr.h"
33 #include "support/std_sstream.h"
35 using lyx::support::contains;
40 using std::istringstream;
42 using std::ostringstream;
45 // With this inset it will be possible to support the latex package
46 // float.sty, and I am sure that with this and some additional support
47 // classes we can support similar functionality in other formats
49 // By using float.sty we will have the same handling for all floats, both
50 // for those already in existance (table and figure) and all user created
51 // ones¹. So suddenly we give the users the possibility of creating new
52 // kinds of floats on the fly. (and with a uniform look)
55 // \newfloat{type}{placement}{ext}[within]
56 // type - The "type" of the new class of floats, like program or
57 // algorithm. After the appropriate \newfloat, commands
58 // such as \begin{program} or \end{algorithm*} will be
60 // placement - The default placement for the given class of floats.
61 // They are like in standard LaTeX: t, b, p and h for top,
62 // bottom, page, and here, respectively. On top of that
63 // there is a new type, H, which does not really correspond
64 // to a float, since it means: put it "here" and nowhere else.
65 // Note, however that the H specifier is special and, because
66 // of implementation details cannot be used in the second
67 // argument of \newfloat.
68 // ext - The file name extension of an auxiliary file for the list
69 // of figures (or whatever). LaTeX writes the captions to
71 // within - This (optional) argument determines whether floats of this
72 // class will be numbered within some sectional unit of the
73 // document. For example, if within is equal to chapter, the
74 // floats will be numbered within chapters.
76 // style - plain, boxed, ruled
77 // \floatname{float}{floatname}
80 // \floatplacement{float}{placement}
83 // \restylefloat{float}
85 // \listof{type}{title}
88 // ¹ the algorithm float is defined using the float.sty package. Like this
90 // \newfloat{algorithm}{htbp}{loa}[<sect>]
91 // \floatname{algorithm}{Algorithm}
93 // The intention is that floats should be definable from two places:
95 // - the "gui" (i.e. by the user)
98 // This should only be done for floats defined in a documentclass and that
99 // does not need any additional packages. The two most known floats in this
100 // category is "table" and "figure". Floats defined in layout files are only
101 // stored in lyx files if the user modifies them.
104 // There should be a gui dialog (and also a collection of lyxfuncs) where
105 // the user can modify existing floats and/or create new ones.
107 // The individual floats will also have some settable
108 // variables: wide and placement.
114 // this should not be hardcoded, but be part of the definition
115 // of the float (JMarc)
116 string const caplayout("Caption");
118 string floatname(string const & type, BufferParams const & bp)
120 FloatList const & floats = bp.getLyXTextClass().floats();
121 FloatList::const_iterator it = floats[type];
122 if (it == floats.end())
125 return _(it->second.name());
131 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
132 : InsetCollapsable(bp)
134 string lab(_("float: "));
135 lab += floatname(type, bp);
137 LyXFont font(LyXFont::ALL_SANE);
140 font.setColor(LColor::collapsable);
144 LyXTextClass const & tclass = bp.getLyXTextClass();
145 if (tclass.hasLayout(caplayout))
146 inset.paragraphs.begin()->layout(tclass[caplayout]);
150 InsetFloat::InsetFloat(InsetFloat const & in)
151 : InsetCollapsable(in), params_(in.params_)
155 InsetFloat::~InsetFloat()
157 InsetFloatMailer mailer(*this);
162 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
164 switch (cmd.action) {
166 case LFUN_INSET_MODIFY: {
167 InsetFloatParams params;
168 InsetFloatMailer::string2params(cmd.argument, params);
170 params_.placement = params.placement;
171 params_.wide = params.wide;
173 wide(params_.wide, cmd.view()->buffer()->params());
174 cmd.view()->updateInset(this);
178 case LFUN_INSET_DIALOG_UPDATE: {
179 InsetFloatMailer(*this).updateDialog(cmd.view());
184 return 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";
204 void InsetFloatParams::read(LyXLex & lex)
208 string token = lex.getString();
209 if (token == "placement") {
211 placement = lex.getString();
213 // take countermeasures
214 lex.pushToken(token);
217 token = lex.getString();
218 if (token == "wide") {
220 string const tmptoken = lex.getString();
221 wide = (tmptoken == "true");
223 lyxerr << "InsetFloat::Read:: Missing wide!"
225 // take countermeasures
226 lex.pushToken(token);
232 void InsetFloat::write(Buffer const & buf, ostream & os) const
235 InsetCollapsable::write(buf, os);
239 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
242 wide(params_.wide, buf.params());
243 InsetCollapsable::read(buf, lex);
247 void InsetFloat::validate(LaTeXFeatures & features) const
249 if (contains(params_.placement, "H")) {
250 features.require("float");
253 features.useFloat(params_.type);
254 InsetCollapsable::validate(features);
258 auto_ptr<InsetBase> InsetFloat::clone() const
260 return auto_ptr<InsetBase>(new InsetFloat(*this));
264 string const InsetFloat::editMessage() const
266 return _("Opened Float Inset");
270 int InsetFloat::latex(Buffer const & buf, ostream & os,
271 LatexRunParams const & runparams) const
273 FloatList const & floats = buf.params().getLyXTextClass().floats();
274 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
275 // Figure out the float placement to use.
276 // From lowest to highest:
277 // - float default placement
278 // - document wide default placement
279 // - specific float placement
281 string const buf_placement = buf.params().float_placement;
282 string const def_placement = floats.defaultPlacement(params_.type);
283 if (!params_.placement.empty()
284 && params_.placement != def_placement) {
285 placement = params_.placement;
286 } else if (params_.placement.empty()
287 && !buf_placement.empty()
288 && buf_placement != def_placement) {
289 placement = buf_placement;
292 // The \n is used to force \begin{<floatname>} to appear in a new line.
293 // The % is needed to prevent two consecutive \n chars in the case
294 // when the current output line is empty.
295 os << "%\n\\begin{" << tmptype << '}';
296 // We only output placement if different from the def_placement.
297 if (!placement.empty()) {
298 os << '[' << placement << ']';
302 int const i = inset.latex(buf, os, runparams);
304 // The \n is used to force \end{<floatname>} to appear in a new line.
305 // In this case, we do not case if the current output line is empty.
306 os << "\n\\end{" << tmptype << "}\n";
312 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os) const
314 FloatList const & floats = buf.params().getLyXTextClass().floats();
315 string const tmptype = params_.type;
316 // Figure out the float placement to use.
317 // From lowest to highest:
318 // - float default placement
319 // - document wide default placement
320 // - specific float placement
321 // This is the same as latex, as linuxdoc is modeled after latex.
324 string const buf_placement = buf.params().float_placement;
325 string const def_placement = floats.defaultPlacement(params_.type);
326 if (!params_.placement.empty()
327 && params_.placement != def_placement) {
328 placement = params_.placement;
329 } else if (params_.placement.empty()
330 && !buf_placement.empty()
331 && buf_placement != def_placement) {
332 placement = buf_placement;
335 os << "\n<" << tmptype ;
336 // We only output placement if different from the def_placement.
337 if (!placement.empty()) {
338 os << " loc=\"" << placement << '"';
342 int const i = inset.linuxdoc(buf, os);
343 os << "</" << tmptype << ">\n";
349 int InsetFloat::docbook(Buffer const & buf, ostream & os, bool mixcont) const
351 os << '<' << params_.type << '>';
352 int const i = inset.docbook(buf, os, mixcont);
353 os << "</" << params_.type << '>';
359 bool InsetFloat::insetAllowed(InsetOld::Code code) const
361 if (code == InsetOld::FLOAT_CODE)
363 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
364 return inset.insetAllowed(code);
365 if ((code == InsetOld::FOOT_CODE) || (code == InsetOld::MARGIN_CODE))
371 bool InsetFloat::showInsetDialog(BufferView * bv) const
373 if (!inset.showInsetDialog(bv)) {
374 InsetFloat * tmp = const_cast<InsetFloat *>(this);
375 InsetFloatMailer mailer(*tmp);
376 mailer.showDialog(bv);
382 void InsetFloat::wide(bool w, BufferParams const & bp)
386 string lab(_("float: "));
387 lab += floatname(params_.type, bp);
396 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
398 ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
399 ParIterator end(inset.paragraphs.end(), inset.paragraphs);
401 // Find a caption layout in one of the (child inset's) pars
402 for (; pit != end; ++pit) {
403 if (pit->layout()->name() == caplayout) {
404 string const name = floatname(params_.type, buf.params());
406 tostr(toclist[name].size() + 1)
407 + ". " + pit->asString(buf, false);
408 lyx::toc::TocItem const item(pit->id(), 0 , str);
409 toclist[name].push_back(item);
415 string const InsetFloatMailer::name_("float");
417 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
422 string const InsetFloatMailer::inset2string(Buffer const &) const
424 return params2string(inset_.params());
428 void InsetFloatMailer::string2params(string const & in,
429 InsetFloatParams & params)
431 params = InsetFloatParams();
436 istringstream data(in);
442 string const token = lex.getString();
447 // This is part of the inset proper that is usually swallowed
448 // by Buffer::readInset
451 string const token = lex.getString();
452 if (token != "Float" || !lex.eatLine())
462 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
465 data << name_ << ' ';