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