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