2 * \file InsetFloatList.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "InsetFloatList.h"
16 #include "BufferParams.h"
17 #include "DispatchResult.h"
19 #include "FloatList.h"
21 #include "LaTeXFeatures.h"
23 #include "Paragraph.h"
24 #include "output_xhtml.h"
25 #include "TextClass.h"
26 #include "TocBackend.h"
28 #include "support/debug.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
35 using namespace lyx::support;
40 InsetFloatList::InsetFloatList(Buffer * buf)
41 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
45 InsetFloatList::InsetFloatList(Buffer * buf, string const & type)
46 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
48 setParam("type", from_ascii(type));
52 ParamInfo const & InsetFloatList::findInfo(string const & /* cmdName */)
54 static ParamInfo param_info_;
55 if (param_info_.empty()) {
56 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
63 bool InsetFloatList::isCompatibleCommand(string const & s)
65 string str = s.substr(0, 6);
66 return str == "listof";
70 docstring InsetFloatList::screenLabel() const
72 FloatList const & floats = buffer().params().documentClass().floats();
73 FloatList::const_iterator it = floats[to_ascii(getParam("type"))];
74 if (it != floats.end())
75 return buffer().B_(it->second.listName());
77 return _("ERROR: Nonexistent float type!");
81 void InsetFloatList::write(ostream & os) const
83 os << "FloatList " << to_ascii(getParam("type")) << "\n";
87 void InsetFloatList::read(Lexer & lex)
89 lex.setContext("InsetFloatList::read");
90 FloatList const & floats = buffer().params().documentClass().floats();
94 setParam("type", lex.getDocString());
95 LYXERR(Debug::INSETS, "FloatList::float_type: "
96 << to_ascii(getParam("type")));
97 if (!floats.typeExist(to_ascii(getParam("type"))))
98 lex.printError("Unknown float type");
100 lex.printError("Parse error");
105 token = lex.getString();
106 if (token == "\\end_inset")
109 if (token != "\\end_inset") {
110 lex.printError("Missing \\end_inset at this point.");
115 int InsetFloatList::latex(odocstream & os, OutputParams const &) const
117 FloatList const & floats = buffer().params().documentClass().floats();
118 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
120 if (cit != floats.end()) {
121 Floating const & fl = cit->second;
122 if (fl.needsFloatPkg())
123 os << "\\listof{" << getParam("type") << "}{"
124 << buffer().B_(fl.listName()) << "}\n";
126 if (!fl.listCommand().empty())
127 os << "\\" << from_ascii(fl.listCommand()) << "\n";
130 << bformat(_("LyX cannot generate a list of %1$s"), getParam("type"))
134 os << "%%\\listof{" << getParam("type") << "}{"
135 << bformat(_("List of %1$s"), getParam("type"))
142 int InsetFloatList::plaintext(odocstream & os, OutputParams const &) const
144 os << screenLabel() << "\n\n";
146 buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os);
148 return PLAINTEXT_NEWLINE;
152 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const &) const {
153 FloatList const & floats = buffer().params().documentClass().floats();
154 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
156 if (cit == floats.end()) {
157 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
164 // Other builtin floats should be handled here. But I'm not sure if that is
165 // even possible yet, since I'm not sure if we have a TOC for such things.
166 // If so, then they should define ListName, as non-builtin floats do, and
167 // then we can use that.
168 // Really, all floats should define that.
169 if (!cit->second.needsFloatPkg()) {
170 // Only two different types allowed here:
171 string const type = cit->second.floattype();
172 if (type == "table") {
174 toclabel = _("List of Tables");
175 } else if (type == "figure") {
177 toclabel = _("List of Figures");
179 LYXERR0("Unknown Builtin Float!");
183 toctype = to_utf8(getParam("type"));
184 toclabel = buffer().B_(cit->second.listName());
187 // FIXME Do we need to check if it exists? If so, we need a new
188 // routine in TocBackend to do that.
189 Toc const & toc = buffer().tocBackend().toc(toctype);
193 // we want to look like a chapter, section, or whatever.
194 // so we're going to look for the layout with the minimum toclevel
195 // number > 0---because we don't want Part.
196 // we'll take the first one, just because.
197 // FIXME This could be specified in the layout file.
198 DocumentClass const & dc = buffer().params().documentClass();
199 TextClass::LayoutList::const_iterator lit = dc.begin();
200 TextClass::LayoutList::const_iterator len = dc.end();
202 Layout const * lay = NULL;
203 for (; lit != len; ++lit) {
204 int const level = lit->toclevel;
205 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
211 string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
212 string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
214 // we'll use our own stream, because we are going to defer everything.
215 // that's how we deal with the fact that we're probably inside a standard
216 // paragraph, and we don't want to be.
217 odocstringstream ods;
220 xs << html::StartTag("div", "class='toc'");
221 xs << html::StartTag("div", tocattr)
223 << html::EndTag("div");
225 Toc::const_iterator it = toc.begin();
226 Toc::const_iterator const en = toc.end();
227 for (; it != en; ++it) {
228 Paragraph const & par = it->dit().innerParagraph();
229 string const attr = "class='lyxtoc-" + toctype + "'";
231 xs << html::StartTag("div", attr);
232 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
233 xs << it->str() << " "
234 << html::StartTag("a", parattr)
236 // There ought to be a simple way to customize this.
237 << XHTMLStream::NextRaw() << ">"
238 << html::EndTag("a");
239 xs << html::EndTag("div");
241 xs << html::EndTag("div");
246 void InsetFloatList::validate(LaTeXFeatures & features) const
248 features.useFloat(to_ascii(getParam("type")));