]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloatList.cpp
23796fcf7d966785683b14b7103e8d2149295384
[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 "LaTeXFeatures.h"
22 #include "Lexer.h"
23 #include "MetricsInfo.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                 if (cit->second.builtin()) {
123                         // Only two different types allowed here:
124                         string const type = cit->second.type();
125                         if (type == "table") {
126                                 os << "\\listoftables\n";
127                         } else if (type == "figure") {
128                                 os << "\\listoffigures\n";
129                         } else {
130                                 os << "%% unknown builtin float\n";
131                         }
132                 } else {
133                         os << "\\listof{" << getParam("type") << "}{"
134                            << buffer().B_(cit->second.listName()) << "}\n";
135                 }
136         } else {
137                 os << "%%\\listof{" << getParam("type") << "}{"
138                    << bformat(_("List of %1$s"), from_utf8(cit->second.name()))
139                    << "}\n";
140         }
141         return 1;
142 }
143
144
145 int InsetFloatList::plaintext(odocstream & os, OutputParams const &) const
146 {
147         os << screenLabel() << "\n\n";
148
149         buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os);
150
151         return PLAINTEXT_NEWLINE;
152 }
153
154
155 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const &) const {
156         FloatList const & floats = buffer().params().documentClass().floats();
157         FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
158
159         if (cit == floats.end()) {
160                 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
161                 return docstring();
162         }
163
164         string toctype;
165         docstring toclabel;
166         if (cit->second.builtin()) {
167                 // Only two different types allowed here:
168                 string const type = cit->second.type();
169                 if (type == "table") {
170                         toctype = "table";
171                         toclabel = _("List of Tables");
172                 } else if (type == "figure") {
173                         toctype = "figure";
174                         toclabel = _("List of Figures");
175                 } else {
176                         LYXERR0("Unknown Builtin Float!");
177                         return docstring();
178                 }
179         } else {
180                 toctype = to_utf8(getParam("type"));
181                 toclabel = buffer().B_(cit->second.listName());
182         }
183
184         // FIXME Do we need to check if it exists? If so, we need a new
185         // routine in TocBackend to do that.
186         Toc const & toc = buffer().tocBackend().toc(toctype);
187         if (toc.empty())
188                 return docstring();
189
190         // we want to look like a chapter, section, or whatever.
191         // so we're going to look for the layout with the minimum toclevel
192         // number > 0---because we don't want Part. 
193         // we'll take the first one, just because.
194         // FIXME This could be specified in the layout file.
195         DocumentClass const & dc = buffer().params().documentClass();
196         TextClass::LayoutList::const_iterator lit = dc.begin();
197         TextClass::LayoutList::const_iterator len = dc.end();
198         int minlevel = 1000;
199         Layout const * lay = NULL;
200         for (; lit != len; ++lit) {
201                 int const level = lit->toclevel;
202                 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
203                         continue;
204                 lay = &*lit;
205                 minlevel = level;
206         }
207         
208         string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
209         string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
210         
211         // we'll use our own stream, because we are going to defer everything.
212         // that's how we deal with the fact that we're probably inside a standard
213         // paragraph, and we don't want to be.
214         odocstringstream ods;
215         XHTMLStream xs(ods);
216
217         xs << StartTag("div", "class='toc'");
218         xs << StartTag("div", tocattr) 
219                  << toclabel 
220                  << EndTag("div");
221         
222         Toc::const_iterator it = toc.begin();
223         Toc::const_iterator const en = toc.end();
224         for (; it != en; ++it) {
225                 Paragraph const & par = it->dit().innerParagraph();
226                 string const attr = "class='lyxtoc-" + toctype + "'";
227                 Font const dummy;
228                 xs << StartTag("div", attr);
229                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
230                 xs << it->str() << " "
231                    << StartTag("a", parattr)
232                    // FIXME XHTML 
233                    // There ought to be a simple way to customize this.
234                    << XHTMLStream::NextRaw() << "&seArr;"
235                    << EndTag("a");
236                 xs << EndTag("div");
237         }
238         xs << EndTag("div");
239         return ods.str();
240 }
241
242
243 void InsetFloatList::validate(LaTeXFeatures & features) const
244 {
245         features.useFloat(to_ascii(getParam("type")));
246 }
247
248
249 } // namespace lyx