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"
46 // With this inset it will be possible to support the latex package
47 // float.sty, and I am sure that with this and some additional support
48 // classes we can support similar functionality in other formats
50 // By using float.sty we will have the same handling for all floats, both
51 // for those already in existance (table and figure) and all user created
52 // ones¹. So suddenly we give the users the possibility of creating new
53 // kinds of floats on the fly. (and with a uniform look)
56 // \newfloat{type}{placement}{ext}[within]
57 // type - The "type" of the new class of floats, like program or
58 // algorithm. After the appropriate \newfloat, commands
59 // such as \begin{program} or \end{algorithm*} will be
61 // placement - The default placement for the given class of floats.
62 // They are like in standard LaTeX: t, b, p and h for top,
63 // bottom, page, and here, respectively. On top of that
64 // there is a new type, H, which does not really correspond
65 // to a float, since it means: put it "here" and nowhere else.
66 // Note, however that the H specifier is special and, because
67 // of implementation details cannot be used in the second
68 // argument of \newfloat.
69 // ext - The file name extension of an auxiliary file for the list
70 // of figures (or whatever). LaTeX writes the captions to
72 // within - This (optional) argument determines whether floats of this
73 // class will be numbered within some sectional unit of the
74 // document. For example, if within is equal to chapter, the
75 // floats will be numbered within chapters.
77 // style - plain, boxed, ruled
78 // \floatname{float}{floatname}
81 // \floatplacement{float}{placement}
84 // \restylefloat{float}
86 // \listof{type}{title}
89 // ¹ the algorithm float is defined using the float.sty package. Like this
91 // \newfloat{algorithm}{htbp}{loa}[<sect>]
92 // \floatname{algorithm}{Algorithm}
94 // The intention is that floats should be definable from two places:
96 // - the "gui" (i.e. by the user)
99 // This should only be done for floats defined in a documentclass and that
100 // does not need any additional packages. The two most known floats in this
101 // category is "table" and "figure". Floats defined in layout files are only
102 // stored in lyx files if the user modifies them.
105 // There should be a gui dialog (and also a collection of lyxfuncs) where
106 // the user can modify existing floats and/or create new ones.
108 // The individual floats will also have some settable
109 // variables: wide and placement.
114 InsetFloat::InsetFloat(Buffer * buf, string const & type)
115 : InsetCollapsable(buf), name_(from_utf8(type))
117 setLabel(_("float: ") + floatName(type));
122 InsetFloat::~InsetFloat()
124 hideDialogs("float", this);
128 docstring InsetFloat::name() const
130 return "Float:" + name_;
134 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
136 if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
139 OutputParams rp(&buffer().params().encoding());
140 return getCaptionText(rp);
144 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
146 switch (cmd.action) {
148 case LFUN_INSET_MODIFY: {
149 InsetFloatParams params;
150 string2params(to_utf8(cmd.argument()), params);
152 // placement, wide and sideways are not used for subfloats
153 if (!params_.subfloat) {
154 params_.placement = params.placement;
155 params_.wide = params.wide;
156 params_.sideways = params.sideways;
163 case LFUN_INSET_DIALOG_UPDATE: {
164 cur.bv().updateDialog("float", params2string(params()));
169 InsetCollapsable::doDispatch(cur, cmd);
175 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
176 FuncStatus & flag) const
178 switch (cmd.action) {
180 case LFUN_INSET_MODIFY:
181 case LFUN_INSET_DIALOG_UPDATE:
182 flag.setEnabled(true);
185 case LFUN_INSET_SETTINGS:
186 if (InsetCollapsable::getStatus(cur, cmd, flag)) {
187 flag.setEnabled(flag.enabled() && !params_.subfloat);
193 return InsetCollapsable::getStatus(cur, cmd, flag);
198 void InsetFloat::updateLabels(ParIterator const & it, UpdateType utype)
201 buffer().masterBuffer()->params().documentClass().counters();
202 if (utype == OutputUpdate) {
203 // counters are local to the float
204 cnts.saveLastCounter();
206 string const saveflt = cnts.current_float();
207 bool const savesubflt = cnts.isSubfloat();
209 bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
210 || it.innerInsetOfType(WRAP_CODE));
211 // floats can only embed subfloats of their own kind
213 params_.type = saveflt;
216 // Tell to captions what the current float is
217 cnts.current_float(params().type);
218 cnts.isSubfloat(subflt);
220 InsetCollapsable::updateLabels(it, utype);
223 cnts.current_float(saveflt);
224 if (utype == OutputUpdate)
225 cnts.restoreLastCounter();
226 cnts.isSubfloat(savesubflt);
230 void InsetFloatParams::write(ostream & os) const
232 os << "Float " << type << '\n';
234 if (!placement.empty())
235 os << "placement " << placement << "\n";
240 os << "wide false\n";
243 os << "sideways true\n";
245 os << "sideways false\n";
249 void InsetFloatParams::read(Lexer & lex)
251 lex.setContext("InsetFloatParams::read");
252 if (lex.checkFor("placement"))
254 lex >> "wide" >> wide;
255 lex >> "sideways" >> sideways;
259 void InsetFloat::write(ostream & os) const
262 InsetCollapsable::write(os);
266 void InsetFloat::read(Lexer & lex)
269 InsetCollapsable::read(lex);
273 void InsetFloat::validate(LaTeXFeatures & features) const
275 if (support::contains(params_.placement, 'H'))
276 features.require("float");
278 if (params_.sideways)
279 features.require("rotfloat");
281 if (features.inFloat())
282 features.require("subfig");
284 features.useFloat(params_.type, features.inFloat());
285 features.inFloat(true);
286 InsetCollapsable::validate(features);
287 features.inFloat(false);
291 docstring InsetFloat::xhtml(XHTMLStream & xs, OutputParams const & rp) const
293 FloatList const & floats = buffer().params().documentClass().floats();
294 Floating const & ftype = floats.getType(params_.type);
295 string const & htmltype = ftype.htmlTag();
296 string const & attr = ftype.htmlAttrib();
298 odocstringstream ods;
299 XHTMLStream newxs(ods);
300 newxs << html::StartTag(htmltype, attr);
301 InsetText::XHTMLOptions const opts =
302 InsetText::WriteLabel | InsetText::WriteInnerTag;
303 docstring deferred = InsetText::insetAsXHTML(newxs, rp, opts);
304 newxs << html::EndTag(htmltype);
306 if (rp.inFloat == OutputParams::NONFLOAT)
307 // In this case, this float needs to be deferred, but we'll put it
308 // before anything the text itself deferred.
309 deferred = ods.str() + '\n' + deferred;
311 // In this case, the whole thing is already being deferred, so
312 // we can write to the stream.
313 // Note that things will already have been escaped, so we do not
314 // want to escape them again.
315 xs << XHTMLStream::NextRaw() << ods.str();
320 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
322 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
323 if (runparams_in.moving_arg)
327 OutputParams rp = runparams_in;
328 docstring const caption = getCaption(rp);
329 if (!caption.empty()) {
333 rp.inFloat = OutputParams::SUBFLOAT;
334 int const i = InsetText::latex(os, rp);
339 OutputParams runparams(runparams_in);
340 runparams.inFloat = OutputParams::MAINFLOAT;
342 FloatList const & floats = buffer().params().documentClass().floats();
343 string tmptype = params_.type;
344 if (params_.sideways)
345 tmptype = "sideways" + params_.type;
346 if (params_.wide && (!params_.sideways ||
347 params_.type == "figure" ||
348 params_.type == "table"))
350 // Figure out the float placement to use.
351 // From lowest to highest:
352 // - float default placement
353 // - document wide default placement
354 // - specific float placement
356 string const buf_placement = buffer().params().float_placement;
357 string const def_placement = floats.defaultPlacement(params_.type);
358 if (!params_.placement.empty()
359 && params_.placement != def_placement) {
360 placement = params_.placement;
361 } else if (params_.placement.empty()
362 && !buf_placement.empty()
363 && buf_placement != def_placement) {
364 placement = buf_placement;
367 // The \n is used to force \begin{<floatname>} to appear in a new line.
368 // The % is needed to prevent two consecutive \n chars in the case
369 // when the current output line is empty.
370 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
371 // We only output placement if different from the def_placement.
372 // sidewaysfloats always use their own page
373 if (!placement.empty() && !params_.sideways) {
374 os << '[' << from_ascii(placement) << ']';
378 int const i = InsetText::latex(os, runparams);
380 // The \n is used to force \end{<floatname>} to appear in a new line.
381 // In this case, we do not case if the current output line is empty.
382 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
388 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
390 os << '[' << buffer().B_("float") << ' '
391 << floatName(params_.type) << ":\n";
392 InsetText::plaintext(os, runparams);
395 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
399 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
401 // FIXME Implement subfloat!
403 os << '<' << from_ascii(params_.type) << '>';
404 int const i = InsetText::docbook(os, runparams);
405 os << "</" << from_ascii(params_.type) << '>';
411 bool InsetFloat::insetAllowed(InsetCode code) const
413 // The case that code == FLOAT_CODE is handled in Text3.cpp,
414 // because we need to know what type of float is meant.
421 return InsetCollapsable::insetAllowed(code);
426 bool InsetFloat::showInsetDialog(BufferView * bv) const
428 if (!InsetText::showInsetDialog(bv))
429 bv->showDialog("float", params2string(params()),
430 const_cast<InsetFloat *>(this));
435 void InsetFloat::setWide(bool w, bool update_label)
443 void InsetFloat::setSideways(bool s, bool update_label)
445 params_.sideways = s;
451 void InsetFloat::setSubfloat(bool s, bool update_label)
453 params_.subfloat = s;
459 void InsetFloat::setNewLabel()
461 docstring lab = _("float: ");
463 if (params_.subfloat)
464 lab = _("subfloat: ");
466 lab += floatName(params_.type);
471 if (params_.sideways)
472 lab += _(" (sideways)");
478 docstring InsetFloat::getCaption(OutputParams const & runparams) const
480 if (paragraphs().empty())
483 InsetCaption const * ins = getCaptionInset();
487 odocstringstream ods;
488 ins->getOptArg(ods, runparams);
490 ins->getArgument(ods, runparams);
496 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
498 params = InsetFloatParams();
502 istringstream data(in);
505 lex.setContext("InsetFloat::string2params");
506 lex >> "float" >> "Float";
507 lex >> params.type; // We have to read the type here!
512 string InsetFloat::params2string(InsetFloatParams const & params)
515 data << "float" << ' ';