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 "iterators.h"
27 #include "LaTeXFeatures.h"
30 #include "outputparams.h"
31 #include "paragraph.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 string lab(_("float: "));
137 lab += floatname(type, bp);
139 LyXFont font(LyXFont::ALL_SANE);
142 font.setColor(LColor::collapsable);
146 LyXTextClass const & tclass = bp.getLyXTextClass();
147 if (tclass.hasLayout(caplayout))
148 inset.paragraphs().begin()->layout(tclass[caplayout]);
152 InsetFloat::~InsetFloat()
154 InsetFloatMailer(*this).hideDialog();
159 InsetFloat::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
161 switch (cmd.action) {
163 case LFUN_INSET_MODIFY: {
164 InsetFloatParams params;
165 InsetFloatMailer::string2params(cmd.argument, params);
166 params_.placement = params.placement;
167 params_.wide = params.wide;
168 wide(params_.wide, cur.bv().buffer()->params());
170 return DispatchResult(true, true);
173 case LFUN_INSET_DIALOG_UPDATE: {
174 InsetFloatMailer(*this).updateDialog(&cur.bv());
175 return DispatchResult(true, true);
179 return InsetCollapsable::priv_dispatch(cur, cmd);
184 void InsetFloatParams::write(ostream & os) const
186 os << "Float " // getInsetName()
189 if (!placement.empty())
190 os << "placement " << placement << "\n";
195 os << "wide false\n";
199 void InsetFloatParams::read(LyXLex & lex)
203 string token = lex.getString();
204 if (token == "placement") {
206 placement = lex.getString();
208 // take countermeasures
209 lex.pushToken(token);
212 token = lex.getString();
213 if (token == "wide") {
215 string const tmptoken = lex.getString();
216 wide = (tmptoken == "true");
218 lyxerr << "InsetFloat::Read:: Missing wide!"
220 // take countermeasures
221 lex.pushToken(token);
227 void InsetFloat::write(Buffer const & buf, ostream & os) const
230 InsetCollapsable::write(buf, os);
234 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
237 wide(params_.wide, buf.params());
238 InsetCollapsable::read(buf, lex);
242 void InsetFloat::validate(LaTeXFeatures & features) const
244 if (contains(params_.placement, 'H')) {
245 features.require("float");
248 features.useFloat(params_.type);
249 InsetCollapsable::validate(features);
253 auto_ptr<InsetBase> InsetFloat::clone() const
255 return auto_ptr<InsetBase>(new InsetFloat(*this));
259 string const InsetFloat::editMessage() const
261 return _("Opened Float Inset");
265 int InsetFloat::latex(Buffer const & buf, ostream & os,
266 OutputParams const & runparams) const
268 FloatList const & floats = buf.params().getLyXTextClass().floats();
269 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
270 // Figure out the float placement to use.
271 // From lowest to highest:
272 // - float default placement
273 // - document wide default placement
274 // - specific float placement
276 string const buf_placement = buf.params().float_placement;
277 string const def_placement = floats.defaultPlacement(params_.type);
278 if (!params_.placement.empty()
279 && params_.placement != def_placement) {
280 placement = params_.placement;
281 } else if (params_.placement.empty()
282 && !buf_placement.empty()
283 && buf_placement != def_placement) {
284 placement = buf_placement;
287 // The \n is used to force \begin{<floatname>} to appear in a new line.
288 // The % is needed to prevent two consecutive \n chars in the case
289 // when the current output line is empty.
290 os << "%\n\\begin{" << tmptype << '}';
291 // We only output placement if different from the def_placement.
292 if (!placement.empty()) {
293 os << '[' << placement << ']';
297 int const i = inset.latex(buf, os, runparams);
299 // The \n is used to force \end{<floatname>} to appear in a new line.
300 // In this case, we do not case if the current output line is empty.
301 os << "\n\\end{" << tmptype << "}\n";
307 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
308 OutputParams const & runparams) const
310 FloatList const & floats = buf.params().getLyXTextClass().floats();
311 string const tmptype = params_.type;
312 // Figure out the float placement to use.
313 // From lowest to highest:
314 // - float default placement
315 // - document wide default placement
316 // - specific float placement
317 // This is the same as latex, as linuxdoc is modeled after latex.
320 string const buf_placement = buf.params().float_placement;
321 string const def_placement = floats.defaultPlacement(params_.type);
322 if (!params_.placement.empty()
323 && params_.placement != def_placement) {
324 placement = params_.placement;
325 } else if (params_.placement.empty()
326 && !buf_placement.empty()
327 && buf_placement != def_placement) {
328 placement = buf_placement;
331 os << "\n<" << tmptype ;
332 // We only output placement if different from the def_placement.
333 if (!placement.empty()) {
334 os << " loc=\"" << placement << '"';
338 int const i = inset.linuxdoc(buf, os, runparams);
339 os << "</" << tmptype << ">\n";
345 int InsetFloat::docbook(Buffer const & buf, ostream & os,
346 OutputParams const & runparams) const
348 os << '<' << params_.type << '>';
349 int const i = inset.docbook(buf, os, runparams);
350 os << "</" << params_.type << '>';
356 bool InsetFloat::insetAllowed(InsetOld::Code code) const
358 return code != InsetOld::FLOAT_CODE
359 && code != InsetOld::FOOT_CODE
360 && code != InsetOld::MARGIN_CODE;
364 bool InsetFloat::showInsetDialog(BufferView * bv) const
366 if (!inset.showInsetDialog(bv))
367 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
372 void InsetFloat::wide(bool w, BufferParams const & bp)
375 string lab = _("float: ") + floatname(params_.type, bp);
382 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
384 ParIterator pit(inset.paragraphs().begin(), inset.paragraphs());
385 ParIterator end(inset.paragraphs().end(), inset.paragraphs());
387 // Find a caption layout in one of the (child inset's) pars
388 for (; pit != end; ++pit) {
389 if (pit->layout()->name() == caplayout) {
390 string const name = floatname(params_.type, buf.params());
392 tostr(toclist[name].size() + 1)
393 + ". " + pit->asString(buf, false);
394 lyx::toc::TocItem const item(pit->id(), 0 , str);
395 toclist[name].push_back(item);
401 string const InsetFloatMailer::name_("float");
403 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
408 string const InsetFloatMailer::inset2string(Buffer const &) const
410 return params2string(inset_.params());
414 void InsetFloatMailer::string2params(string const & in,
415 InsetFloatParams & params)
417 params = InsetFloatParams();
421 istringstream data(in);
427 if (!lex || name != name_)
428 return print_mailer_error("InsetFloatMailer", in, 1, name_);
430 // This is part of the inset proper that is usually swallowed
431 // by LyXText::readInset
434 if (!lex || id != "Float")
435 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
441 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
444 data << name_ << ' ';