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