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 "pariterator.h"
34 #include "support/lstrings.h"
35 #include "support/convert.h"
40 using lyx::odocstream;
41 using lyx::support::contains;
46 using std::istringstream;
48 using std::ostringstream;
51 // With this inset it will be possible to support the latex package
52 // float.sty, and I am sure that with this and some additional support
53 // classes we can support similar functionality in other formats
55 // By using float.sty we will have the same handling for all floats, both
56 // for those already in existance (table and figure) and all user created
57 // ones¹. So suddenly we give the users the possibility of creating new
58 // kinds of floats on the fly. (and with a uniform look)
61 // \newfloat{type}{placement}{ext}[within]
62 // type - The "type" of the new class of floats, like program or
63 // algorithm. After the appropriate \newfloat, commands
64 // such as \begin{program} or \end{algorithm*} will be
66 // placement - The default placement for the given class of floats.
67 // They are like in standard LaTeX: t, b, p and h for top,
68 // bottom, page, and here, respectively. On top of that
69 // there is a new type, H, which does not really correspond
70 // to a float, since it means: put it "here" and nowhere else.
71 // Note, however that the H specifier is special and, because
72 // of implementation details cannot be used in the second
73 // argument of \newfloat.
74 // ext - The file name extension of an auxiliary file for the list
75 // of figures (or whatever). LaTeX writes the captions to
77 // within - This (optional) argument determines whether floats of this
78 // class will be numbered within some sectional unit of the
79 // document. For example, if within is equal to chapter, the
80 // floats will be numbered within chapters.
82 // style - plain, boxed, ruled
83 // \floatname{float}{floatname}
86 // \floatplacement{float}{placement}
89 // \restylefloat{float}
91 // \listof{type}{title}
94 // ¹ the algorithm float is defined using the float.sty package. Like this
96 // \newfloat{algorithm}{htbp}{loa}[<sect>]
97 // \floatname{algorithm}{Algorithm}
99 // The intention is that floats should be definable from two places:
101 // - the "gui" (i.e. by the user)
103 // From layout files.
104 // This should only be done for floats defined in a documentclass and that
105 // does not need any additional packages. The two most known floats in this
106 // category is "table" and "figure". Floats defined in layout files are only
107 // stored in lyx files if the user modifies them.
110 // There should be a gui dialog (and also a collection of lyxfuncs) where
111 // the user can modify existing floats and/or create new ones.
113 // The individual floats will also have some settable
114 // variables: wide and placement.
119 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
120 : InsetCollapsable(bp)
122 setLabel(_("float: ") + floatName(type, bp));
123 LyXFont font(LyXFont::ALL_SANE);
126 font.setColor(LColor::collapsable);
133 InsetFloat::~InsetFloat()
135 InsetFloatMailer(*this).hideDialog();
139 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
141 switch (cmd.action) {
143 case LFUN_INSET_MODIFY: {
144 InsetFloatParams params;
145 InsetFloatMailer::string2params(lyx::to_utf8(cmd.argument()), params);
146 params_.placement = params.placement;
147 params_.wide = params.wide;
148 params_.sideways = params.sideways;
149 wide(params_.wide, cur.buffer().params());
150 sideways(params_.sideways, cur.buffer().params());
154 case LFUN_INSET_DIALOG_UPDATE: {
155 InsetFloatMailer(*this).updateDialog(&cur.bv());
159 case LFUN_MOUSE_RELEASE: {
160 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
161 InsetFloatMailer(*this).showDialog(&cur.bv());
164 InsetCollapsable::doDispatch(cur, cmd);
169 InsetCollapsable::doDispatch(cur, cmd);
175 bool InsetFloat::getStatus(LCursor & cur, FuncRequest const & cmd,
176 FuncStatus & flag) const
178 switch (cmd.action) {
180 case LFUN_INSET_MODIFY:
181 case LFUN_INSET_DIALOG_UPDATE:
186 return InsetCollapsable::getStatus(cur, cmd, flag);
191 void InsetFloatParams::write(ostream & os) const
193 os << "Float " << type << '\n';
195 if (!placement.empty())
196 os << "placement " << placement << "\n";
201 os << "wide false\n";
204 os << "sideways true\n";
206 os << "sideways false\n";
210 void InsetFloatParams::read(LyXLex & lex)
214 if (token == "placement") {
217 // take countermeasures
218 lex.pushToken(token);
221 if (token == "wide") {
224 lyxerr << "InsetFloat::Read:: Missing wide!"
226 // take countermeasures
227 lex.pushToken(token);
230 if (token == "sideways") {
233 lyxerr << "InsetFloat::Read:: Missing sideways!"
235 // take countermeasures
236 lex.pushToken(token);
241 void InsetFloat::write(Buffer const & buf, ostream & os) const
244 InsetCollapsable::write(buf, os);
248 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
251 wide(params_.wide, buf.params());
252 sideways(params_.sideways, buf.params());
253 InsetCollapsable::read(buf, lex);
257 void InsetFloat::validate(LaTeXFeatures & features) const
259 if (contains(params_.placement, 'H')) {
260 features.require("float");
263 if (params_.sideways)
264 features.require("rotating");
266 features.useFloat(params_.type);
267 InsetCollapsable::validate(features);
271 auto_ptr<InsetBase> InsetFloat::doClone() const
273 return auto_ptr<InsetBase>(new InsetFloat(*this));
277 docstring const InsetFloat::editMessage() const
279 return _("Opened Float Inset");
283 int InsetFloat::latex(Buffer const & buf, odocstream & os,
284 OutputParams const & runparams) const
286 FloatList const & floats = buf.params().getLyXTextClass().floats();
287 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
288 if (params_.sideways) {
289 if (params_.type == "table")
290 tmptype = "sidewaystable";
291 else if (params_.type == "figure")
292 tmptype = "sidewaysfigure";
294 // Figure out the float placement to use.
295 // From lowest to highest:
296 // - float default placement
297 // - document wide default placement
298 // - specific float placement
300 string const buf_placement = buf.params().float_placement;
301 string const def_placement = floats.defaultPlacement(params_.type);
302 if (!params_.placement.empty()
303 && params_.placement != def_placement) {
304 placement = params_.placement;
305 } else if (params_.placement.empty()
306 && !buf_placement.empty()
307 && buf_placement != def_placement) {
308 placement = buf_placement;
311 // The \n is used to force \begin{<floatname>} to appear in a new line.
312 // The % is needed to prevent two consecutive \n chars in the case
313 // when the current output line is empty.
314 os << "%\n\\begin{" << lyx::from_ascii(tmptype) << '}';
315 // We only output placement if different from the def_placement.
316 // sidewaysfloats always use their own page
317 if (!placement.empty() && !params_.sideways) {
318 os << '[' << lyx::from_ascii(placement) << ']';
322 int const i = InsetText::latex(buf, os, runparams);
324 // The \n is used to force \end{<floatname>} to appear in a new line.
325 // In this case, we do not case if the current output line is empty.
326 os << "\n\\end{" << lyx::from_ascii(tmptype) << "}\n";
332 int InsetFloat::docbook(Buffer const & buf, odocstream & os,
333 OutputParams const & runparams) const
336 os << '<' << lyx::from_ascii(params_.type) << '>';
337 int const i = InsetText::docbook(buf, os, runparams);
338 os << "</" << lyx::from_ascii(params_.type) << '>';
344 bool InsetFloat::insetAllowed(InsetBase::Code code) const
346 return code != InsetBase::FLOAT_CODE
347 && code != InsetBase::FOOT_CODE
348 && code != InsetBase::MARGIN_CODE;
352 bool InsetFloat::showInsetDialog(BufferView * bv) const
354 if (!InsetText::showInsetDialog(bv))
355 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
360 void InsetFloat::wide(bool w, BufferParams const & bp)
363 docstring lab = _("float: ") + floatName(params_.type, bp);
370 void InsetFloat::sideways(bool s, BufferParams const & bp)
372 params_.sideways = s;
373 docstring lab = _("float: ") + floatName(params_.type, bp);
374 if (params_.sideways)
375 lab += _(" (sideways)");
380 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
382 ParConstIterator pit = par_const_iterator_begin(*this);
383 ParConstIterator end = par_const_iterator_end(*this);
385 // Find a caption layout in one of the (child inset's) pars
386 for (; pit != end; ++pit) {
387 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
388 string const type = params_.type;
389 docstring const str =
390 convert<docstring>(toclist[type].size() + 1)
391 + ". " + pit->asString(buf, false);
392 lyx::toc::TocItem const item(pit, 0, str);
393 toclist[type].push_back(item);
399 string const InsetFloatMailer::name_("float");
401 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
406 string const InsetFloatMailer::inset2string(Buffer const &) const
408 return params2string(inset_.params());
412 void InsetFloatMailer::string2params(string const & in,
413 InsetFloatParams & params)
415 params = InsetFloatParams();
419 istringstream data(in);
425 if (!lex || name != name_)
426 return print_mailer_error("InsetFloatMailer", in, 1, name_);
428 // This is part of the inset proper that is usually swallowed
429 // by LyXText::readInset
432 if (!lex || id != "Float")
433 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
435 // We have to read the type here!
441 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
444 data << name_ << ' ';