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