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