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