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 : InsetCaptionable(buf)
118 string2params(params_str, params_);
119 setCaptionType(params_.type);
123 // Enforce equality of float type and caption type.
124 void InsetFloat::setCaptionType(std::string const & type)
126 InsetCaptionable::setCaptionType(type);
127 params_.type = captionType();
128 // check if the float type exists
129 if (buffer().params().documentClass().floats().typeExist(params_.type))
130 setLabel(_("float: ") + floatName(params_.type));
132 setLabel(bformat(_("ERROR: Unknown float type: %1$s"), from_utf8(params_.type)));
136 docstring InsetFloat::layoutName() const
138 return "Float:" + from_utf8(params_.type);
142 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
145 return InsetCaptionable::toolTip(bv, x, y);
147 OutputParams rp(&buffer().params().encoding());
148 return getCaptionText(rp);
152 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
154 switch (cmd.action()) {
156 case LFUN_INSET_MODIFY: {
157 InsetFloatParams params;
158 string2params(to_utf8(cmd.argument()), params);
159 cur.recordUndoInset(this);
161 // placement, wide and sideways are not used for subfloats
162 if (!params_.subfloat) {
163 params_.placement = params.placement;
164 params_.wide = params.wide;
165 params_.sideways = params.sideways;
168 if (params_.type != params.type)
169 setCaptionType(params.type);
170 // what we really want here is a TOC update, but that means
171 // a full buffer update
172 cur.forceBufferUpdate();
176 case LFUN_INSET_DIALOG_UPDATE: {
177 cur.bv().updateDialog("float", params2string(params()));
182 InsetCaptionable::doDispatch(cur, cmd);
188 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
189 FuncStatus & flag) const
191 switch (cmd.action()) {
193 case LFUN_INSET_MODIFY:
194 case LFUN_INSET_DIALOG_UPDATE:
195 flag.setEnabled(true);
198 case LFUN_INSET_SETTINGS:
199 if (InsetCaptionable::getStatus(cur, cmd, flag)) {
200 flag.setEnabled(flag.enabled() && !params_.subfloat);
205 case LFUN_NEWLINE_INSERT:
206 if (params_.subfloat) {
207 flag.setEnabled(false);
212 return InsetCaptionable::getStatus(cur, cmd, flag);
217 bool InsetFloat::hasSubCaptions(ParIterator const & it) const
219 return (it.innerInsetOfType(FLOAT_CODE) || it.innerInsetOfType(WRAP_CODE));
223 void InsetFloatParams::write(ostream & os) const
226 // Better this than creating a parse error. This in fact happens in the
227 // parameters dialog via InsetFloatParams::params2string.
228 os << "senseless" << '\n';
232 if (!placement.empty())
233 os << "placement " << placement << "\n";
238 os << "wide false\n";
241 os << "sideways true\n";
243 os << "sideways false\n";
247 void InsetFloatParams::read(Lexer & lex)
249 lex.setContext("InsetFloatParams::read");
251 if (lex.checkFor("placement"))
253 lex >> "wide" >> wide;
254 lex >> "sideways" >> sideways;
258 void InsetFloat::write(ostream & os) const
262 InsetCaptionable::write(os);
266 void InsetFloat::read(Lexer & lex)
269 InsetCaptionable::read(lex);
270 setCaptionType(params_.type);
274 void InsetFloat::validate(LaTeXFeatures & features) const
276 if (support::contains(params_.placement, 'H'))
277 features.require("float");
279 if (params_.sideways)
280 features.require("rotfloat");
282 if (features.inFloat())
283 features.require("subfig");
285 features.useFloat(params_.type, features.inFloat());
286 features.inFloat(true);
287 InsetCaptionable::validate(features);
288 features.inFloat(false);
292 docstring InsetFloat::xhtml(XHTMLStream & xs, OutputParams const & rp) const
294 FloatList const & floats = buffer().params().documentClass().floats();
295 Floating const & ftype = floats.getType(params_.type);
296 string const & htmltype = ftype.htmlTag();
297 string const & attr = ftype.htmlAttrib();
299 odocstringstream ods;
300 XHTMLStream newxs(ods);
301 newxs << html::StartTag(htmltype, attr);
302 InsetText::XHTMLOptions const opts =
303 InsetText::WriteLabel | InsetText::WriteInnerTag;
304 docstring deferred = InsetText::insetAsXHTML(newxs, rp, opts);
305 newxs << html::EndTag(htmltype);
307 if (rp.inFloat == OutputParams::NONFLOAT)
308 // In this case, this float needs to be deferred, but we'll put it
309 // before anything the text itself deferred.
310 deferred = ods.str() + '\n' + deferred;
312 // In this case, the whole thing is already being deferred, so
313 // we can write to the stream.
314 // Note that things will already have been escaped, so we do not
315 // want to escape them again.
316 xs << XHTMLStream::ESCAPE_NONE << ods.str();
321 void InsetFloat::latex(otexstream & os, OutputParams const & runparams_in) const
323 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
324 if (runparams_in.moving_arg)
328 OutputParams rp = runparams_in;
329 rp.moving_arg = true;
330 docstring const caption = getCaption(rp);
331 if (!caption.empty()) {
335 // The main argument is the contents of the float. This is not a moving argument.
336 rp.moving_arg = false;
337 rp.inFloat = OutputParams::SUBFLOAT;
338 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 && floats.allowsSideways(params_.type))
349 tmptype = "sideways" + params_.type;
350 if (params_.wide && floats.allowsWide(params_.type)
351 && (!params_.sideways ||
352 params_.type == "figure" ||
353 params_.type == "table"))
355 // Figure out the float placement to use.
356 // From lowest to highest:
357 // - float default placement
358 // - document wide default placement
359 // - specific float placement
361 string const buf_placement = buffer().params().float_placement;
362 string const def_placement = floats.defaultPlacement(params_.type);
363 if (!params_.placement.empty()
364 && params_.placement != def_placement) {
365 tmpplacement = params_.placement;
366 } else if (params_.placement.empty()
367 && !buf_placement.empty()
368 && buf_placement != def_placement) {
369 tmpplacement = buf_placement;
372 // Check if placement is allowed by this float
373 string const allowed_placement =
374 floats.allowedPlacement(params_.type);
376 string::const_iterator lit = tmpplacement.begin();
377 string::const_iterator end = tmpplacement.end();
378 for (; lit != end; ++lit) {
379 if (contains(allowed_placement, *lit))
383 // Force \begin{<floatname>} to appear in a new line.
384 os << breakln << "\\begin{" << from_ascii(tmptype) << '}';
385 if (runparams.lastid != -1)
386 os.texrow().start(runparams.lastid, runparams.lastpos);
387 // We only output placement if different from the def_placement.
388 // sidewaysfloats always use their own page
389 if (!placement.empty() && !params_.sideways)
390 os << '[' << from_ascii(placement) << ']';
393 InsetText::latex(os, runparams);
395 // Force \end{<floatname>} to appear in a new line.
396 os << breakln << "\\end{" << from_ascii(tmptype) << "}\n";
400 int InsetFloat::plaintext(odocstringstream & os, OutputParams const & runparams, size_t max_length) const
402 os << '[' << buffer().B_("float") << ' '
403 << floatName(params_.type) << ":\n";
404 InsetText::plaintext(os, runparams, max_length);
407 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
411 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
413 // FIXME Implement subfloat!
415 os << '<' << from_ascii(params_.type) << '>';
416 int const i = InsetText::docbook(os, runparams);
417 os << "</" << from_ascii(params_.type) << '>';
423 bool InsetFloat::insetAllowed(InsetCode code) const
425 // The case that code == FLOAT_CODE is handled in Text3.cpp,
426 // because we need to know what type of float is meant.
433 return InsetCaptionable::insetAllowed(code);
438 void InsetFloat::setWide(bool w, bool update_label)
440 if (!buffer().params().documentClass().floats().allowsWide(params_.type))
441 params_.wide = false;
449 void InsetFloat::setSideways(bool s, bool update_label)
451 if (!buffer().params().documentClass().floats().allowsSideways(params_.type))
452 params_.sideways = false;
454 params_.sideways = s;
460 void InsetFloat::setSubfloat(bool s, bool update_label)
462 params_.subfloat = s;
468 void InsetFloat::setNewLabel()
470 docstring lab = _("float: ");
472 if (params_.subfloat)
473 lab = _("subfloat: ");
475 lab += floatName(params_.type);
477 FloatList const & floats = buffer().params().documentClass().floats();
479 if (params_.wide && floats.allowsWide(params_.type))
482 if (params_.sideways && floats.allowsSideways(params_.type))
483 lab += _(" (sideways)");
489 bool InsetFloat::allowsCaptionVariation(std::string const & newtype) const
491 return !params_.subfloat && newtype != "LongTableNoNumber";
495 docstring InsetFloat::getCaption(OutputParams const & runparams) const
497 if (paragraphs().empty())
500 InsetCaption const * ins = getCaptionInset();
505 odocstringstream ods;
506 otexstream os(ods, texrow);
507 ins->getArgs(os, runparams);
509 odocstringstream odss;
510 otexstream oss(odss, texrow);
511 ins->getArgument(oss, runparams);
512 docstring arg = odss.str();
514 if (arg.find(']') != docstring::npos)
515 arg = '{' + arg + '}';
522 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
524 params = InsetFloatParams();
528 istringstream data(in);
531 lex.setContext("InsetFloat::string2params");
536 string InsetFloat::params2string(InsetFloatParams const & params)