]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Make listings dialog translatable (mostly strings from InsetListingsParams), fix...
[lyx.git] / src / insets / InsetCommand.cpp
1 /**
2  * \file InsetCommand.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetCommand.h"
15
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "DispatchResult.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "gettext.h"
22 #include "Lexer.h"
23 #include "MetricsInfo.h"
24
25 #include <sstream>
26
27
28 namespace lyx {
29
30 using std::string;
31 using std::istringstream;
32 using std::ostream;
33 using std::ostringstream;
34
35
36 InsetCommand::InsetCommand(InsetCommandParams const & p,
37                            string const & mailer_name)
38         : p_(p),
39           mailer_name_(mailer_name),
40           mouse_hover_(false),
41           updateButtonLabel_(true)
42 {}
43
44
45 InsetCommand::~InsetCommand()
46 {
47         if (!mailer_name_.empty())
48                 InsetCommandMailer(mailer_name_, *this).hideDialog();
49 }
50
51
52 bool InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
53 {
54         if (updateButtonLabel_) {
55                 updateButtonLabel_ = false;
56                 button_.update(getScreenLabel(*mi.base.bv->buffer()),
57                                editable() != NOT_EDITABLE);
58         }
59         button_.metrics(mi, dim);
60         bool const changed = dim_ != dim;
61         dim_ = dim;
62         return changed;
63 }
64
65
66 bool InsetCommand::setMouseHover(bool mouse_hover)
67 {
68         mouse_hover_ = mouse_hover;
69         return true;
70 }
71
72
73 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
74 {
75         setPosCache(pi, x, y);
76         button_.setRenderState(mouse_hover_);
77         button_.draw(pi, x, y);
78 }
79
80
81 void InsetCommand::setParams(InsetCommandParams const & p)
82 {
83         p_ = p;
84         updateButtonLabel_ = true;
85 }
86
87
88 int InsetCommand::latex(Buffer const &, odocstream & os,
89                         OutputParams const &) const
90 {
91         os << getCommand();
92         return 0;
93 }
94
95
96 int InsetCommand::plaintext(Buffer const & buf, odocstream & os,
97                             OutputParams const &) const
98 {
99         docstring const str = "[" + buf.B_("LaTeX Command: ") + from_utf8(getCmdName()) + "]";
100         os << str;
101         return str.size();
102 }
103
104
105 int InsetCommand::docbook(Buffer const &, odocstream &,
106                           OutputParams const &) const
107 {
108         return 0;
109 }
110
111
112 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
113 {
114         switch (cmd.action) {
115         case LFUN_INSET_REFRESH:
116                 updateButtonLabel_ = true;
117                 break;
118
119         case LFUN_INSET_MODIFY: {
120                 InsetCommandParams p(p_.getCmdName());
121                 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
122                 if (p.getCmdName().empty())
123                         cur.noUpdate();
124                 else
125                         setParams(p);
126                 break;
127         }
128
129         case LFUN_INSET_DIALOG_UPDATE: {
130                 string const name = to_utf8(cmd.argument());
131                 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
132                 break;
133         }
134
135         case LFUN_MOUSE_RELEASE: {
136                 if (!mailer_name_.empty())
137                         InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
138                 break;
139         }
140
141         default:
142                 Inset::doDispatch(cur, cmd);
143                 break;
144         }
145
146 }
147
148
149 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
150         FuncStatus & status) const
151 {
152         switch (cmd.action) {
153         // suppress these
154         case LFUN_ERT_INSERT:
155                 status.enabled(false);
156                 return true;
157         // we handle these
158         case LFUN_INSET_REFRESH:
159         case LFUN_INSET_MODIFY:
160         case LFUN_INSET_DIALOG_UPDATE:
161                 status.enabled(true);
162                 return true;
163         default:
164                 return Inset::getStatus(cur, cmd, status);
165         }
166 }
167
168
169 void InsetCommand::replaceContents(std::string const & from, string const & to)
170 {
171         if (getContents() == from)
172                 setContents(to);
173 }
174
175
176 InsetCommandMailer::InsetCommandMailer(string const & name,
177                                        InsetCommand & inset)
178         : name_(name), inset_(inset)
179 {}
180
181
182 string const InsetCommandMailer::inset2string(Buffer const &) const
183 {
184         return params2string(name(), inset_.params());
185 }
186
187
188 void InsetCommandMailer::string2params(string const & name,
189                                        string const & in,
190                                        InsetCommandParams & params)
191 {
192         params.clear();
193         if (in.empty())
194                 return;
195
196         istringstream data(in);
197         Lexer lex(0,0);
198         lex.setStream(data);
199
200         string n;
201         lex >> n;
202         if (!lex || n != name)
203                 return print_mailer_error("InsetCommandMailer", in, 1, name);
204
205         // This is part of the inset proper that is usually swallowed
206         // by Text::readInset
207         string id;
208         lex >> id;
209         if (!lex || id != "LatexCommand")
210                 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
211
212         params.read(lex);
213 }
214
215
216 string const
217 InsetCommandMailer::params2string(string const & name,
218                                   InsetCommandParams const & params)
219 {
220         ostringstream data;
221         data << name << ' ';
222         params.write(data);
223         data << "\\end_inset\n";
224         return data.str();
225 }
226
227
228 } // namespace lyx