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 "BufferView.h"
19 #include "DispatchResult.h"
21 #include "FloatList.h"
23 #include "FuncRequest.h"
25 #include "LaTeXFeatures.h"
27 #include "Paragraph.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
30 #include "TocBackend.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
39 using namespace lyx::support;
44 InsetFloatList::InsetFloatList(Buffer * buf)
45 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
49 InsetFloatList::InsetFloatList(Buffer * buf, string const & type)
50 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
52 setParam("type", from_ascii(type));
56 ParamInfo const & InsetFloatList::findInfo(string const & /* cmdName */)
58 static ParamInfo param_info_;
59 if (param_info_.empty()) {
60 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
67 bool InsetFloatList::isCompatibleCommand(string const & s)
69 string str = s.substr(0, 6);
70 return str == "listof";
74 docstring InsetFloatList::screenLabel() const
76 FloatList const & floats = buffer().params().documentClass().floats();
77 FloatList::const_iterator it = floats[to_ascii(getParam("type"))];
78 if (it != floats.end())
79 return buffer().B_(it->second.listName());
81 return _("ERROR: Nonexistent float type!");
85 void InsetFloatList::doDispatch(Cursor & cur, FuncRequest & cmd) {
86 switch (cmd.action()) {
87 case LFUN_MOUSE_RELEASE:
88 if (!cur.selection() && cmd.button() == mouse_button::button1) {
89 cur.bv().showDialog("toc", params2string(params()));
95 InsetCommand::doDispatch(cur, cmd);
100 void InsetFloatList::write(ostream & os) const
102 os << "FloatList " << to_ascii(getParam("type")) << "\n";
106 void InsetFloatList::read(Lexer & lex)
108 lex.setContext("InsetFloatList::read");
109 FloatList const & floats = buffer().params().documentClass().floats();
113 setParam("type", lex.getDocString());
114 LYXERR(Debug::INSETS, "FloatList::float_type: "
115 << to_ascii(getParam("type")));
116 if (!floats.typeExist(to_ascii(getParam("type"))))
117 lex.printError("Unknown float type");
119 lex.printError("Parse error");
124 token = lex.getString();
125 if (token == "\\end_inset")
128 if (token != "\\end_inset") {
129 lex.printError("Missing \\end_inset at this point.");
134 void InsetFloatList::latex(otexstream & os, OutputParams const &) const
136 FloatList const & floats = buffer().params().documentClass().floats();
137 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
139 if (cit != floats.end()) {
140 Floating const & fl = cit->second;
141 if (fl.usesFloatPkg()) {
142 docstring const name =
143 buffer().language()->translateLayout(fl.listName());
144 os << "\\listof{" << getParam("type") << "}{"
147 if (!fl.listCommand().empty())
148 os << "\\" << from_ascii(fl.listCommand()) << "\n";
151 << bformat(_("LyX cannot generate a list of %1$s"), getParam("type"))
155 string const flName = "List of " + to_utf8(getParam("type"));
156 docstring const name = buffer().language()->translateLayout(flName);
157 os << "%%\\listof{" << getParam("type") << "}{"
163 int InsetFloatList::plaintext(odocstringstream & os,
164 OutputParams const &, size_t max_length) const
166 os << screenLabel() << "\n\n";
168 buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os, max_length);
170 return PLAINTEXT_NEWLINE;
174 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const &) const {
175 FloatList const & floats = buffer().params().documentClass().floats();
176 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
178 if (cit == floats.end()) {
179 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
186 // Other builtin floats should be handled here. But I'm not sure if that is
187 // even possible yet, since I'm not sure if we have a TOC for such things.
188 // If so, then they should define ListName, as non-builtin floats do, and
189 // then we can use that.
190 // Really, all floats should define that.
191 if (cit->second.isPredefined()) {
192 // Only two different types allowed here:
193 string const type = cit->second.floattype();
194 if (type == "table") {
196 toclabel = _("List of Tables");
197 } else if (type == "figure") {
199 toclabel = _("List of Figures");
201 LYXERR0("Unknown Builtin Float!");
205 toctype = to_utf8(getParam("type"));
206 toclabel = buffer().B_(cit->second.listName());
209 // FIXME Do we need to check if it exists? If so, we need a new
210 // routine in TocBackend to do that.
211 Toc const & toc = buffer().tocBackend().toc(toctype);
215 // we want to look like a chapter, section, or whatever.
216 // so we're going to look for the layout with the minimum toclevel
217 // number > 0---because we don't want Part.
218 // we'll take the first one, just because.
219 // FIXME This could be specified in the layout file.
220 DocumentClass const & dc = buffer().params().documentClass();
221 TextClass::LayoutList::const_iterator lit = dc.begin();
222 TextClass::LayoutList::const_iterator len = dc.end();
224 Layout const * lay = NULL;
225 for (; lit != len; ++lit) {
226 int const level = lit->toclevel;
227 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
233 string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
234 string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
236 // we'll use our own stream, because we are going to defer everything.
237 // that's how we deal with the fact that we're probably inside a standard
238 // paragraph, and we don't want to be.
239 odocstringstream ods;
242 xs << html::StartTag("div", "class='toc'");
243 xs << html::StartTag("div", tocattr)
245 << html::EndTag("div");
247 Toc::const_iterator it = toc.begin();
248 Toc::const_iterator const en = toc.end();
249 for (; it != en; ++it) {
250 Paragraph const & par = it->dit().innerParagraph();
251 string const attr = "class='lyxtoc-" + toctype + "'";
253 xs << html::StartTag("div", attr);
254 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
255 xs << it->str() << " "
256 << html::StartTag("a", parattr)
258 // There ought to be a simple way to customize this.
259 << XHTMLStream::ESCAPE_NONE << ">"
260 << html::EndTag("a");
261 xs << html::EndTag("div");
263 xs << html::EndTag("div");
268 void InsetFloatList::validate(LaTeXFeatures & features) const
270 features.useFloat(to_ascii(getParam("type")));