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 "ParIterator.h"
31 #include "TextClass.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
38 #include "frontends/Application.h"
45 // With this inset it will be possible to support the latex package
46 // float.sty, and I am sure that with this and some additional support
47 // classes we can support similar functionality in other formats
49 // By using float.sty we will have the same handling for all floats, both
50 // for those already in existance (table and figure) and all user created
51 // ones¹. So suddenly we give the users the possibility of creating new
52 // kinds of floats on the fly. (and with a uniform look)
55 // \newfloat{type}{placement}{ext}[within]
56 // type - The "type" of the new class of floats, like program or
57 // algorithm. After the appropriate \newfloat, commands
58 // such as \begin{program} or \end{algorithm*} will be
60 // placement - The default placement for the given class of floats.
61 // They are like in standard LaTeX: t, b, p and h for top,
62 // bottom, page, and here, respectively. On top of that
63 // there is a new type, H, which does not really correspond
64 // to a float, since it means: put it "here" and nowhere else.
65 // Note, however that the H specifier is special and, because
66 // of implementation details cannot be used in the second
67 // argument of \newfloat.
68 // ext - The file name extension of an auxiliary file for the list
69 // of figures (or whatever). LaTeX writes the captions to
71 // within - This (optional) argument determines whether floats of this
72 // class will be numbered within some sectional unit of the
73 // document. For example, if within is equal to chapter, the
74 // floats will be numbered within chapters.
76 // style - plain, boxed, ruled
77 // \floatname{float}{floatname}
80 // \floatplacement{float}{placement}
83 // \restylefloat{float}
85 // \listof{type}{title}
88 // ¹ the algorithm float is defined using the float.sty package. Like this
90 // \newfloat{algorithm}{htbp}{loa}[<sect>]
91 // \floatname{algorithm}{Algorithm}
93 // The intention is that floats should be definable from two places:
95 // - the "gui" (i.e. by the user)
98 // This should only be done for floats defined in a documentclass and that
99 // does not need any additional packages. The two most known floats in this
100 // category is "table" and "figure". Floats defined in layout files are only
101 // stored in lyx files if the user modifies them.
104 // There should be a gui dialog (and also a collection of lyxfuncs) where
105 // the user can modify existing floats and/or create new ones.
107 // The individual floats will also have some settable
108 // variables: wide and placement.
113 InsetFloat::InsetFloat(Buffer const & buf, string const & type)
114 : InsetCollapsable(buf), name_(from_utf8(type))
116 setLabel(_("float: ") + floatName(type));
121 InsetFloat::~InsetFloat()
123 hideDialogs("float", this);
127 docstring InsetFloat::name() const
129 return "Float:" + name_;
133 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
135 if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
138 OutputParams rp(&buffer().params().encoding());
139 return getCaptionText(rp);
143 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
145 switch (cmd.action) {
147 case LFUN_INSET_MODIFY: {
148 InsetFloatParams params;
149 string2params(to_utf8(cmd.argument()), params);
151 // placement, wide and sideways are not used for subfloats
152 if (!params_.subfloat) {
153 params_.placement = params.placement;
154 params_.wide = params.wide;
155 params_.sideways = params.sideways;
162 case LFUN_INSET_DIALOG_UPDATE: {
163 cur.bv().updateDialog("float", params2string(params()));
168 InsetCollapsable::doDispatch(cur, cmd);
174 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
175 FuncStatus & flag) const
177 switch (cmd.action) {
179 case LFUN_INSET_MODIFY:
180 case LFUN_INSET_DIALOG_UPDATE:
181 flag.setEnabled(true);
185 return InsetCollapsable::getStatus(cur, cmd, flag);
190 void InsetFloat::updateLabels(ParIterator const & it)
193 buffer().masterBuffer()->params().documentClass().counters();
194 string const saveflt = cnts.current_float();
195 bool const savesubflt = cnts.isSubfloat();
197 bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
198 || it.innerInsetOfType(WRAP_CODE));
199 // floats can only embed subfloats of their own kind
201 params_.type = saveflt;
204 // Tell to captions what the current float is
205 cnts.current_float(params().type);
206 cnts.isSubfloat(subflt);
208 InsetCollapsable::updateLabels(it);
211 cnts.current_float(saveflt);
212 cnts.isSubfloat(savesubflt);
216 void InsetFloatParams::write(ostream & os) const
218 os << "Float " << type << '\n';
220 if (!placement.empty())
221 os << "placement " << placement << "\n";
226 os << "wide false\n";
229 os << "sideways true\n";
231 os << "sideways false\n";
235 void InsetFloatParams::read(Lexer & lex)
237 lex.setContext("InsetFloatParams::read");
238 if (lex.checkFor("placement"))
240 lex >> "wide" >> wide;
241 lex >> "sideways" >> sideways;
245 void InsetFloat::write(ostream & os) const
248 InsetCollapsable::write(os);
252 void InsetFloat::read(Lexer & lex)
255 InsetCollapsable::read(lex);
259 void InsetFloat::validate(LaTeXFeatures & features) const
261 if (support::contains(params_.placement, 'H'))
262 features.require("float");
264 if (params_.sideways)
265 features.require("rotfloat");
267 if (features.inFloat())
268 features.require("subfig");
270 features.useFloat(params_.type, features.inFloat());
271 features.inFloat(true);
272 InsetCollapsable::validate(features);
273 features.inFloat(false);
277 docstring InsetFloat::xhtml(odocstream & os, OutputParams const & rp) const
279 FloatList const & floats = buffer().params().documentClass().floats();
280 Floating const & ftype = floats.getType(params_.type);
281 string const htmltype = ftype.htmlType().empty() ?
282 "div" : ftype.htmlType();
283 string const htmlclass = ftype.htmlClass().empty() ?
284 "float-" + params_.type : ftype.htmlClass();
285 docstring const otag =
286 from_ascii("<" + htmltype + " class='float " + htmlclass + "'>\n");
287 docstring const ctag = from_ascii("</" + htmltype + ">\n");
289 odocstringstream out;
292 docstring def = InsetText::xhtml(out, rp);
295 if (rp.inFloat == OutputParams::NONFLOAT)
296 // In this case, this float needs to be deferred, but we'll put it
297 // before anything the text itself deferred.
298 def = out.str() + '\n' + def;
300 // In this case, the whole thing is already being deferred, so
301 // we can write to the stream.
307 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
309 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
310 if (runparams_in.moving_arg)
314 OutputParams rp = runparams_in;
315 docstring const caption = getCaption(rp);
316 if (!caption.empty()) {
320 rp.inFloat = OutputParams::SUBFLOAT;
321 int const i = InsetText::latex(os, rp);
326 OutputParams runparams(runparams_in);
327 runparams.inFloat = OutputParams::MAINFLOAT;
329 FloatList const & floats = buffer().params().documentClass().floats();
330 string tmptype = params_.type;
331 if (params_.sideways)
332 tmptype = "sideways" + params_.type;
333 if (params_.wide && (!params_.sideways ||
334 params_.type == "figure" ||
335 params_.type == "table"))
337 // Figure out the float placement to use.
338 // From lowest to highest:
339 // - float default placement
340 // - document wide default placement
341 // - specific float placement
343 string const buf_placement = buffer().params().float_placement;
344 string const def_placement = floats.defaultPlacement(params_.type);
345 if (!params_.placement.empty()
346 && params_.placement != def_placement) {
347 placement = params_.placement;
348 } else if (params_.placement.empty()
349 && !buf_placement.empty()
350 && buf_placement != def_placement) {
351 placement = buf_placement;
354 // The \n is used to force \begin{<floatname>} to appear in a new line.
355 // The % is needed to prevent two consecutive \n chars in the case
356 // when the current output line is empty.
357 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
358 // We only output placement if different from the def_placement.
359 // sidewaysfloats always use their own page
360 if (!placement.empty() && !params_.sideways) {
361 os << '[' << from_ascii(placement) << ']';
365 int const i = InsetText::latex(os, runparams);
367 // The \n is used to force \end{<floatname>} to appear in a new line.
368 // In this case, we do not case if the current output line is empty.
369 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
375 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
377 os << '[' << buffer().B_("float") << ' '
378 << floatName(params_.type) << ":\n";
379 InsetText::plaintext(os, runparams);
382 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
386 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
388 // FIXME Implement subfloat!
390 os << '<' << from_ascii(params_.type) << '>';
391 int const i = InsetText::docbook(os, runparams);
392 os << "</" << from_ascii(params_.type) << '>';
398 bool InsetFloat::insetAllowed(InsetCode code) const
400 // The case that code == FLOAT_CODE is handled in Text3.cpp,
401 // because we need to know what type of float is meant.
408 return InsetCollapsable::insetAllowed(code);
413 bool InsetFloat::showInsetDialog(BufferView * bv) const
415 if (!InsetText::showInsetDialog(bv))
416 bv->showDialog("float", params2string(params()),
417 const_cast<InsetFloat *>(this));
422 void InsetFloat::setWide(bool w, bool update_label)
430 void InsetFloat::setSideways(bool s, bool update_label)
432 params_.sideways = s;
438 void InsetFloat::setSubfloat(bool s, bool update_label)
440 params_.subfloat = s;
446 void InsetFloat::setNewLabel()
448 docstring lab = _("float: ");
450 if (params_.subfloat)
451 lab = _("subfloat: ");
453 lab += floatName(params_.type);
458 if (params_.sideways)
459 lab += _(" (sideways)");
465 docstring InsetFloat::getCaption(OutputParams const & runparams) const
467 if (paragraphs().empty())
470 InsetCaption const * ins = getCaptionInset();
474 odocstringstream ods;
475 ins->getOptArg(ods, runparams);
477 ins->getArgument(ods, runparams);
483 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
485 params = InsetFloatParams();
489 istringstream data(in);
492 lex.setContext("InsetFloat::string2params");
493 lex >> "float" >> "Float";
494 lex >> params.type; // We have to read the type here!
499 string InsetFloat::params2string(InsetFloatParams const & params)
502 data << "float" << ' ';