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 "dispatchresult.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
26 #include "LaTeXFeatures.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "pariterator.h"
33 #include "support/lstrings.h"
34 #include "support/tostr.h"
38 using lyx::support::contains;
43 using std::istringstream;
45 using std::ostringstream;
48 // With this inset it will be possible to support the latex package
49 // float.sty, and I am sure that with this and some additional support
50 // classes we can support similar functionality in other formats
52 // By using float.sty we will have the same handling for all floats, both
53 // for those already in existance (table and figure) and all user created
54 // ones¹. So suddenly we give the users the possibility of creating new
55 // kinds of floats on the fly. (and with a uniform look)
58 // \newfloat{type}{placement}{ext}[within]
59 // type - The "type" of the new class of floats, like program or
60 // algorithm. After the appropriate \newfloat, commands
61 // such as \begin{program} or \end{algorithm*} will be
63 // placement - The default placement for the given class of floats.
64 // They are like in standard LaTeX: t, b, p and h for top,
65 // bottom, page, and here, respectively. On top of that
66 // there is a new type, H, which does not really correspond
67 // to a float, since it means: put it "here" and nowhere else.
68 // Note, however that the H specifier is special and, because
69 // of implementation details cannot be used in the second
70 // argument of \newfloat.
71 // ext - The file name extension of an auxiliary file for the list
72 // of figures (or whatever). LaTeX writes the captions to
74 // within - This (optional) argument determines whether floats of this
75 // class will be numbered within some sectional unit of the
76 // document. For example, if within is equal to chapter, the
77 // floats will be numbered within chapters.
79 // style - plain, boxed, ruled
80 // \floatname{float}{floatname}
83 // \floatplacement{float}{placement}
86 // \restylefloat{float}
88 // \listof{type}{title}
91 // ¹ the algorithm float is defined using the float.sty package. Like this
93 // \newfloat{algorithm}{htbp}{loa}[<sect>]
94 // \floatname{algorithm}{Algorithm}
96 // The intention is that floats should be definable from two places:
98 // - the "gui" (i.e. by the user)
100 // From layout files.
101 // This should only be done for floats defined in a documentclass and that
102 // does not need any additional packages. The two most known floats in this
103 // category is "table" and "figure". Floats defined in layout files are only
104 // stored in lyx files if the user modifies them.
107 // There should be a gui dialog (and also a collection of lyxfuncs) where
108 // the user can modify existing floats and/or create new ones.
110 // The individual floats will also have some settable
111 // variables: wide and placement.
117 // this should not be hardcoded, but be part of the definition
118 // of the float (JMarc)
119 string const caplayout("Caption");
121 string floatname(string const & type, BufferParams const & bp)
123 FloatList const & floats = bp.getLyXTextClass().floats();
124 FloatList::const_iterator it = floats[type];
125 if (it == floats.end())
128 return _(it->second.name());
134 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
135 : InsetCollapsable(bp)
137 setLabel(_("float: ") + 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 paragraphs().begin()->layout(tclass[caplayout]);
151 InsetFloat::~InsetFloat()
153 InsetFloatMailer(*this).hideDialog();
157 void InsetFloat::priv_dispatch(LCursor & cur, FuncRequest & cmd)
159 switch (cmd.action) {
161 case LFUN_INSET_MODIFY: {
162 InsetFloatParams params;
163 InsetFloatMailer::string2params(cmd.argument, params);
164 params_.placement = params.placement;
165 params_.wide = params.wide;
166 params_.sideways = params.sideways;
167 wide(params_.wide, cur.buffer().params());
168 sideways(params_.sideways, cur.buffer().params());
173 case LFUN_INSET_DIALOG_UPDATE: {
174 InsetFloatMailer(*this).updateDialog(&cur.bv());
178 case LFUN_MOUSE_RELEASE: {
179 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
180 InsetFloatMailer(*this).showDialog(&cur.bv());
183 InsetCollapsable::priv_dispatch(cur, cmd);
188 InsetCollapsable::priv_dispatch(cur, cmd);
194 void InsetFloatParams::write(ostream & os) const
196 os << "Float " << type << '\n';
198 if (!placement.empty())
199 os << "placement " << placement << "\n";
204 os << "wide false\n";
207 os << "sideways true\n";
209 os << "sideways false\n";
213 void InsetFloatParams::read(LyXLex & lex)
217 if (token == "placement") {
220 // take countermeasures
221 lex.pushToken(token);
224 if (token == "wide") {
227 lyxerr << "InsetFloat::Read:: Missing wide!"
229 // take countermeasures
230 lex.pushToken(token);
233 if (token == "sideways") {
236 lyxerr << "InsetFloat::Read:: Missing sideways!"
238 // take countermeasures
239 lex.pushToken(token);
244 void InsetFloat::write(Buffer const & buf, ostream & os) const
247 InsetCollapsable::write(buf, os);
251 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
254 wide(params_.wide, buf.params());
255 sideways(params_.sideways, buf.params());
256 InsetCollapsable::read(buf, lex);
260 void InsetFloat::validate(LaTeXFeatures & features) const
262 if (contains(params_.placement, 'H')) {
263 features.require("float");
266 if (params_.sideways)
267 features.require("rotating");
269 features.useFloat(params_.type);
270 InsetCollapsable::validate(features);
274 auto_ptr<InsetBase> InsetFloat::clone() const
276 return auto_ptr<InsetBase>(new InsetFloat(*this));
280 string const InsetFloat::editMessage() const
282 return _("Opened Float Inset");
286 int InsetFloat::latex(Buffer const & buf, ostream & os,
287 OutputParams const & runparams) const
289 FloatList const & floats = buf.params().getLyXTextClass().floats();
290 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
291 if (params_.sideways) {
292 if (params_.type == "table")
293 tmptype = "sidewaystable";
294 else if (params_.type == "figure")
295 tmptype = "sidewaysfigure";
297 // Figure out the float placement to use.
298 // From lowest to highest:
299 // - float default placement
300 // - document wide default placement
301 // - specific float placement
303 string const buf_placement = buf.params().float_placement;
304 string const def_placement = floats.defaultPlacement(params_.type);
305 if (!params_.placement.empty()
306 && params_.placement != def_placement) {
307 placement = params_.placement;
308 } else if (params_.placement.empty()
309 && !buf_placement.empty()
310 && buf_placement != def_placement) {
311 placement = buf_placement;
314 // The \n is used to force \begin{<floatname>} to appear in a new line.
315 // The % is needed to prevent two consecutive \n chars in the case
316 // when the current output line is empty.
317 os << "%\n\\begin{" << tmptype << '}';
318 // We only output placement if different from the def_placement.
319 // sidewaysfloats always use their own page
320 if (!placement.empty() && !params_.sideways) {
321 os << '[' << placement << ']';
325 int const i = InsetText::latex(buf, os, runparams);
327 // The \n is used to force \end{<floatname>} to appear in a new line.
328 // In this case, we do not case if the current output line is empty.
329 os << "\n\\end{" << tmptype << "}\n";
335 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
336 OutputParams const & runparams) const
338 FloatList const & floats = buf.params().getLyXTextClass().floats();
339 string const tmptype = params_.type;
340 // Figure out the float placement to use.
341 // From lowest to highest:
342 // - float default placement
343 // - document wide default placement
344 // - specific float placement
345 // This is the same as latex, as linuxdoc is modeled after latex.
348 string const buf_placement = buf.params().float_placement;
349 string const def_placement = floats.defaultPlacement(params_.type);
350 if (!params_.placement.empty()
351 && params_.placement != def_placement) {
352 placement = params_.placement;
353 } else if (params_.placement.empty()
354 && !buf_placement.empty()
355 && buf_placement != def_placement) {
356 placement = buf_placement;
359 os << "\n<" << tmptype ;
360 // We only output placement if different from the def_placement.
361 if (!placement.empty()) {
362 os << " loc=\"" << placement << '"';
366 int const i = InsetText::linuxdoc(buf, os, runparams);
367 os << "</" << tmptype << ">\n";
373 int InsetFloat::docbook(Buffer const & buf, ostream & os,
374 OutputParams const & runparams) const
376 os << '<' << params_.type << '>';
377 int const i = InsetText::docbook(buf, os, runparams);
378 os << "</" << params_.type << '>';
384 bool InsetFloat::insetAllowed(InsetOld::Code code) const
386 return code != InsetOld::FLOAT_CODE
387 && code != InsetOld::FOOT_CODE
388 && code != InsetOld::MARGIN_CODE;
392 bool InsetFloat::showInsetDialog(BufferView * bv) const
394 if (!InsetText::showInsetDialog(bv))
395 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
400 void InsetFloat::wide(bool w, BufferParams const & bp)
403 string lab = _("float: ") + floatname(params_.type, bp);
410 void InsetFloat::sideways(bool s, BufferParams const & bp)
412 params_.sideways = s;
413 string lab = _("float: ") + floatname(params_.type, bp);
414 if (params_.sideways)
415 lab += _(" (sideways)");
420 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
422 ParConstIterator pit = par_const_iterator_begin(*this);
423 ParConstIterator end = par_const_iterator_end(*this);
425 // Find a caption layout in one of the (child inset's) pars
426 for (; pit != end; ++pit) {
427 if (pit->layout()->name() == caplayout) {
428 string const name = floatname(params_.type, buf.params());
430 tostr(toclist[name].size() + 1)
431 + ". " + pit->asString(buf, false);
432 lyx::toc::TocItem const item(pit->id(), 0 , str);
433 toclist[name].push_back(item);
439 string const InsetFloatMailer::name_("float");
441 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
446 string const InsetFloatMailer::inset2string(Buffer const &) const
448 return params2string(inset_.params());
452 void InsetFloatMailer::string2params(string const & in,
453 InsetFloatParams & params)
455 params = InsetFloatParams();
459 istringstream data(in);
465 if (!lex || name != name_)
466 return print_mailer_error("InsetFloatMailer", in, 1, name_);
468 // This is part of the inset proper that is usually swallowed
469 // by LyXText::readInset
472 if (!lex || id != "Float")
473 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
475 // We have to read the type here!
481 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
484 data << name_ << ' ';