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"
33 #include "texstream.h"
34 #include "TextClass.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
41 #include "frontends/Application.h"
44 using namespace lyx::support;
49 // With this inset it will be possible to support the latex package
50 // float.sty, and I am sure that with this and some additional support
51 // classes we can support similar functionality in other formats
53 // By using float.sty we will have the same handling for all floats, both
54 // for those already in existance (table and figure) and all user created
55 // ones¹. So suddenly we give the users the possibility of creating new
56 // kinds of floats on the fly. (and with a uniform look)
59 // \newfloat{type}{placement}{ext}[within]
60 // type - The "type" of the new class of floats, like program or
61 // algorithm. After the appropriate \newfloat, commands
62 // such as \begin{program} or \end{algorithm*} will be
64 // placement - The default placement for the given class of floats.
65 // They are like in standard LaTeX: t, b, p and h for top,
66 // bottom, page, and here, respectively. On top of that
67 // there is a new type, H, which does not really correspond
68 // to a float, since it means: put it "here" and nowhere else.
69 // Note, however that the H specifier is special and, because
70 // of implementation details cannot be used in the second
71 // argument of \newfloat.
72 // ext - The file name extension of an auxiliary file for the list
73 // of figures (or whatever). LaTeX writes the captions to
75 // within - This (optional) argument determines whether floats of this
76 // class will be numbered within some sectional unit of the
77 // document. For example, if within is equal to chapter, the
78 // floats will be numbered within chapters.
80 // style - plain, boxed, ruled
81 // \floatname{float}{floatname}
84 // \floatplacement{float}{placement}
87 // \restylefloat{float}
89 // \listof{type}{title}
92 // ¹ the algorithm float is defined using the float.sty package. Like this
94 // \newfloat{algorithm}{htbp}{loa}[<sect>]
95 // \floatname{algorithm}{Algorithm}
97 // The intention is that floats should be definable from two places:
99 // - the "gui" (i.e. by the user)
101 // From layout files.
102 // This should only be done for floats defined in a documentclass and that
103 // does not need any additional packages. The two most known floats in this
104 // category is "table" and "figure". Floats defined in layout files are only
105 // stored in lyx files if the user modifies them.
108 // There should be a gui dialog (and also a collection of lyxfuncs) where
109 // the user can modify existing floats and/or create new ones.
111 // The individual floats will also have some settable
112 // variables: wide and placement.
116 //FIXME: why do we set in stone the type here?
117 InsetFloat::InsetFloat(Buffer * buf, string const & params_str)
118 : InsetCaptionable(buf)
120 string2params(params_str, params_);
121 setCaptionType(params_.type);
125 // Enforce equality of float type and caption type.
126 void InsetFloat::setCaptionType(std::string const & type)
128 InsetCaptionable::setCaptionType(type);
129 params_.type = captionType();
130 // check if the float type exists
131 if (buffer().params().documentClass().floats().typeExist(params_.type))
134 setLabel(bformat(_("ERROR: Unknown float type: %1$s"), from_utf8(params_.type)));
138 docstring InsetFloat::layoutName() const
140 return "Float:" + from_utf8(params_.type);
144 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
147 return InsetCaptionable::toolTip(bv, x, y);
149 OutputParams rp(&buffer().params().encoding());
150 return getCaptionText(rp);
154 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
156 switch (cmd.action()) {
158 case LFUN_INSET_MODIFY: {
159 InsetFloatParams params;
160 string2params(to_utf8(cmd.argument()), params);
161 cur.recordUndoInset(this);
163 // placement, wide and sideways are not used for subfloats
164 if (!params_.subfloat) {
165 params_.placement = params.placement;
166 params_.wide = params.wide;
167 params_.sideways = params.sideways;
169 params_.alignment = params.alignment;
171 if (params_.type != params.type)
172 setCaptionType(params.type);
173 // what we really want here is a TOC update, but that means
174 // a full buffer update
175 cur.forceBufferUpdate();
179 case LFUN_INSET_DIALOG_UPDATE: {
180 cur.bv().updateDialog("float", params2string(params()));
185 InsetCaptionable::doDispatch(cur, cmd);
191 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
192 FuncStatus & flag) const
194 switch (cmd.action()) {
196 case LFUN_INSET_MODIFY:
197 case LFUN_INSET_DIALOG_UPDATE:
198 flag.setEnabled(true);
201 case LFUN_INSET_SETTINGS:
202 if (InsetCaptionable::getStatus(cur, cmd, flag)) {
203 flag.setEnabled(flag.enabled() && !params_.subfloat);
208 case LFUN_NEWLINE_INSERT:
209 if (params_.subfloat) {
210 flag.setEnabled(false);
217 return InsetCaptionable::getStatus(cur, cmd, flag);
222 bool InsetFloat::hasSubCaptions(ParIterator const & it) const
224 return (it.innerInsetOfType(FLOAT_CODE) || it.innerInsetOfType(WRAP_CODE));
228 string InsetFloat::getAlignment() const
231 string const buf_alignment = buffer().params().float_alignment;
232 if (params_.alignment == "document"
233 && !buf_alignment.empty()) {
234 alignment = buf_alignment;
235 } else if (!params_.alignment.empty()
236 && params_.alignment != "class"
237 && params_.alignment != "document") {
238 alignment = params_.alignment;
244 LyXAlignment InsetFloat::contentAlignment() const
246 LyXAlignment align = LYX_ALIGN_NONE;
247 string alignment = getAlignment();
248 if (alignment == "left")
249 align = LYX_ALIGN_LEFT;
250 else if (alignment == "center")
251 align = LYX_ALIGN_CENTER;
252 else if (alignment == "right")
253 align = LYX_ALIGN_RIGHT;
259 void InsetFloatParams::write(ostream & os) const
262 // Better this than creating a parse error. This in fact happens in the
263 // parameters dialog via InsetFloatParams::params2string.
264 os << "senseless" << '\n';
268 if (!placement.empty())
269 os << "placement " << placement << "\n";
270 if (!alignment.empty())
271 os << "alignment " << alignment << "\n";
276 os << "wide false\n";
279 os << "sideways true\n";
281 os << "sideways false\n";
285 void InsetFloatParams::read(Lexer & lex)
287 lex.setContext("InsetFloatParams::read");
289 if (lex.checkFor("placement"))
291 if (lex.checkFor("alignment"))
293 lex >> "wide" >> wide;
294 lex >> "sideways" >> sideways;
298 void InsetFloat::write(ostream & os) const
302 InsetCaptionable::write(os);
306 void InsetFloat::read(Lexer & lex)
309 InsetCaptionable::read(lex);
310 setCaptionType(params_.type);
314 void InsetFloat::validate(LaTeXFeatures & features) const
316 if (support::contains(params_.placement, 'H'))
317 features.require("float");
319 if (params_.sideways)
320 features.require("rotfloat");
322 if (features.inFloat())
323 features.require("subfig");
325 if (features.inDeletedInset()) {
326 features.require("tikz");
327 features.require("ct-tikz-object-sout");
330 features.useFloat(params_.type, features.inFloat());
331 features.inFloat(true);
332 InsetCaptionable::validate(features);
333 features.inFloat(false);
337 docstring InsetFloat::xhtml(XHTMLStream & xs, OutputParams const & rp) const
339 FloatList const & floats = buffer().params().documentClass().floats();
340 Floating const & ftype = floats.getType(params_.type);
341 string const & htmltype = ftype.htmlTag();
342 string const & attr = ftype.htmlAttrib();
344 odocstringstream ods;
345 XHTMLStream newxs(ods);
346 newxs << html::StartTag(htmltype, attr);
347 InsetText::XHTMLOptions const opts =
348 InsetText::WriteLabel | InsetText::WriteInnerTag;
349 docstring deferred = InsetText::insetAsXHTML(newxs, rp, opts);
350 newxs << html::EndTag(htmltype);
352 if (rp.inFloat == OutputParams::NONFLOAT) {
353 // In this case, this float needs to be deferred, but we'll put it
354 // before anything the text itself deferred.
355 deferred = ods.str() + '\n' + deferred;
357 // In this case, the whole thing is already being deferred, so
358 // we can write to the stream.
359 // Note that things will already have been escaped, so we do not
360 // want to escape them again.
361 xs << XHTMLStream::ESCAPE_NONE << ods.str();
367 void InsetFloat::latex(otexstream & os, OutputParams const & runparams_in) const
369 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
370 if (!paragraphs().empty() && !runparams_in.nice)
371 // improve TexRow precision in non-nice mode
374 if (runparams_in.moving_arg)
378 OutputParams rp = runparams_in;
379 rp.moving_arg = true;
380 os << getCaption(rp);
382 // The main argument is the contents of the float. This is not a moving argument.
383 rp.moving_arg = false;
384 rp.inFloat = OutputParams::SUBFLOAT;
385 InsetText::latex(os, rp);
390 OutputParams runparams(runparams_in);
391 runparams.inFloat = OutputParams::MAINFLOAT;
393 FloatList const & floats = buffer().params().documentClass().floats();
394 string tmptype = params_.type;
395 if (params_.sideways && floats.allowsSideways(params_.type))
396 tmptype = "sideways" + params_.type;
397 if (params_.wide && floats.allowsWide(params_.type)
398 && (!params_.sideways ||
399 params_.type == "figure" ||
400 params_.type == "table"))
402 // Figure out the float placement to use.
403 // From lowest to highest:
404 // - float default placement
405 // - document wide default placement
406 // - specific float placement
408 string const buf_placement = buffer().params().float_placement;
409 string const def_placement = floats.defaultPlacement(params_.type);
410 if (params_.placement == "document"
411 && !buf_placement.empty()
412 && buf_placement != def_placement) {
413 tmpplacement = buf_placement;
414 } else if (!params_.placement.empty()
415 && params_.placement != "document"
416 && params_.placement != def_placement) {
417 tmpplacement = params_.placement;
420 // Check if placement is allowed by this float
421 string const allowed_placement =
422 floats.allowedPlacement(params_.type);
424 string::const_iterator lit = tmpplacement.begin();
425 string::const_iterator end = tmpplacement.end();
426 for (; lit != end; ++lit) {
427 if (contains(allowed_placement, *lit))
431 // Force \begin{<floatname>} to appear in a new line.
432 os << breakln << "\\begin{" << from_ascii(tmptype) << '}';
433 if (runparams.lastid != -1)
434 os.texrow().start(runparams.lastid, runparams.lastpos);
435 // We only output placement if different from the def_placement.
436 // sidewaysfloats always use their own page,
437 // therefore don't output the p option that is always set
438 if (!placement.empty()
439 && (!params_.sideways || from_ascii(placement) != "p"))
440 os << '[' << from_ascii(placement) << ']';
443 if (runparams.inDeletedInset) {
444 // This has to be done manually since we need it inside the float
445 OutputParams::CtObject ctobject = runparams.ctObject;
446 runparams.ctObject = OutputParams::CT_DISPLAYOBJECT;
447 Changes::latexMarkChange(os, buffer().params(), Change(Change::UNCHANGED),
448 Change(Change::DELETED), runparams);
449 runparams.ctObject = ctobject;
452 string alignment = getAlignment();
453 if (alignment == "left")
454 os << "\\raggedright" << breakln;
455 else if (alignment == "center")
456 os << "\\centering" << breakln;
457 else if (alignment == "right")
458 os << "\\raggedleft" << breakln;
460 InsetText::latex(os, runparams);
462 if (runparams.inDeletedInset)
465 // Force \end{<floatname>} to appear in a new line.
466 os << breakln << "\\end{" << from_ascii(tmptype) << "}\n";
470 int InsetFloat::plaintext(odocstringstream & os, OutputParams const & runparams, size_t max_length) const
472 os << '[' << buffer().B_("float") << ' '
473 << floatName(params_.type) << ":\n";
474 InsetText::plaintext(os, runparams, max_length);
477 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
481 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
483 // FIXME Implement subfloat!
485 os << '<' << from_ascii(params_.type) << '>';
486 int const i = InsetText::docbook(os, runparams);
487 os << "</" << from_ascii(params_.type) << '>';
493 bool InsetFloat::insetAllowed(InsetCode code) const
495 // The case that code == FLOAT_CODE is handled in Text3.cpp,
496 // because we need to know what type of float is meant.
503 return InsetCaptionable::insetAllowed(code);
508 void InsetFloat::setWide(bool w, bool update_label)
510 if (!buffer().params().documentClass().floats().allowsWide(params_.type))
511 params_.wide = false;
519 void InsetFloat::setSideways(bool s, bool update_label)
521 if (!buffer().params().documentClass().floats().allowsSideways(params_.type))
522 params_.sideways = false;
524 params_.sideways = s;
530 void InsetFloat::setSubfloat(bool s, bool update_label)
532 params_.subfloat = s;
538 void InsetFloat::setNewLabel()
540 docstring lab = _("float: ");
542 if (params_.subfloat)
543 lab = _("subfloat: ");
545 lab += floatName(params_.type);
547 FloatList const & floats = buffer().params().documentClass().floats();
549 if (params_.wide && floats.allowsWide(params_.type))
552 if (params_.sideways && floats.allowsSideways(params_.type))
553 lab += _(" (sideways)");
559 bool InsetFloat::allowsCaptionVariation(std::string const & newtype) const
561 return !params_.subfloat && newtype != "Unnumbered";
565 TexString InsetFloat::getCaption(OutputParams const & runparams) const
567 InsetCaption const * ins = getCaptionInset();
572 ins->getArgs(os, runparams);
575 // increase TexRow precision in non-nice mode
578 otexstringstream os2;
579 ins->getArgument(os2, runparams);
580 TexString ts = os2.release();
581 docstring & arg = ts.str;
583 if (arg.find(']') != docstring::npos)
584 arg = '{' + arg + '}';
593 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
595 params = InsetFloatParams();
599 istringstream data(in);
602 lex.setContext("InsetFloat::string2params");
607 string InsetFloat::params2string(InsetFloatParams const & params)