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"
25 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
30 #include "outputparams.h"
31 #include "paragraph.h"
32 #include "pariterator.h"
34 #include "support/lstrings.h"
35 #include "support/convert.h"
39 using lyx::support::contains;
44 using std::istringstream;
46 using std::ostringstream;
49 // With this inset it will be possible to support the latex package
50 // float.sty, and I am sure that with this and some additional support
51 // classes we can support similar functionality in other formats
53 // By using float.sty we will have the same handling for all floats, both
54 // for those already in existance (table and figure) and all user created
55 // ones¹. So suddenly we give the users the possibility of creating new
56 // kinds of floats on the fly. (and with a uniform look)
59 // \newfloat{type}{placement}{ext}[within]
60 // type - The "type" of the new class of floats, like program or
61 // algorithm. After the appropriate \newfloat, commands
62 // such as \begin{program} or \end{algorithm*} will be
64 // placement - The default placement for the given class of floats.
65 // They are like in standard LaTeX: t, b, p and h for top,
66 // bottom, page, and here, respectively. On top of that
67 // there is a new type, H, which does not really correspond
68 // to a float, since it means: put it "here" and nowhere else.
69 // Note, however that the H specifier is special and, because
70 // of implementation details cannot be used in the second
71 // argument of \newfloat.
72 // ext - The file name extension of an auxiliary file for the list
73 // of figures (or whatever). LaTeX writes the captions to
75 // within - This (optional) argument determines whether floats of this
76 // class will be numbered within some sectional unit of the
77 // document. For example, if within is equal to chapter, the
78 // floats will be numbered within chapters.
80 // style - plain, boxed, ruled
81 // \floatname{float}{floatname}
84 // \floatplacement{float}{placement}
87 // \restylefloat{float}
89 // \listof{type}{title}
92 // ¹ the algorithm float is defined using the float.sty package. Like this
94 // \newfloat{algorithm}{htbp}{loa}[<sect>]
95 // \floatname{algorithm}{Algorithm}
97 // The intention is that floats should be definable from two places:
99 // - the "gui" (i.e. by the user)
101 // From layout files.
102 // This should only be done for floats defined in a documentclass and that
103 // does not need any additional packages. The two most known floats in this
104 // category is "table" and "figure". Floats defined in layout files are only
105 // stored in lyx files if the user modifies them.
108 // There should be a gui dialog (and also a collection of lyxfuncs) where
109 // the user can modify existing floats and/or create new ones.
111 // The individual floats will also have some settable
112 // variables: wide and placement.
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 setLabel(_("float: ") + floatname(type, bp));
135 LyXFont font(LyXFont::ALL_SANE);
138 font.setColor(LColor::collapsable);
145 InsetFloat::~InsetFloat()
147 InsetFloatMailer(*this).hideDialog();
151 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
153 switch (cmd.action) {
155 case LFUN_INSET_MODIFY: {
156 InsetFloatParams params;
157 InsetFloatMailer::string2params(cmd.argument, params);
158 params_.placement = params.placement;
159 params_.wide = params.wide;
160 params_.sideways = params.sideways;
161 wide(params_.wide, cur.buffer().params());
162 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 bool InsetFloat::getStatus(LCursor & cur, FuncRequest const & cmd,
188 FuncStatus & flag) const
190 switch (cmd.action) {
192 case LFUN_INSET_MODIFY:
193 case LFUN_INSET_DIALOG_UPDATE:
198 return InsetCollapsable::getStatus(cur, cmd, flag);
203 void InsetFloatParams::write(ostream & os) const
205 os << "Float " << type << '\n';
207 if (!placement.empty())
208 os << "placement " << placement << "\n";
213 os << "wide false\n";
216 os << "sideways true\n";
218 os << "sideways false\n";
222 void InsetFloatParams::read(LyXLex & lex)
226 if (token == "placement") {
229 // take countermeasures
230 lex.pushToken(token);
233 if (token == "wide") {
236 lyxerr << "InsetFloat::Read:: Missing wide!"
238 // take countermeasures
239 lex.pushToken(token);
242 if (token == "sideways") {
245 lyxerr << "InsetFloat::Read:: Missing sideways!"
247 // take countermeasures
248 lex.pushToken(token);
253 void InsetFloat::write(Buffer const & buf, ostream & os) const
256 InsetCollapsable::write(buf, os);
260 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
263 wide(params_.wide, buf.params());
264 sideways(params_.sideways, buf.params());
265 InsetCollapsable::read(buf, lex);
269 void InsetFloat::validate(LaTeXFeatures & features) const
271 if (contains(params_.placement, 'H')) {
272 features.require("float");
275 if (params_.sideways)
276 features.require("rotating");
278 features.useFloat(params_.type);
279 InsetCollapsable::validate(features);
283 auto_ptr<InsetBase> InsetFloat::doClone() const
285 return auto_ptr<InsetBase>(new InsetFloat(*this));
289 string const InsetFloat::editMessage() const
291 return _("Opened Float Inset");
295 int InsetFloat::latex(Buffer const & buf, ostream & os,
296 OutputParams const & runparams) const
298 FloatList const & floats = buf.params().getLyXTextClass().floats();
299 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
300 if (params_.sideways) {
301 if (params_.type == "table")
302 tmptype = "sidewaystable";
303 else if (params_.type == "figure")
304 tmptype = "sidewaysfigure";
306 // Figure out the float placement to use.
307 // From lowest to highest:
308 // - float default placement
309 // - document wide default placement
310 // - specific float placement
312 string const buf_placement = buf.params().float_placement;
313 string const def_placement = floats.defaultPlacement(params_.type);
314 if (!params_.placement.empty()
315 && params_.placement != def_placement) {
316 placement = params_.placement;
317 } else if (params_.placement.empty()
318 && !buf_placement.empty()
319 && buf_placement != def_placement) {
320 placement = buf_placement;
323 // The \n is used to force \begin{<floatname>} to appear in a new line.
324 // The % is needed to prevent two consecutive \n chars in the case
325 // when the current output line is empty.
326 os << "%\n\\begin{" << tmptype << '}';
327 // We only output placement if different from the def_placement.
328 // sidewaysfloats always use their own page
329 if (!placement.empty() && !params_.sideways) {
330 os << '[' << placement << ']';
334 int const i = InsetText::latex(buf, os, runparams);
336 // The \n is used to force \end{<floatname>} to appear in a new line.
337 // In this case, we do not case if the current output line is empty.
338 os << "\n\\end{" << tmptype << "}\n";
344 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
345 OutputParams const & runparams) const
347 FloatList const & floats = buf.params().getLyXTextClass().floats();
348 string const tmptype = params_.type;
349 // Figure out the float placement to use.
350 // From lowest to highest:
351 // - float default placement
352 // - document wide default placement
353 // - specific float placement
354 // This is the same as latex, as linuxdoc is modeled after latex.
357 string const buf_placement = buf.params().float_placement;
358 string const def_placement = floats.defaultPlacement(params_.type);
359 if (!params_.placement.empty()
360 && params_.placement != def_placement) {
361 placement = params_.placement;
362 } else if (params_.placement.empty()
363 && !buf_placement.empty()
364 && buf_placement != def_placement) {
365 placement = buf_placement;
368 os << "\n<" << tmptype ;
369 // We only output placement if different from the def_placement.
370 if (!placement.empty()) {
371 os << " loc=\"" << placement << '"';
375 int const i = InsetText::linuxdoc(buf, os, runparams);
376 os << "</" << tmptype << ">\n";
382 int InsetFloat::docbook(Buffer const & buf, ostream & os,
383 OutputParams const & runparams) const
385 os << '<' << params_.type << '>';
386 int const i = InsetText::docbook(buf, os, runparams);
387 os << "</" << params_.type << '>';
393 bool InsetFloat::insetAllowed(InsetBase::Code code) const
395 return code != InsetBase::FLOAT_CODE
396 && code != InsetBase::FOOT_CODE
397 && code != InsetBase::MARGIN_CODE;
401 bool InsetFloat::showInsetDialog(BufferView * bv) const
403 if (!InsetText::showInsetDialog(bv))
404 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
409 void InsetFloat::wide(bool w, BufferParams const & bp)
412 string lab = _("float: ") + floatname(params_.type, bp);
419 void InsetFloat::sideways(bool s, BufferParams const & bp)
421 params_.sideways = s;
422 string lab = _("float: ") + floatname(params_.type, bp);
423 if (params_.sideways)
424 lab += _(" (sideways)");
429 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
431 ParConstIterator pit = par_const_iterator_begin(*this);
432 ParConstIterator end = par_const_iterator_end(*this);
434 // Find a caption layout in one of the (child inset's) pars
435 for (; pit != end; ++pit) {
436 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
437 string const type = params_.type;
439 convert<string>(toclist[type].size() + 1)
440 + ". " + pit->asString(buf, false);
441 lyx::toc::TocItem const item(pit->id(), 0 , str);
442 toclist[type].push_back(item);
448 string const InsetFloatMailer::name_("float");
450 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
455 string const InsetFloatMailer::inset2string(Buffer const &) const
457 return params2string(inset_.params());
461 void InsetFloatMailer::string2params(string const & in,
462 InsetFloatParams & params)
464 params = InsetFloatParams();
468 istringstream data(in);
474 if (!lex || name != name_)
475 return print_mailer_error("InsetFloatMailer", in, 1, name_);
477 // This is part of the inset proper that is usually swallowed
478 // by LyXText::readInset
481 if (!lex || id != "Float")
482 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
484 // We have to read the type here!
490 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
493 data << name_ << ' ';