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/convert.h"
40 using std::istringstream;
42 using std::ostringstream;
45 // With this inset it will be possible to support the latex package
46 // float.sty, and I am sure that with this and some additional support
47 // classes we can support similar functionality in other formats
49 // By using float.sty we will have the same handling for all floats, both
50 // for those already in existance (table and figure) and all user created
51 // ones¹. So suddenly we give the users the possibility of creating new
52 // kinds of floats on the fly. (and with a uniform look)
55 // \newfloat{type}{placement}{ext}[within]
56 // type - The "type" of the new class of floats, like program or
57 // algorithm. After the appropriate \newfloat, commands
58 // such as \begin{program} or \end{algorithm*} will be
60 // placement - The default placement for the given class of floats.
61 // They are like in standard LaTeX: t, b, p and h for top,
62 // bottom, page, and here, respectively. On top of that
63 // there is a new type, H, which does not really correspond
64 // to a float, since it means: put it "here" and nowhere else.
65 // Note, however that the H specifier is special and, because
66 // of implementation details cannot be used in the second
67 // argument of \newfloat.
68 // ext - The file name extension of an auxiliary file for the list
69 // of figures (or whatever). LaTeX writes the captions to
71 // within - This (optional) argument determines whether floats of this
72 // class will be numbered within some sectional unit of the
73 // document. For example, if within is equal to chapter, the
74 // floats will be numbered within chapters.
76 // style - plain, boxed, ruled
77 // \floatname{float}{floatname}
80 // \floatplacement{float}{placement}
83 // \restylefloat{float}
85 // \listof{type}{title}
88 // ¹ the algorithm float is defined using the float.sty package. Like this
90 // \newfloat{algorithm}{htbp}{loa}[<sect>]
91 // \floatname{algorithm}{Algorithm}
93 // The intention is that floats should be definable from two places:
95 // - the "gui" (i.e. by the user)
98 // This should only be done for floats defined in a documentclass and that
99 // does not need any additional packages. The two most known floats in this
100 // category is "table" and "figure". Floats defined in layout files are only
101 // stored in lyx files if the user modifies them.
104 // There should be a gui dialog (and also a collection of lyxfuncs) where
105 // the user can modify existing floats and/or create new ones.
107 // The individual floats will also have some settable
108 // variables: wide and placement.
113 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
114 : InsetCollapsable(bp), name_(from_utf8(type))
116 setLabel(_("float: ") + floatName(type, bp));
117 FontInfo font = sane_font;
120 font.setColor(Color_collapsable);
126 InsetFloat::~InsetFloat()
128 InsetFloatMailer(*this).hideDialog();
132 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
134 switch (cmd.action) {
136 case LFUN_INSET_MODIFY: {
137 InsetFloatParams params;
138 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
139 params_.placement = params.placement;
140 params_.wide = params.wide;
141 params_.sideways = params.sideways;
142 wide(params_.wide, cur.buffer().params());
143 sideways(params_.sideways, cur.buffer().params());
147 case LFUN_INSET_DIALOG_UPDATE: {
148 InsetFloatMailer(*this).updateDialog(&cur.bv());
152 case LFUN_MOUSE_RELEASE: {
153 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
154 InsetFloatMailer(*this).showDialog(&cur.bv());
157 InsetCollapsable::doDispatch(cur, cmd);
162 InsetCollapsable::doDispatch(cur, cmd);
168 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
169 FuncStatus & flag) const
171 switch (cmd.action) {
173 case LFUN_INSET_MODIFY:
174 case LFUN_INSET_DIALOG_UPDATE:
179 return InsetCollapsable::getStatus(cur, cmd, flag);
184 void InsetFloat::updateLabels(Buffer const & buf, ParIterator const & it)
186 Counters & cnts = buf.params().getTextClass().counters();
187 string const saveflt = cnts.current_float();
189 // Tell to captions what the current float is
190 cnts.current_float(params().type);
192 InsetCollapsable::updateLabels(buf, it);
195 cnts.current_float(saveflt);
199 void InsetFloatParams::write(ostream & os) const
201 os << "Float " << type << '\n';
203 if (!placement.empty())
204 os << "placement " << placement << "\n";
209 os << "wide false\n";
212 os << "sideways true\n";
214 os << "sideways false\n";
218 void InsetFloatParams::read(Lexer & lex)
222 if (token == "placement") {
225 // take countermeasures
226 lex.pushToken(token);
229 if (token == "wide") {
232 lyxerr << "InsetFloat::Read:: Missing wide!"
234 // take countermeasures
235 lex.pushToken(token);
238 if (token == "sideways") {
241 lyxerr << "InsetFloat::Read:: Missing sideways!"
243 // take countermeasures
244 lex.pushToken(token);
249 void InsetFloat::write(Buffer const & buf, ostream & os) const
252 InsetCollapsable::write(buf, os);
256 void InsetFloat::read(Buffer const & buf, Lexer & lex)
259 wide(params_.wide, buf.params());
260 sideways(params_.sideways, buf.params());
261 InsetCollapsable::read(buf, lex);
265 void InsetFloat::validate(LaTeXFeatures & features) const
267 if (support::contains(params_.placement, 'H')) {
268 features.require("float");
271 if (params_.sideways)
272 features.require("rotating");
274 features.useFloat(params_.type);
275 InsetCollapsable::validate(features);
279 Inset * InsetFloat::clone() const
281 return new InsetFloat(*this);
285 docstring const InsetFloat::editMessage() const
287 return _("Opened Float Inset");
291 int InsetFloat::latex(Buffer const & buf, odocstream & os,
292 OutputParams const & runparams) const
294 FloatList const & floats = buf.params().getTextClass().floats();
295 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
296 if (params_.sideways) {
297 if (params_.type == "table")
298 tmptype = "sidewaystable";
299 else if (params_.type == "figure")
300 tmptype = "sidewaysfigure";
302 // Figure out the float placement to use.
303 // From lowest to highest:
304 // - float default placement
305 // - document wide default placement
306 // - specific float placement
308 string const buf_placement = buf.params().float_placement;
309 string const def_placement = floats.defaultPlacement(params_.type);
310 if (!params_.placement.empty()
311 && params_.placement != def_placement) {
312 placement = params_.placement;
313 } else if (params_.placement.empty()
314 && !buf_placement.empty()
315 && buf_placement != def_placement) {
316 placement = buf_placement;
319 // The \n is used to force \begin{<floatname>} to appear in a new line.
320 // The % is needed to prevent two consecutive \n chars in the case
321 // when the current output line is empty.
322 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
323 // We only output placement if different from the def_placement.
324 // sidewaysfloats always use their own page
325 if (!placement.empty() && !params_.sideways) {
326 os << '[' << from_ascii(placement) << ']';
330 int const i = InsetText::latex(buf, os, runparams);
332 // The \n is used to force \end{<floatname>} to appear in a new line.
333 // In this case, we do not case if the current output line is empty.
334 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
340 int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
341 OutputParams const & runparams) const
343 os << '[' << buf.B_("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
344 InsetText::plaintext(buf, os, runparams);
347 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
351 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
352 OutputParams const & runparams) const
355 os << '<' << from_ascii(params_.type) << '>';
356 int const i = InsetText::docbook(buf, os, runparams);
357 os << "</" << from_ascii(params_.type) << '>';
363 bool InsetFloat::insetAllowed(InsetCode code) const
365 return code != FLOAT_CODE
367 && code != MARGIN_CODE;
371 bool InsetFloat::showInsetDialog(BufferView * bv) const
373 if (!InsetText::showInsetDialog(bv))
374 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
379 void InsetFloat::wide(bool w, BufferParams const & bp)
382 docstring lab = _("float: ") + floatName(params_.type, bp);
389 void InsetFloat::sideways(bool s, BufferParams const & bp)
391 params_.sideways = s;
392 docstring lab = _("float: ") + floatName(params_.type, bp);
393 if (params_.sideways)
394 lab += _(" (sideways)");
399 string const InsetFloatMailer::name_("float");
401 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
406 string const InsetFloatMailer::inset2string(Buffer const &) const
408 return params2string(inset_.params());
412 void InsetFloatMailer::string2params(string const & in,
413 InsetFloatParams & params)
415 params = InsetFloatParams();
419 istringstream data(in);
425 if (!lex || name != name_)
426 return print_mailer_error("InsetFloatMailer", in, 1, name_);
428 // This is part of the inset proper that is usually swallowed
429 // by Text::readInset
432 if (!lex || id != "Float")
433 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
435 // We have to read the type here!
441 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
444 data << name_ << ' ';