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 "BufferView.h"
20 #include "FloatList.h"
21 #include "funcrequest.h"
23 #include "iterators.h"
24 #include "LaTeXFeatures.h"
26 #include "paragraph.h"
28 #include "support/lstrings.h"
29 #include "support/tostr.h"
31 #include "support/std_sstream.h"
33 using namespace lyx::support;
37 using std::istringstream;
39 using std::ostringstream;
42 // With this inset it will be possible to support the latex package
43 // float.sty, and I am sure that with this and some additional support
44 // classes we can support similar functionality in other formats
46 // By using float.sty we will have the same handling for all floats, both
47 // for those already in existance (table and figure) and all user created
48 // ones¹. So suddenly we give the users the possibility of creating new
49 // kinds of floats on the fly. (and with a uniform look)
52 // \newfloat{type}{placement}{ext}[within]
53 // type - The "type" of the new class of floats, like program or
54 // algorithm. After the appropriate \newfloat, commands
55 // such as \begin{program} or \end{algorithm*} will be
57 // placement - The default placement for the given class of floats.
58 // They are like in standard LaTeX: t, b, p and h for top,
59 // bottom, page, and here, respectively. On top of that
60 // there is a new type, H, which does not really correspond
61 // to a float, since it means: put it "here" and nowhere else.
62 // Note, however that the H specifier is special and, because
63 // of implementation details cannot be used in the second
64 // argument of \newfloat.
65 // ext - The file name extension of an auxiliary file for the list
66 // of figures (or whatever). LaTeX writes the captions to
68 // within - This (optional) argument determines whether floats of this
69 // class will be numbered within some sectional unit of the
70 // document. For example, if within is equal to chapter, the
71 // floats will be numbered within chapters.
73 // style - plain, boxed, ruled
74 // \floatname{float}{floatname}
77 // \floatplacement{float}{placement}
80 // \restylefloat{float}
82 // \listof{type}{title}
85 // ¹ the algorithm float is defined using the float.sty package. Like this
87 // \newfloat{algorithm}{htbp}{loa}[<sect>]
88 // \floatname{algorithm}{Algorithm}
90 // The intention is that floats should be definable from two places:
92 // - the "gui" (i.e. by the user)
95 // This should only be done for floats defined in a documentclass and that
96 // does not need any additional packages. The two most known floats in this
97 // category is "table" and "figure". Floats defined in layout files are only
98 // stored in lyx files if the user modifies them.
101 // There should be a gui dialog (and also a collection of lyxfuncs) where
102 // the user can modify existing floats and/or create new ones.
104 // The individual floats will also have some settable
105 // variables: wide and placement.
111 // this should not be hardcoded, but be part of the definition
112 // of the float (JMarc)
113 string const caplayout("Caption");
115 string floatname(string const & type, BufferParams const & bp)
117 FloatList const & floats = bp.getLyXTextClass().floats();
118 FloatList::const_iterator it = floats[type];
119 if (it == floats.end())
122 return _(it->second.name());
128 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
129 : InsetCollapsable(bp)
131 string lab(_("float: "));
132 lab += floatname(type, bp);
134 LyXFont font(LyXFont::ALL_SANE);
137 font.setColor(LColor::collapsable);
141 LyXTextClass const & tclass = bp.getLyXTextClass();
142 if (tclass.hasLayout(caplayout))
143 inset.paragraphs.begin()->layout(tclass[caplayout]);
147 InsetFloat::InsetFloat(InsetFloat const & in)
148 : InsetCollapsable(in), params_(in.params_)
152 InsetFloat::~InsetFloat()
154 InsetFloatMailer mailer(*this);
159 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
161 switch (cmd.action) {
163 case LFUN_INSET_MODIFY: {
164 InsetFloatParams params;
165 InsetFloatMailer::string2params(cmd.argument, params);
167 params_.placement = params.placement;
168 params_.wide = params.wide;
170 wide(params_.wide, cmd.view()->buffer()->params);
171 cmd.view()->updateInset(this);
175 case LFUN_INSET_DIALOG_UPDATE: {
176 InsetFloatMailer(*this).updateDialog(cmd.view());
181 return InsetCollapsable::localDispatch(cmd);
186 void InsetFloatParams::write(ostream & os) const
188 os << "Float " // getInsetName()
191 if (!placement.empty())
192 os << "placement " << placement << "\n";
197 os << "wide false\n";
201 void InsetFloatParams::read(LyXLex & lex)
205 string token = lex.getString();
206 if (token == "placement") {
208 placement = lex.getString();
210 // take countermeasures
211 lex.pushToken(token);
214 token = lex.getString();
215 if (token == "wide") {
217 string const tmptoken = lex.getString();
218 wide = (tmptoken == "true");
220 lyxerr << "InsetFloat::Read:: Missing wide!"
222 // take countermeasures
223 lex.pushToken(token);
229 void InsetFloat::write(Buffer const & buf, ostream & os) const
232 InsetCollapsable::write(buf, os);
236 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
239 wide(params_.wide, buf.params);
240 InsetCollapsable::read(buf, lex);
244 void InsetFloat::validate(LaTeXFeatures & features) const
246 if (contains(params_.placement, "H")) {
247 features.require("float");
250 features.useFloat(params_.type);
251 InsetCollapsable::validate(features);
255 auto_ptr<InsetBase> InsetFloat::clone() const
257 return auto_ptr<InsetBase>(new InsetFloat(*this));
261 string const InsetFloat::editMessage() const
263 return _("Opened Float Inset");
267 int InsetFloat::latex(Buffer const & buf, ostream & os,
268 LatexRunParams const & runparams) const
270 FloatList const & floats = buf.params.getLyXTextClass().floats();
271 string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
272 // Figure out the float placement to use.
273 // From lowest to highest:
274 // - float default placement
275 // - document wide default placement
276 // - specific float placement
278 string const buf_placement = buf.params.float_placement;
279 string const def_placement = floats.defaultPlacement(params_.type);
280 if (!params_.placement.empty()
281 && params_.placement != def_placement) {
282 placement = params_.placement;
283 } else if (params_.placement.empty()
284 && !buf_placement.empty()
285 && buf_placement != def_placement) {
286 placement = buf_placement;
289 // The \n is used to force \begin{<floatname>} to appear in a new line.
290 // The % is needed to prevent two consecutive \n chars in the case
291 // when the current output line is empty.
292 os << "%\n\\begin{" << tmptype << '}';
293 // We only output placement if different from the def_placement.
294 if (!placement.empty()) {
295 os << '[' << placement << ']';
299 int const i = inset.latex(buf, os, runparams);
301 // The \n is used to force \end{<floatname>} to appear in a new line.
302 // In this case, we do not case if the current output line is empty.
303 os << "\n\\end{" << tmptype << "}\n";
309 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os) const
311 FloatList const & floats = buf.params.getLyXTextClass().floats();
312 string const tmptype = params_.type;
313 // Figure out the float placement to use.
314 // From lowest to highest:
315 // - float default placement
316 // - document wide default placement
317 // - specific float placement
318 // This is the same as latex, as linuxdoc is modeled after latex.
321 string const buf_placement = buf.params.float_placement;
322 string const def_placement = floats.defaultPlacement(params_.type);
323 if (!params_.placement.empty()
324 && params_.placement != def_placement) {
325 placement = params_.placement;
326 } else if (params_.placement.empty()
327 && !buf_placement.empty()
328 && buf_placement != def_placement) {
329 placement = buf_placement;
332 os << "\n<" << tmptype ;
333 // We only output placement if different from the def_placement.
334 if (!placement.empty()) {
335 os << " loc=\"" << placement << '"';
339 int const i = inset.linuxdoc(buf, os);
340 os << "</" << tmptype << ">\n";
346 int InsetFloat::docbook(Buffer const & buf, ostream & os, bool mixcont) const
348 os << '<' << params_.type << '>';
349 int const i = inset.docbook(buf, os, mixcont);
350 os << "</" << params_.type << '>';
356 bool InsetFloat::insetAllowed(InsetOld::Code code) const
358 if (code == InsetOld::FLOAT_CODE)
360 if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
361 return inset.insetAllowed(code);
362 if ((code == InsetOld::FOOT_CODE) || (code == InsetOld::MARGIN_CODE))
368 bool InsetFloat::showInsetDialog(BufferView * bv) const
370 if (!inset.showInsetDialog(bv)) {
371 InsetFloat * tmp = const_cast<InsetFloat *>(this);
372 InsetFloatMailer mailer(*tmp);
373 mailer.showDialog(bv);
379 void InsetFloat::wide(bool w, BufferParams const & bp)
383 string lab(_("float: "));
384 lab += floatname(params_.type, bp);
393 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
395 ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
396 ParIterator end(inset.paragraphs.end(), inset.paragraphs);
398 // Find a caption layout in one of the (child inset's) pars
399 for (; pit != end; ++pit) {
400 if (pit->layout()->name() == caplayout) {
401 string const name = floatname(params_.type, buf.params);
403 tostr(toclist[name].size() + 1)
404 + ". " + pit->asString(buf, false);
405 lyx::toc::TocItem const item(pit->id(), 0 , str);
406 toclist[name].push_back(item);
412 string const InsetFloatMailer::name_("float");
414 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
419 string const InsetFloatMailer::inset2string(Buffer const &) const
421 return params2string(inset_.params());
425 void InsetFloatMailer::string2params(string const & in,
426 InsetFloatParams & params)
428 params = InsetFloatParams();
433 istringstream data(STRCONV(in));
439 string const token = lex.getString();
444 // This is part of the inset proper that is usually swallowed
445 // by Buffer::readInset
448 string const token = lex.getString();
449 if (token != "Float" || !lex.eatLine())
459 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
462 data << name_ << ' ';
464 return STRCONV(data.str());