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"
27 #include "paragraph.h"
29 #include "support/lstrings.h"
30 #include "support/tostr.h"
32 #include "support/std_sstream.h"
34 using lyx::support::contains;
39 using std::istringstream;
41 using std::ostringstream;
44 // With this inset it will be possible to support the latex package
45 // float.sty, and I am sure that with this and some additional support
46 // classes we can support similar functionality in other formats
48 // By using float.sty we will have the same handling for all floats, both
49 // for those already in existance (table and figure) and all user created
50 // ones¹. So suddenly we give the users the possibility of creating new
51 // kinds of floats on the fly. (and with a uniform look)
54 // \newfloat{type}{placement}{ext}[within]
55 // type - The "type" of the new class of floats, like program or
56 // algorithm. After the appropriate \newfloat, commands
57 // such as \begin{program} or \end{algorithm*} will be
59 // placement - The default placement for the given class of floats.
60 // They are like in standard LaTeX: t, b, p and h for top,
61 // bottom, page, and here, respectively. On top of that
62 // there is a new type, H, which does not really correspond
63 // to a float, since it means: put it "here" and nowhere else.
64 // Note, however that the H specifier is special and, because
65 // of implementation details cannot be used in the second
66 // argument of \newfloat.
67 // ext - The file name extension of an auxiliary file for the list
68 // of figures (or whatever). LaTeX writes the captions to
70 // within - This (optional) argument determines whether floats of this
71 // class will be numbered within some sectional unit of the
72 // document. For example, if within is equal to chapter, the
73 // floats will be numbered within chapters.
75 // style - plain, boxed, ruled
76 // \floatname{float}{floatname}
79 // \floatplacement{float}{placement}
82 // \restylefloat{float}
84 // \listof{type}{title}
87 // ¹ the algorithm float is defined using the float.sty package. Like this
89 // \newfloat{algorithm}{htbp}{loa}[<sect>]
90 // \floatname{algorithm}{Algorithm}
92 // The intention is that floats should be definable from two places:
94 // - the "gui" (i.e. by the user)
97 // This should only be done for floats defined in a documentclass and that
98 // does not need any additional packages. The two most known floats in this
99 // category is "table" and "figure". Floats defined in layout files are only
100 // stored in lyx files if the user modifies them.
103 // There should be a gui dialog (and also a collection of lyxfuncs) where
104 // the user can modify existing floats and/or create new ones.
106 // The individual floats will also have some settable
107 // variables: wide and placement.
113 // this should not be hardcoded, but be part of the definition
114 // of the float (JMarc)
115 string const caplayout("Caption");
117 string floatname(string const & type, BufferParams const & bp)
119 FloatList const & floats = bp.getLyXTextClass().floats();
120 FloatList::const_iterator it = floats[type];
121 if (it == floats.end())
124 return _(it->second.name());
130 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
131 : InsetCollapsable(bp)
133 string lab(_("float: "));
134 lab += floatname(type, bp);
136 LyXFont font(LyXFont::ALL_SANE);
139 font.setColor(LColor::collapsable);
143 LyXTextClass const & tclass = bp.getLyXTextClass();
144 if (tclass.hasLayout(caplayout))
145 inset.paragraphs.begin()->layout(tclass[caplayout]);
149 InsetFloat::InsetFloat(InsetFloat const & in)
150 : InsetCollapsable(in), params_(in.params_)
154 InsetFloat::~InsetFloat()
156 InsetFloatMailer mailer(*this);
161 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
163 switch (cmd.action) {
165 case LFUN_INSET_MODIFY: {
166 InsetFloatParams params;
167 InsetFloatMailer::string2params(cmd.argument, params);
169 params_.placement = params.placement;
170 params_.wide = params.wide;
172 wide(params_.wide, cmd.view()->buffer()->params());
173 cmd.view()->updateInset(this);
177 case LFUN_INSET_DIALOG_UPDATE: {
178 InsetFloatMailer(*this).updateDialog(cmd.view());
183 return InsetCollapsable::localDispatch(cmd);
188 void InsetFloatParams::write(ostream & os) const
190 os << "Float " // getInsetName()
193 if (!placement.empty())
194 os << "placement " << placement << "\n";
199 os << "wide false\n";
203 void InsetFloatParams::read(LyXLex & lex)
207 string token = lex.getString();
208 if (token == "placement") {
210 placement = lex.getString();
212 // take countermeasures
213 lex.pushToken(token);
216 token = lex.getString();
217 if (token == "wide") {
219 string const tmptoken = lex.getString();
220 wide = (tmptoken == "true");
222 lyxerr << "InsetFloat::Read:: Missing wide!"
224 // take countermeasures
225 lex.pushToken(token);
231 void InsetFloat::write(Buffer const & buf, ostream & os) const
234 InsetCollapsable::write(buf, os);
238 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
241 wide(params_.wide, buf.params());
242 InsetCollapsable::read(buf, lex);
246 void InsetFloat::validate(LaTeXFeatures & features) const
248 if (contains(params_.placement, "H")) {
249 features.require("float");
252 features.useFloat(params_.type);
253 InsetCollapsable::validate(features);
257 auto_ptr<InsetBase> InsetFloat::clone() const
259 return auto_ptr<InsetBase>(new InsetFloat(*this));
263 string const InsetFloat::editMessage() const
265 return _("Opened Float Inset");
269 int InsetFloat::latex(Buffer const & buf, ostream & os,
270 LatexRunParams const & runparams) const
272 FloatList const & floats = buf.params().getLyXTextClass().floats();
273 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
274 // Figure out the float placement to use.
275 // From lowest to highest:
276 // - float default placement
277 // - document wide default placement
278 // - specific float placement
280 string const buf_placement = buf.params().float_placement;
281 string const def_placement = floats.defaultPlacement(params_.type);
282 if (!params_.placement.empty()
283 && params_.placement != def_placement) {
284 placement = params_.placement;
285 } else if (params_.placement.empty()
286 && !buf_placement.empty()
287 && buf_placement != def_placement) {
288 placement = buf_placement;
291 // The \n is used to force \begin{<floatname>} to appear in a new line.
292 // The % is needed to prevent two consecutive \n chars in the case
293 // when the current output line is empty.
294 os << "%\n\\begin{" << tmptype << '}';
295 // We only output placement if different from the def_placement.
296 if (!placement.empty()) {
297 os << '[' << placement << ']';
301 int const i = inset.latex(buf, os, runparams);
303 // The \n is used to force \end{<floatname>} to appear in a new line.
304 // In this case, we do not case if the current output line is empty.
305 os << "\n\\end{" << tmptype << "}\n";
311 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os) const
313 FloatList const & floats = buf.params().getLyXTextClass().floats();
314 string const tmptype = params_.type;
315 // Figure out the float placement to use.
316 // From lowest to highest:
317 // - float default placement
318 // - document wide default placement
319 // - specific float placement
320 // This is the same as latex, as linuxdoc is modeled after latex.
323 string const buf_placement = buf.params().float_placement;
324 string const def_placement = floats.defaultPlacement(params_.type);
325 if (!params_.placement.empty()
326 && params_.placement != def_placement) {
327 placement = params_.placement;
328 } else if (params_.placement.empty()
329 && !buf_placement.empty()
330 && buf_placement != def_placement) {
331 placement = buf_placement;
334 os << "\n<" << tmptype ;
335 // We only output placement if different from the def_placement.
336 if (!placement.empty()) {
337 os << " loc=\"" << placement << '"';
341 int const i = inset.linuxdoc(buf, os);
342 os << "</" << tmptype << ">\n";
348 int InsetFloat::docbook(Buffer const & buf, ostream & os, bool mixcont) const
350 os << '<' << params_.type << '>';
351 int const i = inset.docbook(buf, os, mixcont);
352 os << "</" << params_.type << '>';
358 bool InsetFloat::insetAllowed(InsetOld::Code code) const
360 if (code == InsetOld::FLOAT_CODE)
362 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
363 return inset.insetAllowed(code);
364 if ((code == InsetOld::FOOT_CODE) || (code == InsetOld::MARGIN_CODE))
370 bool InsetFloat::showInsetDialog(BufferView * bv) const
372 if (!inset.showInsetDialog(bv)) {
373 InsetFloat * tmp = const_cast<InsetFloat *>(this);
374 InsetFloatMailer mailer(*tmp);
375 mailer.showDialog(bv);
381 void InsetFloat::wide(bool w, BufferParams const & bp)
385 string lab(_("float: "));
386 lab += floatname(params_.type, bp);
395 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
397 ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
398 ParIterator end(inset.paragraphs.end(), inset.paragraphs);
400 // Find a caption layout in one of the (child inset's) pars
401 for (; pit != end; ++pit) {
402 if (pit->layout()->name() == caplayout) {
403 string const name = floatname(params_.type, buf.params());
405 tostr(toclist[name].size() + 1)
406 + ". " + pit->asString(buf, false);
407 lyx::toc::TocItem const item(pit->id(), 0 , str);
408 toclist[name].push_back(item);
414 string const InsetFloatMailer::name_("float");
416 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
421 string const InsetFloatMailer::inset2string(Buffer const &) const
423 return params2string(inset_.params());
427 void InsetFloatMailer::string2params(string const & in,
428 InsetFloatParams & params)
430 params = InsetFloatParams();
435 istringstream data(in);
441 string const token = lex.getString();
446 // This is part of the inset proper that is usually swallowed
447 // by Buffer::readInset
450 string const token = lex.getString();
451 if (token != "Float" || !lex.eatLine())
461 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
464 data << name_ << ' ';