]> git.lyx.org Git - features.git/blob - src/insets/InsetFloatList.cpp
e0dedf0b80856a95b7c4bb2760fcbbad7d903703
[features.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 "TextClass.h"
25 #include "TocBackend.h"
26
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include <ostream>
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38
39 InsetFloatList::InsetFloatList()
40         : InsetCommand(InsetCommandParams(FLOAT_LIST_CODE), "toc")
41 {}
42
43
44 InsetFloatList::InsetFloatList(string const & type)
45         : InsetCommand(InsetCommandParams(FLOAT_LIST_CODE), "toc")
46 {
47         setParam("type", from_ascii(type));
48 }
49
50
51 ParamInfo const & InsetFloatList::findInfo(string const & /* cmdName */)
52 {
53         static ParamInfo param_info_;
54         if (param_info_.empty()) {
55                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
56         }
57         return param_info_;
58 }
59
60
61 //HACK
62 bool InsetFloatList::isCompatibleCommand(string const & s)
63 {
64         string str = s.substr(0, 6);
65         return str == "listof";
66 }
67
68
69 docstring InsetFloatList::screenLabel() const
70 {
71         FloatList const & floats = buffer().params().documentClass().floats();
72         FloatList::const_iterator it = floats[to_ascii(getParam("type"))];
73         if (it != floats.end())
74                 return buffer().B_(it->second.listName());
75         else
76                 return _("ERROR: Nonexistent float type!");
77 }
78
79
80 void InsetFloatList::write(ostream & os) const
81 {
82         os << "FloatList " << to_ascii(getParam("type")) << "\n";
83 }
84
85
86 void InsetFloatList::read(Lexer & lex)
87 {
88         lex.setContext("InsetFloatList::read");
89         FloatList const & floats = buffer().params().documentClass().floats();
90         string token;
91
92         if (lex.eatLine()) {
93                 setParam("type", lex.getDocString());
94                 LYXERR(Debug::INSETS, "FloatList::float_type: "
95                                       << to_ascii(getParam("type")));
96                 if (!floats.typeExist(to_ascii(getParam("type"))))
97                         lex.printError("Unknown float type");
98         } else {
99                 lex.printError("Parse error");
100         }
101
102         while (lex.isOK()) {
103                 lex.next();
104                 token = lex.getString();
105                 if (token == "\\end_inset")
106                         break;
107         }
108         if (token != "\\end_inset") {
109                 lex.printError("Missing \\end_inset at this point.");
110         }
111 }
112
113
114 int InsetFloatList::latex(odocstream & os, OutputParams const &) const
115 {
116         FloatList const & floats = buffer().params().documentClass().floats();
117         FloatList::const_iterator cit = floats[to_ascii(getParam("type"))];
118
119         if (cit != floats.end()) {
120                 if (cit->second.builtin()) {
121                         // Only two different types allowed here:
122                         string const type = cit->second.type();
123                         if (type == "table") {
124                                 os << "\\listoftables\n";
125                         } else if (type == "figure") {
126                                 os << "\\listoffigures\n";
127                         } else {
128                                 os << "%% unknown builtin float\n";
129                         }
130                 } else {
131                         os << "\\listof{" << getParam("type") << "}{"
132                            << buffer().B_(cit->second.listName()) << "}\n";
133                 }
134         } else {
135                 os << "%%\\listof{" << getParam("type") << "}{"
136                    << bformat(_("List of %1$s"), from_utf8(cit->second.name()))
137                    << "}\n";
138         }
139         return 1;
140 }
141
142
143 int InsetFloatList::plaintext(odocstream & os, OutputParams const &) const
144 {
145         os << screenLabel() << "\n\n";
146
147         buffer().tocBackend().writePlaintextTocList(to_ascii(getParam("type")), os);
148
149         return PLAINTEXT_NEWLINE;
150 }
151
152
153 void InsetFloatList::validate(LaTeXFeatures & features) const
154 {
155         features.useFloat(to_ascii(getParam("type")));
156 }
157
158
159 } // namespace lyx