]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
267ecb7d5fc2c391bae48a90c92f3a1b3b12a0c0
[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 "BufferView.h"
17 #include "DispatchResult.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "gettext.h"
21 #include "Lexer.h"
22 #include "MetricsInfo.h"
23
24 #include <sstream>
25
26
27 namespace lyx {
28
29 using std::string;
30 using std::istringstream;
31 using std::ostream;
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 bool 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         bool const changed = dim_ != dim;
60         dim_ = dim;
61         return changed;
62 }
63
64
65 bool InsetCommand::setMouseHover(bool mouse_hover)
66 {
67         mouse_hover_ = mouse_hover;
68         return true;
69 }
70
71
72 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
73 {
74         setPosCache(pi, x, y);
75         button_.setRenderState(mouse_hover_);
76         button_.draw(pi, x, y);
77 }
78
79
80 void InsetCommand::setParams(InsetCommandParams const & p)
81 {
82         p_ = p;
83         updateButtonLabel_ = true;
84 }
85
86
87 int InsetCommand::latex(Buffer const &, odocstream & os,
88                         OutputParams const &) const
89 {
90         os << getCommand();
91         return 0;
92 }
93
94
95 int InsetCommand::plaintext(Buffer const &, odocstream & os,
96                             OutputParams const &) const
97 {
98         docstring const str = "[" + _("LaTeX Command: ") + from_utf8(getCmdName()) + "]";
99         os << str;
100         return str.size();
101 }
102
103
104 int InsetCommand::docbook(Buffer const &, odocstream &,
105                           OutputParams const &) const
106 {
107         return 0;
108 }
109
110
111 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
112 {
113         switch (cmd.action) {
114         case LFUN_INSET_REFRESH:
115                 updateButtonLabel_ = true;
116                 break;
117
118         case LFUN_INSET_MODIFY: {
119                 InsetCommandParams p(p_.getCmdName());
120                 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
121                 if (p.getCmdName().empty())
122                         cur.noUpdate();
123                 else
124                         setParams(p);
125                 break;
126         }
127
128         case LFUN_INSET_DIALOG_UPDATE: {
129                 string const name = to_utf8(cmd.argument());
130                 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
131                 break;
132         }
133
134         case LFUN_MOUSE_RELEASE: {
135                 if (!mailer_name_.empty())
136                         InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
137                 break;
138         }
139
140         default:
141                 InsetBase::doDispatch(cur, cmd);
142                 break;
143         }
144
145 }
146
147
148 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
149         FuncStatus & status) const
150 {
151         switch (cmd.action) {
152         // suppress these
153         case LFUN_ERT_INSERT:
154                 status.enabled(false);
155                 return true;
156         // we handle these
157         case LFUN_INSET_REFRESH:
158         case LFUN_INSET_MODIFY:
159         case LFUN_INSET_DIALOG_UPDATE:
160                 status.enabled(true);
161                 return true;
162         default:
163                 return InsetBase::getStatus(cur, cmd, status);
164         }
165 }
166
167
168 void InsetCommand::replaceContents(std::string const & from, string const & to)
169 {
170         if (getContents() == from)
171                 setContents(to);
172 }
173
174
175 InsetCommandMailer::InsetCommandMailer(string const & name,
176                                        InsetCommand & inset)
177         : name_(name), inset_(inset)
178 {}
179
180
181 string const InsetCommandMailer::inset2string(Buffer const &) const
182 {
183         return params2string(name(), inset_.params());
184 }
185
186
187 void InsetCommandMailer::string2params(string const & name,
188                                        string const & in,
189                                        InsetCommandParams & params)
190 {
191         params.clear();
192         if (in.empty())
193                 return;
194
195         istringstream data(in);
196         Lexer lex(0,0);
197         lex.setStream(data);
198
199         string n;
200         lex >> n;
201         if (!lex || n != name)
202                 return print_mailer_error("InsetCommandMailer", in, 1, name);
203
204         // This is part of the inset proper that is usually swallowed
205         // by LyXText::readInset
206         string id;
207         lex >> id;
208         if (!lex || id != "LatexCommand")
209                 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
210
211         params.read(lex);
212 }
213
214
215 string const
216 InsetCommandMailer::params2string(string const & name,
217                                   InsetCommandParams const & params)
218 {
219         ostringstream data;
220         data << name << ' ';
221         params.write(data);
222         data << "\\end_inset\n";
223         return data.str();
224 }
225
226
227 } // namespace lyx