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.
16 #include "InsetFloat.h"
17 #include "InsetCaption.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
24 #include "DispatchResult.h"
26 #include "FloatList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
31 #include "output_xhtml.h"
32 #include "ParIterator.h"
34 #include "texstream.h"
35 #include "TextClass.h"
36 #include "InsetList.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
43 #include "frontends/Application.h"
46 using namespace lyx::support;
51 // With this inset it will be possible to support the latex package
52 // float.sty, and I am sure that with this and some additional support
53 // classes we can support similar functionality in other formats
55 // By using float.sty we will have the same handling for all floats, both
56 // for those already in existance (table and figure) and all user created
57 // ones¹. So suddenly we give the users the possibility of creating new
58 // kinds of floats on the fly. (and with a uniform look)
61 // \newfloat{type}{placement}{ext}[within]
62 // type - The "type" of the new class of floats, like program or
63 // algorithm. After the appropriate \newfloat, commands
64 // such as \begin{program} or \end{algorithm*} will be
66 // placement - The default placement for the given class of floats.
67 // They are like in standard LaTeX: t, b, p and h for top,
68 // bottom, page, and here, respectively. On top of that
69 // there is a new type, H, which does not really correspond
70 // to a float, since it means: put it "here" and nowhere else.
71 // Note, however that the H specifier is special and, because
72 // of implementation details cannot be used in the second
73 // argument of \newfloat.
74 // ext - The file name extension of an auxiliary file for the list
75 // of figures (or whatever). LaTeX writes the captions to
77 // within - This (optional) argument determines whether floats of this
78 // class will be numbered within some sectional unit of the
79 // document. For example, if within is equal to chapter, the
80 // floats will be numbered within chapters.
82 // style - plain, boxed, ruled
83 // \floatname{float}{floatname}
86 // \floatplacement{float}{placement}
89 // \restylefloat{float}
91 // \listof{type}{title}
94 // ¹ the algorithm float is defined using the float.sty package. Like this
96 // \newfloat{algorithm}{htbp}{loa}[<sect>]
97 // \floatname{algorithm}{Algorithm}
99 // The intention is that floats should be definable from two places:
101 // - the "gui" (i.e. by the user)
103 // From layout files.
104 // This should only be done for floats defined in a documentclass and that
105 // does not need any additional packages. The two most known floats in this
106 // category is "table" and "figure". Floats defined in layout files are only
107 // stored in lyx files if the user modifies them.
110 // There should be a gui dialog (and also a collection of lyxfuncs) where
111 // the user can modify existing floats and/or create new ones.
113 // The individual floats will also have some settable
114 // variables: wide and placement.
118 //FIXME: why do we set in stone the type here?
119 InsetFloat::InsetFloat(Buffer * buf, string const & params_str)
120 : InsetCaptionable(buf)
122 string2params(params_str, params_);
123 setCaptionType(params_.type);
127 // Enforce equality of float type and caption type.
128 void InsetFloat::setCaptionType(std::string const & type)
130 InsetCaptionable::setCaptionType(type);
131 params_.type = captionType();
132 // check if the float type exists
133 if (buffer().params().documentClass().floats().typeExist(params_.type))
136 setLabel(bformat(_("ERROR: Unknown float type: %1$s"), from_utf8(params_.type)));
140 docstring InsetFloat::layoutName() const
142 return "Float:" + from_utf8(params_.type);
146 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
149 return InsetCaptionable::toolTip(bv, x, y);
151 OutputParams rp(&buffer().params().encoding());
152 return getCaptionText(rp);
156 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
158 switch (cmd.action()) {
160 case LFUN_INSET_MODIFY: {
161 InsetFloatParams params;
162 string2params(to_utf8(cmd.argument()), params);
163 cur.recordUndoInset(this);
165 // placement, wide and sideways are not used for subfloats
166 if (!params_.subfloat) {
167 params_.placement = params.placement;
168 params_.wide = params.wide;
169 params_.sideways = params.sideways;
171 params_.alignment = params.alignment;
173 if (params_.type != params.type)
174 setCaptionType(params.type);
175 // what we really want here is a TOC update, but that means
176 // a full buffer update
177 cur.forceBufferUpdate();
181 case LFUN_INSET_DIALOG_UPDATE: {
182 cur.bv().updateDialog("float", params2string(params()));
187 InsetCaptionable::doDispatch(cur, cmd);
193 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
194 FuncStatus & flag) const
196 switch (cmd.action()) {
198 case LFUN_INSET_MODIFY:
199 case LFUN_INSET_DIALOG_UPDATE:
200 flag.setEnabled(true);
203 case LFUN_INSET_SETTINGS:
204 if (InsetCaptionable::getStatus(cur, cmd, flag)) {
205 flag.setEnabled(flag.enabled() && !params_.subfloat);
210 case LFUN_NEWLINE_INSERT:
211 if (params_.subfloat) {
212 flag.setEnabled(false);
219 return InsetCaptionable::getStatus(cur, cmd, flag);
224 bool InsetFloat::hasSubCaptions(ParIterator const & it) const
226 return (it.innerInsetOfType(FLOAT_CODE) || it.innerInsetOfType(WRAP_CODE));
230 string InsetFloat::getAlignment() const
233 string const buf_alignment = buffer().params().float_alignment;
234 if (params_.alignment == "document"
235 && !buf_alignment.empty()) {
236 alignment = buf_alignment;
237 } else if (!params_.alignment.empty()
238 && params_.alignment != "class"
239 && params_.alignment != "document") {
240 alignment = params_.alignment;
246 LyXAlignment InsetFloat::contentAlignment() const
248 LyXAlignment align = LYX_ALIGN_NONE;
249 string alignment = getAlignment();
250 if (alignment == "left")
251 align = LYX_ALIGN_LEFT;
252 else if (alignment == "center")
253 align = LYX_ALIGN_CENTER;
254 else if (alignment == "right")
255 align = LYX_ALIGN_RIGHT;
261 void InsetFloatParams::write(ostream & os) const
264 // Better this than creating a parse error. This in fact happens in the
265 // parameters dialog via InsetFloatParams::params2string.
266 os << "senseless" << '\n';
270 if (!placement.empty())
271 os << "placement " << placement << "\n";
272 if (!alignment.empty())
273 os << "alignment " << alignment << "\n";
278 os << "wide false\n";
281 os << "sideways true\n";
283 os << "sideways false\n";
287 void InsetFloatParams::read(Lexer & lex)
289 lex.setContext("InsetFloatParams::read");
291 if (lex.checkFor("placement"))
293 if (lex.checkFor("alignment"))
295 lex >> "wide" >> wide;
296 lex >> "sideways" >> sideways;
300 void InsetFloat::write(ostream & os) const
304 InsetCaptionable::write(os);
308 void InsetFloat::read(Lexer & lex)
311 InsetCaptionable::read(lex);
312 setCaptionType(params_.type);
316 void InsetFloat::validate(LaTeXFeatures & features) const
318 if (support::contains(params_.placement, 'H'))
319 features.require("float");
321 if (params_.sideways)
322 features.require("rotfloat");
324 if (features.inFloat())
325 features.require("subfig");
327 if (features.inDeletedInset()) {
328 features.require("tikz");
329 features.require("ct-tikz-object-sout");
332 features.useFloat(params_.type, features.inFloat());
333 features.inFloat(true);
334 InsetCaptionable::validate(features);
335 features.inFloat(false);
339 docstring InsetFloat::xhtml(XMLStream & xs, OutputParams const & rp) const
341 FloatList const & floats = buffer().params().documentClass().floats();
342 Floating const & ftype = floats.getType(params_.type);
343 string const & htmltype = ftype.htmlTag();
344 string const & attr = ftype.htmlAttrib();
346 odocstringstream ods;
347 XMLStream newxs(ods);
348 newxs << xml::StartTag(htmltype, attr);
349 InsetText::XHTMLOptions const opts =
350 InsetText::WriteLabel | InsetText::WriteInnerTag;
351 docstring deferred = InsetText::insetAsXHTML(newxs, rp, opts);
352 newxs << xml::EndTag(htmltype);
354 if (rp.inFloat == OutputParams::NONFLOAT) {
355 // In this case, this float needs to be deferred, but we'll put it
356 // before anything the text itself deferred.
357 deferred = ods.str() + '\n' + deferred;
359 // In this case, the whole thing is already being deferred, so
360 // we can write to the stream.
361 // Note that things will already have been escaped, so we do not
362 // want to escape them again.
363 xs << XMLStream::ESCAPE_NONE << ods.str();
369 void InsetFloat::latex(otexstream & os, OutputParams const & runparams_in) const
371 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
372 if (!paragraphs().empty() && !runparams_in.nice)
373 // improve TexRow precision in non-nice mode
376 if (runparams_in.moving_arg)
380 OutputParams rp = runparams_in;
381 rp.moving_arg = true;
382 os << getCaption(rp);
384 // The main argument is the contents of the float. This is not a moving argument.
385 rp.moving_arg = false;
386 rp.inFloat = OutputParams::SUBFLOAT;
387 InsetText::latex(os, rp);
392 OutputParams runparams(runparams_in);
393 runparams.inFloat = OutputParams::MAINFLOAT;
395 FloatList const & floats = buffer().params().documentClass().floats();
396 string tmptype = params_.type;
397 if (params_.sideways && floats.allowsSideways(params_.type))
398 tmptype = "sideways" + params_.type;
399 if (params_.wide && floats.allowsWide(params_.type)
400 && (!params_.sideways ||
401 params_.type == "figure" ||
402 params_.type == "table"))
404 // Figure out the float placement to use.
405 // From lowest to highest:
406 // - float default placement
407 // - document wide default placement
408 // - specific float placement
410 string const buf_placement = buffer().params().float_placement;
411 string const def_placement = floats.defaultPlacement(params_.type);
412 if (params_.placement == "document"
413 && !buf_placement.empty()
414 && buf_placement != def_placement) {
415 tmpplacement = buf_placement;
416 } else if (!params_.placement.empty()
417 && params_.placement != "document"
418 && params_.placement != def_placement) {
419 tmpplacement = params_.placement;
422 // Check if placement is allowed by this float
423 string const allowed_placement =
424 floats.allowedPlacement(params_.type);
426 string::const_iterator lit = tmpplacement.begin();
427 string::const_iterator end = tmpplacement.end();
428 for (; lit != end; ++lit) {
429 if (contains(allowed_placement, *lit))
433 // Force \begin{<floatname>} to appear in a new line.
434 os << breakln << "\\begin{" << from_ascii(tmptype) << '}';
435 if (runparams.lastid != -1)
436 os.texrow().start(runparams.lastid, runparams.lastpos);
437 // We only output placement if different from the def_placement.
438 // sidewaysfloats always use their own page,
439 // therefore don't output the p option that is always set
440 if (!placement.empty()
441 && (!params_.sideways || from_ascii(placement) != "p"))
442 os << '[' << from_ascii(placement) << ']';
445 if (runparams.inDeletedInset) {
446 // This has to be done manually since we need it inside the float
447 OutputParams::CtObject ctobject = runparams.ctObject;
448 runparams.ctObject = OutputParams::CT_DISPLAYOBJECT;
449 Changes::latexMarkChange(os, buffer().params(), Change(Change::UNCHANGED),
450 Change(Change::DELETED), runparams);
451 runparams.ctObject = ctobject;
454 string alignment = getAlignment();
455 if (alignment == "left")
456 os << "\\raggedright" << breakln;
457 else if (alignment == "center")
458 os << "\\centering" << breakln;
459 else if (alignment == "right")
460 os << "\\raggedleft" << breakln;
462 InsetText::latex(os, runparams);
464 if (runparams.inDeletedInset)
467 // Force \end{<floatname>} to appear in a new line.
468 os << breakln << "\\end{" << from_ascii(tmptype) << "}\n";
472 int InsetFloat::plaintext(odocstringstream & os, OutputParams const & runparams, size_t max_length) const
474 os << '[' << buffer().B_("float") << ' '
475 << floatName(params_.type) << ":\n";
476 InsetText::plaintext(os, runparams, max_length);
479 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
483 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
485 // FIXME Implement subfloat!
487 os << '<' << from_ascii(params_.type) << '>';
488 int const i = InsetText::docbook(os, runparams);
489 os << "</" << from_ascii(params_.type) << '>';
495 bool InsetFloat::insetAllowed(InsetCode code) const
497 // The case that code == FLOAT_CODE is handled in Text3.cpp,
498 // because we need to know what type of float is meant.
505 return InsetCaptionable::insetAllowed(code);
510 void InsetFloat::setWide(bool w, bool update_label)
512 if (!buffer().params().documentClass().floats().allowsWide(params_.type))
513 params_.wide = false;
521 void InsetFloat::setSideways(bool s, bool update_label)
523 if (!buffer().params().documentClass().floats().allowsSideways(params_.type))
524 params_.sideways = false;
526 params_.sideways = s;
532 void InsetFloat::setSubfloat(bool s, bool update_label)
534 params_.subfloat = s;
540 void InsetFloat::setNewLabel()
542 docstring lab = _("float: ");
544 if (params_.subfloat)
545 lab = _("subfloat: ");
547 lab += floatName(params_.type);
549 FloatList const & floats = buffer().params().documentClass().floats();
551 if (params_.wide && floats.allowsWide(params_.type))
554 if (params_.sideways && floats.allowsSideways(params_.type))
555 lab += _(" (sideways)");
561 bool InsetFloat::allowsCaptionVariation(std::string const & newtype) const
563 return !params_.subfloat && newtype != "Unnumbered";
567 TexString InsetFloat::getCaption(OutputParams const & runparams) const
569 InsetCaption const * ins = getCaptionInset();
574 ins->getArgs(os, runparams);
577 // increase TexRow precision in non-nice mode
580 otexstringstream os2;
581 ins->getArgument(os2, runparams);
582 TexString ts = os2.release();
583 docstring & arg = ts.str;
585 if (arg.find(']') != docstring::npos)
586 arg = '{' + arg + '}';
595 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
597 params = InsetFloatParams();
601 istringstream data(in);
604 lex.setContext("InsetFloat::string2params");
609 string InsetFloat::params2string(InsetFloatParams const & params)