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