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"
26 #include "LaTeXFeatures.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "pariterator.h"
33 #include "support/lstrings.h"
34 #include "support/tostr.h"
35 #include "support/std_sstream.h"
37 using lyx::support::contains;
42 using std::istringstream;
44 using std::ostringstream;
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.
116 // this should not be hardcoded, but be part of the definition
117 // of the float (JMarc)
118 string const caplayout("Caption");
120 string floatname(string const & type, BufferParams const & bp)
122 FloatList const & floats = bp.getLyXTextClass().floats();
123 FloatList::const_iterator it = floats[type];
124 if (it == floats.end())
127 return _(it->second.name());
133 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
134 : InsetCollapsable(bp)
136 setLabel(_("float: ") + floatname(type, bp));
137 LyXFont font(LyXFont::ALL_SANE);
140 font.setColor(LColor::collapsable);
144 LyXTextClass const & tclass = bp.getLyXTextClass();
145 if (tclass.hasLayout(caplayout))
146 paragraphs().begin()->layout(tclass[caplayout]);
150 InsetFloat::~InsetFloat()
152 InsetFloatMailer(*this).hideDialog();
156 void InsetFloat::priv_dispatch(LCursor & cur, FuncRequest & cmd)
158 switch (cmd.action) {
160 case LFUN_INSET_MODIFY: {
161 InsetFloatParams params;
162 InsetFloatMailer::string2params(cmd.argument, params);
163 params_.placement = params.placement;
164 params_.wide = params.wide;
165 params_.sideways = params.sideways;
166 wide(params_.wide, cur.buffer().params());
167 sideways(params_.sideways, cur.buffer().params());
172 case LFUN_INSET_DIALOG_UPDATE: {
173 InsetFloatMailer(*this).updateDialog(&cur.bv());
177 case LFUN_MOUSE_RELEASE: {
178 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
179 InsetFloatMailer(*this).showDialog(&cur.bv());
182 InsetCollapsable::priv_dispatch(cur, cmd);
187 InsetCollapsable::priv_dispatch(cur, cmd);
193 void InsetFloatParams::write(ostream & os) const
195 os << "Float " << type << '\n';
197 if (!placement.empty())
198 os << "placement " << placement << "\n";
203 os << "wide false\n";
206 os << "sideways true\n";
208 os << "sideways false\n";
212 void InsetFloatParams::read(LyXLex & lex)
216 if (token == "placement") {
219 // take countermeasures
220 lex.pushToken(token);
223 if (token == "wide") {
226 lyxerr << "InsetFloat::Read:: Missing wide!"
228 // take countermeasures
229 lex.pushToken(token);
232 if (token == "sideways") {
235 lyxerr << "InsetFloat::Read:: Missing sideways!"
237 // take countermeasures
238 lex.pushToken(token);
243 void InsetFloat::write(Buffer const & buf, ostream & os) const
246 InsetCollapsable::write(buf, os);
250 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
253 wide(params_.wide, buf.params());
254 sideways(params_.sideways, buf.params());
255 InsetCollapsable::read(buf, lex);
259 void InsetFloat::validate(LaTeXFeatures & features) const
261 if (contains(params_.placement, 'H')) {
262 features.require("float");
265 if (params_.sideways)
266 features.require("rotating");
268 features.useFloat(params_.type);
269 InsetCollapsable::validate(features);
273 auto_ptr<InsetBase> InsetFloat::clone() const
275 return auto_ptr<InsetBase>(new InsetFloat(*this));
279 string const InsetFloat::editMessage() const
281 return _("Opened Float Inset");
285 int InsetFloat::latex(Buffer const & buf, ostream & os,
286 OutputParams const & runparams) const
288 FloatList const & floats = buf.params().getLyXTextClass().floats();
289 string tmptype = (params_.wide ? params_.type + "*" : params_.type);
290 if (params_.sideways) {
291 if (params_.type == "table")
292 tmptype = "sidewaystable";
293 else if (params_.type == "figure")
294 tmptype = "sidewaysfigure";
296 // Figure out the float placement to use.
297 // From lowest to highest:
298 // - float default placement
299 // - document wide default placement
300 // - specific float placement
302 string const buf_placement = buf.params().float_placement;
303 string const def_placement = floats.defaultPlacement(params_.type);
304 if (!params_.placement.empty()
305 && params_.placement != def_placement) {
306 placement = params_.placement;
307 } else if (params_.placement.empty()
308 && !buf_placement.empty()
309 && buf_placement != def_placement) {
310 placement = buf_placement;
313 // The \n is used to force \begin{<floatname>} to appear in a new line.
314 // The % is needed to prevent two consecutive \n chars in the case
315 // when the current output line is empty.
316 os << "%\n\\begin{" << tmptype << '}';
317 // We only output placement if different from the def_placement.
318 // sidewaysfloats always use their own page
319 if (!placement.empty() && !params_.sideways) {
320 os << '[' << placement << ']';
324 int const i = InsetText::latex(buf, os, runparams);
326 // The \n is used to force \end{<floatname>} to appear in a new line.
327 // In this case, we do not case if the current output line is empty.
328 os << "\n\\end{" << tmptype << "}\n";
334 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
335 OutputParams const & runparams) const
337 FloatList const & floats = buf.params().getLyXTextClass().floats();
338 string const tmptype = params_.type;
339 // Figure out the float placement to use.
340 // From lowest to highest:
341 // - float default placement
342 // - document wide default placement
343 // - specific float placement
344 // This is the same as latex, as linuxdoc is modeled after latex.
347 string const buf_placement = buf.params().float_placement;
348 string const def_placement = floats.defaultPlacement(params_.type);
349 if (!params_.placement.empty()
350 && params_.placement != def_placement) {
351 placement = params_.placement;
352 } else if (params_.placement.empty()
353 && !buf_placement.empty()
354 && buf_placement != def_placement) {
355 placement = buf_placement;
358 os << "\n<" << tmptype ;
359 // We only output placement if different from the def_placement.
360 if (!placement.empty()) {
361 os << " loc=\"" << placement << '"';
365 int const i = InsetText::linuxdoc(buf, os, runparams);
366 os << "</" << tmptype << ">\n";
372 int InsetFloat::docbook(Buffer const & buf, ostream & os,
373 OutputParams const & runparams) const
375 os << '<' << params_.type << '>';
376 int const i = InsetText::docbook(buf, os, runparams);
377 os << "</" << params_.type << '>';
383 bool InsetFloat::insetAllowed(InsetOld::Code code) const
385 return code != InsetOld::FLOAT_CODE
386 && code != InsetOld::FOOT_CODE
387 && code != InsetOld::MARGIN_CODE;
391 bool InsetFloat::showInsetDialog(BufferView * bv) const
393 if (!InsetText::showInsetDialog(bv))
394 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
399 void InsetFloat::wide(bool w, BufferParams const & bp)
402 string lab = _("float: ") + floatname(params_.type, bp);
409 void InsetFloat::sideways(bool s, BufferParams const & bp)
411 params_.sideways = s;
412 string lab = _("float: ") + floatname(params_.type, bp);
413 if (params_.sideways)
414 lab += _(" (sideways)");
419 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
421 ParConstIterator pit = par_const_iterator_begin(*this);
422 ParConstIterator end = par_const_iterator_end(*this);
424 // Find a caption layout in one of the (child inset's) pars
425 for (; pit != end; ++pit) {
426 if (pit->layout()->name() == caplayout) {
427 string const name = floatname(params_.type, buf.params());
429 tostr(toclist[name].size() + 1)
430 + ". " + pit->asString(buf, false);
431 lyx::toc::TocItem const item(pit->id(), 0 , str);
432 toclist[name].push_back(item);
438 string const InsetFloatMailer::name_("float");
440 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
445 string const InsetFloatMailer::inset2string(Buffer const &) const
447 return params2string(inset_.params());
451 void InsetFloatMailer::string2params(string const & in,
452 InsetFloatParams & params)
454 params = InsetFloatParams();
458 istringstream data(in);
464 if (!lex || name != name_)
465 return print_mailer_error("InsetFloatMailer", in, 1, name_);
467 // This is part of the inset proper that is usually swallowed
468 // by LyXText::readInset
471 if (!lex || id != "Float")
472 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
474 // We have to read the type here!
480 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
483 data << name_ << ' ';