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