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/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
40 #include "frontends/Application.h"
47 // With this inset it will be possible to support the latex package
48 // float.sty, and I am sure that with this and some additional support
49 // classes we can support similar functionality in other formats
51 // By using float.sty we will have the same handling for all floats, both
52 // for those already in existance (table and figure) and all user created
53 // ones¹. So suddenly we give the users the possibility of creating new
54 // kinds of floats on the fly. (and with a uniform look)
57 // \newfloat{type}{placement}{ext}[within]
58 // type - The "type" of the new class of floats, like program or
59 // algorithm. After the appropriate \newfloat, commands
60 // such as \begin{program} or \end{algorithm*} will be
62 // placement - The default placement for the given class of floats.
63 // They are like in standard LaTeX: t, b, p and h for top,
64 // bottom, page, and here, respectively. On top of that
65 // there is a new type, H, which does not really correspond
66 // to a float, since it means: put it "here" and nowhere else.
67 // Note, however that the H specifier is special and, because
68 // of implementation details cannot be used in the second
69 // argument of \newfloat.
70 // ext - The file name extension of an auxiliary file for the list
71 // of figures (or whatever). LaTeX writes the captions to
73 // within - This (optional) argument determines whether floats of this
74 // class will be numbered within some sectional unit of the
75 // document. For example, if within is equal to chapter, the
76 // floats will be numbered within chapters.
78 // style - plain, boxed, ruled
79 // \floatname{float}{floatname}
82 // \floatplacement{float}{placement}
85 // \restylefloat{float}
87 // \listof{type}{title}
90 // ¹ the algorithm float is defined using the float.sty package. Like this
92 // \newfloat{algorithm}{htbp}{loa}[<sect>]
93 // \floatname{algorithm}{Algorithm}
95 // The intention is that floats should be definable from two places:
97 // - the "gui" (i.e. by the user)
100 // This should only be done for floats defined in a documentclass and that
101 // does not need any additional packages. The two most known floats in this
102 // category is "table" and "figure". Floats defined in layout files are only
103 // stored in lyx files if the user modifies them.
106 // There should be a gui dialog (and also a collection of lyxfuncs) where
107 // the user can modify existing floats and/or create new ones.
109 // The individual floats will also have some settable
110 // variables: wide and placement.
115 InsetFloat::InsetFloat(Buffer const & buf, string const & type)
116 : InsetCollapsable(buf), name_(from_utf8(type))
118 setLabel(_("float: ") + floatName(type, buf.params()));
123 InsetFloat::~InsetFloat()
125 hideDialogs("float", this);
129 docstring InsetFloat::name() const
131 return "Float:" + name_;
135 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
137 if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
140 OutputParams rp(&buffer().params().encoding());
141 return getCaptionText(rp);
145 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
147 switch (cmd.action) {
149 case LFUN_INSET_MODIFY: {
150 InsetFloatParams params;
151 string2params(to_utf8(cmd.argument()), params);
153 // placement, wide and sideways are not used for subfloats
154 if (!params_.subfloat) {
155 params_.placement = params.placement;
156 params_.wide = params.wide;
157 params_.sideways = params.sideways;
158 setWide(params_.wide, cur.buffer()->params(), false);
159 setSideways(params_.sideways, cur.buffer()->params(), false);
162 setNewLabel(cur.buffer()->params());
166 case LFUN_INSET_DIALOG_UPDATE: {
167 cur.bv().updateDialog("float", params2string(params()));
172 InsetCollapsable::doDispatch(cur, cmd);
178 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
179 FuncStatus & flag) const
181 switch (cmd.action) {
183 case LFUN_INSET_MODIFY:
184 case LFUN_INSET_DIALOG_UPDATE:
185 flag.setEnabled(true);
189 return InsetCollapsable::getStatus(cur, cmd, flag);
194 void InsetFloat::updateLabels(ParIterator const & it)
197 buffer().masterBuffer()->params().documentClass().counters();
198 string const saveflt = cnts.current_float();
199 bool const savesubflt = cnts.isSubfloat();
201 bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
202 || it.innerInsetOfType(WRAP_CODE));
203 // floats can only embed subfloats of their own kind
205 params_.type = saveflt;
206 setSubfloat(subflt, buffer().params());
208 // Tell to captions what the current float is
209 cnts.current_float(params().type);
210 cnts.isSubfloat(subflt);
212 InsetCollapsable::updateLabels(it);
215 cnts.current_float(saveflt);
216 cnts.isSubfloat(savesubflt);
220 void InsetFloatParams::write(ostream & os) const
222 os << "Float " << type << '\n';
224 if (!placement.empty())
225 os << "placement " << placement << "\n";
230 os << "wide false\n";
233 os << "sideways true\n";
235 os << "sideways false\n";
239 void InsetFloatParams::read(Lexer & lex)
241 lex.setContext("InsetFloatParams::read");
242 if (lex.checkFor("placement"))
244 lex >> "wide" >> wide;
245 lex >> "sideways" >> sideways;
249 void InsetFloat::write(ostream & os) const
252 InsetCollapsable::write(os);
256 void InsetFloat::read(Lexer & lex)
259 InsetCollapsable::read(lex);
263 void InsetFloat::validate(LaTeXFeatures & features) const
265 if (support::contains(params_.placement, 'H'))
266 features.require("float");
268 if (params_.sideways)
269 features.require("rotfloat");
271 if (features.inFloat())
272 features.require("subfig");
274 features.useFloat(params_.type, features.inFloat());
275 features.inFloat(true);
276 InsetCollapsable::validate(features);
277 features.inFloat(false);
281 docstring InsetFloat::editMessage() const
283 return _("Opened Float Inset");
287 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
289 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
290 if (runparams_in.moving_arg)
294 OutputParams rp = runparams_in;
295 docstring const caption = getCaption(rp);
296 if (!caption.empty()) {
300 rp.inFloat = OutputParams::SUBFLOAT;
301 int const i = InsetText::latex(os, rp);
306 OutputParams runparams(runparams_in);
307 runparams.inFloat = OutputParams::MAINFLOAT;
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 InsetCaption const * InsetFloat::getCaptionInset() const
440 ParagraphList::const_iterator pit = paragraphs().begin();
441 for (; pit != paragraphs().end(); ++pit) {
442 InsetList::const_iterator it = pit->insetList().begin();
443 for (; it != pit->insetList().end(); ++it) {
444 Inset & inset = *it->inset;
445 if (inset.lyxCode() == CAPTION_CODE) {
446 InsetCaption const * ins =
447 static_cast<InsetCaption const *>(it->inset);
456 docstring InsetFloat::getCaption(OutputParams const & runparams) const
458 if (paragraphs().empty())
461 InsetCaption const * ins = getCaptionInset();
465 odocstringstream ods;
466 ins->getOptArg(ods, runparams);
468 ins->getArgument(ods, runparams);
474 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
476 if (paragraphs().empty())
479 InsetCaption const * ins = getCaptionInset();
483 odocstringstream ods;
484 ins->getCaptionText(ods, runparams);
489 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
491 params = InsetFloatParams();
495 istringstream data(in);
498 lex.setContext("InsetFloat::string2params");
499 lex >> "float" >> "Float";
500 lex >> params.type; // We have to read the type here!
505 string InsetFloat::params2string(InsetFloatParams const & params)
508 data << "float" << ' ';