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 docstring InsetFloat::xhtml(odocstream & os, OutputParams const & rp) const
289 FloatList const & floats = buffer().params().documentClass().floats();
290 Floating const & ftype = floats.getType(params_.type);
291 string const htmltype = ftype.htmlType().empty() ?
292 "div" : ftype.htmlType();
293 string const htmlclass = ftype.htmlClass().empty() ?
294 "float-" + params_.type : ftype.htmlClass();
295 docstring const otag =
296 from_ascii("<" + htmltype + " class='float " + htmlclass + "'>\n");
297 docstring const ctag = from_ascii("</" + htmltype + ">\n");
299 odocstringstream out;
301 docstring caption = getCaptionHTML(rp);
303 if (!caption.empty())
304 out << "<div class='float-caption'>" << caption << "</div>\n";
306 docstring def = InsetText::xhtml(out, rp);
309 if (rp.inFloat == OutputParams::NONFLOAT)
310 // In this case, this float needs to be deferred, but we'll put it
311 // before anything the text itself deferred.
312 def = out.str() + '\n' + def;
314 // In this case, the whole thing is already being deferred, so
315 // we can write to the stream.
321 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
323 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
324 if (runparams_in.moving_arg)
328 OutputParams rp = runparams_in;
329 docstring const caption = getCaption(rp);
330 if (!caption.empty()) {
334 rp.inFloat = OutputParams::SUBFLOAT;
335 int const i = InsetText::latex(os, rp);
340 OutputParams runparams(runparams_in);
341 runparams.inFloat = OutputParams::MAINFLOAT;
343 FloatList const & floats = buffer().params().documentClass().floats();
344 string tmptype = params_.type;
345 if (params_.sideways)
346 tmptype = "sideways" + params_.type;
347 if (params_.wide && (!params_.sideways ||
348 params_.type == "figure" ||
349 params_.type == "table"))
351 // Figure out the float placement to use.
352 // From lowest to highest:
353 // - float default placement
354 // - document wide default placement
355 // - specific float placement
357 string const buf_placement = buffer().params().float_placement;
358 string const def_placement = floats.defaultPlacement(params_.type);
359 if (!params_.placement.empty()
360 && params_.placement != def_placement) {
361 placement = params_.placement;
362 } else if (params_.placement.empty()
363 && !buf_placement.empty()
364 && buf_placement != def_placement) {
365 placement = buf_placement;
368 // The \n is used to force \begin{<floatname>} to appear in a new line.
369 // The % is needed to prevent two consecutive \n chars in the case
370 // when the current output line is empty.
371 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
372 // We only output placement if different from the def_placement.
373 // sidewaysfloats always use their own page
374 if (!placement.empty() && !params_.sideways) {
375 os << '[' << from_ascii(placement) << ']';
379 int const i = InsetText::latex(os, runparams);
381 // The \n is used to force \end{<floatname>} to appear in a new line.
382 // In this case, we do not case if the current output line is empty.
383 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
389 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
391 os << '[' << buffer().B_("float") << ' '
392 << floatName(params_.type, buffer().params()) << ":\n";
393 InsetText::plaintext(os, runparams);
396 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
400 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
402 // FIXME Implement subfloat!
404 os << '<' << from_ascii(params_.type) << '>';
405 int const i = InsetText::docbook(os, runparams);
406 os << "</" << from_ascii(params_.type) << '>';
412 bool InsetFloat::insetAllowed(InsetCode code) const
414 return code != FOOT_CODE
415 && code != MARGIN_CODE
416 && (code != FLOAT_CODE || !params_.subfloat);
420 bool InsetFloat::showInsetDialog(BufferView * bv) const
422 if (!InsetText::showInsetDialog(bv))
423 bv->showDialog("float", params2string(params()),
424 const_cast<InsetFloat *>(this));
429 void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
437 void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
439 params_.sideways = s;
445 void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
447 params_.subfloat = s;
453 void InsetFloat::setNewLabel(BufferParams const & bp)
455 docstring lab = _("float: ");
457 if (params_.subfloat)
458 lab = _("subfloat: ");
460 lab += floatName(params_.type, bp);
465 if (params_.sideways)
466 lab += _(" (sideways)");
472 InsetCaption const * InsetFloat::getCaptionInset() const
474 ParagraphList::const_iterator pit = paragraphs().begin();
475 for (; pit != paragraphs().end(); ++pit) {
476 InsetList::const_iterator it = pit->insetList().begin();
477 for (; it != pit->insetList().end(); ++it) {
478 Inset & inset = *it->inset;
479 if (inset.lyxCode() == CAPTION_CODE) {
480 InsetCaption const * ins =
481 static_cast<InsetCaption const *>(it->inset);
490 docstring InsetFloat::getCaption(OutputParams const & runparams) const
492 if (paragraphs().empty())
495 InsetCaption const * ins = getCaptionInset();
499 odocstringstream ods;
500 ins->getOptArg(ods, runparams);
502 ins->getArgument(ods, runparams);
508 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
510 if (paragraphs().empty())
513 InsetCaption const * ins = getCaptionInset();
517 odocstringstream ods;
518 ins->getCaptionText(ods, runparams);
523 docstring InsetFloat::getCaptionHTML(OutputParams const & runparams) const
525 if (paragraphs().empty())
528 InsetCaption const * ins = getCaptionInset();
532 odocstringstream ods;
533 docstring def = ins->getCaptionHTML(ods, runparams);
540 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
542 params = InsetFloatParams();
546 istringstream data(in);
549 lex.setContext("InsetFloat::string2params");
550 lex >> "float" >> "Float";
551 lex >> params.type; // We have to read the type here!
556 string InsetFloat::params2string(InsetFloatParams const & params)
559 data << "float" << ' ';