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 "texstream.h"
30 #include "TextClass.h"
31 #include "TocBackend.h"
33 #include "support/debug.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
40 using namespace lyx::support;
45 InsetFloatList::InsetFloatList(Buffer * buf)
46 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
50 InsetFloatList::InsetFloatList(Buffer * buf, string const & type)
51 : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE))
53 setParam("type", from_ascii(type));
57 ParamInfo const & InsetFloatList::findInfo(string const & /* cmdName */)
59 static ParamInfo param_info_;
60 if (param_info_.empty()) {
61 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
68 bool InsetFloatList::isCompatibleCommand(string const & s)
70 string str = s.substr(0, 6);
71 return str == "listof";
75 docstring InsetFloatList::screenLabel() const
77 FloatList const & floats = buffer().params().documentClass().floats();
78 FloatList::const_iterator it = floats[to_ascii(getParam("type"))];
79 if (it != floats.end())
80 return buffer().B_(it->second.listName());
82 return _("ERROR: Nonexistent float type!");
86 void InsetFloatList::doDispatch(Cursor & cur, FuncRequest & cmd) {
87 switch (cmd.action()) {
88 case LFUN_MOUSE_RELEASE:
89 if (!cur.selection() && cmd.button() == mouse_button::button1) {
90 cur.bv().showDialog("toc", params2string(params()));
96 InsetCommand::doDispatch(cur, cmd);
101 void InsetFloatList::write(ostream & os) const
103 os << "FloatList " << to_ascii(getParam("type")) << "\n";
107 void InsetFloatList::read(Lexer & lex)
109 lex.setContext("InsetFloatList::read");
110 FloatList const & floats = buffer().params().documentClass().floats();
114 setParam("type", lex.getDocString());
115 LYXERR(Debug::INSETS, "FloatList::float_type: "
116 << to_ascii(getParam("type")));
117 if (!floats.typeExist(to_ascii(getParam("type"))))
118 lex.printError("Unknown float type");
120 lex.printError("Parse error");
125 token = lex.getString();
126 if (token == "\\end_inset")
129 if (token != "\\end_inset") {
130 lex.printError("Missing \\end_inset at this point.");
135 void InsetFloatList::latex(otexstream & os, OutputParams const &) const
137 FloatList const & floats = buffer().params().documentClass().floats();
138 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
140 if (cit != floats.end()) {
141 Floating const & fl = cit->second;
142 if (fl.usesFloatPkg()) {
143 docstring const name =
144 buffer().language()->translateLayout(fl.listName());
145 os << "\\listof{" << getParam("type") << "}{"
148 if (!fl.listCommand().empty())
149 os << "\\" << from_ascii(fl.listCommand()) << "\n";
152 << bformat(_("LyX cannot generate a list of %1$s"), getParam("type"))
156 string const flName = "List of " + to_utf8(getParam("type"));
157 docstring const name = buffer().language()->translateLayout(flName);
158 os << "%%\\listof{" << getParam("type") << "}{"
164 int InsetFloatList::plaintext(odocstringstream & os,
165 OutputParams const &, size_t max_length) const
167 os << screenLabel() << "\n\n";
169 buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os, max_length);
171 return PLAINTEXT_NEWLINE;
175 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const & op) const {
176 FloatList const & floats = buffer().params().documentClass().floats();
177 FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
179 if (cit == floats.end()) {
180 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
187 // Other builtin floats should be handled here. But I'm not sure if that is
188 // even possible yet, since I'm not sure if we have a TOC for such things.
189 // If so, then they should define ListName, as non-builtin floats do, and
190 // then we can use that.
191 // Really, all floats should define that.
192 if (cit->second.isPredefined()) {
193 // Only two different types allowed here:
194 string const type = cit->second.floattype();
195 if (type == "table") {
197 toclabel = translateIfPossible(from_ascii("List of Tables"),
198 op.local_font->language()->lang());
199 } else if (type == "figure") {
201 toclabel = translateIfPossible(from_ascii("List of Figures"),
202 op.local_font->language()->lang());
204 LYXERR0("Unknown Builtin Float!");
208 toctype = to_utf8(getParam("type"));
209 toclabel = translateIfPossible(from_utf8(cit->second.listName()),
210 op.local_font->language()->lang());
213 shared_ptr<Toc const> toc = buffer().tocBackend().toc(toctype);
217 // we want to look like a chapter, section, or whatever.
218 // so we're going to look for the layout with the minimum toclevel
219 // number > 0---because we don't want Part.
220 // we'll take the first one, just because.
221 // FIXME This could be specified in the layout file.
222 DocumentClass const & dc = buffer().params().documentClass();
223 TextClass::LayoutList::const_iterator lit = dc.begin();
224 TextClass::LayoutList::const_iterator len = dc.end();
226 Layout const * lay = NULL;
227 for (; lit != len; ++lit) {
228 int const level = lit->toclevel;
229 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
235 string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
236 string const tocattr = "class='tochead toc-" + toctype + tocclass + "'";
238 // we'll use our own stream, because we are going to defer everything.
239 // that's how we deal with the fact that we're probably inside a standard
240 // paragraph, and we don't want to be.
241 odocstringstream ods;
244 InsetLayout const & il = getLayout();
245 string const & tag = il.htmltag();
246 xs << html::StartTag("div", "class='toc toc-floats'");
247 xs << html::StartTag(tag, tocattr)
249 << html::EndTag(tag);
251 Toc::const_iterator it = toc->begin();
252 Toc::const_iterator const en = toc->end();
253 for (; it != en; ++it) {
254 if (it->str().empty())
256 Paragraph const & par = it->dit().innerParagraph();
257 string const attr = "class='lyxtoc-floats lyxtoc-" + toctype + "'";
258 xs << html::StartTag("div", attr);
259 string const parattr = "href='#" + par.magicLabel() + "' class='lyxtoc-floats'";
260 xs << html::StartTag("a", parattr)
262 << html::EndTag("a");
263 xs << html::EndTag("div");
265 xs << html::EndTag("div");
270 void InsetFloatList::validate(LaTeXFeatures & features) const
272 features.useFloat(to_ascii(getParam("type")));
273 features.useInsetLayout(getLayout());
277 docstring InsetFloatList::layoutName() const
279 return "FloatList:" + getParam("type");