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