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
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "InsetFloat.h"
16 #include "InsetCaption.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
23 #include "DispatchResult.h"
25 #include "FloatList.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "InsetList.h"
29 #include "LaTeXFeatures.h"
31 #include "OutputParams.h"
32 #include "ParIterator.h"
33 #include "TextClass.h"
35 #include "support/debug.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/docstream.h"
39 #include "support/convert.h"
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(Buffer const & buf, string const & type)
115 : InsetCollapsable(buf), name_(from_utf8(type))
117 setLabel(_("float: ") + floatName(type, buf.params()));
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 params_.subfloat = params.subfloat;
139 wide(params_.wide, cur.buffer().params());
140 sideways(params_.sideways, cur.buffer().params());
144 case LFUN_INSET_DIALOG_UPDATE: {
145 InsetFloatMailer(*this).updateDialog(&cur.bv());
150 InsetCollapsable::doDispatch(cur, cmd);
156 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
157 FuncStatus & flag) const
159 switch (cmd.action) {
161 case LFUN_INSET_MODIFY:
162 case LFUN_INSET_DIALOG_UPDATE:
167 return InsetCollapsable::getStatus(cur, cmd, flag);
172 void InsetFloat::updateLabels(ParIterator const & it)
174 Counters & cnts = buffer().params().documentClass().counters();
175 string const saveflt = cnts.current_float();
176 bool const savesubflt = cnts.isSubfloat();
178 bool const subflt = it.innerInsetOfType(FLOAT_CODE);
179 // floats can only embed subfloats of their own kind
181 params_.type = saveflt;
182 subfloat(subflt, buffer().params());
184 // Tell to captions what the current float is
185 cnts.current_float(params().type);
186 cnts.isSubfloat(subflt);
188 InsetCollapsable::updateLabels(it);
191 cnts.current_float(saveflt);
192 cnts.isSubfloat(savesubflt);
196 void InsetFloatParams::write(ostream & os) const
198 os << "Float " << type << '\n';
200 if (!placement.empty())
201 os << "placement " << placement << "\n";
206 os << "wide false\n";
209 os << "sideways true\n";
211 os << "sideways false\n";
215 void InsetFloatParams::read(Lexer & lex)
219 if (token == "placement") {
222 // take countermeasures
223 lex.pushToken(token);
226 if (token == "wide") {
229 lyxerr << "InsetFloat::Read:: Missing wide!"
231 // take countermeasures
232 lex.pushToken(token);
235 if (token == "sideways") {
238 lyxerr << "InsetFloat::Read:: Missing sideways!"
240 // take countermeasures
241 lex.pushToken(token);
246 void InsetFloat::write(ostream & os) const
249 InsetCollapsable::write(os);
253 void InsetFloat::read(Lexer & lex)
256 wide(params_.wide, buffer().params());
257 sideways(params_.sideways, buffer().params());
258 subfloat(params_.subfloat, buffer().params());
259 InsetCollapsable::read(lex);
263 void InsetFloat::validate(LaTeXFeatures & features) const
265 if (support::contains(params_.placement, 'H'))
266 features.require("float");
268 if (params_.sideways)
269 features.require("rotfloat");
271 if (params_.subfloat)
272 features.require("subfig");
274 features.useFloat(params_.type, params_.subfloat);
275 InsetCollapsable::validate(features);
279 docstring InsetFloat::editMessage() const
281 return _("Opened Float Inset");
285 int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
287 if (params_.subfloat) {
288 if (runparams.moving_arg)
292 OutputParams rp = runparams;
293 docstring const caption = getCaption(rp);
294 if (!caption.empty()) {
298 int const i = InsetText::latex(os, runparams);
304 FloatList const & floats = buffer().params().documentClass().floats();
305 string tmptype = params_.type;
306 if (params_.sideways)
307 tmptype = "sideways" + params_.type;
308 if (params_.wide && (!params_.sideways ||
309 params_.type == "figure" ||
310 params_.type == "table"))
312 // Figure out the float placement to use.
313 // From lowest to highest:
314 // - float default placement
315 // - document wide default placement
316 // - specific float placement
318 string const buf_placement = buffer().params().float_placement;
319 string const def_placement = floats.defaultPlacement(params_.type);
320 if (!params_.placement.empty()
321 && params_.placement != def_placement) {
322 placement = params_.placement;
323 } else if (params_.placement.empty()
324 && !buf_placement.empty()
325 && buf_placement != def_placement) {
326 placement = buf_placement;
329 // The \n is used to force \begin{<floatname>} to appear in a new line.
330 // The % is needed to prevent two consecutive \n chars in the case
331 // when the current output line is empty.
332 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
333 // We only output placement if different from the def_placement.
334 // sidewaysfloats always use their own page
335 if (!placement.empty() && !params_.sideways) {
336 os << '[' << from_ascii(placement) << ']';
340 int const i = InsetText::latex(os, runparams);
342 // The \n is used to force \end{<floatname>} to appear in a new line.
343 // In this case, we do not case if the current output line is empty.
344 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
350 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
352 os << '[' << buffer().B_("float") << ' '
353 << floatName(params_.type, buffer().params()) << ":\n";
354 InsetText::plaintext(os, runparams);
357 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
361 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
363 // FIXME Implement subfloat!
365 os << '<' << from_ascii(params_.type) << '>';
366 int const i = InsetText::docbook(os, runparams);
367 os << "</" << from_ascii(params_.type) << '>';
373 bool InsetFloat::insetAllowed(InsetCode code) const
375 return code != FOOT_CODE
376 && code != MARGIN_CODE
377 && (code != FLOAT_CODE || !params_.subfloat);
381 bool InsetFloat::showInsetDialog(BufferView * bv) const
383 if (!InsetText::showInsetDialog(bv))
384 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
389 void InsetFloat::wide(bool w, BufferParams const & bp)
392 docstring lab = _("float: ") + floatName(params_.type, bp);
399 void InsetFloat::sideways(bool s, BufferParams const & bp)
401 params_.sideways = s;
402 docstring lab = _("float: ") + floatName(params_.type, bp);
403 if (params_.sideways)
404 lab += _(" (sideways)");
409 void InsetFloat::subfloat(bool s, BufferParams const & bp)
411 params_.subfloat = s;
412 docstring lab = _("float: ") + floatName(params_.type, bp);
414 lab = _("subfloat: ") + floatName(params_.type, bp);
419 docstring InsetFloat::getCaption(OutputParams const & runparams) const
421 if (paragraphs().empty())
424 ParagraphList::const_iterator pit = paragraphs().begin();
425 for (; pit != paragraphs().end(); ++pit) {
426 InsetList::const_iterator it = pit->insetList().begin();
427 for (; it != pit->insetList().end(); ++it) {
428 Inset & inset = *it->inset;
429 if (inset.lyxCode() == CAPTION_CODE) {
430 odocstringstream ods;
432 static_cast<InsetCaption *>(it->inset);
433 ins->getOptArg(ods, runparams);
435 ins->getArgument(ods, runparams);
445 string const InsetFloatMailer::name_("float");
447 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
452 string const InsetFloatMailer::inset2string(Buffer const &) const
454 return params2string(inset_.params());
458 void InsetFloatMailer::string2params(string const & in,
459 InsetFloatParams & params)
461 params = InsetFloatParams();
465 istringstream data(in);
471 if (!lex || name != name_)
472 return print_mailer_error("InsetFloatMailer", in, 1, name_);
474 // This is part of the inset proper that is usually swallowed
475 // by Text::readInset
478 if (!lex || id != "Float")
479 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
481 // We have to read the type here!
487 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
490 data << name_ << ' ';