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));
118 FontInfo font = sane_font;
121 font.setColor(Color_collapsable);
127 InsetFloat::~InsetFloat()
129 InsetFloatMailer(*this).hideDialog();
133 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
135 switch (cmd.action) {
137 case LFUN_INSET_MODIFY: {
138 InsetFloatParams params;
139 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
140 params_.placement = params.placement;
141 params_.wide = params.wide;
142 params_.sideways = params.sideways;
143 wide(params_.wide, cur.buffer().params());
144 sideways(params_.sideways, cur.buffer().params());
148 case LFUN_INSET_DIALOG_UPDATE: {
149 InsetFloatMailer(*this).updateDialog(&cur.bv());
153 case LFUN_MOUSE_RELEASE: {
154 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
155 InsetFloatMailer(*this).showDialog(&cur.bv());
158 InsetCollapsable::doDispatch(cur, cmd);
163 InsetCollapsable::doDispatch(cur, cmd);
169 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
170 FuncStatus & flag) const
172 switch (cmd.action) {
174 case LFUN_INSET_MODIFY:
175 case LFUN_INSET_DIALOG_UPDATE:
180 return InsetCollapsable::getStatus(cur, cmd, flag);
185 void InsetFloat::updateLabels(Buffer const & buf, ParIterator const & it)
187 Counters & cnts = buf.params().getTextClass().counters();
188 string const saveflt = cnts.current_float();
190 // Tell to captions what the current float is
191 cnts.current_float(params().type);
193 InsetCollapsable::updateLabels(buf, it);
196 cnts.current_float(saveflt);
200 void InsetFloatParams::write(ostream & os) const
202 os << "Float " << type << '\n';
204 if (!placement.empty())
205 os << "placement " << placement << "\n";
210 os << "wide false\n";
213 os << "sideways true\n";
215 os << "sideways false\n";
219 void InsetFloatParams::read(Lexer & lex)
223 if (token == "placement") {
226 // take countermeasures
227 lex.pushToken(token);
230 if (token == "wide") {
233 lyxerr << "InsetFloat::Read:: Missing wide!"
235 // take countermeasures
236 lex.pushToken(token);
239 if (token == "sideways") {
242 lyxerr << "InsetFloat::Read:: Missing sideways!"
244 // take countermeasures
245 lex.pushToken(token);
250 void InsetFloat::write(Buffer const & buf, ostream & os) const
253 InsetCollapsable::write(buf, os);
257 void InsetFloat::read(Buffer const & buf, Lexer & lex)
260 wide(params_.wide, buf.params());
261 sideways(params_.sideways, buf.params());
262 InsetCollapsable::read(buf, lex);
266 void InsetFloat::validate(LaTeXFeatures & features) const
268 if (support::contains(params_.placement, 'H')) {
269 features.require("float");
272 if (params_.sideways)
273 features.require("rotating");
275 features.useFloat(params_.type);
276 InsetCollapsable::validate(features);
280 Inset * InsetFloat::clone() const
282 return new InsetFloat(*this);
286 docstring const InsetFloat::editMessage() const
288 return _("Opened Float Inset");
292 int InsetFloat::latex(Buffer const & buf, odocstream & os,
293 OutputParams const & runparams) const
295 FloatList const & floats = buf.params().getTextClass().floats();
296 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
297 if (params_.sideways) {
298 if (params_.type == "table")
299 tmptype = "sidewaystable";
300 else if (params_.type == "figure")
301 tmptype = "sidewaysfigure";
303 // Figure out the float placement to use.
304 // From lowest to highest:
305 // - float default placement
306 // - document wide default placement
307 // - specific float placement
309 string const buf_placement = buf.params().float_placement;
310 string const def_placement = floats.defaultPlacement(params_.type);
311 if (!params_.placement.empty()
312 && params_.placement != def_placement) {
313 placement = params_.placement;
314 } else if (params_.placement.empty()
315 && !buf_placement.empty()
316 && buf_placement != def_placement) {
317 placement = buf_placement;
320 // The \n is used to force \begin{<floatname>} to appear in a new line.
321 // The % is needed to prevent two consecutive \n chars in the case
322 // when the current output line is empty.
323 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
324 // We only output placement if different from the def_placement.
325 // sidewaysfloats always use their own page
326 if (!placement.empty() && !params_.sideways) {
327 os << '[' << from_ascii(placement) << ']';
331 int const i = InsetText::latex(buf, os, runparams);
333 // The \n is used to force \end{<floatname>} to appear in a new line.
334 // In this case, we do not case if the current output line is empty.
335 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
341 int InsetFloat::plaintext(Buffer const & buf, odocstream & os,
342 OutputParams const & runparams) const
344 os << '[' << buf.B_("float") << ' ' << floatName(params_.type, buf.params()) << ":\n";
345 InsetText::plaintext(buf, os, runparams);
348 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
352 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
353 OutputParams const & runparams) const
356 os << '<' << from_ascii(params_.type) << '>';
357 int const i = InsetText::docbook(buf, os, runparams);
358 os << "</" << from_ascii(params_.type) << '>';
364 bool InsetFloat::insetAllowed(InsetCode code) const
366 return code != FLOAT_CODE
368 && code != MARGIN_CODE;
372 bool InsetFloat::showInsetDialog(BufferView * bv) const
374 if (!InsetText::showInsetDialog(bv))
375 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
380 void InsetFloat::wide(bool w, BufferParams const & bp)
383 docstring lab = _("float: ") + floatName(params_.type, bp);
390 void InsetFloat::sideways(bool s, BufferParams const & bp)
392 params_.sideways = s;
393 docstring lab = _("float: ") + floatName(params_.type, bp);
394 if (params_.sideways)
395 lab += _(" (sideways)");
400 string const InsetFloatMailer::name_("float");
402 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
407 string const InsetFloatMailer::inset2string(Buffer const &) const
409 return params2string(inset_.params());
413 void InsetFloatMailer::string2params(string const & in,
414 InsetFloatParams & params)
416 params = InsetFloatParams();
420 istringstream data(in);
426 if (!lex || name != name_)
427 return print_mailer_error("InsetFloatMailer", in, 1, name_);
429 // This is part of the inset proper that is usually swallowed
430 // by Text::readInset
433 if (!lex || id != "Float")
434 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
436 // We have to read the type here!
442 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
445 data << name_ << ' ';