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 "LaTeXFeatures.h"
30 #include "output_xhtml.h"
31 #include "ParIterator.h"
32 #include "TextClass.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
39 #include "frontends/Application.h"
42 using namespace lyx::support;
47 // With this inset it will be possible to support the latex package
48 // float.sty, and I am sure that with this and some additional support
49 // classes we can support similar functionality in other formats
51 // By using float.sty we will have the same handling for all floats, both
52 // for those already in existance (table and figure) and all user created
53 // ones¹. So suddenly we give the users the possibility of creating new
54 // kinds of floats on the fly. (and with a uniform look)
57 // \newfloat{type}{placement}{ext}[within]
58 // type - The "type" of the new class of floats, like program or
59 // algorithm. After the appropriate \newfloat, commands
60 // such as \begin{program} or \end{algorithm*} will be
62 // placement - The default placement for the given class of floats.
63 // They are like in standard LaTeX: t, b, p and h for top,
64 // bottom, page, and here, respectively. On top of that
65 // there is a new type, H, which does not really correspond
66 // to a float, since it means: put it "here" and nowhere else.
67 // Note, however that the H specifier is special and, because
68 // of implementation details cannot be used in the second
69 // argument of \newfloat.
70 // ext - The file name extension of an auxiliary file for the list
71 // of figures (or whatever). LaTeX writes the captions to
73 // within - This (optional) argument determines whether floats of this
74 // class will be numbered within some sectional unit of the
75 // document. For example, if within is equal to chapter, the
76 // floats will be numbered within chapters.
78 // style - plain, boxed, ruled
79 // \floatname{float}{floatname}
82 // \floatplacement{float}{placement}
85 // \restylefloat{float}
87 // \listof{type}{title}
90 // ¹ the algorithm float is defined using the float.sty package. Like this
92 // \newfloat{algorithm}{htbp}{loa}[<sect>]
93 // \floatname{algorithm}{Algorithm}
95 // The intention is that floats should be definable from two places:
97 // - the "gui" (i.e. by the user)
100 // This should only be done for floats defined in a documentclass and that
101 // does not need any additional packages. The two most known floats in this
102 // category is "table" and "figure". Floats defined in layout files are only
103 // stored in lyx files if the user modifies them.
106 // There should be a gui dialog (and also a collection of lyxfuncs) where
107 // the user can modify existing floats and/or create new ones.
109 // The individual floats will also have some settable
110 // variables: wide and placement.
114 //FIXME: why do we set in stone the type here?
115 InsetFloat::InsetFloat(Buffer * buf, string params_str)
116 : InsetCollapsable(buf)
118 string2params(params_str, params_);
122 docstring InsetFloat::name() const
124 return "Float:" + from_utf8(params_.type);
128 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
130 if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
133 OutputParams rp(&buffer().params().encoding());
134 return getCaptionText(rp);
138 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
140 switch (cmd.action()) {
142 case LFUN_INSET_MODIFY: {
143 InsetFloatParams params;
144 string2params(to_utf8(cmd.argument()), params);
146 // placement, wide and sideways are not used for subfloats
147 if (!params_.subfloat) {
148 params_.placement = params.placement;
149 params_.wide = params.wide;
150 params_.sideways = params.sideways;
153 if (params_.type != params.type) {
154 params_.type = params.type;
155 buffer().updateBuffer();
160 case LFUN_INSET_DIALOG_UPDATE: {
161 cur.bv().updateDialog("float", params2string(params()));
166 InsetCollapsable::doDispatch(cur, cmd);
172 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
173 FuncStatus & flag) const
175 switch (cmd.action()) {
177 case LFUN_INSET_MODIFY:
178 case LFUN_INSET_DIALOG_UPDATE:
179 flag.setEnabled(true);
182 case LFUN_INSET_SETTINGS:
183 if (InsetCollapsable::getStatus(cur, cmd, flag)) {
184 flag.setEnabled(flag.enabled() && !params_.subfloat);
190 return InsetCollapsable::getStatus(cur, cmd, flag);
195 void InsetFloat::updateBuffer(ParIterator const & it, UpdateType utype)
198 buffer().masterBuffer()->params().documentClass().counters();
199 if (utype == OutputUpdate) {
200 // counters are local to the float
201 cnts.saveLastCounter();
203 string const saveflt = cnts.current_float();
204 bool const savesubflt = cnts.isSubfloat();
206 bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
207 || it.innerInsetOfType(WRAP_CODE));
208 // floats can only embed subfloats of their own kind
210 params_.type = saveflt;
213 // Tell to captions what the current float is
214 cnts.current_float(params().type);
215 cnts.isSubfloat(subflt);
217 InsetCollapsable::updateBuffer(it, utype);
220 cnts.current_float(saveflt);
221 if (utype == OutputUpdate)
222 cnts.restoreLastCounter();
223 cnts.isSubfloat(savesubflt);
227 void InsetFloatParams::write(ostream & os) const
231 if (!placement.empty())
232 os << "placement " << placement << "\n";
237 os << "wide false\n";
240 os << "sideways true\n";
242 os << "sideways false\n";
246 void InsetFloatParams::read(Lexer & lex)
248 lex.setContext("InsetFloatParams::read");
250 if (lex.checkFor("placement"))
252 lex >> "wide" >> wide;
253 lex >> "sideways" >> sideways;
257 void InsetFloat::write(ostream & os) const
261 InsetCollapsable::write(os);
265 void InsetFloat::read(Lexer & lex)
268 InsetCollapsable::read(lex);
269 // check if the float type exists
270 if (buffer().params().documentClass().floats().typeExist(params_.type))
271 setLabel(_("float: ") + floatName(params_.type));
273 setLabel(bformat(_("ERROR: Unknown float type: %1$s"), from_utf8(params_.type)));
277 void InsetFloat::validate(LaTeXFeatures & features) const
279 if (support::contains(params_.placement, 'H'))
280 features.require("float");
282 if (params_.sideways)
283 features.require("rotfloat");
285 if (features.inFloat())
286 features.require("subfig");
288 features.useFloat(params_.type, features.inFloat());
289 features.inFloat(true);
290 InsetCollapsable::validate(features);
291 features.inFloat(false);
295 docstring InsetFloat::xhtml(XHTMLStream & xs, OutputParams const & rp) const
297 FloatList const & floats = buffer().params().documentClass().floats();
298 Floating const & ftype = floats.getType(params_.type);
299 string const & htmltype = ftype.htmlTag();
300 string const & attr = ftype.htmlAttrib();
302 odocstringstream ods;
303 XHTMLStream newxs(ods);
304 newxs << html::StartTag(htmltype, attr);
305 InsetText::XHTMLOptions const opts =
306 InsetText::WriteLabel | InsetText::WriteInnerTag;
307 docstring deferred = InsetText::insetAsXHTML(newxs, rp, opts);
308 newxs << html::EndTag(htmltype);
310 if (rp.inFloat == OutputParams::NONFLOAT)
311 // In this case, this float needs to be deferred, but we'll put it
312 // before anything the text itself deferred.
313 deferred = ods.str() + '\n' + deferred;
315 // In this case, the whole thing is already being deferred, so
316 // we can write to the stream.
317 // Note that things will already have been escaped, so we do not
318 // want to escape them again.
319 xs << XHTMLStream::NextRaw() << ods.str();
324 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
326 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
327 if (runparams_in.moving_arg)
331 OutputParams rp = runparams_in;
332 docstring const caption = getCaption(rp);
333 if (!caption.empty()) {
337 rp.inFloat = OutputParams::SUBFLOAT;
338 int const i = InsetText::latex(os, rp);
343 OutputParams runparams(runparams_in);
344 runparams.inFloat = OutputParams::MAINFLOAT;
346 FloatList const & floats = buffer().params().documentClass().floats();
347 string tmptype = params_.type;
348 if (params_.sideways)
349 tmptype = "sideways" + params_.type;
350 if (params_.wide && (!params_.sideways ||
351 params_.type == "figure" ||
352 params_.type == "table"))
354 // Figure out the float placement to use.
355 // From lowest to highest:
356 // - float default placement
357 // - document wide default placement
358 // - specific float placement
360 string const buf_placement = buffer().params().float_placement;
361 string const def_placement = floats.defaultPlacement(params_.type);
362 if (!params_.placement.empty()
363 && params_.placement != def_placement) {
364 placement = params_.placement;
365 } else if (params_.placement.empty()
366 && !buf_placement.empty()
367 && buf_placement != def_placement) {
368 placement = buf_placement;
371 // The \n is used to force \begin{<floatname>} to appear in a new line.
372 // The % is needed to prevent two consecutive \n chars in the case
373 // when the current output line is empty.
374 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
375 // We only output placement if different from the def_placement.
376 // sidewaysfloats always use their own page
377 if (!placement.empty() && !params_.sideways) {
378 os << '[' << from_ascii(placement) << ']';
382 int const i = InsetText::latex(os, runparams);
384 // The \n is used to force \end{<floatname>} to appear in a new line.
385 // In this case, we do not case if the current output line is empty.
386 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
392 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
394 os << '[' << buffer().B_("float") << ' '
395 << floatName(params_.type) << ":\n";
396 InsetText::plaintext(os, runparams);
399 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
403 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
405 // FIXME Implement subfloat!
407 os << '<' << from_ascii(params_.type) << '>';
408 int const i = InsetText::docbook(os, runparams);
409 os << "</" << from_ascii(params_.type) << '>';
415 bool InsetFloat::insetAllowed(InsetCode code) const
417 // The case that code == FLOAT_CODE is handled in Text3.cpp,
418 // because we need to know what type of float is meant.
425 return InsetCollapsable::insetAllowed(code);
430 void InsetFloat::setWide(bool w, bool update_label)
438 void InsetFloat::setSideways(bool s, bool update_label)
440 params_.sideways = s;
446 void InsetFloat::setSubfloat(bool s, bool update_label)
448 params_.subfloat = s;
454 void InsetFloat::setNewLabel()
456 docstring lab = _("float: ");
458 if (params_.subfloat)
459 lab = _("subfloat: ");
461 lab += floatName(params_.type);
466 if (params_.sideways)
467 lab += _(" (sideways)");
473 docstring InsetFloat::getCaption(OutputParams const & runparams) const
475 if (paragraphs().empty())
478 InsetCaption const * ins = getCaptionInset();
482 odocstringstream ods;
483 ins->getOptArg(ods, runparams);
485 ins->getArgument(ods, runparams);
491 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
493 params = InsetFloatParams();
497 istringstream data(in);
500 lex.setContext("InsetFloat::string2params");
505 string InsetFloat::params2string(InsetFloatParams const & params)