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::toolTip(BufferView const & bv, int x, int y) const
132 if (InsetCollapsable::toolTip(bv, x, y).empty())
135 docstring default_tip;
137 default_tip = _("Left-click to collapse the inset");
139 default_tip = _("Left-click to open the inset");
140 OutputParams rp(&buffer().params().encoding());
141 docstring caption_tip = getCaptionText(rp);
142 if (!isOpen() && !caption_tip.empty())
143 return caption_tip + '\n' + default_tip;
148 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
150 switch (cmd.action) {
152 case LFUN_INSET_MODIFY: {
153 InsetFloatParams params;
154 string2params(to_utf8(cmd.argument()), params);
156 // placement, wide and sideways are not used for subfloats
157 if (!params_.subfloat) {
158 params_.placement = params.placement;
159 params_.wide = params.wide;
160 params_.sideways = params.sideways;
161 setWide(params_.wide, cur.buffer().params(), false);
162 setSideways(params_.sideways, cur.buffer().params(), false);
165 setNewLabel(cur.buffer().params());
169 case LFUN_INSET_DIALOG_UPDATE: {
170 cur.bv().updateDialog("float", params2string(params()));
175 InsetCollapsable::doDispatch(cur, cmd);
181 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
182 FuncStatus & flag) const
184 switch (cmd.action) {
186 case LFUN_INSET_MODIFY:
187 case LFUN_INSET_DIALOG_UPDATE:
188 flag.setEnabled(true);
192 return InsetCollapsable::getStatus(cur, cmd, flag);
197 void InsetFloat::updateLabels(ParIterator const & it)
199 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
200 string const saveflt = cnts.current_float();
201 bool const savesubflt = cnts.isSubfloat();
203 bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
204 || it.innerInsetOfType(WRAP_CODE));
205 // floats can only embed subfloats of their own kind
207 params_.type = saveflt;
208 setSubfloat(subflt, buffer().params());
210 // Tell to captions what the current float is
211 cnts.current_float(params().type);
212 cnts.isSubfloat(subflt);
214 InsetCollapsable::updateLabels(it);
217 cnts.current_float(saveflt);
218 cnts.isSubfloat(savesubflt);
222 void InsetFloatParams::write(ostream & os) const
224 os << "Float " << type << '\n';
226 if (!placement.empty())
227 os << "placement " << placement << "\n";
232 os << "wide false\n";
235 os << "sideways true\n";
237 os << "sideways false\n";
241 void InsetFloatParams::read(Lexer & lex)
243 lex.setContext("InsetFloatParams::read");
244 if (lex.checkFor("placement"))
246 lex >> "wide" >> wide;
247 lex >> "sideways" >> sideways;
251 void InsetFloat::write(ostream & os) const
254 InsetCollapsable::write(os);
258 void InsetFloat::read(Lexer & lex)
261 setWide(params_.wide, buffer().params());
262 setSideways(params_.sideways, buffer().params());
263 setSubfloat(params_.subfloat, buffer().params());
264 InsetCollapsable::read(lex);
268 void InsetFloat::validate(LaTeXFeatures & features) const
270 if (support::contains(params_.placement, 'H'))
271 features.require("float");
273 if (params_.sideways)
274 features.require("rotfloat");
276 if (params_.subfloat)
277 features.require("subfig");
279 features.useFloat(params_.type, params_.subfloat);
280 InsetCollapsable::validate(features);
284 docstring InsetFloat::editMessage() const
286 return _("Opened Float Inset");
290 int InsetFloat::latex(odocstream & os, OutputParams const & runparams) const
292 if (params_.subfloat) {
293 if (runparams.moving_arg)
297 OutputParams rp = runparams;
298 docstring const caption = getCaption(rp);
299 if (!caption.empty()) {
303 int const i = InsetText::latex(os, runparams);
309 FloatList const & floats = buffer().params().documentClass().floats();
310 string tmptype = params_.type;
311 if (params_.sideways)
312 tmptype = "sideways" + params_.type;
313 if (params_.wide && (!params_.sideways ||
314 params_.type == "figure" ||
315 params_.type == "table"))
317 // Figure out the float placement to use.
318 // From lowest to highest:
319 // - float default placement
320 // - document wide default placement
321 // - specific float placement
323 string const buf_placement = buffer().params().float_placement;
324 string const def_placement = floats.defaultPlacement(params_.type);
325 if (!params_.placement.empty()
326 && params_.placement != def_placement) {
327 placement = params_.placement;
328 } else if (params_.placement.empty()
329 && !buf_placement.empty()
330 && buf_placement != def_placement) {
331 placement = buf_placement;
334 // The \n is used to force \begin{<floatname>} to appear in a new line.
335 // The % is needed to prevent two consecutive \n chars in the case
336 // when the current output line is empty.
337 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
338 // We only output placement if different from the def_placement.
339 // sidewaysfloats always use their own page
340 if (!placement.empty() && !params_.sideways) {
341 os << '[' << from_ascii(placement) << ']';
345 int const i = InsetText::latex(os, runparams);
347 // The \n is used to force \end{<floatname>} to appear in a new line.
348 // In this case, we do not case if the current output line is empty.
349 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
355 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
357 os << '[' << buffer().B_("float") << ' '
358 << floatName(params_.type, buffer().params()) << ":\n";
359 InsetText::plaintext(os, runparams);
362 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
366 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
368 // FIXME Implement subfloat!
370 os << '<' << from_ascii(params_.type) << '>';
371 int const i = InsetText::docbook(os, runparams);
372 os << "</" << from_ascii(params_.type) << '>';
378 bool InsetFloat::insetAllowed(InsetCode code) const
380 return code != FOOT_CODE
381 && code != MARGIN_CODE
382 && (code != FLOAT_CODE || !params_.subfloat);
386 bool InsetFloat::showInsetDialog(BufferView * bv) const
388 if (!InsetText::showInsetDialog(bv))
389 bv->showDialog("float", params2string(params()),
390 const_cast<InsetFloat *>(this));
395 void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
403 void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
405 params_.sideways = s;
411 void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
413 params_.subfloat = s;
419 void InsetFloat::setNewLabel(BufferParams const & bp)
421 docstring lab = _("float: ");
423 if( params_.subfloat )
424 lab = _("subfloat: ");
426 lab += floatName(params_.type, bp);
431 if (params_.sideways)
432 lab += _(" (sideways)");
438 docstring InsetFloat::getCaption(OutputParams const & runparams) const
440 if (paragraphs().empty())
443 ParagraphList::const_iterator pit = paragraphs().begin();
444 for (; pit != paragraphs().end(); ++pit) {
445 InsetList::const_iterator it = pit->insetList().begin();
446 for (; it != pit->insetList().end(); ++it) {
447 Inset & inset = *it->inset;
448 if (inset.lyxCode() == CAPTION_CODE) {
449 odocstringstream ods;
451 static_cast<InsetCaption *>(it->inset);
452 ins->getOptArg(ods, runparams);
454 ins->getArgument(ods, runparams);
464 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
466 if (paragraphs().empty())
469 ParagraphList::const_iterator pit = paragraphs().begin();
470 for (; pit != paragraphs().end(); ++pit) {
471 InsetList::const_iterator it = pit->insetList().begin();
472 for (; it != pit->insetList().end(); ++it) {
473 Inset & inset = *it->inset;
474 if (inset.lyxCode() == CAPTION_CODE) {
475 odocstringstream ods;
477 static_cast<InsetCaption *>(it->inset);
478 ins->getCaptionText(ods, runparams);
487 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
489 params = InsetFloatParams();
493 istringstream data(in);
496 lex.setContext("InsetFloat::string2params");
497 lex >> "float" >> "Float";
498 lex >> params.type; // We have to read the type here!
503 string InsetFloat::params2string(InsetFloatParams const & params)
506 data << "float" << ' ';