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;
18 int (*nrnpy_hoccommand_exec_strret)(Object*, char*, int);
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", s_->string(), obj_->ctemplate->sym->name);
73  } else {
74  sprintf(buf, "%s", s_->string());
75  }
76  Oc::help(buf);
77 #endif
78 }
79 
80 const char* ccc = "PythonObject";
81 const char* HocCommand::name() {
82  if (po_ == NULL) {
83  return s_->string();
84  } else {
85  return ccc;
86  }
87 }
88 
90  if (!s_) {
91  return;
92  }
93  char buf[256];
94  if (obj_) {
95  sprintf(buf, "// execute(\"%s\", %p)\n", name(), obj_);
96  } else {
97  sprintf(buf, "{%s}\n", name());
98  }
100 }
101 
102 int HocCommand::execute(bool notify) {
103  int err;
104  if (po_) {
106  err = (*nrnpy_hoccommand_exec)(po_);
107  } else {
108  if (!s_) {
109  return 0;
110  }
111  char buf[256];
112  sprintf(buf, "{%s}\n", s_->string());
113  err = hoc_obj_run(buf, obj_);
114  }
115 #if HAVE_IV
116  if (notify) {
117  Oc oc;
118  oc.notify();
119  }
120 #endif
121  return err;
122 }
123 int HocCommand::exec_strret(char* buf, int size, bool notify) {
124  assert(po_) int err = (*nrnpy_hoccommand_exec_strret)(po_, buf, size);
125 #if HAVE_IV
126  if (notify) {
127  Oc oc;
128  oc.notify();
129  }
130 #endif
131  return err;
132 }
133 int HocCommand::execute(const char* s, bool notify) {
134  assert(po_ == NULL);
135  char buf[256];
136  sprintf(buf, "{%s}\n", s);
137  int err = hoc_obj_run(buf, obj_);
138 #if HAVE_IV
139  if (notify) {
140  Oc oc;
141  oc.notify();
142  }
143 #endif
144  return err;
145 }
146 
147 double HocCommand::func_call(int narg, int* perr) {
148  if (po_) {
149  if (nrnpy_func_call) {
150  return (*nrnpy_func_call)(po_, narg, perr);
151  }
152  *perr = 1;
153  return 0.0;
154  }
155  Symbol* s = NULL;
156  if (obj_ && obj_->ctemplate) {
157  s = hoc_table_lookup(name(), obj_->ctemplate->symtable);
158  }
159  if (!s) {
160  s = hoc_lookup(name());
161  }
162  if (!s) {
163  hoc_execerror(name(), "is not a symbol in HocCommand::func_call");
164  }
165  return hoc_call_objfunc(s, narg, obj_);
166 }
167 
168 #if HAVE_IV // to end of file
169 
170 HocCommandAction::HocCommandAction(HocCommand* hc) {
171  hc_ = hc;
172 }
173 
174 HocCommandAction::~HocCommandAction() {
175  delete hc_;
176 }
177 
179  hc_->execute();
180 }
181 
182 HocCommandTool::HocCommandTool(HocCommand* hc)
183  : Rubberband() {
184  hc_ = hc;
185 }
186 
187 HocCommandTool::~HocCommandTool() {
188  delete hc_;
189 }
190 
191 bool HocCommandTool::event(Event& e) {
192  char buf[256];
193  Coord x, y;
194  int kd;
195 #ifdef WIN32
196  if (e.type() != Event::down && e.type() != Event::up && e.window()->canvas()->any_damage()) {
197  return true;
198  }
199 #endif
200  if (e.type() == Event::down) {
202  Resource::ref(this);
203  e.grab(this);
204 #ifdef WIN32
205  e.window()->grab_pointer();
206 #endif
207  }
208  kd = e.control_is_down() + e.shift_is_down() * 2 + e.meta_is_down() * 4;
209  // transformer().inverse_transform(e.pointer_x(), e.pointer_y(), x, y);
210  // the hoc callback may change the size of the view
212  t.transform(e.pointer_x(), e.pointer_y(), x, y);
213  // printf("%g %g %g %g\n", e.pointer_x(), e.pointer_y(), x, y);
214  if (e.type() == Event::up) {
215  e.ungrab(this);
216 #ifdef WIN32
217  e.window()->ungrab_pointer();
218 #endif
219  }
220  if (hc_->pyobject()) {
221  (*nrnpy_cmdtool)(hc_->pyobject(), e.type(), x, y, kd);
222  Oc oc;
223  oc.notify();
224  } else {
225  sprintf(buf, "%s(%d, %g, %g, %d)", hc_->name(), e.type(), x, y, kd);
226  hc_->execute(buf, true);
227  }
228  if (e.type() == Event::up) {
229  Resource::unref(this);
230  }
231  return true;
232 }
233 #endif
#define Transformer
Definition: _defines.h:316
#define Coord
Definition: _defines.h:19
#define Event
Definition: _defines.h:107
#define CopyString
Definition: _defines.h:2
short type
Definition: cabvars.h:9
virtual void audit()
Definition: objcmd.cpp:89
double func_call(int narg, int *perr=NULL)
Definition: objcmd.cpp:147
CopyString * s_
Definition: objcmd.h:42
int exec_strret(char *buf, int size, bool notify=true)
Definition: objcmd.cpp:123
HocCommand(const char *)
Definition: objcmd.cpp:22
virtual ~HocCommand()
Definition: objcmd.cpp:56
virtual void update(Observable *)
Definition: objcmd.cpp:50
int execute(bool notify=true)
Definition: objcmd.cpp:102
Object * po_
Definition: objcmd.h:43
Object * obj_
Definition: objcmd.h:41
const char * name()
Definition: objcmd.cpp:81
void init(const char *, Object *)
Definition: objcmd.cpp:41
virtual void help()
Definition: objcmd.cpp:68
Definition: ivoc.h:36
static void help(const char *)
void notify()
virtual void ref() const
Definition: resource.cpp:47
virtual void unref() const
Definition: resource.cpp:52
const char * string() const
Definition: string.h:139
static XYView * current_pick_view()
const Transformer & s2o() const
Definition: scenevie.h:139
Symbol * hoc_table_lookup(const char *, Symlist *)
Definition: symbol.cpp:61
void execute(Inst *p)
Definition: code.cpp:2661
double t
Definition: cvodeobj.cpp:59
sprintf(buf, " if (secondorder) {\n" " int _i;\n" " for (_i = 0; _i < %d; ++_i) {\n" " _p[_slist%d[_i]] += dt*_p[_dlist%d[_i]];\n" " }}\n", numeqn, listnum, listnum)
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:754
char buf[512]
Definition: init.cpp:13
void hoc_audit_command(const char *buf)
Definition: audit.cpp:122
double hoc_call_objfunc(Symbol *s, int narg, Object *ob)
Definition: hoc_oop.cpp:389
void hoc_obj_ref(Object *obj)
Definition: hoc_oop.cpp:1810
char * hoc_object_name(Object *ob)
Definition: hoc_oop.cpp:72
Symbol * hoc_lookup(const char *)
void hoc_obj_unref(Object *obj)
Definition: hoc_oop.cpp:1828
#define assert(ex)
Definition: hocassrt.h:32
void
void nrn_notify_when_void_freed(void *p, Observer *ob)
Definition: ivoc.cpp:52
void nrn_notify_pointer_disconnect(Observer *ob)
Definition: ivoc.cpp:70
int hoc_obj_run(const char *, Object *)
Definition: hoc_oop.cpp:322
static int narg()
Definition: ivocvect.cpp:150
void(* nrnpy_cmdtool)(Object *, int type, double x, double y, int kd)
Definition: objcmd.cpp:19
double(* nrnpy_func_call)(Object *, int, int *)
Definition: objcmd.cpp:20
const char * ccc
Definition: objcmd.cpp:80
int(* nrnpy_hoccommand_exec_strret)(Object *, char *, int)
Definition: objcmd.cpp:18
int(* nrnpy_hoccommand_exec)(Object *)
Definition: objcmd.cpp:17
Object * hoc_thisobject
Definition: hoc_oop.cpp:122
void handle_old_focus()
#define e
Definition: passive0.cpp:22
#define NULL
Definition: sptree.h:16
Definition: hocdec.h:227
Definition: model.h:57