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"
20 #include "FuncRequest.h"
22 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
25 #include "Paragraph.h"
26 #include "output_xhtml.h"
27 #include "TextClass.h"
28 #include "TocBackend.h"
30 #include "support/debug.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
37 using namespace lyx::support;
42 InsetFloatList::InsetFloatList(Buffer * buf)
43 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE), "toc")
47 InsetFloatList::InsetFloatList(Buffer * buf, string const & type)
48 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE), "toc")
50 setParam("type", from_ascii(type));
54 ParamInfo const & InsetFloatList::findInfo(string const & /* cmdName */)
56 static ParamInfo param_info_;
57 if (param_info_.empty()) {
58 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
65 bool InsetFloatList::isCompatibleCommand(string const & s)
67 string str = s.substr(0, 6);
68 return str == "listof";
72 docstring InsetFloatList::screenLabel() const
74 FloatList const & floats = buffer().params().documentClass().floats();
75 FloatList::const_iterator it = floats[to_ascii(getParam("type"))];
76 if (it != floats.end())
77 return buffer().B_(it->second.listName());
79 return _("ERROR: Nonexistent float type!");
83 void InsetFloatList::write(ostream & os) const
85 os << "FloatList " << to_ascii(getParam("type")) << "\n";
89 void InsetFloatList::read(Lexer & lex)
91 lex.setContext("InsetFloatList::read");
92 FloatList const & floats = buffer().params().documentClass().floats();
96 setParam("type", lex.getDocString());
97 LYXERR(Debug::INSETS, "FloatList::float_type: "
98 << to_ascii(getParam("type")));
99 if (!floats.typeExist(to_ascii(getParam("type"))))
100 lex.printError("Unknown float type");
102 lex.printError("Parse error");
107 token = lex.getString();
108 if (token == "\\end_inset")
111 if (token != "\\end_inset") {
112 lex.printError("Missing \\end_inset at this point.");
117 int InsetFloatList::latex(odocstream & os, OutputParams const &) const
119 FloatList const & floats = buffer().params().documentClass().floats();
120 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
122 if (cit != floats.end()) {
123 Floating const & fl = cit->second;
124 if (fl.needsFloatPkg())
125 os << "\\listof{" << getParam("type") << "}{"
126 << buffer().B_(fl.listName()) << "}\n";
128 if (!fl.listCommand().empty())
129 os << "\\" << from_ascii(fl.listCommand()) << "\n";
132 << bformat(_("LyX cannot generate a list of %1$s"), getParam("type"))
136 os << "%%\\listof{" << getParam("type") << "}{"
137 << bformat(_("List of %1$s"), getParam("type"))
144 int InsetFloatList::plaintext(odocstream & os, OutputParams const &) const
146 os << screenLabel() << "\n\n";
148 buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os);
150 return PLAINTEXT_NEWLINE;
154 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const &) const {
155 FloatList const & floats = buffer().params().documentClass().floats();
156 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
158 if (cit == floats.end()) {
159 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
166 // Other builtin floats should be handled here. But I'm not sure if that is
167 // even possible yet, since I'm not sure if we have a TOC for such things.
168 // If so, then they should define ListName, as non-builtin floats do, and
169 // then we can use that.
170 // Really, all floats should define that.
171 if (!cit->second.needsFloatPkg()) {
172 // Only two different types allowed here:
173 string const type = cit->second.floattype();
174 if (type == "table") {
176 toclabel = _("List of Tables");
177 } else if (type == "figure") {
179 toclabel = _("List of Figures");
181 LYXERR0("Unknown Builtin Float!");
185 toctype = to_utf8(getParam("type"));
186 toclabel = buffer().B_(cit->second.listName());
189 // FIXME Do we need to check if it exists? If so, we need a new
190 // routine in TocBackend to do that.
191 Toc const & toc = buffer().tocBackend().toc(toctype);
195 // we want to look like a chapter, section, or whatever.
196 // so we're going to look for the layout with the minimum toclevel
197 // number > 0---because we don't want Part.
198 // we'll take the first one, just because.
199 // FIXME This could be specified in the layout file.
200 DocumentClass const & dc = buffer().params().documentClass();
201 TextClass::LayoutList::const_iterator lit = dc.begin();
202 TextClass::LayoutList::const_iterator len = dc.end();
204 Layout const * lay = NULL;
205 for (; lit != len; ++lit) {
206 int const level = lit->toclevel;
207 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
213 string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
214 string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
216 // we'll use our own stream, because we are going to defer everything.
217 // that's how we deal with the fact that we're probably inside a standard
218 // paragraph, and we don't want to be.
219 odocstringstream ods;
222 xs << html::StartTag("div", "class='toc'");
223 xs << html::StartTag("div", tocattr)
225 << html::EndTag("div");
227 Toc::const_iterator it = toc.begin();
228 Toc::const_iterator const en = toc.end();
229 for (; it != en; ++it) {
230 Paragraph const & par = it->dit().innerParagraph();
231 string const attr = "class='lyxtoc-" + toctype + "'";
233 xs << html::StartTag("div", attr);
234 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
235 xs << it->str() << " "
236 << html::StartTag("a", parattr)
238 // There ought to be a simple way to customize this.
239 << XHTMLStream::NextRaw() << "⇘"
240 << html::EndTag("a");
241 xs << html::EndTag("div");
243 xs << html::EndTag("div");
248 void InsetFloatList::validate(LaTeXFeatures & features) const
250 features.useFloat(to_ascii(getParam("type")));