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