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 "TocBackend.h"
34 #include "support/lstrings.h"
35 #include "support/convert.h"
40 using support::contains;
45 using std::istringstream;
47 using std::ostringstream;
50 // With this inset it will be possible to support the latex package
51 // float.sty, and I am sure that with this and some additional support
52 // classes we can support similar functionality in other formats
54 // By using float.sty we will have the same handling for all floats, both
55 // for those already in existance (table and figure) and all user created
56 // ones¹. So suddenly we give the users the possibility of creating new
57 // kinds of floats on the fly. (and with a uniform look)
60 // \newfloat{type}{placement}{ext}[within]
61 // type - The "type" of the new class of floats, like program or
62 // algorithm. After the appropriate \newfloat, commands
63 // such as \begin{program} or \end{algorithm*} will be
65 // placement - The default placement for the given class of floats.
66 // They are like in standard LaTeX: t, b, p and h for top,
67 // bottom, page, and here, respectively. On top of that
68 // there is a new type, H, which does not really correspond
69 // to a float, since it means: put it "here" and nowhere else.
70 // Note, however that the H specifier is special and, because
71 // of implementation details cannot be used in the second
72 // argument of \newfloat.
73 // ext - The file name extension of an auxiliary file for the list
74 // of figures (or whatever). LaTeX writes the captions to
76 // within - This (optional) argument determines whether floats of this
77 // class will be numbered within some sectional unit of the
78 // document. For example, if within is equal to chapter, the
79 // floats will be numbered within chapters.
81 // style - plain, boxed, ruled
82 // \floatname{float}{floatname}
85 // \floatplacement{float}{placement}
88 // \restylefloat{float}
90 // \listof{type}{title}
93 // ¹ the algorithm float is defined using the float.sty package. Like this
95 // \newfloat{algorithm}{htbp}{loa}[<sect>]
96 // \floatname{algorithm}{Algorithm}
98 // The intention is that floats should be definable from two places:
100 // - the "gui" (i.e. by the user)
102 // From layout files.
103 // This should only be done for floats defined in a documentclass and that
104 // does not need any additional packages. The two most known floats in this
105 // category is "table" and "figure". Floats defined in layout files are only
106 // stored in lyx files if the user modifies them.
109 // There should be a gui dialog (and also a collection of lyxfuncs) where
110 // the user can modify existing floats and/or create new ones.
112 // The individual floats will also have some settable
113 // variables: wide and placement.
118 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
119 : InsetCollapsable(bp), name_(from_utf8(type))
121 setLabel(_("float: ") + floatName(type, bp));
122 LyXFont font(LyXFont::ALL_SANE);
125 font.setColor(Color::collapsable);
131 InsetFloat::~InsetFloat()
133 InsetFloatMailer(*this).hideDialog();
137 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
139 switch (cmd.action) {
141 case LFUN_INSET_MODIFY: {
142 InsetFloatParams params;
143 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
144 params_.placement = params.placement;
145 params_.wide = params.wide;
146 params_.sideways = params.sideways;
147 wide(params_.wide, cur.buffer().params());
148 sideways(params_.sideways, cur.buffer().params());
152 case LFUN_INSET_DIALOG_UPDATE: {
153 InsetFloatMailer(*this).updateDialog(&cur.bv());
157 case LFUN_MOUSE_RELEASE: {
158 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
159 InsetFloatMailer(*this).showDialog(&cur.bv());
162 InsetCollapsable::doDispatch(cur, cmd);
167 InsetCollapsable::doDispatch(cur, cmd);
173 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
174 FuncStatus & flag) const
176 switch (cmd.action) {
178 case LFUN_INSET_MODIFY:
179 case LFUN_INSET_DIALOG_UPDATE:
184 return InsetCollapsable::getStatus(cur, cmd, flag);
189 void InsetFloatParams::write(ostream & os) const
191 os << "Float " << type << '\n';
193 if (!placement.empty())
194 os << "placement " << placement << "\n";
199 os << "wide false\n";
202 os << "sideways true\n";
204 os << "sideways false\n";
208 void InsetFloatParams::read(Lexer & lex)
212 if (token == "placement") {
215 // take countermeasures
216 lex.pushToken(token);
219 if (token == "wide") {
222 lyxerr << "InsetFloat::Read:: Missing wide!"
224 // take countermeasures
225 lex.pushToken(token);
228 if (token == "sideways") {
231 lyxerr << "InsetFloat::Read:: Missing sideways!"
233 // take countermeasures
234 lex.pushToken(token);
239 void InsetFloat::write(Buffer const & buf, ostream & os) const
242 InsetCollapsable::write(buf, os);
246 void InsetFloat::read(Buffer const & buf, Lexer & lex)
249 wide(params_.wide, buf.params());
250 sideways(params_.sideways, buf.params());
251 InsetCollapsable::read(buf, lex);
255 void InsetFloat::validate(LaTeXFeatures & features) const
257 if (contains(params_.placement, 'H')) {
258 features.require("float");
261 if (params_.sideways)
262 features.require("rotating");
264 features.useFloat(params_.type);
265 InsetCollapsable::validate(features);
269 auto_ptr<Inset> InsetFloat::doClone() const
271 return auto_ptr<Inset>(new InsetFloat(*this));
275 docstring const InsetFloat::editMessage() const
277 return _("Opened Float Inset");
281 int InsetFloat::latex(Buffer const & buf, odocstream & os,
282 OutputParams const & runparams) const
284 FloatList const & floats = buf.params().getLyXTextClass().floats();
285 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
286 if (params_.sideways) {
287 if (params_.type == "table")
288 tmptype = "sidewaystable";
289 else if (params_.type == "figure")
290 tmptype = "sidewaysfigure";
292 // Figure out the float placement to use.
293 // From lowest to highest:
294 // - float default placement
295 // - document wide default placement
296 // - specific float placement
298 string const buf_placement = buf.params().float_placement;
299 string const def_placement = floats.defaultPlacement(params_.type);
300 if (!params_.placement.empty()
301 && params_.placement != def_placement) {
302 placement = params_.placement;
303 } else if (params_.placement.empty()
304 && !buf_placement.empty()
305 && buf_placement != def_placement) {
306 placement = buf_placement;
309 // The \n is used to force \begin{<floatname>} to appear in a new line.
310 // The % is needed to prevent two consecutive \n chars in the case
311 // when the current output line is empty.
312 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
313 // We only output placement if different from the def_placement.
314 // sidewaysfloats always use their own page
315 if (!placement.empty() && !params_.sideways) {
316 os << '[' << from_ascii(placement) << ']';
320 int const i = InsetText::latex(buf, os, runparams);
322 // The \n is used to force \end{<floatname>} to appear in a new line.
323 // In this case, we do not case if the current output line is empty.
324 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
330 int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
331 OutputParams const & runparams) const
333 os << '[' << _("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
334 InsetText::plaintext(buf, os, runparams);
337 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
341 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
342 OutputParams const & runparams) const
345 os << '<' << from_ascii(params_.type) << '>';
346 int const i = InsetText::docbook(buf, os, runparams);
347 os << "</" << from_ascii(params_.type) << '>';
353 bool InsetFloat::insetAllowed(Inset::Code code) const
355 return code != Inset::FLOAT_CODE
356 && code != Inset::FOOT_CODE
357 && code != Inset::MARGIN_CODE;
361 bool InsetFloat::showInsetDialog(BufferView * bv) const
363 if (!InsetText::showInsetDialog(bv))
364 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
369 void InsetFloat::wide(bool w, BufferParams const & bp)
372 docstring lab = _("float: ") + floatName(params_.type, bp);
379 void InsetFloat::sideways(bool s, BufferParams const & bp)
381 params_.sideways = s;
382 docstring lab = _("float: ") + floatName(params_.type, bp);
383 if (params_.sideways)
384 lab += _(" (sideways)");
389 void InsetFloat::addToToc(TocList & toclist, Buffer const & buf) const
391 // Is there a need to provide a list of float insets?
394 // Abdel (01/02/2006): I'll let this code for reference in case
395 // there's a need to do something similar for another kind of
397 ParConstIterator pit = par_const_iterator_begin(*this);
398 ParConstIterator end = par_const_iterator_end(*this);
400 // Find a caption layout in one of the (child inset's) pars
401 for (; pit != end; ++pit) {
402 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
403 Toc & toc = toclist[params_.type];
404 docstring const str =
405 convert<docstring>(toc.size() + 1)
406 + ". " + pit->asString(buf, false);
407 toc.push_back(TocItem(pit, 0, str));
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();
434 istringstream data(in);
440 if (!lex || name != name_)
441 return print_mailer_error("InsetFloatMailer", in, 1, name_);
443 // This is part of the inset proper that is usually swallowed
444 // by LyXText::readInset
447 if (!lex || id != "Float")
448 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
450 // We have to read the type here!
456 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
459 data << name_ << ' ';