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"
38 using support::contains;
43 using std::istringstream;
45 using std::ostringstream;
48 // With this inset it will be possible to support the latex package
49 // float.sty, and I am sure that with this and some additional support
50 // classes we can support similar functionality in other formats
52 // By using float.sty we will have the same handling for all floats, both
53 // for those already in existance (table and figure) and all user created
54 // ones¹. So suddenly we give the users the possibility of creating new
55 // kinds of floats on the fly. (and with a uniform look)
58 // \newfloat{type}{placement}{ext}[within]
59 // type - The "type" of the new class of floats, like program or
60 // algorithm. After the appropriate \newfloat, commands
61 // such as \begin{program} or \end{algorithm*} will be
63 // placement - The default placement for the given class of floats.
64 // They are like in standard LaTeX: t, b, p and h for top,
65 // bottom, page, and here, respectively. On top of that
66 // there is a new type, H, which does not really correspond
67 // to a float, since it means: put it "here" and nowhere else.
68 // Note, however that the H specifier is special and, because
69 // of implementation details cannot be used in the second
70 // argument of \newfloat.
71 // ext - The file name extension of an auxiliary file for the list
72 // of figures (or whatever). LaTeX writes the captions to
74 // within - This (optional) argument determines whether floats of this
75 // class will be numbered within some sectional unit of the
76 // document. For example, if within is equal to chapter, the
77 // floats will be numbered within chapters.
79 // style - plain, boxed, ruled
80 // \floatname{float}{floatname}
83 // \floatplacement{float}{placement}
86 // \restylefloat{float}
88 // \listof{type}{title}
91 // ¹ the algorithm float is defined using the float.sty package. Like this
93 // \newfloat{algorithm}{htbp}{loa}[<sect>]
94 // \floatname{algorithm}{Algorithm}
96 // The intention is that floats should be definable from two places:
98 // - the "gui" (i.e. by the user)
100 // From layout files.
101 // This should only be done for floats defined in a documentclass and that
102 // does not need any additional packages. The two most known floats in this
103 // category is "table" and "figure". Floats defined in layout files are only
104 // stored in lyx files if the user modifies them.
107 // There should be a gui dialog (and also a collection of lyxfuncs) where
108 // the user can modify existing floats and/or create new ones.
110 // The individual floats will also have some settable
111 // variables: wide and placement.
116 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
117 : InsetCollapsable(bp), name_(from_utf8(type))
119 setLabel(_("float: ") + floatName(type, bp));
120 Font font(Font::ALL_SANE);
123 font.setColor(Color::collapsable);
129 InsetFloat::~InsetFloat()
131 InsetFloatMailer(*this).hideDialog();
135 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
137 switch (cmd.action) {
139 case LFUN_INSET_MODIFY: {
140 InsetFloatParams params;
141 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
142 params_.placement = params.placement;
143 params_.wide = params.wide;
144 params_.sideways = params.sideways;
145 wide(params_.wide, cur.buffer().params());
146 sideways(params_.sideways, cur.buffer().params());
150 case LFUN_INSET_DIALOG_UPDATE: {
151 InsetFloatMailer(*this).updateDialog(&cur.bv());
155 case LFUN_MOUSE_RELEASE: {
156 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
157 InsetFloatMailer(*this).showDialog(&cur.bv());
160 InsetCollapsable::doDispatch(cur, cmd);
165 InsetCollapsable::doDispatch(cur, cmd);
171 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
172 FuncStatus & flag) const
174 switch (cmd.action) {
176 case LFUN_INSET_MODIFY:
177 case LFUN_INSET_DIALOG_UPDATE:
182 return InsetCollapsable::getStatus(cur, cmd, flag);
187 void InsetFloat::updateLabels(Buffer const & buf, ParIterator const & it)
189 Counters & cnts = buf.params().getTextClass().counters();
190 string const saveflt = cnts.current_float();
192 // Tell to captions what the current float is
193 cnts.current_float(params().type);
195 InsetCollapsable::updateLabels(buf, it);
198 cnts.current_float(saveflt);
202 void InsetFloatParams::write(ostream & os) const
204 os << "Float " << type << '\n';
206 if (!placement.empty())
207 os << "placement " << placement << "\n";
212 os << "wide false\n";
215 os << "sideways true\n";
217 os << "sideways false\n";
221 void InsetFloatParams::read(Lexer & lex)
225 if (token == "placement") {
228 // take countermeasures
229 lex.pushToken(token);
232 if (token == "wide") {
235 lyxerr << "InsetFloat::Read:: Missing wide!"
237 // take countermeasures
238 lex.pushToken(token);
241 if (token == "sideways") {
244 lyxerr << "InsetFloat::Read:: Missing sideways!"
246 // take countermeasures
247 lex.pushToken(token);
252 void InsetFloat::write(Buffer const & buf, ostream & os) const
255 InsetCollapsable::write(buf, os);
259 void InsetFloat::read(Buffer const & buf, Lexer & lex)
262 wide(params_.wide, buf.params());
263 sideways(params_.sideways, buf.params());
264 InsetCollapsable::read(buf, lex);
268 void InsetFloat::validate(LaTeXFeatures & features) const
270 if (contains(params_.placement, 'H')) {
271 features.require("float");
274 if (params_.sideways)
275 features.require("rotating");
277 features.useFloat(params_.type);
278 InsetCollapsable::validate(features);
282 auto_ptr<Inset> InsetFloat::doClone() const
284 return auto_ptr<Inset>(new InsetFloat(*this));
288 docstring const InsetFloat::editMessage() const
290 return _("Opened Float Inset");
294 int InsetFloat::latex(Buffer const & buf, odocstream & os,
295 OutputParams const & runparams) const
297 FloatList const & floats = buf.params().getTextClass().floats();
298 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
299 if (params_.sideways) {
300 if (params_.type == "table")
301 tmptype = "sidewaystable";
302 else if (params_.type == "figure")
303 tmptype = "sidewaysfigure";
305 // Figure out the float placement to use.
306 // From lowest to highest:
307 // - float default placement
308 // - document wide default placement
309 // - specific float placement
311 string const buf_placement = buf.params().float_placement;
312 string const def_placement = floats.defaultPlacement(params_.type);
313 if (!params_.placement.empty()
314 && params_.placement != def_placement) {
315 placement = params_.placement;
316 } else if (params_.placement.empty()
317 && !buf_placement.empty()
318 && buf_placement != def_placement) {
319 placement = buf_placement;
322 // The \n is used to force \begin{<floatname>} to appear in a new line.
323 // The % is needed to prevent two consecutive \n chars in the case
324 // when the current output line is empty.
325 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
326 // We only output placement if different from the def_placement.
327 // sidewaysfloats always use their own page
328 if (!placement.empty() && !params_.sideways) {
329 os << '[' << from_ascii(placement) << ']';
333 int const i = InsetText::latex(buf, os, runparams);
335 // The \n is used to force \end{<floatname>} to appear in a new line.
336 // In this case, we do not case if the current output line is empty.
337 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
343 int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
344 OutputParams const & runparams) const
346 os << '[' << buf.B_("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
347 InsetText::plaintext(buf, os, runparams);
350 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
354 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
355 OutputParams const & runparams) const
358 os << '<' << from_ascii(params_.type) << '>';
359 int const i = InsetText::docbook(buf, os, runparams);
360 os << "</" << from_ascii(params_.type) << '>';
366 bool InsetFloat::insetAllowed(Inset::Code code) const
368 return code != Inset::FLOAT_CODE
369 && code != Inset::FOOT_CODE
370 && code != Inset::MARGIN_CODE;
374 bool InsetFloat::showInsetDialog(BufferView * bv) const
376 if (!InsetText::showInsetDialog(bv))
377 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
382 void InsetFloat::wide(bool w, BufferParams const & bp)
385 docstring lab = _("float: ") + floatName(params_.type, bp);
392 void InsetFloat::sideways(bool s, BufferParams const & bp)
394 params_.sideways = s;
395 docstring lab = _("float: ") + floatName(params_.type, bp);
396 if (params_.sideways)
397 lab += _(" (sideways)");
402 string const InsetFloatMailer::name_("float");
404 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
409 string const InsetFloatMailer::inset2string(Buffer const &) const
411 return params2string(inset_.params());
415 void InsetFloatMailer::string2params(string const & in,
416 InsetFloatParams & params)
418 params = InsetFloatParams();
422 istringstream data(in);
428 if (!lex || name != name_)
429 return print_mailer_error("InsetFloatMailer", in, 1, name_);
431 // This is part of the inset proper that is usually swallowed
432 // by Text::readInset
435 if (!lex || id != "Float")
436 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
438 // We have to read the type here!
444 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
447 data << name_ << ' ';