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/convert.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 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 setLabel(_("float: ") + floatname(type, bp));
134 LyXFont font(LyXFont::ALL_SANE);
137 font.setColor(LColor::collapsable);
144 InsetFloat::~InsetFloat()
146 InsetFloatMailer(*this).hideDialog();
150 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
152 switch (cmd.action) {
154 case LFUN_INSET_MODIFY: {
155 InsetFloatParams params;
156 InsetFloatMailer::string2params(cmd.argument, params);
157 params_.placement = params.placement;
158 params_.wide = params.wide;
159 params_.sideways = params.sideways;
160 wide(params_.wide, cur.buffer().params());
161 sideways(params_.sideways, cur.buffer().params());
166 case LFUN_INSET_DIALOG_UPDATE: {
167 InsetFloatMailer(*this).updateDialog(&cur.bv());
171 case LFUN_MOUSE_RELEASE: {
172 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
173 InsetFloatMailer(*this).showDialog(&cur.bv());
176 InsetCollapsable::doDispatch(cur, cmd);
181 InsetCollapsable::doDispatch(cur, cmd);
187 void InsetFloatParams::write(ostream & os) const
189 os << "Float " << type << '\n';
191 if (!placement.empty())
192 os << "placement " << placement << "\n";
197 os << "wide false\n";
200 os << "sideways true\n";
202 os << "sideways false\n";
206 void InsetFloatParams::read(LyXLex & lex)
210 if (token == "placement") {
213 // take countermeasures
214 lex.pushToken(token);
217 if (token == "wide") {
220 lyxerr << "InsetFloat::Read:: Missing wide!"
222 // take countermeasures
223 lex.pushToken(token);
226 if (token == "sideways") {
229 lyxerr << "InsetFloat::Read:: Missing sideways!"
231 // take countermeasures
232 lex.pushToken(token);
237 void InsetFloat::write(Buffer const & buf, ostream & os) const
240 InsetCollapsable::write(buf, os);
244 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
247 wide(params_.wide, buf.params());
248 sideways(params_.sideways, buf.params());
249 InsetCollapsable::read(buf, lex);
253 void InsetFloat::validate(LaTeXFeatures & features) const
255 if (contains(params_.placement, 'H')) {
256 features.require("float");
259 if (params_.sideways)
260 features.require("rotating");
262 features.useFloat(params_.type);
263 InsetCollapsable::validate(features);
267 auto_ptr<InsetBase> InsetFloat::doClone() const
269 return auto_ptr<InsetBase>(new InsetFloat(*this));
273 string const InsetFloat::editMessage() const
275 return _("Opened Float Inset");
279 int InsetFloat::latex(Buffer const & buf, ostream & os,
280 OutputParams const & runparams) const
282 FloatList const & floats = buf.params().getLyXTextClass().floats();
283 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
284 if (params_.sideways) {
285 if (params_.type == "table")
286 tmptype = "sidewaystable";
287 else if (params_.type == "figure")
288 tmptype = "sidewaysfigure";
290 // Figure out the float placement to use.
291 // From lowest to highest:
292 // - float default placement
293 // - document wide default placement
294 // - specific float placement
296 string const buf_placement = buf.params().float_placement;
297 string const def_placement = floats.defaultPlacement(params_.type);
298 if (!params_.placement.empty()
299 && params_.placement != def_placement) {
300 placement = params_.placement;
301 } else if (params_.placement.empty()
302 && !buf_placement.empty()
303 && buf_placement != def_placement) {
304 placement = buf_placement;
307 // The \n is used to force \begin{<floatname>} to appear in a new line.
308 // The % is needed to prevent two consecutive \n chars in the case
309 // when the current output line is empty.
310 os << "%\n\\begin{" << tmptype << '}';
311 // We only output placement if different from the def_placement.
312 // sidewaysfloats always use their own page
313 if (!placement.empty() && !params_.sideways) {
314 os << '[' << placement << ']';
318 int const i = InsetText::latex(buf, os, runparams);
320 // The \n is used to force \end{<floatname>} to appear in a new line.
321 // In this case, we do not case if the current output line is empty.
322 os << "\n\\end{" << tmptype << "}\n";
328 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
329 OutputParams const & runparams) const
331 FloatList const & floats = buf.params().getLyXTextClass().floats();
332 string const tmptype = params_.type;
333 // Figure out the float placement to use.
334 // From lowest to highest:
335 // - float default placement
336 // - document wide default placement
337 // - specific float placement
338 // This is the same as latex, as linuxdoc is modeled after latex.
341 string const buf_placement = buf.params().float_placement;
342 string const def_placement = floats.defaultPlacement(params_.type);
343 if (!params_.placement.empty()
344 && params_.placement != def_placement) {
345 placement = params_.placement;
346 } else if (params_.placement.empty()
347 && !buf_placement.empty()
348 && buf_placement != def_placement) {
349 placement = buf_placement;
352 os << "\n<" << tmptype ;
353 // We only output placement if different from the def_placement.
354 if (!placement.empty()) {
355 os << " loc=\"" << placement << '"';
359 int const i = InsetText::linuxdoc(buf, os, runparams);
360 os << "</" << tmptype << ">\n";
366 int InsetFloat::docbook(Buffer const & buf, ostream & os,
367 OutputParams const & runparams) const
369 os << '<' << params_.type << '>';
370 int const i = InsetText::docbook(buf, os, runparams);
371 os << "</" << params_.type << '>';
377 bool InsetFloat::insetAllowed(InsetBase::Code code) const
379 return code != InsetBase::FLOAT_CODE
380 && code != InsetBase::FOOT_CODE
381 && code != InsetBase::MARGIN_CODE;
385 bool InsetFloat::showInsetDialog(BufferView * bv) const
387 if (!InsetText::showInsetDialog(bv))
388 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
393 void InsetFloat::wide(bool w, BufferParams const & bp)
396 string lab = _("float: ") + floatname(params_.type, bp);
403 void InsetFloat::sideways(bool s, BufferParams const & bp)
405 params_.sideways = s;
406 string lab = _("float: ") + floatname(params_.type, bp);
407 if (params_.sideways)
408 lab += _(" (sideways)");
413 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
415 ParConstIterator pit = par_const_iterator_begin(*this);
416 ParConstIterator end = par_const_iterator_end(*this);
418 // Find a caption layout in one of the (child inset's) pars
419 for (; pit != end; ++pit) {
420 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
421 string const name = floatname(params_.type, buf.params());
423 convert<string>(toclist[name].size() + 1)
424 + ". " + pit->asString(buf, false);
425 lyx::toc::TocItem const item(pit->id(), 0 , str);
426 toclist[name].push_back(item);
432 string const InsetFloatMailer::name_("float");
434 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
439 string const InsetFloatMailer::inset2string(Buffer const &) const
441 return params2string(inset_.params());
445 void InsetFloatMailer::string2params(string const & in,
446 InsetFloatParams & params)
448 params = InsetFloatParams();
452 istringstream data(in);
458 if (!lex || name != name_)
459 return print_mailer_error("InsetFloatMailer", in, 1, name_);
461 // This is part of the inset proper that is usually swallowed
462 // by LyXText::readInset
465 if (!lex || id != "Float")
466 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
468 // We have to read the type here!
474 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
477 data << name_ << ' ';