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