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"
39 using lyx::support::contains;
44 using std::istringstream;
46 using std::ostringstream;
49 // With this inset it will be possible to support the latex package
50 // float.sty, and I am sure that with this and some additional support
51 // classes we can support similar functionality in other formats
53 // By using float.sty we will have the same handling for all floats, both
54 // for those already in existance (table and figure) and all user created
55 // ones¹. So suddenly we give the users the possibility of creating new
56 // kinds of floats on the fly. (and with a uniform look)
59 // \newfloat{type}{placement}{ext}[within]
60 // type - The "type" of the new class of floats, like program or
61 // algorithm. After the appropriate \newfloat, commands
62 // such as \begin{program} or \end{algorithm*} will be
64 // placement - The default placement for the given class of floats.
65 // They are like in standard LaTeX: t, b, p and h for top,
66 // bottom, page, and here, respectively. On top of that
67 // there is a new type, H, which does not really correspond
68 // to a float, since it means: put it "here" and nowhere else.
69 // Note, however that the H specifier is special and, because
70 // of implementation details cannot be used in the second
71 // argument of \newfloat.
72 // ext - The file name extension of an auxiliary file for the list
73 // of figures (or whatever). LaTeX writes the captions to
75 // within - This (optional) argument determines whether floats of this
76 // class will be numbered within some sectional unit of the
77 // document. For example, if within is equal to chapter, the
78 // floats will be numbered within chapters.
80 // style - plain, boxed, ruled
81 // \floatname{float}{floatname}
84 // \floatplacement{float}{placement}
87 // \restylefloat{float}
89 // \listof{type}{title}
92 // ¹ the algorithm float is defined using the float.sty package. Like this
94 // \newfloat{algorithm}{htbp}{loa}[<sect>]
95 // \floatname{algorithm}{Algorithm}
97 // The intention is that floats should be definable from two places:
99 // - the "gui" (i.e. by the user)
101 // From layout files.
102 // This should only be done for floats defined in a documentclass and that
103 // does not need any additional packages. The two most known floats in this
104 // category is "table" and "figure". Floats defined in layout files are only
105 // stored in lyx files if the user modifies them.
108 // There should be a gui dialog (and also a collection of lyxfuncs) where
109 // the user can modify existing floats and/or create new ones.
111 // The individual floats will also have some settable
112 // variables: wide and placement.
118 string floatname(string const & type, BufferParams const & bp)
120 FloatList const & floats = bp.getLyXTextClass().floats();
121 FloatList::const_iterator it = floats[type];
122 if (it == floats.end())
126 return lyx::to_utf8(_(it->second.name()));
132 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
133 : InsetCollapsable(bp)
136 setLabel(lyx::to_utf8(_("float: ")) + floatname(type, bp));
137 LyXFont font(LyXFont::ALL_SANE);
140 font.setColor(LColor::collapsable);
147 InsetFloat::~InsetFloat()
149 InsetFloatMailer(*this).hideDialog();
153 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
155 switch (cmd.action) {
157 case LFUN_INSET_MODIFY: {
158 InsetFloatParams params;
159 InsetFloatMailer::string2params(lyx::to_utf8(cmd.argument()), params);
160 params_.placement = params.placement;
161 params_.wide = params.wide;
162 params_.sideways = params.sideways;
163 wide(params_.wide, cur.buffer().params());
164 sideways(params_.sideways, cur.buffer().params());
168 case LFUN_INSET_DIALOG_UPDATE: {
169 InsetFloatMailer(*this).updateDialog(&cur.bv());
173 case LFUN_MOUSE_RELEASE: {
174 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
175 InsetFloatMailer(*this).showDialog(&cur.bv());
178 InsetCollapsable::doDispatch(cur, cmd);
183 InsetCollapsable::doDispatch(cur, cmd);
189 bool InsetFloat::getStatus(LCursor & cur, FuncRequest const & cmd,
190 FuncStatus & flag) const
192 switch (cmd.action) {
194 case LFUN_INSET_MODIFY:
195 case LFUN_INSET_DIALOG_UPDATE:
200 return InsetCollapsable::getStatus(cur, cmd, flag);
205 void InsetFloatParams::write(ostream & os) const
207 os << "Float " << type << '\n';
209 if (!placement.empty())
210 os << "placement " << placement << "\n";
215 os << "wide false\n";
218 os << "sideways true\n";
220 os << "sideways false\n";
224 void InsetFloatParams::read(LyXLex & lex)
228 if (token == "placement") {
231 // take countermeasures
232 lex.pushToken(token);
235 if (token == "wide") {
238 lyxerr << "InsetFloat::Read:: Missing wide!"
240 // take countermeasures
241 lex.pushToken(token);
244 if (token == "sideways") {
247 lyxerr << "InsetFloat::Read:: Missing sideways!"
249 // take countermeasures
250 lex.pushToken(token);
255 void InsetFloat::write(Buffer const & buf, ostream & os) const
258 InsetCollapsable::write(buf, os);
262 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
265 wide(params_.wide, buf.params());
266 sideways(params_.sideways, buf.params());
267 InsetCollapsable::read(buf, lex);
271 void InsetFloat::validate(LaTeXFeatures & features) const
273 if (contains(params_.placement, 'H')) {
274 features.require("float");
277 if (params_.sideways)
278 features.require("rotating");
280 features.useFloat(params_.type);
281 InsetCollapsable::validate(features);
285 auto_ptr<InsetBase> InsetFloat::doClone() const
287 return auto_ptr<InsetBase>(new InsetFloat(*this));
291 string const InsetFloat::editMessage() const
294 return lyx::to_utf8(_("Opened Float Inset"));
298 int InsetFloat::latex(Buffer const & buf, ostream & os,
299 OutputParams const & runparams) const
301 FloatList const & floats = buf.params().getLyXTextClass().floats();
302 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
303 if (params_.sideways) {
304 if (params_.type == "table")
305 tmptype = "sidewaystable";
306 else if (params_.type == "figure")
307 tmptype = "sidewaysfigure";
309 // Figure out the float placement to use.
310 // From lowest to highest:
311 // - float default placement
312 // - document wide default placement
313 // - specific float placement
315 string const buf_placement = buf.params().float_placement;
316 string const def_placement = floats.defaultPlacement(params_.type);
317 if (!params_.placement.empty()
318 && params_.placement != def_placement) {
319 placement = params_.placement;
320 } else if (params_.placement.empty()
321 && !buf_placement.empty()
322 && buf_placement != def_placement) {
323 placement = buf_placement;
326 // The \n is used to force \begin{<floatname>} to appear in a new line.
327 // The % is needed to prevent two consecutive \n chars in the case
328 // when the current output line is empty.
329 os << "%\n\\begin{" << tmptype << '}';
330 // We only output placement if different from the def_placement.
331 // sidewaysfloats always use their own page
332 if (!placement.empty() && !params_.sideways) {
333 os << '[' << placement << ']';
337 int const i = InsetText::latex(buf, os, runparams);
339 // The \n is used to force \end{<floatname>} to appear in a new line.
340 // In this case, we do not case if the current output line is empty.
341 os << "\n\\end{" << tmptype << "}\n";
347 int InsetFloat::docbook(Buffer const & buf, ostream & os,
348 OutputParams const & runparams) const
350 os << '<' << params_.type << '>';
351 int const i = InsetText::docbook(buf, os, runparams);
352 os << "</" << params_.type << '>';
358 bool InsetFloat::insetAllowed(InsetBase::Code code) const
360 return code != InsetBase::FLOAT_CODE
361 && code != InsetBase::FOOT_CODE
362 && code != InsetBase::MARGIN_CODE;
366 bool InsetFloat::showInsetDialog(BufferView * bv) const
368 if (!InsetText::showInsetDialog(bv))
369 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
374 void InsetFloat::wide(bool w, BufferParams const & bp)
378 string lab = lyx::to_utf8(_("float: ")) + floatname(params_.type, bp);
385 void InsetFloat::sideways(bool s, BufferParams const & bp)
387 params_.sideways = s;
389 string lab = lyx::to_utf8(_("float: ")) + floatname(params_.type, bp);
390 if (params_.sideways)
391 lab += lyx::to_utf8(_(" (sideways)"));
396 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
398 ParConstIterator pit = par_const_iterator_begin(*this);
399 ParConstIterator end = par_const_iterator_end(*this);
401 // Find a caption layout in one of the (child inset's) pars
402 for (; pit != end; ++pit) {
403 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
404 string const type = params_.type;
406 convert<string>(toclist[type].size() + 1)
407 + ". " + pit->asString(buf, false);
408 lyx::toc::TocItem const item(pit, 0 , str);
409 toclist[type].push_back(item);
415 string const InsetFloatMailer::name_("float");
417 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
422 string const InsetFloatMailer::inset2string(Buffer const &) const
424 return params2string(inset_.params());
428 void InsetFloatMailer::string2params(string const & in,
429 InsetFloatParams & params)
431 params = InsetFloatParams();
435 istringstream data(in);
441 if (!lex || name != name_)
442 return print_mailer_error("InsetFloatMailer", in, 1, name_);
444 // This is part of the inset proper that is usually swallowed
445 // by LyXText::readInset
448 if (!lex || id != "Float")
449 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
451 // We have to read the type here!
457 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
460 data << name_ << ' ';