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 & op) 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 = translateIfPossible(from_ascii("List of Tables"),
197 op.local_font->language()->lang());
198 } else if (type == "figure") {
200 toclabel = translateIfPossible(from_ascii("List of Figures"),
201 op.local_font->language()->lang());
203 LYXERR0("Unknown Builtin Float!");
207 toctype = to_utf8(getParam("type"));
208 toclabel = translateIfPossible(from_utf8(cit->second.listName()),
209 op.local_font->language()->lang());
212 // FIXME Do we need to check if it exists? If so, we need a new
213 // routine in TocBackend to do that.
214 Toc const & toc = buffer().tocBackend().toc(toctype);
218 // we want to look like a chapter, section, or whatever.
219 // so we're going to look for the layout with the minimum toclevel
220 // number > 0---because we don't want Part.
221 // we'll take the first one, just because.
222 // FIXME This could be specified in the layout file.
223 DocumentClass const & dc = buffer().params().documentClass();
224 TextClass::LayoutList::const_iterator lit = dc.begin();
225 TextClass::LayoutList::const_iterator len = dc.end();
227 Layout const * lay = NULL;
228 for (; lit != len; ++lit) {
229 int const level = lit->toclevel;
230 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
236 string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
237 string const tocattr = "class='tochead toc-" + toctype + tocclass + "'";
239 // we'll use our own stream, because we are going to defer everything.
240 // that's how we deal with the fact that we're probably inside a standard
241 // paragraph, and we don't want to be.
242 odocstringstream ods;
245 InsetLayout const & il = getLayout();
246 string const & tag = il.htmltag();
247 xs << html::StartTag("div", "class='toc toc-floats'");
248 xs << html::StartTag(tag, tocattr)
250 << html::EndTag(tag);
252 Toc::const_iterator it = toc.begin();
253 Toc::const_iterator const en = toc.end();
254 for (; it != en; ++it) {
255 Paragraph const & par = it->dit().innerParagraph();
256 string const attr = "class='lyxtoc-floats lyxtoc-" + toctype + "'";
257 xs << html::StartTag("div", attr);
258 string const parattr = "href='#" + par.magicLabel() + "' class='lyxtoc-floats'";
259 xs << html::StartTag("a", parattr)
261 << html::EndTag("a");
262 xs << html::EndTag("div");
264 xs << html::EndTag("div");
269 void InsetFloatList::validate(LaTeXFeatures & features) const
271 features.useFloat(to_ascii(getParam("type")));
272 features.useInsetLayout(getLayout());
276 docstring InsetFloatList::layoutName() const
278 return "FloatList:" + getParam("type");