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"
26 #include "LaTeXFeatures.h"
28 #include "OutputParams.h"
29 #include "TextClass.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/docstream.h"
35 #include "support/convert.h"
42 // With this inset it will be possible to support the latex package
43 // float.sty, and I am sure that with this and some additional support
44 // classes we can support similar functionality in other formats
46 // By using float.sty we will have the same handling for all floats, both
47 // for those already in existance (table and figure) and all user created
48 // ones¹. So suddenly we give the users the possibility of creating new
49 // kinds of floats on the fly. (and with a uniform look)
52 // \newfloat{type}{placement}{ext}[within]
53 // type - The "type" of the new class of floats, like program or
54 // algorithm. After the appropriate \newfloat, commands
55 // such as \begin{program} or \end{algorithm*} will be
57 // placement - The default placement for the given class of floats.
58 // They are like in standard LaTeX: t, b, p and h for top,
59 // bottom, page, and here, respectively. On top of that
60 // there is a new type, H, which does not really correspond
61 // to a float, since it means: put it "here" and nowhere else.
62 // Note, however that the H specifier is special and, because
63 // of implementation details cannot be used in the second
64 // argument of \newfloat.
65 // ext - The file name extension of an auxiliary file for the list
66 // of figures (or whatever). LaTeX writes the captions to
68 // within - This (optional) argument determines whether floats of this
69 // class will be numbered within some sectional unit of the
70 // document. For example, if within is equal to chapter, the
71 // floats will be numbered within chapters.
73 // style - plain, boxed, ruled
74 // \floatname{float}{floatname}
77 // \floatplacement{float}{placement}
80 // \restylefloat{float}
82 // \listof{type}{title}
85 // ¹ the algorithm float is defined using the float.sty package. Like this
87 // \newfloat{algorithm}{htbp}{loa}[<sect>]
88 // \floatname{algorithm}{Algorithm}
90 // The intention is that floats should be definable from two places:
92 // - the "gui" (i.e. by the user)
95 // This should only be done for floats defined in a documentclass and that
96 // does not need any additional packages. The two most known floats in this
97 // category is "table" and "figure". Floats defined in layout files are only
98 // stored in lyx files if the user modifies them.
101 // There should be a gui dialog (and also a collection of lyxfuncs) where
102 // the user can modify existing floats and/or create new ones.
104 // The individual floats will also have some settable
105 // variables: wide and placement.
110 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
111 : InsetCollapsable(bp), name_(from_utf8(type))
113 setLabel(_("float: ") + floatName(type, bp));
118 InsetFloat::~InsetFloat()
120 InsetFloatMailer(*this).hideDialog();
124 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
126 switch (cmd.action) {
128 case LFUN_INSET_MODIFY: {
129 InsetFloatParams params;
130 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
131 params_.placement = params.placement;
132 params_.wide = params.wide;
133 params_.sideways = params.sideways;
134 wide(params_.wide, cur.buffer().params());
135 sideways(params_.sideways, cur.buffer().params());
139 case LFUN_INSET_DIALOG_UPDATE: {
140 InsetFloatMailer(*this).updateDialog(&cur.bv());
144 case LFUN_MOUSE_RELEASE: {
145 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
146 InsetFloatMailer(*this).showDialog(&cur.bv());
149 InsetCollapsable::doDispatch(cur, cmd);
154 InsetCollapsable::doDispatch(cur, cmd);
160 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
161 FuncStatus & flag) const
163 switch (cmd.action) {
165 case LFUN_INSET_MODIFY:
166 case LFUN_INSET_DIALOG_UPDATE:
171 return InsetCollapsable::getStatus(cur, cmd, flag);
176 void InsetFloat::updateLabels(ParIterator const & it)
178 Counters & cnts = buffer().params().documentClass().counters();
179 string const saveflt = cnts.current_float();
181 // Tell to captions what the current float is
182 cnts.current_float(params().type);
184 InsetCollapsable::updateLabels(it);
187 cnts.current_float(saveflt);
191 void InsetFloatParams::write(ostream & os) const
193 os << "Float " << type << '\n';
195 if (!placement.empty())
196 os << "placement " << placement << "\n";
201 os << "wide false\n";
204 os << "sideways true\n";
206 os << "sideways false\n";
210 void InsetFloatParams::read(Lexer & lex)
214 if (token == "placement") {
217 // take countermeasures
218 lex.pushToken(token);
221 if (token == "wide") {
224 lyxerr << "InsetFloat::Read:: Missing wide!"
226 // take countermeasures
227 lex.pushToken(token);
230 if (token == "sideways") {
233 lyxerr << "InsetFloat::Read:: Missing sideways!"
235 // take countermeasures
236 lex.pushToken(token);
241 void InsetFloat::write(ostream & os) const
244 InsetCollapsable::write(os);
248 void InsetFloat::read(Lexer & lex)
251 wide(params_.wide, buffer().params());
252 sideways(params_.sideways, buffer().params());
253 InsetCollapsable::read(lex);
257 void InsetFloat::validate(LaTeXFeatures & features) const
259 if (support::contains(params_.placement, 'H')) {
260 features.require("float");
263 if (params_.sideways)
264 features.require("rotfloat");
266 features.useFloat(params_.type);
267 InsetCollapsable::validate(features);
271 Inset * InsetFloat::clone() const
273 return new InsetFloat(*this);
277 docstring InsetFloat::editMessage() const
279 return _("Opened Float Inset");
283 int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
285 FloatList const & floats = buffer().params().documentClass().floats();
286 string tmptype = params_.type;
287 if (params_.sideways)
288 tmptype = "sideways" + params_.type;
289 if (params_.wide && (!params_.sideways ||
290 params_.type == "figure" ||
291 params_.type == "table"))
293 // Figure out the float placement to use.
294 // From lowest to highest:
295 // - float default placement
296 // - document wide default placement
297 // - specific float placement
299 string const buf_placement = buffer().params().float_placement;
300 string const def_placement = floats.defaultPlacement(params_.type);
301 if (!params_.placement.empty()
302 && params_.placement != def_placement) {
303 placement = params_.placement;
304 } else if (params_.placement.empty()
305 && !buf_placement.empty()
306 && buf_placement != def_placement) {
307 placement = buf_placement;
310 // The \n is used to force \begin{<floatname>} to appear in a new line.
311 // The % is needed to prevent two consecutive \n chars in the case
312 // when the current output line is empty.
313 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
314 // We only output placement if different from the def_placement.
315 // sidewaysfloats always use their own page
316 if (!placement.empty() && !params_.sideways) {
317 os << '[' << from_ascii(placement) << ']';
321 int const i = InsetText::latex(os, runparams);
323 // The \n is used to force \end{<floatname>} to appear in a new line.
324 // In this case, we do not case if the current output line is empty.
325 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
331 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
333 os << '[' << buffer().B_("float") << ' '
334 << floatName(params_.type, buffer().params()) << ":\n";
335 InsetText::plaintext(os, runparams);
338 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
342 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
345 os << '<' << from_ascii(params_.type) << '>';
346 int const i = InsetText::docbook(os, runparams);
347 os << "</" << from_ascii(params_.type) << '>';
353 bool InsetFloat::insetAllowed(InsetCode code) const
355 return code != FLOAT_CODE
357 && code != 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 string const InsetFloatMailer::name_("float");
391 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
396 string const InsetFloatMailer::inset2string(Buffer const &) const
398 return params2string(inset_.params());
402 void InsetFloatMailer::string2params(string const & in,
403 InsetFloatParams & params)
405 params = InsetFloatParams();
409 istringstream data(in);
415 if (!lex || name != name_)
416 return print_mailer_error("InsetFloatMailer", in, 1, name_);
418 // This is part of the inset proper that is usually swallowed
419 // by Text::readInset
422 if (!lex || id != "Float")
423 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
425 // We have to read the type here!
431 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
434 data << name_ << ' ';