]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloatlist.C
ws changes only
[lyx.git] / src / insets / insetfloatlist.C
1 /**
2  * \file insetfloatlist.C
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 "Floating.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "LaTeXFeatures.h"
23 #include "lyxlex.h"
24 #include "metricsinfo.h"
25 #include "toc.h"
26
27 #include "support/lstrings.h"
28
29 using lyx::support::bformat;
30
31 using std::endl;
32 using std::string;
33 using std::ostream;
34
35
36 InsetFloatList::InsetFloatList()
37         : InsetCommand(InsetCommandParams())
38 {}
39
40
41 InsetFloatList::InsetFloatList(string const & type)
42         : InsetCommand(InsetCommandParams())
43 {
44         setCmdName(type);
45 }
46
47
48 InsetFloatList::~InsetFloatList()
49 {
50         InsetCommandMailer mailer("toc", *this);
51         mailer.hideDialog();
52 }
53
54
55 string const InsetFloatList::getScreenLabel(Buffer const & buf) const
56 {
57         FloatList const & floats = buf.params().getLyXTextClass().floats();
58         FloatList::const_iterator it = floats[getCmdName()];
59         if (it != floats.end())
60                 return _(it->second.listName());
61         else
62                 return _("ERROR: Nonexistent float type!");
63 }
64
65
66 InsetOld::Code InsetFloatList::lyxCode() const
67 {
68         return InsetOld::FLOAT_LIST_CODE;
69 }
70
71
72 void InsetFloatList::write(Buffer const &, ostream & os) const
73 {
74         os << "FloatList " << getCmdName() << "\n";
75 }
76
77
78 void InsetFloatList::read(Buffer const & buf, LyXLex & lex)
79 {
80         FloatList const & floats = buf.params().getLyXTextClass().floats();
81         string token;
82
83         if (lex.eatLine()) {
84                 setCmdName(lex.getString());
85                 lyxerr[Debug::INSETS] << "FloatList::float_type: " << getCmdName() << endl;
86                 if (!floats.typeExist(getCmdName()))
87                         lex.printError("InsetFloatList: Unknown float type: `$$Token'");
88         } else
89                 lex.printError("InsetFloatList: Parse error: `$$Token'");
90         while (lex.isOK()) {
91                 lex.nextToken();
92                 token = lex.getString();
93                 if (token == "\\end_inset")
94                         break;
95         }
96         if (token != "\\end_inset") {
97                 lex.printError("Missing \\end_inset at this point. "
98                                "Read: `$$Token'");
99         }
100 }
101
102
103 void InsetFloatList::metrics(MetricsInfo & mi, Dimension & dim) const
104 {
105         InsetCommand::metrics(mi, dim);
106         int center_indent = (mi.base.textwidth - dim.wid) / 2;
107     Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
108         button().setBox(b);
109
110         dim.wid = mi.base.textwidth;
111         dim_ = dim;
112 }
113
114
115 void InsetFloatList::draw(PainterInfo & pi, int x, int y) const
116 {
117         InsetCommand::draw(pi, x + button().box().x1, y);
118 }
119
120
121 dispatch_result InsetFloatList::localDispatch(FuncRequest const & cmd)
122 {
123         switch (cmd.action) {
124                 case LFUN_MOUSE_RELEASE:
125                         if (button().box().contains(cmd.x, cmd.y))
126                                 InsetCommandMailer("toc", *this).showDialog(cmd.view());
127                         return DISPATCHED;
128
129                 case LFUN_INSET_DIALOG_SHOW:
130                         InsetCommandMailer("toc", *this).showDialog(cmd.view());
131                         return DISPATCHED;
132
133                 default:
134                         return InsetCommand::localDispatch(cmd);
135         }
136 }
137
138
139 int InsetFloatList::latex(Buffer const & buf, ostream & os,
140                           LatexRunParams const &) const
141 {
142         FloatList const & floats = buf.params().getLyXTextClass().floats();
143         FloatList::const_iterator cit = floats[getCmdName()];
144
145         if (cit != floats.end()) {
146                 if (cit->second.builtin()) {
147                         // Only two different types allowed here:
148                         string const type = cit->second.type();
149                         if (type == "table") {
150                                 os << "\\listoftables\n";
151                         } else if (type == "figure") {
152                                 os << "\\listoffigures\n";
153                         } else {
154                                 os << "%% unknown builtin float\n";
155                         }
156                 } else {
157                         os << "\\listof{" << getCmdName() << "}{"
158                            << cit->second.listName() << "}\n";
159                 }
160         } else {
161                 os << "%%\\listof{" << getCmdName() << "}{"
162                    << bformat(_("List of %1$s"), cit->second.name())
163                    << "}\n";
164         }
165         return 1;
166 }
167
168
169 int InsetFloatList::ascii(Buffer const & buffer, ostream & os, int) const
170 {
171         os << getScreenLabel(buffer) << "\n\n";
172
173         lyx::toc::asciiTocList(getCmdName(), buffer, os);
174
175         os << "\n";
176         return 0;
177 }
178
179
180 void InsetFloatList::validate(LaTeXFeatures & features) const
181 {
182         features.useFloat(getCmdName());
183 }