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