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"
33 #include "TocBackend.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
40 #include "frontends/Application.h"
43 using namespace lyx::support;
48 // With this inset it will be possible to support the latex package
49 // float.sty, and I am sure that with this and some additional support
50 // classes we can support similar functionality in other formats
52 // By using float.sty we will have the same handling for all floats, both
53 // for those already in existance (table and figure) and all user created
54 // ones¹. So suddenly we give the users the possibility of creating new
55 // kinds of floats on the fly. (and with a uniform look)
58 // \newfloat{type}{placement}{ext}[within]
59 // type - The "type" of the new class of floats, like program or
60 // algorithm. After the appropriate \newfloat, commands
61 // such as \begin{program} or \end{algorithm*} will be
63 // placement - The default placement for the given class of floats.
64 // They are like in standard LaTeX: t, b, p and h for top,
65 // bottom, page, and here, respectively. On top of that
66 // there is a new type, H, which does not really correspond
67 // to a float, since it means: put it "here" and nowhere else.
68 // Note, however that the H specifier is special and, because
69 // of implementation details cannot be used in the second
70 // argument of \newfloat.
71 // ext - The file name extension of an auxiliary file for the list
72 // of figures (or whatever). LaTeX writes the captions to
74 // within - This (optional) argument determines whether floats of this
75 // class will be numbered within some sectional unit of the
76 // document. For example, if within is equal to chapter, the
77 // floats will be numbered within chapters.
79 // style - plain, boxed, ruled
80 // \floatname{float}{floatname}
83 // \floatplacement{float}{placement}
86 // \restylefloat{float}
88 // \listof{type}{title}
91 // ¹ the algorithm float is defined using the float.sty package. Like this
93 // \newfloat{algorithm}{htbp}{loa}[<sect>]
94 // \floatname{algorithm}{Algorithm}
96 // The intention is that floats should be definable from two places:
98 // - the "gui" (i.e. by the user)
100 // From layout files.
101 // This should only be done for floats defined in a documentclass and that
102 // does not need any additional packages. The two most known floats in this
103 // category is "table" and "figure". Floats defined in layout files are only
104 // stored in lyx files if the user modifies them.
107 // There should be a gui dialog (and also a collection of lyxfuncs) where
108 // the user can modify existing floats and/or create new ones.
110 // The individual floats will also have some settable
111 // variables: wide and placement.
115 //FIXME: why do we set in stone the type here?
116 InsetFloat::InsetFloat(Buffer * buf, string params_str)
117 : InsetCollapsable(buf)
119 string2params(params_str, params_);
123 docstring InsetFloat::layoutName() const
125 return "Float:" + from_utf8(params_.type);
129 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
132 return InsetCollapsable::toolTip(bv, x, y);
134 OutputParams rp(&buffer().params().encoding());
135 return getCaptionText(rp);
139 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
141 switch (cmd.action()) {
143 case LFUN_INSET_MODIFY: {
144 InsetFloatParams params;
145 string2params(to_utf8(cmd.argument()), params);
146 cur.recordUndoInset(this);
148 // placement, wide and sideways are not used for subfloats
149 if (!params_.subfloat) {
150 params_.placement = params.placement;
151 params_.wide = params.wide;
152 params_.sideways = params.sideways;
155 if (params_.type != params.type) {
156 params_.type = params.type;
157 cur.forceBufferUpdate();
159 // what we really want here is a TOC update, but that means
160 // a full buffer update
161 cur.forceBufferUpdate();
165 case LFUN_INSET_DIALOG_UPDATE: {
166 cur.bv().updateDialog("float", params2string(params()));
171 InsetCollapsable::doDispatch(cur, cmd);
177 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
178 FuncStatus & flag) const
180 switch (cmd.action()) {
182 case LFUN_INSET_MODIFY:
183 case LFUN_INSET_DIALOG_UPDATE:
184 flag.setEnabled(true);
187 case LFUN_INSET_SETTINGS:
188 if (InsetCollapsable::getStatus(cur, cmd, flag)) {
189 flag.setEnabled(flag.enabled() && !params_.subfloat);
194 case LFUN_NEWLINE_INSERT:
195 if (params_.subfloat) {
196 flag.setEnabled(false);
201 return InsetCollapsable::getStatus(cur, cmd, flag);
206 void InsetFloat::addToToc(DocIterator const & cpit, bool output_active) const
208 string const & type = params().type;
209 DocIterator pit = cpit;
210 pit.push_back(CursorSlice(const_cast<InsetFloat &>(*this)));
212 int length = output_active ? INT_MAX : TOC_ENTRY_LENGTH;
213 text().forOutliner(str, length);
214 shared_ptr<TocBuilder> builder = buffer().tocBackend().builder(type);
215 builder->pushItem(pit, str, output_active);
216 // Proceed with the rest of the inset.
217 InsetCollapsable::addToToc(cpit, output_active);
222 void InsetFloat::updateBuffer(ParIterator const & it, UpdateType utype)
225 buffer().masterBuffer()->params().documentClass().counters();
226 if (utype == OutputUpdate) {
227 // counters are local to the float
228 cnts.saveLastCounter();
230 string const saveflt = cnts.current_float();
231 bool const savesubflt = cnts.isSubfloat();
233 bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
234 || it.innerInsetOfType(WRAP_CODE));
235 // floats can only embed subfloats of their own kind
237 params_.type = saveflt;
240 // Tell to captions what the current float is
241 cnts.current_float(params().type);
242 cnts.isSubfloat(subflt);
244 InsetCollapsable::updateBuffer(it, utype);
247 cnts.current_float(saveflt);
248 if (utype == OutputUpdate)
249 cnts.restoreLastCounter();
250 cnts.isSubfloat(savesubflt);
254 void InsetFloatParams::write(ostream & os) const
258 if (!placement.empty())
259 os << "placement " << placement << "\n";
264 os << "wide false\n";
267 os << "sideways true\n";
269 os << "sideways false\n";
273 void InsetFloatParams::read(Lexer & lex)
275 lex.setContext("InsetFloatParams::read");
277 if (lex.checkFor("placement"))
279 lex >> "wide" >> wide;
280 lex >> "sideways" >> sideways;
284 void InsetFloat::write(ostream & os) const
288 InsetCollapsable::write(os);
292 void InsetFloat::read(Lexer & lex)
295 InsetCollapsable::read(lex);
296 // check if the float type exists
297 if (buffer().params().documentClass().floats().typeExist(params_.type))
298 setLabel(_("float: ") + floatName(params_.type));
300 setLabel(bformat(_("ERROR: Unknown float type: %1$s"), from_utf8(params_.type)));
304 void InsetFloat::validate(LaTeXFeatures & features) const
306 if (support::contains(params_.placement, 'H'))
307 features.require("float");
309 if (params_.sideways)
310 features.require("rotfloat");
312 if (features.inFloat())
313 features.require("subfig");
315 features.useFloat(params_.type, features.inFloat());
316 features.inFloat(true);
317 InsetCollapsable::validate(features);
318 features.inFloat(false);
322 docstring InsetFloat::xhtml(XHTMLStream & xs, OutputParams const & rp) const
324 FloatList const & floats = buffer().params().documentClass().floats();
325 Floating const & ftype = floats.getType(params_.type);
326 string const & htmltype = ftype.htmlTag();
327 string const & attr = ftype.htmlAttrib();
329 odocstringstream ods;
330 XHTMLStream newxs(ods);
331 newxs << html::StartTag(htmltype, attr);
332 InsetText::XHTMLOptions const opts =
333 InsetText::WriteLabel | InsetText::WriteInnerTag;
334 docstring deferred = InsetText::insetAsXHTML(newxs, rp, opts);
335 newxs << html::EndTag(htmltype);
337 if (rp.inFloat == OutputParams::NONFLOAT)
338 // In this case, this float needs to be deferred, but we'll put it
339 // before anything the text itself deferred.
340 deferred = ods.str() + '\n' + deferred;
342 // In this case, the whole thing is already being deferred, so
343 // we can write to the stream.
344 // Note that things will already have been escaped, so we do not
345 // want to escape them again.
346 xs << XHTMLStream::ESCAPE_NONE << ods.str();
351 void InsetFloat::latex(otexstream & os, OutputParams const & runparams_in) const
353 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
354 if (runparams_in.moving_arg)
358 OutputParams rp = runparams_in;
359 rp.moving_arg = true;
360 docstring const caption = getCaption(rp);
361 if (!caption.empty()) {
365 // The main argument is the contents of the float. This is not a moving argument.
366 rp.moving_arg = false;
367 rp.inFloat = OutputParams::SUBFLOAT;
368 InsetText::latex(os, rp);
373 OutputParams runparams(runparams_in);
374 runparams.inFloat = OutputParams::MAINFLOAT;
376 FloatList const & floats = buffer().params().documentClass().floats();
377 string tmptype = params_.type;
378 if (params_.sideways && floats.allowsSideways(params_.type))
379 tmptype = "sideways" + params_.type;
380 if (params_.wide && floats.allowsWide(params_.type)
381 && (!params_.sideways ||
382 params_.type == "figure" ||
383 params_.type == "table"))
385 // Figure out the float placement to use.
386 // From lowest to highest:
387 // - float default placement
388 // - document wide default placement
389 // - specific float placement
391 string const buf_placement = buffer().params().float_placement;
392 string const def_placement = floats.defaultPlacement(params_.type);
393 if (!params_.placement.empty()
394 && params_.placement != def_placement) {
395 tmpplacement = params_.placement;
396 } else if (params_.placement.empty()
397 && !buf_placement.empty()
398 && buf_placement != def_placement) {
399 tmpplacement = buf_placement;
402 // Check if placement is allowed by this float
403 string const allowed_placement =
404 floats.allowedPlacement(params_.type);
406 string::const_iterator lit = tmpplacement.begin();
407 string::const_iterator end = tmpplacement.end();
408 for (; lit != end; ++lit) {
409 if (contains(allowed_placement, *lit))
413 // Force \begin{<floatname>} to appear in a new line.
414 os << breakln << "\\begin{" << from_ascii(tmptype) << '}';
415 if (runparams.lastid != -1)
416 os.texrow().start(runparams.lastid, runparams.lastpos);
417 // We only output placement if different from the def_placement.
418 // sidewaysfloats always use their own page
419 if (!placement.empty() && !params_.sideways)
420 os << '[' << from_ascii(placement) << ']';
423 InsetText::latex(os, runparams);
425 // Force \end{<floatname>} to appear in a new line.
426 os << breakln << "\\end{" << from_ascii(tmptype) << "}\n";
430 int InsetFloat::plaintext(odocstringstream & os, OutputParams const & runparams, size_t max_length) const
432 os << '[' << buffer().B_("float") << ' '
433 << floatName(params_.type) << ":\n";
434 InsetText::plaintext(os, runparams, max_length);
437 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
441 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
443 // FIXME Implement subfloat!
445 os << '<' << from_ascii(params_.type) << '>';
446 int const i = InsetText::docbook(os, runparams);
447 os << "</" << from_ascii(params_.type) << '>';
453 bool InsetFloat::insetAllowed(InsetCode code) const
455 // The case that code == FLOAT_CODE is handled in Text3.cpp,
456 // because we need to know what type of float is meant.
463 return InsetCollapsable::insetAllowed(code);
468 void InsetFloat::setWide(bool w, bool update_label)
470 if (!buffer().params().documentClass().floats().allowsWide(params_.type))
471 params_.wide = false;
479 void InsetFloat::setSideways(bool s, bool update_label)
481 if (!buffer().params().documentClass().floats().allowsSideways(params_.type))
482 params_.sideways = false;
484 params_.sideways = s;
490 void InsetFloat::setSubfloat(bool s, bool update_label)
492 params_.subfloat = s;
498 void InsetFloat::setNewLabel()
500 docstring lab = _("float: ");
502 if (params_.subfloat)
503 lab = _("subfloat: ");
505 lab += floatName(params_.type);
507 FloatList const & floats = buffer().params().documentClass().floats();
509 if (params_.wide && floats.allowsWide(params_.type))
512 if (params_.sideways && floats.allowsSideways(params_.type))
513 lab += _(" (sideways)");
519 bool InsetFloat::allowsCaptionVariation(std::string const & newtype) const
521 return !params_.subfloat && newtype != "LongTableNoNumber";
525 docstring InsetFloat::getCaption(OutputParams const & runparams) const
527 if (paragraphs().empty())
530 InsetCaption const * ins = getCaptionInset();
535 odocstringstream ods;
536 otexstream os(ods, texrow);
537 ins->getArgs(os, runparams);
539 odocstringstream odss;
540 otexstream oss(odss, texrow);
541 ins->getArgument(oss, runparams);
542 docstring arg = odss.str();
544 if (arg.find(']') != docstring::npos)
545 arg = '{' + arg + '}';
552 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
554 params = InsetFloatParams();
558 istringstream data(in);
561 lex.setContext("InsetFloat::string2params");
566 string InsetFloat::params2string(InsetFloatParams const & params)