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"
22 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
32 #include "support/lstrings.h"
33 #include "support/docstream.h"
34 #include "support/convert.h"
41 using std::istringstream;
43 using std::ostringstream;
46 // With this inset it will be possible to support the latex package
47 // float.sty, and I am sure that with this and some additional support
48 // classes we can support similar functionality in other formats
50 // By using float.sty we will have the same handling for all floats, both
51 // for those already in existance (table and figure) and all user created
52 // ones¹. So suddenly we give the users the possibility of creating new
53 // kinds of floats on the fly. (and with a uniform look)
56 // \newfloat{type}{placement}{ext}[within]
57 // type - The "type" of the new class of floats, like program or
58 // algorithm. After the appropriate \newfloat, commands
59 // such as \begin{program} or \end{algorithm*} will be
61 // placement - The default placement for the given class of floats.
62 // They are like in standard LaTeX: t, b, p and h for top,
63 // bottom, page, and here, respectively. On top of that
64 // there is a new type, H, which does not really correspond
65 // to a float, since it means: put it "here" and nowhere else.
66 // Note, however that the H specifier is special and, because
67 // of implementation details cannot be used in the second
68 // argument of \newfloat.
69 // ext - The file name extension of an auxiliary file for the list
70 // of figures (or whatever). LaTeX writes the captions to
72 // within - This (optional) argument determines whether floats of this
73 // class will be numbered within some sectional unit of the
74 // document. For example, if within is equal to chapter, the
75 // floats will be numbered within chapters.
77 // style - plain, boxed, ruled
78 // \floatname{float}{floatname}
81 // \floatplacement{float}{placement}
84 // \restylefloat{float}
86 // \listof{type}{title}
89 // ¹ the algorithm float is defined using the float.sty package. Like this
91 // \newfloat{algorithm}{htbp}{loa}[<sect>]
92 // \floatname{algorithm}{Algorithm}
94 // The intention is that floats should be definable from two places:
96 // - the "gui" (i.e. by the user)
99 // This should only be done for floats defined in a documentclass and that
100 // does not need any additional packages. The two most known floats in this
101 // category is "table" and "figure". Floats defined in layout files are only
102 // stored in lyx files if the user modifies them.
105 // There should be a gui dialog (and also a collection of lyxfuncs) where
106 // the user can modify existing floats and/or create new ones.
108 // The individual floats will also have some settable
109 // variables: wide and placement.
114 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
115 : InsetCollapsable(bp), name_(from_utf8(type))
117 setLabel(_("float: ") + floatName(type, bp));
122 InsetFloat::~InsetFloat()
124 InsetFloatMailer(*this).hideDialog();
128 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
130 switch (cmd.action) {
132 case LFUN_INSET_MODIFY: {
133 InsetFloatParams params;
134 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
135 params_.placement = params.placement;
136 params_.wide = params.wide;
137 params_.sideways = params.sideways;
138 wide(params_.wide, cur.buffer().params());
139 sideways(params_.sideways, cur.buffer().params());
143 case LFUN_INSET_DIALOG_UPDATE: {
144 InsetFloatMailer(*this).updateDialog(&cur.bv());
148 case LFUN_MOUSE_RELEASE: {
149 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
150 InsetFloatMailer(*this).showDialog(&cur.bv());
153 InsetCollapsable::doDispatch(cur, cmd);
158 InsetCollapsable::doDispatch(cur, cmd);
164 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
165 FuncStatus & flag) const
167 switch (cmd.action) {
169 case LFUN_INSET_MODIFY:
170 case LFUN_INSET_DIALOG_UPDATE:
175 return InsetCollapsable::getStatus(cur, cmd, flag);
180 void InsetFloat::updateLabels(Buffer const & buf, ParIterator const & it)
182 Counters & cnts = buf.params().getTextClass().counters();
183 string const saveflt = cnts.current_float();
185 // Tell to captions what the current float is
186 cnts.current_float(params().type);
188 InsetCollapsable::updateLabels(buf, it);
191 cnts.current_float(saveflt);
195 void InsetFloatParams::write(ostream & os) const
197 os << "Float " << type << '\n';
199 if (!placement.empty())
200 os << "placement " << placement << "\n";
205 os << "wide false\n";
208 os << "sideways true\n";
210 os << "sideways false\n";
214 void InsetFloatParams::read(Lexer & lex)
218 if (token == "placement") {
221 // take countermeasures
222 lex.pushToken(token);
225 if (token == "wide") {
228 lyxerr << "InsetFloat::Read:: Missing wide!"
230 // take countermeasures
231 lex.pushToken(token);
234 if (token == "sideways") {
237 lyxerr << "InsetFloat::Read:: Missing sideways!"
239 // take countermeasures
240 lex.pushToken(token);
245 void InsetFloat::write(Buffer const & buf, ostream & os) const
248 InsetCollapsable::write(buf, os);
252 void InsetFloat::read(Buffer const & buf, Lexer & lex)
255 wide(params_.wide, buf.params());
256 sideways(params_.sideways, buf.params());
257 InsetCollapsable::read(buf, lex);
261 void InsetFloat::validate(LaTeXFeatures & features) const
263 if (support::contains(params_.placement, 'H')) {
264 features.require("float");
267 if (params_.sideways)
268 features.require("rotating");
270 features.useFloat(params_.type);
271 InsetCollapsable::validate(features);
275 Inset * InsetFloat::clone() const
277 return new InsetFloat(*this);
281 docstring const InsetFloat::editMessage() const
283 return _("Opened Float Inset");
287 int InsetFloat::latex(Buffer const & buf, odocstream & os,
288 OutputParams const & runparams) const
290 FloatList const & floats = buf.params().getTextClass().floats();
291 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
292 if (params_.sideways) {
293 if (params_.type == "table")
294 tmptype = "sidewaystable";
295 else if (params_.type == "figure")
296 tmptype = "sidewaysfigure";
298 // Figure out the float placement to use.
299 // From lowest to highest:
300 // - float default placement
301 // - document wide default placement
302 // - specific float placement
304 string const buf_placement = buf.params().float_placement;
305 string const def_placement = floats.defaultPlacement(params_.type);
306 if (!params_.placement.empty()
307 && params_.placement != def_placement) {
308 placement = params_.placement;
309 } else if (params_.placement.empty()
310 && !buf_placement.empty()
311 && buf_placement != def_placement) {
312 placement = buf_placement;
315 // The \n is used to force \begin{<floatname>} to appear in a new line.
316 // The % is needed to prevent two consecutive \n chars in the case
317 // when the current output line is empty.
318 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
319 // We only output placement if different from the def_placement.
320 // sidewaysfloats always use their own page
321 if (!placement.empty() && !params_.sideways) {
322 os << '[' << from_ascii(placement) << ']';
326 int const i = InsetText::latex(buf, os, runparams);
328 // The \n is used to force \end{<floatname>} to appear in a new line.
329 // In this case, we do not case if the current output line is empty.
330 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
336 int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
337 OutputParams const & runparams) const
339 os << '[' << buf.B_("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
340 InsetText::plaintext(buf, os, runparams);
343 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
347 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
348 OutputParams const & runparams) const
351 os << '<' << from_ascii(params_.type) << '>';
352 int const i = InsetText::docbook(buf, os, runparams);
353 os << "</" << from_ascii(params_.type) << '>';
359 bool InsetFloat::insetAllowed(InsetCode code) const
361 return code != FLOAT_CODE
363 && code != MARGIN_CODE;
367 bool InsetFloat::showInsetDialog(BufferView * bv) const
369 if (!InsetText::showInsetDialog(bv))
370 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
375 void InsetFloat::wide(bool w, BufferParams const & bp)
378 docstring lab = _("float: ") + floatName(params_.type, bp);
385 void InsetFloat::sideways(bool s, BufferParams const & bp)
387 params_.sideways = s;
388 docstring lab = _("float: ") + floatName(params_.type, bp);
389 if (params_.sideways)
390 lab += _(" (sideways)");
395 string const InsetFloatMailer::name_("float");
397 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
402 string const InsetFloatMailer::inset2string(Buffer const &) const
404 return params2string(inset_.params());
408 void InsetFloatMailer::string2params(string const & in,
409 InsetFloatParams & params)
411 params = InsetFloatParams();
415 istringstream data(in);
421 if (!lex || name != name_)
422 return print_mailer_error("InsetFloatMailer", in, 1, name_);
424 // This is part of the inset proper that is usually swallowed
425 // by Text::readInset
428 if (!lex || id != "Float")
429 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
431 // We have to read the type here!
437 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
440 data << name_ << ' ';