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
9 * Full author contact details are available in file CREDITS.
14 #include "insetfloat.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
21 #include "dispatchresult.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
30 #include "outputparams.h"
31 #include "paragraph.h"
32 #include "TocBackend.h"
34 #include "support/lstrings.h"
35 #include "support/convert.h"
40 using support::contains;
45 using std::istringstream;
47 using std::ostringstream;
50 // With this inset it will be possible to support the latex package
51 // float.sty, and I am sure that with this and some additional support
52 // classes we can support similar functionality in other formats
54 // By using float.sty we will have the same handling for all floats, both
55 // for those already in existance (table and figure) and all user created
56 // ones¹. So suddenly we give the users the possibility of creating new
57 // kinds of floats on the fly. (and with a uniform look)
60 // \newfloat{type}{placement}{ext}[within]
61 // type - The "type" of the new class of floats, like program or
62 // algorithm. After the appropriate \newfloat, commands
63 // such as \begin{program} or \end{algorithm*} will be
65 // placement - The default placement for the given class of floats.
66 // They are like in standard LaTeX: t, b, p and h for top,
67 // bottom, page, and here, respectively. On top of that
68 // there is a new type, H, which does not really correspond
69 // to a float, since it means: put it "here" and nowhere else.
70 // Note, however that the H specifier is special and, because
71 // of implementation details cannot be used in the second
72 // argument of \newfloat.
73 // ext - The file name extension of an auxiliary file for the list
74 // of figures (or whatever). LaTeX writes the captions to
76 // within - This (optional) argument determines whether floats of this
77 // class will be numbered within some sectional unit of the
78 // document. For example, if within is equal to chapter, the
79 // floats will be numbered within chapters.
81 // style - plain, boxed, ruled
82 // \floatname{float}{floatname}
85 // \floatplacement{float}{placement}
88 // \restylefloat{float}
90 // \listof{type}{title}
93 // ¹ the algorithm float is defined using the float.sty package. Like this
95 // \newfloat{algorithm}{htbp}{loa}[<sect>]
96 // \floatname{algorithm}{Algorithm}
98 // The intention is that floats should be definable from two places:
100 // - the "gui" (i.e. by the user)
102 // From layout files.
103 // This should only be done for floats defined in a documentclass and that
104 // does not need any additional packages. The two most known floats in this
105 // category is "table" and "figure". Floats defined in layout files are only
106 // stored in lyx files if the user modifies them.
109 // There should be a gui dialog (and also a collection of lyxfuncs) where
110 // the user can modify existing floats and/or create new ones.
112 // The individual floats will also have some settable
113 // variables: wide and placement.
118 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
119 : InsetCollapsable(bp)
121 setLabel(_("float: ") + floatName(type, bp));
122 LyXFont font(LyXFont::ALL_SANE);
125 font.setColor(LColor::collapsable);
128 setInsetName(from_utf8(type));
132 InsetFloat::~InsetFloat()
134 InsetFloatMailer(*this).hideDialog();
138 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
140 switch (cmd.action) {
142 case LFUN_INSET_MODIFY: {
143 InsetFloatParams params;
144 InsetFloatMailer::string2params(to_utf8(cmd.argument()), params);
145 params_.placement = params.placement;
146 params_.wide = params.wide;
147 params_.sideways = params.sideways;
148 wide(params_.wide, cur.buffer().params());
149 sideways(params_.sideways, cur.buffer().params());
153 case LFUN_INSET_DIALOG_UPDATE: {
154 InsetFloatMailer(*this).updateDialog(&cur.bv());
158 case LFUN_MOUSE_RELEASE: {
159 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
160 InsetFloatMailer(*this).showDialog(&cur.bv());
163 InsetCollapsable::doDispatch(cur, cmd);
168 InsetCollapsable::doDispatch(cur, cmd);
174 bool InsetFloat::getStatus(LCursor & cur, FuncRequest const & cmd,
175 FuncStatus & flag) const
177 switch (cmd.action) {
179 case LFUN_INSET_MODIFY:
180 case LFUN_INSET_DIALOG_UPDATE:
185 return InsetCollapsable::getStatus(cur, cmd, flag);
190 void InsetFloatParams::write(ostream & os) const
192 os << "Float " << type << '\n';
194 if (!placement.empty())
195 os << "placement " << placement << "\n";
200 os << "wide false\n";
203 os << "sideways true\n";
205 os << "sideways false\n";
209 void InsetFloatParams::read(LyXLex & lex)
213 if (token == "placement") {
216 // take countermeasures
217 lex.pushToken(token);
220 if (token == "wide") {
223 lyxerr << "InsetFloat::Read:: Missing wide!"
225 // take countermeasures
226 lex.pushToken(token);
229 if (token == "sideways") {
232 lyxerr << "InsetFloat::Read:: Missing sideways!"
234 // take countermeasures
235 lex.pushToken(token);
240 void InsetFloat::write(Buffer const & buf, ostream & os) const
243 InsetCollapsable::write(buf, os);
247 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
250 wide(params_.wide, buf.params());
251 sideways(params_.sideways, buf.params());
252 InsetCollapsable::read(buf, lex);
256 void InsetFloat::validate(LaTeXFeatures & features) const
258 if (contains(params_.placement, 'H')) {
259 features.require("float");
262 if (params_.sideways)
263 features.require("rotating");
265 features.useFloat(params_.type);
266 InsetCollapsable::validate(features);
270 auto_ptr<InsetBase> InsetFloat::doClone() const
272 return auto_ptr<InsetBase>(new InsetFloat(*this));
276 docstring const InsetFloat::editMessage() const
278 return _("Opened Float Inset");
282 int InsetFloat::latex(Buffer const & buf, odocstream & os,
283 OutputParams const & runparams) const
285 FloatList const & floats = buf.params().getLyXTextClass().floats();
286 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
287 if (params_.sideways) {
288 if (params_.type == "table")
289 tmptype = "sidewaystable";
290 else if (params_.type == "figure")
291 tmptype = "sidewaysfigure";
293 // Figure out the float placement to use.
294 // From lowest to highest:
295 // - float default placement
296 // - document wide default placement
297 // - specific float placement
299 string const buf_placement = buf.params().float_placement;
300 string const def_placement = floats.defaultPlacement(params_.type);
301 if (!params_.placement.empty()
302 && params_.placement != def_placement) {
303 placement = params_.placement;
304 } else if (params_.placement.empty()
305 && !buf_placement.empty()
306 && buf_placement != def_placement) {
307 placement = buf_placement;
310 // The \n is used to force \begin{<floatname>} to appear in a new line.
311 // The % is needed to prevent two consecutive \n chars in the case
312 // when the current output line is empty.
313 os << "%\n\\begin{" << from_ascii(tmptype) << '}';
314 // We only output placement if different from the def_placement.
315 // sidewaysfloats always use their own page
316 if (!placement.empty() && !params_.sideways) {
317 os << '[' << from_ascii(placement) << ']';
321 int const i = InsetText::latex(buf, os, runparams);
323 // The \n is used to force \end{<floatname>} to appear in a new line.
324 // In this case, we do not case if the current output line is empty.
325 os << "\n\\end{" << from_ascii(tmptype) << "}\n";
331 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
332 OutputParams const & runparams) const
335 os << '<' << from_ascii(params_.type) << '>';
336 int const i = InsetText::docbook(buf, os, runparams);
337 os << "</" << from_ascii(params_.type) << '>';
343 bool InsetFloat::insetAllowed(InsetBase::Code code) const
345 return code != InsetBase::FLOAT_CODE
346 && code != InsetBase::FOOT_CODE
347 && code != InsetBase::MARGIN_CODE;
351 bool InsetFloat::showInsetDialog(BufferView * bv) const
353 if (!InsetText::showInsetDialog(bv))
354 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
359 void InsetFloat::wide(bool w, BufferParams const & bp)
362 docstring lab = _("float: ") + floatName(params_.type, bp);
369 void InsetFloat::sideways(bool s, BufferParams const & bp)
371 params_.sideways = s;
372 docstring lab = _("float: ") + floatName(params_.type, bp);
373 if (params_.sideways)
374 lab += _(" (sideways)");
379 void InsetFloat::addToToc(TocList & toclist, Buffer const & buf) const
381 ParConstIterator pit = par_const_iterator_begin(*this);
382 ParConstIterator end = par_const_iterator_end(*this);
384 // Find a caption layout in one of the (child inset's) pars
385 for (; pit != end; ++pit) {
386 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
387 string const type = params_.type;
388 docstring const str =
389 convert<docstring>(toclist[type].size() + 1)
390 + ". " + pit->asString(buf, false);
391 TocItem const item(pit, 0, str);
392 toclist[type].push_back(item);
398 string const InsetFloatMailer::name_("float");
400 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
405 string const InsetFloatMailer::inset2string(Buffer const &) const
407 return params2string(inset_.params());
411 void InsetFloatMailer::string2params(string const & in,
412 InsetFloatParams & params)
414 params = InsetFloatParams();
418 istringstream data(in);
424 if (!lex || name != name_)
425 return print_mailer_error("InsetFloatMailer", in, 1, name_);
427 // This is part of the inset proper that is usually swallowed
428 // by LyXText::readInset
431 if (!lex || id != "Float")
432 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
434 // We have to read the type here!
440 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
443 data << name_ << ' ';