]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloatList.cpp
28485ceb0dfa2fa8edb44c3ba75bde29cbcdf567
[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         if (!cit->second.needsFloatPkg()) {
155                 // Only two different types allowed here:
156                 string const type = cit->second.floattype();
157                 if (type == "table") {
158                         toctype = "table";
159                         toclabel = _("List of Tables");
160                 } else if (type == "figure") {
161                         toctype = "figure";
162                         toclabel = _("List of Figures");
163                 } else {
164                         LYXERR0("Unknown Builtin Float!");
165                         return docstring();
166                 }
167         } else {
168                 toctype = to_utf8(getParam("type"));
169                 toclabel = buffer().B_(cit->second.listName());
170         }
171
172         // FIXME Do we need to check if it exists? If so, we need a new
173         // routine in TocBackend to do that.
174         Toc const & toc = buffer().tocBackend().toc(toctype);
175         if (toc.empty())
176                 return docstring();
177
178         // we want to look like a chapter, section, or whatever.
179         // so we're going to look for the layout with the minimum toclevel
180         // number > 0---because we don't want Part. 
181         // we'll take the first one, just because.
182         // FIXME This could be specified in the layout file.
183         DocumentClass const & dc = buffer().params().documentClass();
184         TextClass::LayoutList::const_iterator lit = dc.begin();
185         TextClass::LayoutList::const_iterator len = dc.end();
186         int minlevel = 1000;
187         Layout const * lay = NULL;
188         for (; lit != len; ++lit) {
189                 int const level = lit->toclevel;
190                 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
191                         continue;
192                 lay = &*lit;
193                 minlevel = level;
194         }
195         
196         string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
197         string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
198         
199         // we'll use our own stream, because we are going to defer everything.
200         // that's how we deal with the fact that we're probably inside a standard
201         // paragraph, and we don't want to be.
202         odocstringstream ods;
203         XHTMLStream xs(ods);
204
205         xs << html::StartTag("div", "class='toc'");
206         xs << html::StartTag("div", tocattr) 
207                  << toclabel 
208                  << html::EndTag("div");
209         
210         Toc::const_iterator it = toc.begin();
211         Toc::const_iterator const en = toc.end();
212         for (; it != en; ++it) {
213                 Paragraph const & par = it->dit().innerParagraph();
214                 string const attr = "class='lyxtoc-" + toctype + "'";
215                 Font const dummy;
216                 xs << html::StartTag("div", attr);
217                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
218                 xs << it->str() << " "
219                    << html::StartTag("a", parattr)
220                    // FIXME XHTML 
221                    // There ought to be a simple way to customize this.
222                    << XHTMLStream::NextRaw() << "&seArr;"
223                    << html::EndTag("a");
224                 xs << html::EndTag("div");
225         }
226         xs << html::EndTag("div");
227         return ods.str();
228 }
229
230
231 void InsetFloatList::validate(LaTeXFeatures & features) const
232 {
233         features.useFloat(to_ascii(getParam("type")));
234 }
235
236
237 } // namespace lyx