]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloatList.cpp
Initialize recently added forceownlines_ bool
[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 & op) 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 = translateIfPossible(from_ascii("List of Tables"),
197                                                        op.local_font->language()->lang());
198                 } else if (type == "figure") {
199                         toctype = "figure";
200                         toclabel = translateIfPossible(from_ascii("List of Figures"),
201                                                        op.local_font->language()->lang());
202                 } else {
203                         LYXERR0("Unknown Builtin Float!");
204                         return docstring();
205                 }
206         } else {
207                 toctype = to_utf8(getParam("type"));
208                 toclabel = translateIfPossible(from_utf8(cit->second.listName()),
209                                                op.local_font->language()->lang());
210         }
211
212         // FIXME Do we need to check if it exists? If so, we need a new
213         // routine in TocBackend to do that.
214         Toc const & toc = buffer().tocBackend().toc(toctype);
215         if (toc.empty())
216                 return docstring();
217
218         // we want to look like a chapter, section, or whatever.
219         // so we're going to look for the layout with the minimum toclevel
220         // number > 0---because we don't want Part. 
221         // we'll take the first one, just because.
222         // FIXME This could be specified in the layout file.
223         DocumentClass const & dc = buffer().params().documentClass();
224         TextClass::LayoutList::const_iterator lit = dc.begin();
225         TextClass::LayoutList::const_iterator len = dc.end();
226         int minlevel = 1000;
227         Layout const * lay = NULL;
228         for (; lit != len; ++lit) {
229                 int const level = lit->toclevel;
230                 if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel))
231                         continue;
232                 lay = &*lit;
233                 minlevel = level;
234         }
235         
236         string const tocclass = lay ? " " + lay->defaultCSSClass(): "";
237         string const tocattr = "class='tochead toc-" + toctype + tocclass + "'";
238         
239         // we'll use our own stream, because we are going to defer everything.
240         // that's how we deal with the fact that we're probably inside a standard
241         // paragraph, and we don't want to be.
242         odocstringstream ods;
243         XHTMLStream xs(ods);
244
245         InsetLayout const & il = getLayout();
246         string const & tag = il.htmltag();
247         xs << html::StartTag("div", "class='toc toc-floats'");
248         xs << html::StartTag(tag, tocattr)
249                  << toclabel 
250                  << html::EndTag(tag);
251         
252         Toc::const_iterator it = toc.begin();
253         Toc::const_iterator const en = toc.end();
254         for (; it != en; ++it) {
255                 Paragraph const & par = it->dit().innerParagraph();
256                 string const attr = "class='lyxtoc-floats lyxtoc-" + toctype + "'";
257                 xs << html::StartTag("div", attr);
258                 string const parattr = "href='#" + par.magicLabel() + "' class='lyxtoc-floats'";
259                 xs << html::StartTag("a", parattr)
260                    << it->str()
261                    << html::EndTag("a");
262                 xs << html::EndTag("div");
263         }
264         xs << html::EndTag("div");
265         return ods.str();
266 }
267
268
269 void InsetFloatList::validate(LaTeXFeatures & features) const
270 {
271         features.useFloat(to_ascii(getParam("type")));
272         features.useInsetLayout(getLayout());
273 }
274
275
276 docstring InsetFloatList::layoutName() const
277
278         return "FloatList:" + getParam("type");
279 }
280
281
282 } // namespace lyx