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/convert.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"
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.
116 InsetFloat::InsetFloat(Buffer const & buf, string const & type)
117 : InsetCollapsable(buf), name_(from_utf8(type))
119 setLabel(_("float: ") + floatName(type, buf.params()));
124 InsetFloat::~InsetFloat()
126 hideDialogs("float", this);
130 docstring InsetFloat::name() const
132 return "Float:" + name_;
136 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
138 if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
141 OutputParams rp(&buffer().params().encoding());
142 return getCaptionText(rp);
146 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
148 switch (cmd.action) {
150 case LFUN_INSET_MODIFY: {
151 InsetFloatParams params;
152 string2params(to_utf8(cmd.argument()), params);
154 // placement, wide and sideways are not used for subfloats
155 if (!params_.subfloat) {
156 params_.placement = params.placement;
157 params_.wide = params.wide;
158 params_.sideways = params.sideways;
159 setWide(params_.wide, cur.buffer()->params(), false);
160 setSideways(params_.sideways, cur.buffer()->params(), false);
163 setNewLabel(cur.buffer()->params());
167 case LFUN_INSET_DIALOG_UPDATE: {
168 cur.bv().updateDialog("float", params2string(params()));
173 InsetCollapsable::doDispatch(cur, cmd);
179 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
180 FuncStatus & flag) const
182 switch (cmd.action) {
184 case LFUN_INSET_MODIFY:
185 case LFUN_INSET_DIALOG_UPDATE:
186 flag.setEnabled(true);
190 return InsetCollapsable::getStatus(cur, cmd, flag);
195 void InsetFloat::updateLabels(ParIterator const & it)
198 buffer().masterBuffer()->params().documentClass().counters();
199 string const saveflt = cnts.current_float();
200 bool const savesubflt = cnts.isSubfloat();
202 bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
203 || it.innerInsetOfType(WRAP_CODE));
204 // floats can only embed subfloats of their own kind
206 params_.type = saveflt;
207 setSubfloat(subflt, buffer().params());
209 // Tell to captions what the current float is
210 cnts.current_float(params().type);
211 cnts.isSubfloat(subflt);
213 InsetCollapsable::updateLabels(it);
216 cnts.current_float(saveflt);
217 cnts.isSubfloat(savesubflt);
221 void InsetFloatParams::write(ostream & os) const
223 os << "Float " << type << '\n';
225 if (!placement.empty())
226 os << "placement " << placement << "\n";
231 os << "wide false\n";
234 os << "sideways true\n";
236 os << "sideways false\n";
240 void InsetFloatParams::read(Lexer & lex)
242 lex.setContext("InsetFloatParams::read");
243 if (lex.checkFor("placement"))
245 lex >> "wide" >> wide;
246 lex >> "sideways" >> sideways;
250 void InsetFloat::write(ostream & os) const
253 InsetCollapsable::write(os);
257 void InsetFloat::read(Lexer & lex)
260 InsetCollapsable::read(lex);
264 void InsetFloat::validate(LaTeXFeatures & features) const
266 if (support::contains(params_.placement, 'H'))
267 features.require("float");
269 if (params_.sideways)
270 features.require("rotfloat");
272 if (features.inFloat())
273 features.require("subfig");
275 features.useFloat(params_.type, features.inFloat());
276 features.inFloat(true);
277 InsetCollapsable::validate(features);
278 features.inFloat(false);
282 docstring InsetFloat::editMessage() const
284 return _("Opened Float Inset");
288 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
290 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
291 if (runparams_in.moving_arg)
295 OutputParams rp = runparams_in;
296 docstring const caption = getCaption(rp);
297 if (!caption.empty()) {
301 rp.inFloat = OutputParams::SUBFLOAT;
302 int const i = InsetText::latex(os, rp);
307 OutputParams runparams(runparams_in);
308 runparams.inFloat = OutputParams::MAINFLOAT;
310 FloatList const & floats = buffer().params().documentClass().floats();
311 string tmptype = params_.type;
312 if (params_.sideways)
313 tmptype = "sideways" + params_.type;
314 if (params_.wide && (!params_.sideways ||
315 params_.type == "figure" ||
316 params_.type == "table"))
318 // Figure out the float placement to use.
319 // From lowest to highest:
320 // - float default placement
321 // - document wide default placement
322 // - specific float placement
324 string const buf_placement = buffer().params().float_placement;
325 string const def_placement = floats.defaultPlacement(params_.type);
326 if (!params_.placement.empty()
327 && params_.placement != def_placement) {
328 placement = params_.placement;
329 } else if (params_.placement.empty()
330 && !buf_placement.empty()
331 && buf_placement != def_placement) {
332 placement = buf_placement;
335 // The \n is used to force \begin{<floatname>} to appear in a new line.
336 // The % is needed to prevent two consecutive \n chars in the case
337 // when the current output line is empty.
338 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
339 // We only output placement if different from the def_placement.
340 // sidewaysfloats always use their own page
341 if (!placement.empty() && !params_.sideways) {
342 os << '[' << from_ascii(placement) << ']';
346 int const i = InsetText::latex(os, runparams);
348 // The \n is used to force \end{<floatname>} to appear in a new line.
349 // In this case, we do not case if the current output line is empty.
350 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
356 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
358 os << '[' << buffer().B_("float") << ' '
359 << floatName(params_.type, buffer().params()) << ":\n";
360 InsetText::plaintext(os, runparams);
363 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
367 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
369 // FIXME Implement subfloat!
371 os << '<' << from_ascii(params_.type) << '>';
372 int const i = InsetText::docbook(os, runparams);
373 os << "</" << from_ascii(params_.type) << '>';
379 bool InsetFloat::insetAllowed(InsetCode code) const
381 return code != FOOT_CODE
382 && code != MARGIN_CODE
383 && (code != FLOAT_CODE || !params_.subfloat);
387 bool InsetFloat::showInsetDialog(BufferView * bv) const
389 if (!InsetText::showInsetDialog(bv))
390 bv->showDialog("float", params2string(params()),
391 const_cast<InsetFloat *>(this));
396 void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
404 void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
406 params_.sideways = s;
412 void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
414 params_.subfloat = s;
420 void InsetFloat::setNewLabel(BufferParams const & bp)
422 docstring lab = _("float: ");
424 if (params_.subfloat)
425 lab = _("subfloat: ");
427 lab += floatName(params_.type, bp);
432 if (params_.sideways)
433 lab += _(" (sideways)");
439 docstring InsetFloat::getCaption(OutputParams const & runparams) const
441 if (paragraphs().empty())
444 ParagraphList::const_iterator pit = paragraphs().begin();
445 for (; pit != paragraphs().end(); ++pit) {
446 InsetList::const_iterator it = pit->insetList().begin();
447 for (; it != pit->insetList().end(); ++it) {
448 Inset & inset = *it->inset;
449 if (inset.lyxCode() == CAPTION_CODE) {
450 odocstringstream ods;
452 static_cast<InsetCaption *>(it->inset);
453 ins->getOptArg(ods, runparams);
455 ins->getArgument(ods, runparams);
465 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
467 if (paragraphs().empty())
470 ParagraphList::const_iterator pit = paragraphs().begin();
471 for (; pit != paragraphs().end(); ++pit) {
472 InsetList::const_iterator it = pit->insetList().begin();
473 for (; it != pit->insetList().end(); ++it) {
474 Inset & inset = *it->inset;
475 if (inset.lyxCode() == CAPTION_CODE) {
476 odocstringstream ods;
478 static_cast<InsetCaption *>(it->inset);
479 ins->getCaptionText(ods, runparams);
488 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
490 params = InsetFloatParams();
494 istringstream data(in);
497 lex.setContext("InsetFloat::string2params");
498 lex >> "float" >> "Float";
499 lex >> params.type; // We have to read the type here!
504 string InsetFloat::params2string(InsetFloatParams const & params)
507 data << "float" << ' ';