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