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