NEURON
grglyph.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 // hoc level Glyph implementation for graphing
3 #include <stdio.h>
4 #include "classreg.h"
5 #include "oc2iv.h"
6 #if HAVE_IV
7 #include "ivoc.h"
8 #include <InterViews/printer.h>
9 #include <InterViews/image.h>
10 #include "grglyph.h"
11 #include "idraw.h"
12 
13 extern Image* gif_image(const char*);
14 #else
15 #include <InterViews/resource.h>
16 class GrGlyph : public Resource {
17 public:
18  GrGlyph(Object*);
19  virtual ~GrGlyph();
20  Object** temp_objvar();
21 private:
23 };
24 #endif // HAVE_IV
25 
26 #include "gui-redirect.h"
27 
28 extern Object** (*nrnpy_gui_helper_)(const char* name, Object* obj);
29 extern double (*nrnpy_object_to_double_)(Object*);
30 
31 double gr_addglyph(void* v) {
32  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Graph.addglyph", v);
33 #if HAVE_IV
34 IFGUI
35  Graph* g = (Graph*)v;
36  Object* obj = *hoc_objgetarg(1);
37  check_obj_type(obj, "Glyph");
38  GrGlyph* gl = (GrGlyph*)(obj->u.this_pointer);
39  Coord x = *getarg(2);
40  Coord y = *getarg(3);
41  Coord sx = ifarg(4) ? *getarg(4) : 1.;
42  Coord sy = ifarg(5) ? *getarg(5) : 1.;
43  Coord rot = ifarg(6) ? *getarg(6) : 0.;
44  int fix = ifarg(7) ? int(chkarg(7, 0, 2)) : 0;
45  GrGlyphItem* ggi = new GrGlyphItem(gl, sx, sy, rot);
46  switch(fix) {
47  case 0:
48  g->append(ggi);
49  break;
50  case 1:
51  g->append_fixed(ggi);
52  break;
53  case 2:
54  g->append_viewfixed(ggi);
55  break;
56  }
57  g->move(g->count()-1, x, y);
58 ENDGUI
59 #endif
60  return 0.;
61 }
62 
63 static Object** g_new_path(void* v) {
64  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.path", v);
65  GrGlyph* g = (GrGlyph*)v;
66 #if HAVE_IV
67 IFGUI
68  g->new_path();
69 ENDGUI
70 #endif
71  return g->temp_objvar();
72 }
73 
74 static Object** g_move_to(void* v) {
75  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.m", v);
76  GrGlyph* g = (GrGlyph*)v;
77 #if HAVE_IV
78 IFGUI
79  g->move_to(*getarg(1), *getarg(2));
80 ENDGUI
81 #endif
82  return g->temp_objvar();
83 }
84 
85 static Object** g_line_to(void* v) {
86  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.l", v);
87  GrGlyph* g = (GrGlyph*)v;
88 #if HAVE_IV
89 IFGUI
90  g->line_to(*getarg(1), *getarg(2));
91 ENDGUI
92 #endif
93  return g->temp_objvar();
94 }
95 
96 static Object** g_control_point(void* v) {
97  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.cpt", v);
98  GrGlyph* g = (GrGlyph*)v;
99 #if HAVE_IV
100 IFGUI
101  g->control_point(*getarg(1), *getarg(2));
102 ENDGUI
103 #endif
104  return g->temp_objvar();
105 }
106 
107 static Object** g_curve_to(void* v) {
108  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.curve", v);
109  GrGlyph* g = (GrGlyph*)v;
110 #if HAVE_IV
111 IFGUI
112  g->curve_to(*getarg(1), *getarg(2), *getarg(3), *getarg(4), *getarg(5), *getarg(6));
113 ENDGUI
114 #endif
115  return g->temp_objvar();
116 }
117 
118 static Object** g_stroke(void* v) {
119  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.s", v);
120  GrGlyph* g = (GrGlyph*)v;
121 #if HAVE_IV
122 IFGUI
123  int ci = ifarg(1) ? int(chkarg(1, 0, 10000)) : 1;
124  int bi = ifarg(2) ? int(chkarg(2, 0, 10000)) : 0;
125  g->stroke(ci, bi);
126 ENDGUI
127 #endif
128  return g->temp_objvar();
129 }
130 
131 static Object** g_close_path(void* v) {
132  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.close", v);
133  GrGlyph* g = (GrGlyph*)v;
134 #if HAVE_IV
135 IFGUI
136  g->close_path();
137 ENDGUI
138 #endif
139  return g->temp_objvar();
140 }
141 
142 static Object** g_fill(void* v) {
143  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.fill", v);
144  GrGlyph* g = (GrGlyph*)v;
145 #if HAVE_IV
146 IFGUI
147  int ci = ifarg(1) ? int(chkarg(1, 0, 10000)) : 1;
148  g->fill(ci);
149 ENDGUI
150 #endif
151  return g->temp_objvar();
152 }
153 
154 static Object** g_erase(void* v) {
155  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.erase", v);
156  GrGlyph* g = (GrGlyph*)v;
157 #if HAVE_IV
158 IFGUI
159  g->erase();
160 ENDGUI
161 #endif
162  return g->temp_objvar();
163 }
164 
165 static Object** g_circle(void* v) {
166  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.circle", v);
167  GrGlyph* g = (GrGlyph*)v;
168 #if HAVE_IV
169 IFGUI
170  g->circle(*getarg(1), *getarg(2), *getarg(3));
171 ENDGUI
172 #endif
173  return g->temp_objvar();
174 }
175 
176 static Object** g_gif(void* v) {
177  TRY_GUI_REDIRECT_ACTUAL_OBJ("Glyph.gif", v);
178  GrGlyph* g = (GrGlyph*)v;
179 #if HAVE_IV
180 IFGUI
181  g->gif(gargstr(1));
182 ENDGUI
183 #endif
184  return g->temp_objvar();
185 }
186 
187 static Symbol* sggl_;
188 
190  0, 0
191 };
192 
194  "path", g_new_path,
195  "m", g_move_to,
196  "l", g_line_to,
197  "s", g_stroke,
198  "close", g_close_path,
199  "fill", g_fill,
200  "curve", g_curve_to,
201  "cpt", g_control_point,
202  "erase", g_erase,
203  "gif", g_gif,
204  "circle", g_circle,
205  0, 0
206 };
207 
208 static void* cons(Object* o) {
209  TRY_GUI_REDIRECT_OBJ("Glyph", NULL);
210  GrGlyph* g = new GrGlyph(o);
211  g->ref();
212  return g;
213 }
214 
215 static void destruct(void* v) {
216  TRY_GUI_REDIRECT_NO_RETURN("~Glyph", v);
217  GrGlyph* g = (GrGlyph*)v;
218  g->unref();
219 }
220 
221 void GrGlyph_reg() {
222  class2oc("Glyph", cons, destruct, members, NULL, objmembers, NULL);
223  sggl_ = hoc_lookup("Glyph");
224 }
225 
227  obj_ = o;
228 #if HAVE_IV
229 IFGUI
230  type_ = new DataVec(10);
231  x_ = new DataVec(10);
232  y_ = new DataVec(10);
233  type_->ref();
234  x_->ref();
235  y_->ref();
236  gif_ = NULL;
237 ENDGUI
238 #endif
239 }
240 
242 #if HAVE_IV
243 IFGUI
244  type_->unref();
245  x_->unref();
246  y_->unref();
248 ENDGUI
249 #endif
250 }
251 
253  GrGlyph* gg = (GrGlyph*)this;
254  Object** po;
255  po = hoc_temp_objptr(gg->obj_);
256  return po;
257 }
258 
259 #if HAVE_IV
260 
261 GrGlyphItem::GrGlyphItem(Glyph* g, float sx, float sy, float angle)
262  : GraphItem(g)
263 {
264  t_.scale(sx, sy);
265  t_.rotate(angle);
266 }
268 }
269 
270 void GrGlyphItem::allocate(Canvas* c, const Allocation& a, Extension& e) {
271  e.set(c, a);
272 }
273 
274 void GrGlyphItem::draw(Canvas* c, const Allocation& a) const {
275  c->push_transform();
276  Transformer t = t_;
277  t.translate(a.x(), a.y());
278  c->transform(t);
279  IfIdraw(pict(t));
280 //float m1, m2, m3,m4,m5,m6;
281 //t.matrix(m1,m2,m3,m4,m5,m6);
282 //printf("transformer %g %g %g %g %g %g\n", m1,m2,m3,m4,m5,m6);
283  body()->draw(c, a);
284  c->pop_transform();
285  IfIdraw(end());
286 }
287 
288 void GrGlyphItem::print(Printer* c, const Allocation& a) const {
289  draw(c, a);
290 }
291 
292 void GrGlyph::gif(const char* file) {
293  gif_ = gif_image(file);
294 }
295 void GrGlyph::new_path() {
296  type_->add(1);
297 }
298 void GrGlyph::move_to(Coord x, Coord y) {
299  type_->add(2); x_->add(x); y_->add(y);
300 }
301 void GrGlyph::line_to(Coord x, Coord y) {
302  type_->add(3); x_->add(x); y_->add(y);
303 }
304 void GrGlyph::curve_to(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2) {
305  type_->add(4); x_->add(x); y_->add(y);
306  x_->add(x1); y_->add(y1);
307  x_->add(x2); y_->add(y2);
308 }
309 void GrGlyph::close_path() {
310  type_->add(5);
311 }
312 void GrGlyph::circle(Coord x, Coord y, Coord r) {
313  const Coord p0 = 1.00000000 * r;
314  const Coord p1 = 0.89657547 * r; // cos 30 * sqrt(1 + tan 15 * tan 15)
315  const Coord p2 = 0.70710678 * r; // cos 45
316  const Coord p3 = 0.51763809 * r; // cos 60 * sqrt(1 + tan 15 * tan 15)
317  const Coord p4 = 0.26794919 * r; // tan 15
318  new_path();
319  move_to(x+p0, y);
320  curve_to(x+p2, y+p2, x+p0, y+p4, x+p1, y+p3);
321  curve_to(x, y+p0, x+p3, y+p1, x+p4, y+p0);
322  curve_to(x-p2, y+p2, x-p4, y+p0, x-p3, y+p1);
323  curve_to(x-p0, y, x-p1, y + p3, x-p0, y+p4);
324  curve_to(x-p2, y-p2, x-p0, y-p4, x-p1, y-p3);
325  curve_to(x, y-p0, x-p3, y-p1, x-p4, y-p0);
326  curve_to(x+p2, y-p2, x+p4, y-p0, x+p3, y-p1);
327  curve_to(x+p0, y, x+p1, y-p3, x+p0, y-p4);
328  close_path();
329 }
330 
331 void GrGlyph::stroke(int ci, int bi) {
332  type_->add(6); type_->add(ci); type_->add(bi);
333 }
334 void GrGlyph::fill(int ci) {
335  type_->add(7); type_->add(ci);
336 }
338  type_->add(8); x_->add(x); y_->add(y);
339 }
340 void GrGlyph::erase() {
341  type_->erase();
342  x_->erase();
343  y_->erase();
344  if (gif_) {
345  gif_->unref();
346  gif_ = NULL;
347  }
348 }
349 
350 void GrGlyph::draw(Canvas* c, const Allocation& a) const {
351  int i, j;
352  Coord x, y;
353  if (gif_) {
354  gif_->draw(c, a);
355  }
356  for (i=0, j=0; i < type_->count(); ++i) {
357  switch(int(type_->get_val(i))) {
358  case 1:
359  c->new_path();
360  IfIdraw(new_path());
361  break;
362  case 2:
363  x = x_->get_val(j); y = y_->get_val(j); ++j;
364  c->move_to(x, y);
365  IfIdraw(move_to(x, y));
366  break;
367  case 3:
368  x = x_->get_val(j); y = y_->get_val(j); ++j;
369  c->line_to(x, y);
370  IfIdraw(line_to(x, y));
371  break;
372  case 4:
373  x = x_->get_val(j); y = y_->get_val(j);
374  c->curve_to(x, y, x_->get_val(j+1), y_->get_val(j+1),
375  x_->get_val(j+2), y_->get_val(j+2));
376  IfIdraw(curve_to(x, y, x_->get_val(j+1), y_->get_val(j+1), x_->get_val(j+2), y_->get_val(j+2)));
377  j += 3;
378  break;
379  case 5:
380  c->close_path();
381  IfIdraw(close_path());
382  break;
383  case 6:
384  x = type_->get_val(++i); y = type_->get_val(++i);
385  c->stroke(colors->color(int(x)), brushes->brush(int(y)));
386  IfIdraw(stroke(c, colors->color(int(x)), brushes->brush(int(y))));
387  break;
388  case 7:
389  x = type_->get_val(++i);
390  c->fill(colors->color(int(x)));
391  IfIdraw(fill(c, colors->color(int(x))));
392  break;
393  case 8:
394  x = x_->get_val(j); y = y_->get_val(j); ++j;
395  c->transformer().transform(x, y);
396 //printf("x=%g y=%g\n", x, y);
397  c->push_transform();
398  Transformer t;
399  c->transformer(t);
400  c->rect(x-2, y-2, x+2, y+2, colors->color(1), brushes->brush(0));
401  c->pop_transform();
402  break;
403  }
404  }
405 }
406 
407 void GrGlyph::request(Requisition& r) const {
408  Coord min, max, natural;
409  min = x_->min(); max = x_->max();
410  natural = max - min;
411  r.x_requirement().natural(natural);
412  if (natural > 0) {
413  r.x_requirement().alignment(-min/natural);
414  }
415 
416  min = y_->min(); max = y_->max();
417  natural = max - min;
418  r.y_requirement().natural(natural);
419  if (natural > 0) {
420  r.y_requirement().alignment(-min/natural);
421  }
422 
423  if (gif_) {
424  gif_->request(r);
425  }
426 }
427 
428 #endif
o
Definition: seclist.cpp:180
void GrGlyph_reg()
Definition: grglyph.cpp:221
double max(double a, double b)
Definition: geometry3d.cpp:22
static Object ** g_circle(void *v)
Definition: grglyph.cpp:165
virtual void draw(Canvas *, const Allocation &) const
void set(Canvas *, const Allocation &)
Coord x() const
Definition: geometry.h:290
virtual void print(Printer *, const Allocation &) const
#define TRY_GUI_REDIRECT_NO_RETURN(name, obj)
Definition: gui-redirect.h:44
DataVec * x_
Definition: grglyph.h:44
#define min(a, b)
Definition: matrix.h:157
static Object ** g_move_to(void *v)
Definition: grglyph.cpp:74
#define g
Definition: passive0.cpp:23
#define Glyph
Definition: _defines.h:132
#define Coord
Definition: _defines.h:19
Symbol * hoc_lookup(const char *)
void * this_pointer
Definition: hocdec.h:231
virtual void draw(Canvas *, const Allocation &) const
float get_val(int i) const
Definition: graph.h:175
Definition: graph.h:163
check_obj_type(o, "SectionList")
#define IfIdraw(arg)
Definition: idraw.h:61
#define v
Definition: md1redef.h:4
#define Image
Definition: _defines.h:150
Coord y() const
Definition: geometry.h:291
int count() const
Definition: graph.h:173
const Requirement & x_requirement() const
Definition: geometry.h:249
virtual void ref() const
Definition: resource.cpp:47
ColorPalette * colors
#define TRY_GUI_REDIRECT_ACTUAL_DOUBLE(name, obj)
Definition: gui-redirect.h:66
void curve_to(Coord, Coord, Coord, Coord, Coord, Coord)
static void destruct(void *v)
Definition: grglyph.cpp:215
#define e
Definition: passive0.cpp:24
#define gargstr
Definition: hocdec.h:14
Definition: graph.h:48
double gr_addglyph(void *v)
Definition: grglyph.cpp:31
static Object ** g_line_to(void *v)
Definition: grglyph.cpp:85
static Symbol * sggl_
Definition: grglyph.cpp:187
void erase()
DataVec * type_
Definition: grglyph.h:43
Object ** temp_objvar()
Definition: grglyph.cpp:252
#define Printer
Definition: _defines.h:211
const Requirement & y_requirement() const
Definition: geometry.h:250
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
Definition: hoc_oop.cpp:1581
virtual ~GrGlyph()
Definition: grglyph.cpp:241
void add(float)
static Object ** g_new_path(void *v)
Definition: grglyph.cpp:63
void alignment(float)
Definition: geometry.h:241
int
Definition: nrnmusic.cpp:71
static Object ** g_fill(void *v)
Definition: grglyph.cpp:142
float max() const
double(* nrnpy_object_to_double_)(Object *)
Definition: xmenu.cpp:14
#define ENDGUI
Definition: hocdec.h:352
virtual void request(Requisition &) const
Member_ret_obj_func objmembers[]
Definition: grglyph.cpp:193
#define Canvas
Definition: _defines.h:65
void gif(const char *)
void fill(int color)
const Brush * brush(int) const
void close_path()
size_t j
Definition: model.h:57
virtual void unref() const
Definition: resource.cpp:52
char * name
Definition: init.cpp:16
inode< _nt-> end
Definition: multicore.cpp:985
void erase()
Object * obj_
Definition: grglyph.cpp:22
BrushPalette * brushes
static Object ** g_curve_to(void *v)
Definition: grglyph.cpp:107
void control_point(Coord, Coord)
GrGlyphItem(Glyph *g, float scalex, float scaley, float rot)
static void * cons(Object *o)
Definition: grglyph.cpp:208
int ifarg(int)
Definition: code.cpp:1562
static Object ** g_control_point(void *v)
Definition: grglyph.cpp:96
#define TRY_GUI_REDIRECT_OBJ(name, obj)
Definition: gui-redirect.h:12
static Object ** g_erase(void *v)
Definition: grglyph.cpp:154
void line_to(Coord, Coord)
#define Transformer
Definition: _defines.h:316
void new_path()
static Object ** g_stroke(void *v)
Definition: grglyph.cpp:118
void circle(Coord x, Coord y, Coord r)
Definition: hocdec.h:226
#define TRY_GUI_REDIRECT_ACTUAL_OBJ(name, obj)
Definition: gui-redirect.h:86
#define getarg
Definition: hocdec.h:15
float min() const
Glyph * gif_
Definition: grglyph.h:47
#define i
Definition: md1redef.h:12
#define c
GrGlyph(Object *)
Definition: grglyph.cpp:226
union Object::@54 u
Object ** hoc_temp_objptr(Object *)
Definition: code.cpp:209
#define IFGUI
Definition: hocdec.h:351
double t
Definition: init.cpp:123
DataVec * y_
Definition: grglyph.h:45
virtual void allocate(Canvas *, const Allocation &, Extension &)
void stroke(int color, int brush)
Object ** hoc_objgetarg(int)
Definition: code.cpp:1568
virtual ~GrGlyphItem()
Member_func members[]
Definition: grglyph.cpp:189
const Color * color(int) const
void move_to(Coord, Coord)
return NULL
Definition: cabcode.cpp:461
double chkarg(int, double low, double high)
Definition: code2.cpp:608
static Object ** g_close_path(void *v)
Definition: grglyph.cpp:131
static Object ** g_gif(void *v)
Definition: grglyph.cpp:176
void natural(Coord)
Definition: geometry.h:235