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/convert.h"
38 using lyx::support::contains;
43 using std::istringstream;
45 using std::ostringstream;
48 // With this inset it will be possible to support the latex package
49 // float.sty, and I am sure that with this and some additional support
50 // classes we can support similar functionality in other formats
52 // By using float.sty we will have the same handling for all floats, both
53 // for those already in existance (table and figure) and all user created
54 // ones¹. So suddenly we give the users the possibility of creating new
55 // kinds of floats on the fly. (and with a uniform look)
58 // \newfloat{type}{placement}{ext}[within]
59 // type - The "type" of the new class of floats, like program or
60 // algorithm. After the appropriate \newfloat, commands
61 // such as \begin{program} or \end{algorithm*} will be
63 // placement - The default placement for the given class of floats.
64 // They are like in standard LaTeX: t, b, p and h for top,
65 // bottom, page, and here, respectively. On top of that
66 // there is a new type, H, which does not really correspond
67 // to a float, since it means: put it "here" and nowhere else.
68 // Note, however that the H specifier is special and, because
69 // of implementation details cannot be used in the second
70 // argument of \newfloat.
71 // ext - The file name extension of an auxiliary file for the list
72 // of figures (or whatever). LaTeX writes the captions to
74 // within - This (optional) argument determines whether floats of this
75 // class will be numbered within some sectional unit of the
76 // document. For example, if within is equal to chapter, the
77 // floats will be numbered within chapters.
79 // style - plain, boxed, ruled
80 // \floatname{float}{floatname}
83 // \floatplacement{float}{placement}
86 // \restylefloat{float}
88 // \listof{type}{title}
91 // ¹ the algorithm float is defined using the float.sty package. Like this
93 // \newfloat{algorithm}{htbp}{loa}[<sect>]
94 // \floatname{algorithm}{Algorithm}
96 // The intention is that floats should be definable from two places:
98 // - the "gui" (i.e. by the user)
100 // From layout files.
101 // This should only be done for floats defined in a documentclass and that
102 // does not need any additional packages. The two most known floats in this
103 // category is "table" and "figure". Floats defined in layout files are only
104 // stored in lyx files if the user modifies them.
107 // There should be a gui dialog (and also a collection of lyxfuncs) where
108 // the user can modify existing floats and/or create new ones.
110 // The individual floats will also have some settable
111 // variables: wide and placement.
117 // this should not be hardcoded, but be part of the definition
118 // of the float (JMarc)
119 string const caplayout("Caption");
121 string floatname(string const & type, BufferParams const & bp)
123 FloatList const & floats = bp.getLyXTextClass().floats();
124 FloatList::const_iterator it = floats[type];
125 if (it == floats.end())
128 return _(it->second.name());
134 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
135 : InsetCollapsable(bp)
137 setLabel(_("float: ") + floatname(type, bp));
138 LyXFont font(LyXFont::ALL_SANE);
141 font.setColor(LColor::collapsable);
148 InsetFloat::~InsetFloat()
150 InsetFloatMailer(*this).hideDialog();
154 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
156 switch (cmd.action) {
158 case LFUN_INSET_MODIFY: {
159 InsetFloatParams params;
160 InsetFloatMailer::string2params(cmd.argument, params);
161 params_.placement = params.placement;
162 params_.wide = params.wide;
163 params_.sideways = params.sideways;
164 wide(params_.wide, cur.buffer().params());
165 sideways(params_.sideways, cur.buffer().params());
170 case LFUN_INSET_DIALOG_UPDATE: {
171 InsetFloatMailer(*this).updateDialog(&cur.bv());
175 case LFUN_MOUSE_RELEASE: {
176 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
177 InsetFloatMailer(*this).showDialog(&cur.bv());
180 InsetCollapsable::doDispatch(cur, cmd);
185 InsetCollapsable::doDispatch(cur, cmd);
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 string const InsetFloat::editMessage() const
279 return _("Opened Float Inset");
283 int InsetFloat::latex(Buffer const & buf, ostream & 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{" << 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 << '[' << 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{" << tmptype << "}\n";
332 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
333 OutputParams const & runparams) const
335 FloatList const & floats = buf.params().getLyXTextClass().floats();
336 string const tmptype = params_.type;
337 // Figure out the float placement to use.
338 // From lowest to highest:
339 // - float default placement
340 // - document wide default placement
341 // - specific float placement
342 // This is the same as latex, as linuxdoc is modeled after latex.
345 string const buf_placement = buf.params().float_placement;
346 string const def_placement = floats.defaultPlacement(params_.type);
347 if (!params_.placement.empty()
348 && params_.placement != def_placement) {
349 placement = params_.placement;
350 } else if (params_.placement.empty()
351 && !buf_placement.empty()
352 && buf_placement != def_placement) {
353 placement = buf_placement;
356 os << "\n<" << tmptype ;
357 // We only output placement if different from the def_placement.
358 if (!placement.empty()) {
359 os << " loc=\"" << placement << '"';
363 int const i = InsetText::linuxdoc(buf, os, runparams);
364 os << "</" << tmptype << ">\n";
370 int InsetFloat::docbook(Buffer const & buf, ostream & os,
371 OutputParams const & runparams) const
373 os << '<' << params_.type << '>';
374 int const i = InsetText::docbook(buf, os, runparams);
375 os << "</" << params_.type << '>';
381 bool InsetFloat::insetAllowed(InsetBase::Code code) const
383 return code != InsetBase::FLOAT_CODE
384 && code != InsetBase::FOOT_CODE
385 && code != InsetBase::MARGIN_CODE;
389 bool InsetFloat::showInsetDialog(BufferView * bv) const
391 if (!InsetText::showInsetDialog(bv))
392 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
397 void InsetFloat::wide(bool w, BufferParams const & bp)
400 string lab = _("float: ") + floatname(params_.type, bp);
407 void InsetFloat::sideways(bool s, BufferParams const & bp)
409 params_.sideways = s;
410 string lab = _("float: ") + floatname(params_.type, bp);
411 if (params_.sideways)
412 lab += _(" (sideways)");
417 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
419 ParConstIterator pit = par_const_iterator_begin(*this);
420 ParConstIterator end = par_const_iterator_end(*this);
422 // Find a caption layout in one of the (child inset's) pars
423 for (; pit != end; ++pit) {
424 if (pit->layout()->name() == caplayout) {
425 string const name = floatname(params_.type, buf.params());
427 convert<string>(toclist[name].size() + 1)
428 + ". " + pit->asString(buf, false);
429 lyx::toc::TocItem const item(pit->id(), 0 , str);
430 toclist[name].push_back(item);
436 string const InsetFloatMailer::name_("float");
438 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
443 string const InsetFloatMailer::inset2string(Buffer const &) const
445 return params2string(inset_.params());
449 void InsetFloatMailer::string2params(string const & in,
450 InsetFloatParams & params)
452 params = InsetFloatParams();
456 istringstream data(in);
462 if (!lex || name != name_)
463 return print_mailer_error("InsetFloatMailer", in, 1, name_);
465 // This is part of the inset proper that is usually swallowed
466 // by LyXText::readInset
469 if (!lex || id != "Float")
470 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
472 // We have to read the type here!
478 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
481 data << name_ << ' ';