]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloatList.cpp
Make members of FuncRequest private, per the FIXME there. Again, this is
[lyx.git] / src / insets / InsetFloatList.cpp
1 /**
2  * \file InsetFloatList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetFloatList.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "DispatchResult.h"
18 #include "Floating.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "Language.h"
22 #include "LaTeXFeatures.h"
23 #include "Lexer.h"
24 #include "Paragraph.h"
25 #include "output_xhtml.h"
26 #include "TextClass.h"
27 #include "TocBackend.h"
28
29 #include "support/debug.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32
33 #include <ostream>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39
40
41 InsetFloatList::InsetFloatList(Buffer * buf)
42         : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE), "toc")
43 {}
44
45
46 InsetFloatList::InsetFloatList(Buffer * buf, string const & type)
47         : InsetCommand(buf, InsetCommandParams(FLOAT_LIST_CODE), "toc")
48 {
49         setParam("type", from_ascii(type));
50 }
51
52
53 ParamInfo const & InsetFloatList::findInfo(string const & /* cmdName */)
54 {
55         static ParamInfo param_info_;
56         if (param_info_.empty()) {
57                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
58         }
59         return param_info_;
60 }
61
62
63 //HACK
64 bool InsetFloatList::isCompatibleCommand(string const & s)
65 {
66         string str = s.substr(0, 6);
67         return str == "listof";
68 }
69
70
71 docstring InsetFloatList::screenLabel() const
72 {
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());
77         else
78                 return _("ERROR: Nonexistent float type!");
79 }
80
81
82 void InsetFloatList::write(ostream & os) const
83 {
84         os << "FloatList " << to_ascii(getParam("type")) << "\n";
85 }
86
87
88 void InsetFloatList::read(Lexer & lex)
89 {
90         lex.setContext("InsetFloatList::read");
91         FloatList const & floats = buffer().params().documentClass().floats();
92         string token;
93
94         if (lex.eatLine()) {
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");
100         } else {
101                 lex.printError("Parse error");
102         }
103
104         while (lex.isOK()) {
105                 lex.next();
106                 token = lex.getString();
107                 if (token == "\\end_inset")
108                         break;
109         }
110         if (token != "\\end_inset") {
111                 lex.printError("Missing \\end_inset at this point.");
112         }
113 }
114
115
116 int InsetFloatList::latex(odocstream & os, OutputParams const &) const
117 {
118         FloatList const & floats = buffer().params().documentClass().floats();
119         FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
120
121         if (cit != floats.end()) {
122                 Floating const & fl = cit->second;
123                 if (fl.needsFloatPkg())
124                         os << "\\listof{" << getParam("type") << "}{"
125                            << buffer().B_(fl.listName()) << "}\n"; 
126                 else {
127                         if (!fl.listCommand().empty())
128                                 os << "\\" << from_ascii(fl.listCommand()) << "\n";
129                         else 
130                                 os << "%% "
131                                    << bformat(_("LyX cannot generate a list of %1$s"), getParam("type"))
132                                    << "\n";
133                 }
134         } else {
135                 os << "%%\\listof{" << getParam("type") << "}{"
136                    << bformat(_("List of %1$s"), getParam("type"))
137                    << "}\n";
138         }
139         return 1;
140 }
141
142
143 int InsetFloatList::plaintext(odocstream & os, OutputParams const &) const
144 {
145         os << screenLabel() << "\n\n";
146
147         buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os);
148
149         return PLAINTEXT_NEWLINE;
150 }
151
152
153 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const &) const {
154         FloatList const & floats = buffer().params().documentClass().floats();
155         FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
156
157         if (cit == floats.end()) {
158                 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
159                 return docstring();
160         }
161
162         string toctype;
163         docstring toclabel;
164         // FIXME
165         // Other builtin floats should be handled here. But I'm not sure if that is
166         // even possible yet, since I'm not sure if we have a TOC for such things.
167         // If so, then they should define ListName, as non-builtin floats do, and
168         // then we can use that. 
169         // Really, all floats should define that.
170         if (!cit->second.needsFloatPkg()) {
171                 // Only two different types allowed here:
172                 string const type = cit->second.floattype();
173                 if (type == "table") {
174                         toctype = "table";
175                         toclabel = _("List of Tables");
176                 } else if (type == "figure") {
177                         toctype = "figure";
178                         toclabel = _("List of Figures");
179                 } else {
180                         LYXERR0("Unknown Builtin Float!");
181                         return docstring();
182                 }
183         } else {
184                 toctype = to_utf8(getParam("type"));
185                 toclabel = buffer().B_(cit->second.listName());
186         }
187
188         // FIXME Do we need to check if it exists? If so, we need a new
189         // routine in TocBackend to do that.
190         Toc const & toc = buffer().tocBackend().toc(toctype);
191         if (toc.empty())
192                 return docstring();
193
194         // we want to look like a chapter, section, or whatever.
195         // so we're going to look for the layout with the minimum toclevel
196         // number > 0---because we don't want Part. 
197         // we'll take the first one, just because.
198         // FIXME This could be specified in the layout file.
199         DocumentClass const & dc = buffer().params().documentClass();
200         TextClass::LayoutList::const_iterator lit = dc.begin();
201         TextClass::LayoutList::const_iterator len = dc.end();
202         int minlevel = 1000;
203         Layout const * lay = NULL;
204         for (; lit != len; ++lit) {
205                 int const level = lit->toclevel;
206                 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
207                         continue;
208                 lay = &*lit;
209                 minlevel = level;
210         }
211         
212         string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
213         string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
214         
215         // we'll use our own stream, because we are going to defer everything.
216         // that's how we deal with the fact that we're probably inside a standard
217         // paragraph, and we don't want to be.
218         odocstringstream ods;
219         XHTMLStream xs(ods);
220
221         xs << html::StartTag("div", "class='toc'");
222         xs << html::StartTag("div", tocattr) 
223                  << toclabel 
224                  << html::EndTag("div");
225         
226         Toc::const_iterator it = toc.begin();
227         Toc::const_iterator const en = toc.end();
228         for (; it != en; ++it) {
229                 Paragraph const & par = it->dit().innerParagraph();
230                 string const attr = "class='lyxtoc-" + toctype + "'";
231                 Font const dummy;
232                 xs << html::StartTag("div", attr);
233                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
234                 xs << it->str() << " "
235                    << html::StartTag("a", parattr)
236                    // FIXME XHTML 
237                    // There ought to be a simple way to customize this.
238                    << XHTMLStream::NextRaw() << "&gt;"
239                    << html::EndTag("a");
240                 xs << html::EndTag("div");
241         }
242         xs << html::EndTag("div");
243         return ods.str();
244 }
245
246
247 void InsetFloatList::validate(LaTeXFeatures & features) const
248 {
249         features.useFloat(to_ascii(getParam("type")));
250 }
251
252
253 } // namespace lyx