NEURON
objcmd.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 #if HAVE_IV
4 #include <InterViews/window.h>
5 #include "ivoc.h"
6 #include "scenevie.h"
7 #include "utility.h"
8 #endif
9 
10 #include <ocnotify.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include "objcmd.h"
14 #include "oc2iv.h"
15 
16 extern Object* hoc_thisobject;
19 void (*nrnpy_cmdtool)(Object*, int type, double x, double y, int kd);
20 double (*nrnpy_func_call)(Object*, int, int*);
21 
22 HocCommand::HocCommand(const char* cmd) {
23  init(cmd, hoc_thisobject);
24 }
25 HocCommand::HocCommand(const char* cmd, Object* obj) {
26  init(cmd, obj);
27 }
28 
30  // must wrap a PyObject method or tuple of (method, arg1, ...)
31  // hold a reference to the wrapped PyObject
32  if (strcmp(pobj->ctemplate->sym->name, "PythonObject") != 0) {
33  hoc_execerror(hoc_object_name(pobj), "not a PythonObject");
34  }
35  po_ = pobj;
37  s_ = NULL;
38  obj_ = NULL;
39 }
40 
41 void HocCommand::init(const char* cmd, Object* obj) {
42  s_ = new CopyString(cmd);
43  obj_ = obj;
44  po_ = NULL;
45  if (obj_) {
46  nrn_notify_when_void_freed((void*)obj_, this);
47  }
48 }
49 
50 void HocCommand::update(Observable*) { // obj_ has been freed
51  obj_ = NULL;
52  delete s_;
53  s_ = new CopyString("");
54 }
55 
57  if (obj_) {
59  }
60  if (s_) {
61  delete s_;
62  }
63  if (po_) {
65  }
66 }
67 
69 #if HAVE_IV
70  char buf[200];
71  if (obj_) {
72  sprintf(buf,"%s %s",
73  s_->string(),
74  obj_->ctemplate->sym->name
75  );
76  }else{
77  sprintf(buf, "%s", s_->string());
78  }
79  Oc::help(buf);
80 #endif
81 }
82 
83 const char* ccc = "PythonObject";
84 const char* HocCommand::name() {
85  if (po_ == NULL) {
86  return s_->string();
87  }else{
88  return ccc;
89  }
90 }
91 
93  if (!s_) {
94  return;
95  }
96  char buf[256];
97  if (obj_) {
98  sprintf(buf, "// execute(\"%s\", %p)\n", name(), obj_);
99  }else{
100  sprintf(buf, "{%s}\n", name());
101  }
102  hoc_audit_command(buf);
103 }
104 
105 int HocCommand::execute(bool notify) {
106  int err;
107  if (po_) {
109  err = (*nrnpy_hoccommand_exec)(po_);
110  }else{
111  if (!s_) {
112  return 0;
113  }
114  char buf[256];
115  sprintf(buf, "{%s}\n", s_->string());
116  err = hoc_obj_run(buf, obj_);
117  }
118 #if HAVE_IV
119  if (notify) {
120  Oc oc;
121  oc.notify();
122  }
123 #endif
124  return err;
125 }
126 int HocCommand::exec_strret(char* buf, int size, bool notify) {
127  assert (po_)
128  int err = (*nrnpy_hoccommand_exec_strret)(po_, buf, size);
129 #if HAVE_IV
130  if (notify) {
131  Oc oc;
132  oc.notify();
133  }
134 #endif
135  return err;
136 }
137 int HocCommand::execute(const char* s, bool notify) {
138  assert(po_ == NULL);
139  char buf[256];
140  sprintf(buf, "{%s}\n", s);
141  int err = hoc_obj_run(buf, obj_);
142 #if HAVE_IV
143  if (notify) {
144  Oc oc;
145  oc.notify();
146  }
147 #endif
148  return err;
149 }
150 
151 double HocCommand::func_call(int narg, int* perr) {
152  if (po_) {
153  if (nrnpy_func_call) {
154  return (*nrnpy_func_call)(po_, narg, perr);
155  }
156  *perr = 1;
157  return 0.0;
158  }
159  Symbol* s = NULL;
160  if (obj_ && obj_->ctemplate) {
161  s = hoc_table_lookup(name(), obj_->ctemplate->symtable);
162  }
163  if (!s) {
164  s = hoc_lookup(name());
165  }
166  if (!s) {
167  hoc_execerror(name(), "is not a symbol in HocCommand::func_call");
168  }
169  return hoc_call_objfunc(s, narg, obj_);
170 }
171 
172 #if HAVE_IV // to end of file
173 
174 HocCommandAction::HocCommandAction(HocCommand* hc) {
175  hc_ = hc;
176 }
177 
178 HocCommandAction::~HocCommandAction() {delete hc_;}
179 
181  hc_->execute();
182 }
183 
184 HocCommandTool::HocCommandTool(HocCommand* hc) : Rubberband() {
185  hc_ = hc;
186 }
187 
188 HocCommandTool::~HocCommandTool() {
189  delete hc_;
190 }
191 
192 bool HocCommandTool::event(Event& e) {
193  char buf[256];
194  Coord x, y;
195  int kd;
196 #ifdef WIN32
197  if (e.type() != Event::down && e.type() != Event::up && e.window()->canvas()->any_damage()) {
198  return true;
199  }
200 #endif
201  if (e.type() == Event::down) {
203  Resource::ref(this);
204  e.grab(this);
205 #ifdef WIN32
206  e.window()->grab_pointer();
207 #endif
208  }
209  kd = e.control_is_down() + e.shift_is_down()*2 + e.meta_is_down()*4;
210 // transformer().inverse_transform(e.pointer_x(), e.pointer_y(), x, y);
211 // the hoc callback may change the size of the view
213  t.transform(e.pointer_x(), e.pointer_y(), x, y);
214 //printf("%g %g %g %g\n", e.pointer_x(), e.pointer_y(), x, y);
215  if (e.type() == Event::up) {
216  e.ungrab(this);
217 #ifdef WIN32
218  e.window()->ungrab_pointer();
219 #endif
220  }
221  if (hc_->pyobject()) {
222  (*nrnpy_cmdtool)(hc_->pyobject(), e.type(), x, y, kd);
223  Oc oc;
224  oc.notify();
225  }else{
226  sprintf(buf, "%s(%d, %g, %g, %d)", hc_->name(), e.type(), x, y, kd);
227  hc_->execute(buf, true);
228  }
229  if (e.type() == Event::up) {
230  Resource::unref(this);
231  }
232  return true;
233 }
234 #endif
#define assert(ex)
Definition: hocassrt.h:26
short type
Definition: cabvars.h:10
void execute(Inst *p)
Definition: code.cpp:2651
const Transformer & s2o() const
Definition: scenevie.h:132
#define Rubberband
Definition: _defines.h:244
Definition: ivoc.h:36
#define Coord
Definition: _defines.h:19
Symbol * hoc_lookup(const char *)
void
char * hoc_object_name(Object *ob)
Definition: hoc_oop.cpp:84
static void help(const char *)
void nrn_notify_when_void_freed(void *p, Observer *ob)
Definition: ivoc.cpp:54
int(* nrnpy_hoccommand_exec_strret)(Object *, char *, int)
Definition: objcmd.cpp:18
virtual void audit()
Definition: objcmd.cpp:92
virtual void help()
Definition: objcmd.cpp:68
static int narg()
Definition: ivocvect.cpp:135
const char * ccc
Definition: objcmd.cpp:83
virtual void ref() const
Definition: resource.cpp:47
void notify()
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
#define e
Definition: passive0.cpp:24
const char * string() const
Definition: string.h:139
virtual ~HocCommand()
Definition: objcmd.cpp:56
const char * name()
Definition: objcmd.cpp:84
_CONST char * s
Definition: system.cpp:74
int execute(bool notify=true)
Definition: objcmd.cpp:105
void hoc_obj_unref(Object *obj)
Definition: hoc_oop.cpp:1998
int
Definition: nrnmusic.cpp:71
#define CopyString
Definition: _defines.h:2
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:741
int(* nrnpy_hoccommand_exec)(Object *)
Definition: objcmd.cpp:17
void(* nrnpy_cmdtool)(Object *, int type, double x, double y, int kd)
Definition: objcmd.cpp:19
Definition: model.h:57
virtual void unref() const
Definition: resource.cpp:52
void hoc_obj_ref(Object *obj)
Definition: hoc_oop.cpp:1980
#define Event
Definition: _defines.h:107
Object * obj_
Definition: objcmd.h:34
int exec_strret(char *buf, int size, bool notify=true)
Definition: objcmd.cpp:126
CopyString * s_
Definition: objcmd.h:35
HocCommand(const char *)
Definition: objcmd.cpp:22
#define Transformer
Definition: _defines.h:316
Definition: hocdec.h:226
int hoc_obj_run(const char *, Object *)
Definition: hoc_oop.cpp:323
double func_call(int narg, int *perr=NULL)
Definition: objcmd.cpp:151
void init(const char *, Object *)
Definition: objcmd.cpp:41
Symbol * hoc_table_lookup(const char *, Symlist *)
Definition: symbol.cpp:60
void nrn_notify_pointer_disconnect(Observer *ob)
Definition: ivoc.cpp:72
char buf[512]
Definition: init.cpp:13
void hoc_audit_command(const char *buf)
Definition: audit.cpp:124
double hoc_call_objfunc(Symbol *s, int narg, Object *ob)
Definition: hoc_oop.cpp:390
double(* nrnpy_func_call)(Object *, int, int *)
Definition: objcmd.cpp:20
double t
Definition: init.cpp:123
static XYView * current_pick_view()
return NULL
Definition: cabcode.cpp:461
virtual void update(Observable *)
Definition: objcmd.cpp:50
Object * po_
Definition: objcmd.h:36
Object * hoc_thisobject
Definition: hoc_oop.cpp:132
void handle_old_focus()