]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloatList.cpp
Context menu item to add unknown branch (rest of #7643)
[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(odocstream & os, OutputParams const &) const
164 {
165         os << screenLabel() << "\n\n";
166
167         buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os);
168
169         return PLAINTEXT_NEWLINE;
170 }
171
172
173 docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const &) const {
174         FloatList const & floats = buffer().params().documentClass().floats();
175         FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
176
177         if (cit == floats.end()) {
178                 LYXERR0("Unknown float type `" << getParam("type") << "' in IFL::xhtml.");
179                 return docstring();
180         }
181
182         string toctype;
183         docstring toclabel;
184         // FIXME
185         // Other builtin floats should be handled here. But I'm not sure if that is
186         // even possible yet, since I'm not sure if we have a TOC for such things.
187         // If so, then they should define ListName, as non-builtin floats do, and
188         // then we can use that. 
189         // Really, all floats should define that.
190         if (cit->second.isPredefined()) {
191                 // Only two different types allowed here:
192                 string const type = cit->second.floattype();
193                 if (type == "table") {
194                         toctype = "table";
195                         toclabel = _("List of Tables");
196                 } else if (type == "figure") {
197                         toctype = "figure";
198                         toclabel = _("List of Figures");
199                 } else {
200                         LYXERR0("Unknown Builtin Float!");
201                         return docstring();
202                 }
203         } else {
204                 toctype = to_utf8(getParam("type"));
205                 toclabel = buffer().B_(cit->second.listName());
206         }
207
208         // FIXME Do we need to check if it exists? If so, we need a new
209         // routine in TocBackend to do that.
210         Toc const & toc = buffer().tocBackend().toc(toctype);
211         if (toc.empty())
212                 return docstring();
213
214         // we want to look like a chapter, section, or whatever.
215         // so we're going to look for the layout with the minimum toclevel
216         // number > 0---because we don't want Part. 
217         // we'll take the first one, just because.
218         // FIXME This could be specified in the layout file.
219         DocumentClass const & dc = buffer().params().documentClass();
220         TextClass::LayoutList::const_iterator lit = dc.begin();
221         TextClass::LayoutList::const_iterator len = dc.end();
222         int minlevel = 1000;
223         Layout const * lay = NULL;
224         for (; lit != len; ++lit) {
225                 int const level = lit->toclevel;
226                 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
227                         continue;
228                 lay = &*lit;
229                 minlevel = level;
230         }
231         
232         string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
233         string const tocattr = "class='tochead + toc-" + toctype + " " + tocclass + "'";
234         
235         // we'll use our own stream, because we are going to defer everything.
236         // that's how we deal with the fact that we're probably inside a standard
237         // paragraph, and we don't want to be.
238         odocstringstream ods;
239         XHTMLStream xs(ods);
240
241         xs << html::StartTag("div", "class='toc'");
242         xs << html::StartTag("div", tocattr) 
243                  << toclabel 
244                  << html::EndTag("div");
245         
246         Toc::const_iterator it = toc.begin();
247         Toc::const_iterator const en = toc.end();
248         for (; it != en; ++it) {
249                 Paragraph const & par = it->dit().innerParagraph();
250                 string const attr = "class='lyxtoc-" + toctype + "'";
251                 Font const dummy;
252                 xs << html::StartTag("div", attr);
253                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
254                 xs << it->str() << " "
255                    << html::StartTag("a", parattr)
256                    // FIXME XHTML 
257                    // There ought to be a simple way to customize this.
258                    << XHTMLStream::ESCAPE_NONE << "&gt;"
259                    << html::EndTag("a");
260                 xs << html::EndTag("div");
261         }
262         xs << html::EndTag("div");
263         return ods.str();
264 }
265
266
267 void InsetFloatList::validate(LaTeXFeatures & features) const
268 {
269         features.useFloat(to_ascii(getParam("type")));
270 }
271
272
273 } // namespace lyx