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 "InsetList.h"
29 #include "LaTeXFeatures.h"
31 #include "OutputParams.h"
32 #include "ParIterator.h"
33 #include "TextClass.h"
35 #include "support/debug.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/docstream.h"
39 #include "support/convert.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 const & buf, string const & type)
115 : InsetCollapsable(buf), name_(from_utf8(type))
117 setLabel(_("float: ") + floatName(type, buf.params()));
122 InsetFloat::~InsetFloat()
124 InsetFloatMailer(*this).hideDialog();
128 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
130 switch (cmd.action) {
132 case LFUN_INSET_MODIFY: {
133 InsetFloatParams params;
134 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
135 params_.placement = params.placement;
136 params_.wide = params.wide;
137 params_.sideways = params.sideways;
138 params_.subfloat = params.subfloat;
139 wide(params_.wide, cur.buffer().params());
140 sideways(params_.sideways, cur.buffer().params());
144 case LFUN_INSET_DIALOG_UPDATE: {
145 InsetFloatMailer(*this).updateDialog(&cur.bv());
149 case LFUN_MOUSE_RELEASE: {
150 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
151 if (params_.subfloat)
153 InsetFloatMailer(*this).showDialog(&cur.bv());
156 InsetCollapsable::doDispatch(cur, cmd);
161 InsetCollapsable::doDispatch(cur, cmd);
167 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
168 FuncStatus & flag) const
170 switch (cmd.action) {
172 case LFUN_INSET_MODIFY:
173 case LFUN_INSET_DIALOG_UPDATE:
178 return InsetCollapsable::getStatus(cur, cmd, flag);
183 void InsetFloat::updateLabels(ParIterator const & it)
185 Counters & cnts = buffer().params().documentClass().counters();
186 string const saveflt = cnts.current_float();
187 bool const savesubflt = cnts.isSubfloat();
189 bool const subflt = it.innerInsetOfType(FLOAT_CODE);
190 // floats can only embed subfloats of their own kind
192 params_.type = saveflt;
193 subfloat(subflt, buffer().params());
195 // Tell to captions what the current float is
196 cnts.current_float(params().type);
197 cnts.isSubfloat(subflt);
199 InsetCollapsable::updateLabels(it);
202 cnts.current_float(saveflt);
203 cnts.isSubfloat(savesubflt);
207 void InsetFloatParams::write(ostream & os) const
209 os << "Float " << type << '\n';
211 if (!placement.empty())
212 os << "placement " << placement << "\n";
217 os << "wide false\n";
220 os << "sideways true\n";
222 os << "sideways false\n";
226 void InsetFloatParams::read(Lexer & lex)
230 if (token == "placement") {
233 // take countermeasures
234 lex.pushToken(token);
237 if (token == "wide") {
240 lyxerr << "InsetFloat::Read:: Missing wide!"
242 // take countermeasures
243 lex.pushToken(token);
246 if (token == "sideways") {
249 lyxerr << "InsetFloat::Read:: Missing sideways!"
251 // take countermeasures
252 lex.pushToken(token);
257 void InsetFloat::write(ostream & os) const
260 InsetCollapsable::write(os);
264 void InsetFloat::read(Lexer & lex)
267 wide(params_.wide, buffer().params());
268 sideways(params_.sideways, buffer().params());
269 subfloat(params_.subfloat, buffer().params());
270 InsetCollapsable::read(lex);
274 void InsetFloat::validate(LaTeXFeatures & features) const
276 if (support::contains(params_.placement, 'H'))
277 features.require("float");
279 if (params_.sideways)
280 features.require("rotfloat");
282 if (params_.subfloat)
283 features.require("subfig");
285 features.useFloat(params_.type, params_.subfloat);
286 InsetCollapsable::validate(features);
290 docstring InsetFloat::editMessage() const
292 return _("Opened Float Inset");
296 int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
298 if (params_.subfloat) {
299 if (runparams.moving_arg)
303 OutputParams rp = runparams;
304 docstring const caption = getCaption(rp);
305 if (!caption.empty()) {
309 int const i = InsetText::latex(os, runparams);
315 FloatList const & floats = buffer().params().documentClass().floats();
316 string tmptype = params_.type;
317 if (params_.sideways)
318 tmptype = "sideways" + params_.type;
319 if (params_.wide && (!params_.sideways ||
320 params_.type == "figure" ||
321 params_.type == "table"))
323 // Figure out the float placement to use.
324 // From lowest to highest:
325 // - float default placement
326 // - document wide default placement
327 // - specific float placement
329 string const buf_placement = buffer().params().float_placement;
330 string const def_placement = floats.defaultPlacement(params_.type);
331 if (!params_.placement.empty()
332 && params_.placement != def_placement) {
333 placement = params_.placement;
334 } else if (params_.placement.empty()
335 && !buf_placement.empty()
336 && buf_placement != def_placement) {
337 placement = buf_placement;
340 // The \n is used to force \begin{<floatname>} to appear in a new line.
341 // The % is needed to prevent two consecutive \n chars in the case
342 // when the current output line is empty.
343 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
344 // We only output placement if different from the def_placement.
345 // sidewaysfloats always use their own page
346 if (!placement.empty() && !params_.sideways) {
347 os << '[' << from_ascii(placement) << ']';
351 int const i = InsetText::latex(os, runparams);
353 // The \n is used to force \end{<floatname>} to appear in a new line.
354 // In this case, we do not case if the current output line is empty.
355 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
361 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
363 os << '[' << buffer().B_("float") << ' '
364 << floatName(params_.type, buffer().params()) << ":\n";
365 InsetText::plaintext(os, runparams);
368 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
372 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
374 // FIXME Implement subfloat!
376 os << '<' << from_ascii(params_.type) << '>';
377 int const i = InsetText::docbook(os, runparams);
378 os << "</" << from_ascii(params_.type) << '>';
384 bool InsetFloat::insetAllowed(InsetCode code) const
386 return code != FOOT_CODE
387 && code != MARGIN_CODE
388 && (code != FLOAT_CODE || !params_.subfloat);
392 bool InsetFloat::showInsetDialog(BufferView * bv) const
394 if (!InsetText::showInsetDialog(bv))
395 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
400 void InsetFloat::wide(bool w, BufferParams const & bp)
403 docstring lab = _("float: ") + floatName(params_.type, bp);
410 void InsetFloat::sideways(bool s, BufferParams const & bp)
412 params_.sideways = s;
413 docstring lab = _("float: ") + floatName(params_.type, bp);
414 if (params_.sideways)
415 lab += _(" (sideways)");
420 void InsetFloat::subfloat(bool s, BufferParams const & bp)
422 params_.subfloat = s;
423 docstring lab = _("float: ") + floatName(params_.type, bp);
425 lab = _("subfloat: ") + floatName(params_.type, bp);
430 docstring InsetFloat::getCaption(OutputParams const & runparams) const
432 if (paragraphs().empty())
435 ParagraphList::const_iterator pit = paragraphs().begin();
436 for (; pit != paragraphs().end(); ++pit) {
437 InsetList::const_iterator it = pit->insetList().begin();
438 for (; it != pit->insetList().end(); ++it) {
439 Inset & inset = *it->inset;
440 if (inset.lyxCode() == CAPTION_CODE) {
441 odocstringstream ods;
443 static_cast<InsetCaption *>(it->inset);
444 ins->getOptArg(ods, runparams);
446 ins->getArgument(ods, runparams);
456 string const InsetFloatMailer::name_("float");
458 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
463 string const InsetFloatMailer::inset2string(Buffer const &) const
465 return params2string(inset_.params());
469 void InsetFloatMailer::string2params(string const & in,
470 InsetFloatParams & params)
472 params = InsetFloatParams();
476 istringstream data(in);
482 if (!lex || name != name_)
483 return print_mailer_error("InsetFloatMailer", in, 1, name_);
485 // This is part of the inset proper that is usually swallowed
486 // by Text::readInset
489 if (!lex || id != "Float")
490 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
492 // We have to read the type here!
498 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
501 data << name_ << ' ';