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 "nrnpy.h"
14 #include "objcmd.h"
15 #include "oc2iv.h"
16 
17 extern Object* hoc_thisobject;
18 
19 HocCommand::HocCommand(const char* cmd) {
20  init(cmd, hoc_thisobject);
21 }
22 HocCommand::HocCommand(const char* cmd, Object* obj) {
23  init(cmd, obj);
24 }
25 
27  // must wrap a PyObject method or tuple of (method, arg1, ...)
28  // hold a reference to the wrapped PyObject
29  if (strcmp(pobj->ctemplate->sym->name, "PythonObject") != 0) {
30  hoc_execerror(hoc_object_name(pobj), "not a PythonObject");
31  }
32  po_ = pobj;
34  obj_ = NULL;
35 }
36 
37 void HocCommand::init(const char* cmd, Object* obj) {
38  s_ = std::make_unique<std::string>(cmd);
39  obj_ = obj;
40  po_ = NULL;
41  if (obj_) {
42  nrn_notify_when_void_freed((void*) obj_, this);
43  }
44 }
45 
46 void HocCommand::update(Observable*) { // obj_ has been freed
47  obj_ = NULL;
48  s_ = std::make_unique<std::string>("");
49 }
50 
52  if (obj_) {
54  }
55  if (po_) {
57  }
58 }
59 
61 #if HAVE_IV
62  char buf[200];
63  if (obj_) {
64  Sprintf(buf, "%s %s", s_->c_str(), obj_->ctemplate->sym->name);
65  } else {
66  Sprintf(buf, "%s", s_->c_str());
67  }
68  Oc::help(buf);
69 #endif
70 }
71 
72 const char* ccc = "PythonObject";
73 const char* HocCommand::name() {
74  if (po_ == NULL) {
75  return s_->c_str();
76  } else {
77  return ccc;
78  }
79 }
80 
82  if (!s_) {
83  return;
84  }
85  char buf[256];
86  if (obj_) {
87  Sprintf(buf, "// execute(\"%s\", %p)\n", name(), obj_);
88  } else {
89  Sprintf(buf, "{%s}\n", name());
90  }
92 }
93 
94 int HocCommand::execute(bool notify) {
95  int err;
96  if (po_) {
99  } else {
100  if (!s_) {
101  return 0;
102  }
103  char buf[256];
104  Sprintf(buf, "{%s}\n", s_->c_str());
105  err = hoc_obj_run(buf, obj_);
106  }
107 #if HAVE_IV
108  if (notify) {
109  Oc oc;
110  oc.notify();
111  }
112 #endif
113  return err;
114 }
115 int HocCommand::exec_strret(char* buf, int size, bool notify) {
116  assert(po_);
118 #if HAVE_IV
119  if (notify) {
120  Oc oc;
121  oc.notify();
122  }
123 #endif
124  return err;
125 }
126 int HocCommand::execute(const char* s, bool notify) {
127  assert(po_ == NULL);
128  char buf[256];
129  Sprintf(buf, "{%s}\n", s);
130  int err = hoc_obj_run(buf, obj_);
131 #if HAVE_IV
132  if (notify) {
133  Oc oc;
134  oc.notify();
135  }
136 #endif
137  return err;
138 }
139 
140 double HocCommand::func_call(int narg, int* perr) {
141  if (po_) {
142  if (neuron::python::methods.call_func) {
143  return neuron::python::methods.call_func(po_, narg, perr);
144  }
145  *perr = 1;
146  return 0.0;
147  }
148  Symbol* s = NULL;
149  if (obj_ && obj_->ctemplate) {
151  }
152  if (!s) {
153  s = hoc_lookup(name());
154  }
155  if (!s) {
156  hoc_execerror(name(), "is not a symbol in HocCommand::func_call");
157  }
158  return hoc_call_objfunc(s, narg, obj_);
159 }
160 
161 #if HAVE_IV // to end of file
162 
163 HocCommandAction::HocCommandAction(HocCommand* hc) {
164  hc_ = hc;
165 }
166 
167 HocCommandAction::~HocCommandAction() {
168  delete hc_;
169 }
170 
171 void HocCommandAction::execute() {
172  hc_->execute();
173 }
174 
175 HocCommandTool::HocCommandTool(HocCommand* hc)
176  : Rubberband() {
177  hc_ = hc;
178 }
179 
180 HocCommandTool::~HocCommandTool() {
181  delete hc_;
182 }
183 
184 bool HocCommandTool::event(Event& e) {
185  char buf[256];
186  Coord x, y;
187  int kd;
188 #ifdef WIN32
189  if (e.type() != Event::down && e.type() != Event::up && e.window()->canvas()->any_damage()) {
190  return true;
191  }
192 #endif
193  if (e.type() == Event::down) {
195  Resource::ref(this);
196  e.grab(this);
197 #ifdef WIN32
198  e.window()->grab_pointer();
199 #endif
200  }
201  kd = e.control_is_down() + e.shift_is_down() * 2 + e.meta_is_down() * 4;
202  // transformer().inverse_transform(e.pointer_x(), e.pointer_y(), x, y);
203  // the hoc callback may change the size of the view
205  t.transform(e.pointer_x(), e.pointer_y(), x, y);
206  // printf("%g %g %g %g\n", e.pointer_x(), e.pointer_y(), x, y);
207  if (e.type() == Event::up) {
208  e.ungrab(this);
209 #ifdef WIN32
210  e.window()->ungrab_pointer();
211 #endif
212  }
213  if (hc_->pyobject()) {
214  neuron::python::methods.cmdtool(hc_->pyobject(), e.type(), x, y, kd);
215  Oc oc;
216  oc.notify();
217  } else {
218  Sprintf(buf, "%s(%d, %g, %g, %d)", hc_->name(), e.type(), x, y, kd);
219  hc_->execute(buf, true);
220  }
221  if (e.type() == Event::up) {
222  Resource::unref(this);
223  }
224  return true;
225 }
226 #endif
#define Transformer
Definition: _defines.h:313
#define Coord
Definition: _defines.h:17
#define Event
Definition: _defines.h:105
virtual void audit()
Definition: objcmd.cpp:81
std::unique_ptr< std::string > s_
Definition: objcmd.h:42
double func_call(int narg, int *perr=NULL)
Definition: objcmd.cpp:140
int exec_strret(char *buf, int size, bool notify=true)
Definition: objcmd.cpp:115
HocCommand(const char *)
Definition: objcmd.cpp:19
virtual ~HocCommand()
Definition: objcmd.cpp:51
virtual void update(Observable *)
Definition: objcmd.cpp:46
int execute(bool notify=true)
Definition: objcmd.cpp:94
Object * po_
Definition: objcmd.h:43
Object * obj_
Definition: objcmd.h:41
const char * name()
Definition: objcmd.cpp:73
void init(const char *, Object *)
Definition: objcmd.cpp:37
virtual void help()
Definition: objcmd.cpp:60
Definition: ivoc.h:36
static void help(const char *)
void notify()
virtual void ref() const
Definition: resource.cpp:42
virtual void unref() const
Definition: resource.cpp:47
static XYView * current_pick_view()
const Transformer & s2o() const
Definition: scenevie.h:137
Symbol * hoc_table_lookup(const char *, Symlist *)
Definition: symbol.cpp:48
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:384
void hoc_obj_ref(Object *obj)
Definition: hoc_oop.cpp:1844
char * hoc_object_name(Object *ob)
Definition: hoc_oop.cpp:73
Symbol * hoc_lookup(const char *)
Definition: symbol.cpp:59
void hoc_obj_unref(Object *obj)
Definition: hoc_oop.cpp:1881
#define assert(ex)
Definition: hocassrt.h:24
static int narg()
Definition: ivocvect.cpp:121
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:315
void hoc_execerror(const char *s1, const char *s2)
Definition: nrnoc_aux.cpp:39
impl_ptrs methods
Collection of pointers to functions with python-version-specific implementations.
Definition: nrnpy.cpp:21
int Sprintf(char(&buf)[N], const char *fmt, Args &&... args)
Redirect sprintf to snprintf if the buffer size can be deduced.
Definition: wrap_sprintf.h:14
s
Definition: multisend.cpp:521
static int hoccommand_exec(Object *ho)
Definition: nrnpy_p2h.cpp:339
const char * ccc
Definition: objcmd.cpp:72
Object * hoc_thisobject
Definition: hoc_oop.cpp:121
void handle_old_focus()
#define NULL
Definition: spdefs.h:105
Definition: hocdec.h:173
cTemplate * ctemplate
Definition: hocdec.h:180
Definition: model.h:47
char * name
Definition: model.h:61
Symbol * sym
Definition: hocdec.h:147
Symlist * symtable
Definition: hocdec.h:148
void(* cmdtool)(Object *, int type, double x, double y, int kd)
Definition: nrnpy.h:30
int(* hoccommand_exec_strret)(Object *, char *, int)
Definition: nrnpy.h:38
double(* call_func)(Object *, int, int *)
Definition: nrnpy.h:27
int(* hoccommand_exec)(Object *)
Definition: nrnpy.h:37