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"
22 #include "LaTeXFeatures.h"
24 #include "Paragraph.h"
25 #include "output_xhtml.h"
26 #include "TextClass.h"
27 #include "TocBackend.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
36 using namespace lyx::support;
41 InsetFloatList::InsetFloatList(Buffer * buf)
42 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
46 InsetFloatList::InsetFloatList(Buffer * buf, string const & type)
47 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
49 setParam("type", from_ascii(type));
53 ParamInfo const & InsetFloatList::findInfo(string const & /* cmdName */)
55 static ParamInfo param_info_;
56 if (param_info_.empty()) {
57 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
64 bool InsetFloatList::isCompatibleCommand(string const & s)
66 string str = s.substr(0, 6);
67 return str == "listof";
71 docstring InsetFloatList::screenLabel() const
73 FloatList const & floats = buffer().params().documentClass().floats();
74 FloatList::const_iterator it = floats[to_ascii(getParam("type"))];
75 if (it != floats.end())
76 return buffer().B_(it->second.listName());
78 return _("ERROR: Nonexistent float type!");
82 void InsetFloatList::write(ostream & os) const
84 os << "FloatList " << to_ascii(getParam("type")) << "\n";
88 void InsetFloatList::read(Lexer & lex)
90 lex.setContext("InsetFloatList::read");
91 FloatList const & floats = buffer().params().documentClass().floats();
95 setParam("type", lex.getDocString());
96 LYXERR(Debug::INSETS, "FloatList::float_type: "
97 << to_ascii(getParam("type")));
98 if (!floats.typeExist(to_ascii(getParam("type"))))
99 lex.printError("Unknown float type");
101 lex.printError("Parse error");
106 token = lex.getString();
107 if (token == "\\end_inset")
110 if (token != "\\end_inset") {
111 lex.printError("Missing \\end_inset at this point.");
116 void InsetFloatList::latex(otexstream & os, OutputParams const &) const
118 FloatList const & floats = buffer().params().documentClass().floats();
119 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
121 if (cit != floats.end()) {
122 Floating const & fl = cit->second;
123 if (fl.usesFloatPkg()) {
124 docstring const name =
125 buffer().language()->translateLayout(fl.listName());
126 os << "\\listof{" << getParam("type") << "}{"
129 if (!fl.listCommand().empty())
130 os << "\\" << from_ascii(fl.listCommand()) << "\n";
133 << bformat(_("LyX cannot generate a list of %1$s"), getParam("type"))
137 string const flName = "List of " + to_utf8(getParam("type"));
138 docstring const name = buffer().language()->translateLayout(flName);
139 os << "%%\\listof{" << getParam("type") << "}{"
145 int InsetFloatList::plaintext(odocstream & os, OutputParams const &) const
147 os << screenLabel() << "\n\n";
149 buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os);
151 return PLAINTEXT_NEWLINE;
155 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const &) const {
156 FloatList const & floats = buffer().params().documentClass().floats();
157 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
159 if (cit == floats.end()) {
160 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
167 // Other builtin floats should be handled here. But I'm not sure if that is
168 // even possible yet, since I'm not sure if we have a TOC for such things.
169 // If so, then they should define ListName, as non-builtin floats do, and
170 // then we can use that.
171 // Really, all floats should define that.
172 if (cit->second.isPredefined()) {
173 // Only two different types allowed here:
174 string const type = cit->second.floattype();
175 if (type == "table") {
177 toclabel = _("List of Tables");
178 } else if (type == "figure") {
180 toclabel = _("List of Figures");
182 LYXERR0("Unknown Builtin Float!");
186 toctype = to_utf8(getParam("type"));
187 toclabel = buffer().B_(cit->second.listName());
190 // FIXME Do we need to check if it exists? If so, we need a new
191 // routine in TocBackend to do that.
192 Toc const & toc = buffer().tocBackend().toc(toctype);
196 // we want to look like a chapter, section, or whatever.
197 // so we're going to look for the layout with the minimum toclevel
198 // number > 0---because we don't want Part.
199 // we'll take the first one, just because.
200 // FIXME This could be specified in the layout file.
201 DocumentClass const & dc = buffer().params().documentClass();
202 TextClass::LayoutList::const_iterator lit = dc.begin();
203 TextClass::LayoutList::const_iterator len = dc.end();
205 Layout const * lay = NULL;
206 for (; lit != len; ++lit) {
207 int const level = lit->toclevel;
208 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
214 string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
215 string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
217 // we'll use our own stream, because we are going to defer everything.
218 // that's how we deal with the fact that we're probably inside a standard
219 // paragraph, and we don't want to be.
220 odocstringstream ods;
223 xs << html::StartTag("div", "class='toc'");
224 xs << html::StartTag("div", tocattr)
226 << html::EndTag("div");
228 Toc::const_iterator it = toc.begin();
229 Toc::const_iterator const en = toc.end();
230 for (; it != en; ++it) {
231 Paragraph const & par = it->dit().innerParagraph();
232 string const attr = "class='lyxtoc-" + toctype + "'";
234 xs << html::StartTag("div", attr);
235 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
236 xs << it->str() << " "
237 << html::StartTag("a", parattr)
239 // There ought to be a simple way to customize this.
240 << XHTMLStream::ESCAPE_NONE << ">"
241 << html::EndTag("a");
242 xs << html::EndTag("div");
244 xs << html::EndTag("div");
249 void InsetFloatList::validate(LaTeXFeatures & features) const
251 features.useFloat(to_ascii(getParam("type")));