diff options
author | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
---|---|---|
committer | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
commit | 513389b4c1bc8afe4b2dc9947c534bfeb105e3da (patch) | |
tree | 10e673b35651ac567fed2eda0c679c7ade64cbc6 /apps/plugins/pdbox/PDa/extra/gcanvas.c | |
parent | 95fa7f6a2ef466444fbe3fe87efc6d5db6b77b36 (diff) | |
download | rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.tar.gz rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.zip |
Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin project of Wincent Balin. Stripped some non-sourcefiles and added a rockbox readme that needs a bit more info from Wincent. Is added to CATEGORIES and viewers, but not yet to SUBDIRS (ie doesn't build yet)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21044 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/pdbox/PDa/extra/gcanvas.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/extra/gcanvas.c | 758 |
1 files changed, 758 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/extra/gcanvas.c b/apps/plugins/pdbox/PDa/extra/gcanvas.c new file mode 100644 index 0000000000..aed5c96cec --- /dev/null +++ b/apps/plugins/pdbox/PDa/extra/gcanvas.c | |||
@@ -0,0 +1,758 @@ | |||
1 | /* (C) Guenter Geiger <geiger@xdv.org> */ | ||
2 | |||
3 | |||
4 | #include "m_pd.h" | ||
5 | #include "g_canvas.h" | ||
6 | |||
7 | /* ------------------------ gcanvas ----------------------------- */ | ||
8 | |||
9 | |||
10 | #define BACKGROUNDCOLOR "grey" | ||
11 | |||
12 | #define DEFAULTSIZE 80 | ||
13 | |||
14 | static t_class *gcanvas_class; | ||
15 | |||
16 | typedef struct _gcanvas | ||
17 | { | ||
18 | t_object x_obj; | ||
19 | t_glist * x_glist; | ||
20 | t_outlet* out2; | ||
21 | t_outlet* out3; | ||
22 | int x_width; | ||
23 | int x_height; | ||
24 | int x; | ||
25 | int y; | ||
26 | int x_xgrid; | ||
27 | int x_ygrid; | ||
28 | } t_gcanvas; | ||
29 | |||
30 | |||
31 | static void rectangle(void* cv,void* o,char c,int x, int y,int w,int h,char* color) { | ||
32 | sys_vgui(".x%x.c create rectangle \ | ||
33 | %d %d %d %d -tags %x%c -fill %s\n",cv,x,y,x+w,y+h,o,c,color); | ||
34 | } | ||
35 | |||
36 | static void move_object(void* cv,void* o,char c,int x, int y,int w,int h) { | ||
37 | sys_vgui(".x%x.c coords %x%c %d %d %d %d\n", | ||
38 | cv,o,c,x,y,x+w,y+h); | ||
39 | |||
40 | } | ||
41 | |||
42 | static void color_object(void* cv,void* o,char c,char* color) { | ||
43 | sys_vgui(".x%x.c itemconfigure %x%c -fill %s\n", cv, | ||
44 | o, c,color); | ||
45 | } | ||
46 | |||
47 | static void delete_object(void* cv,void* o,char c) { | ||
48 | sys_vgui(".x%x.c delete %x%c\n", | ||
49 | cv, o,c); | ||
50 | } | ||
51 | |||
52 | static void line(void* cv,void* o,char c,int x,int y,int w,int h,char* color) { | ||
53 | sys_vgui(".x%x.c create line \ | ||
54 | %d %d %d %d -tags %x%c -fill %s\n",cv,x,y,x+w,y+h,o,c,color); | ||
55 | } | ||
56 | |||
57 | |||
58 | /* widget helper functions */ | ||
59 | |||
60 | void gcanvas_drawme(t_gcanvas *x, t_glist *glist, int firsttime) | ||
61 | { | ||
62 | int i; | ||
63 | if (firsttime) { | ||
64 | rectangle(glist_getcanvas(glist),x,'a', | ||
65 | x->x_obj.te_xpix, x->x_obj.te_ypix, | ||
66 | x->x_width, x->x_height,BACKGROUNDCOLOR); | ||
67 | for (i=1;i<x->x_xgrid;i++) | ||
68 | line(glist_getcanvas(glist),x,'b'+ i, | ||
69 | x->x_obj.te_xpix + x->x_width*i/x->x_xgrid, | ||
70 | x->x_obj.te_ypix, | ||
71 | 0, x->x_height,"red"); | ||
72 | for (i=1;i<x->x_ygrid;i++) | ||
73 | line(glist_getcanvas(glist),x,'B'+ i, | ||
74 | x->x_obj.te_xpix, | ||
75 | x->x_obj.te_ypix + x->x_height*i/x->x_ygrid, | ||
76 | x->x_width, 0,"blue"); | ||
77 | } | ||
78 | else { | ||
79 | move_object( | ||
80 | glist_getcanvas(glist),x,'a', | ||
81 | x->x_obj.te_xpix, x->x_obj.te_ypix, | ||
82 | x->x_width, x->x_height); | ||
83 | for (i=1;i<x->x_xgrid;i++) | ||
84 | move_object(glist_getcanvas(glist),x,'b'+ i, | ||
85 | x->x_obj.te_xpix + x->x_width*i/x->x_xgrid, | ||
86 | x->x_obj.te_ypix, | ||
87 | 0, x->x_height); | ||
88 | for (i=1;i<x->x_ygrid;i++) | ||
89 | move_object(glist_getcanvas(glist),x,'B'+ i, | ||
90 | x->x_obj.te_xpix, | ||
91 | x->x_obj.te_ypix + x->x_height*i/x->x_ygrid, | ||
92 | x->x_width, 0); | ||
93 | } | ||
94 | |||
95 | { | ||
96 | /* outlets */ | ||
97 | int n = 3; | ||
98 | int nplus, i; | ||
99 | nplus = (n == 1 ? 1 : n-1); | ||
100 | for (i = 0; i < n; i++) | ||
101 | { | ||
102 | int onset = x->x_obj.te_xpix + (x->x_width - IOWIDTH) * i / nplus; | ||
103 | if (firsttime) | ||
104 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xo%d\n", | ||
105 | glist_getcanvas(glist), | ||
106 | onset, x->x_obj.te_ypix + x->x_height - 1, | ||
107 | onset + IOWIDTH, x->x_obj.te_ypix + x->x_height, | ||
108 | x, i); | ||
109 | else | ||
110 | sys_vgui(".x%x.c coords %xo%d %d %d %d %d\n", | ||
111 | glist_getcanvas(glist), x, i, | ||
112 | onset, x->x_obj.te_ypix + x->x_height - 1, | ||
113 | onset + IOWIDTH, x->x_obj.te_ypix + x->x_height); | ||
114 | } | ||
115 | /* inlets */ | ||
116 | n = 0; | ||
117 | nplus = (n == 1 ? 1 : n-1); | ||
118 | for (i = 0; i < n; i++) | ||
119 | { | ||
120 | int onset = x->x_obj.te_xpix + (x->x_width - IOWIDTH) * i / nplus; | ||
121 | if (firsttime) | ||
122 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xi%d\n", | ||
123 | glist_getcanvas(glist), | ||
124 | onset, x->x_obj.te_ypix, | ||
125 | onset + IOWIDTH, x->x_obj.te_ypix + 1, | ||
126 | x, i); | ||
127 | else | ||
128 | sys_vgui(".x%x.c coords %xi%d %d %d %d %d\n", | ||
129 | glist_getcanvas(glist), x, i, | ||
130 | onset, x->x_obj.te_ypix, | ||
131 | onset + IOWIDTH, x->x_obj.te_ypix + 1); | ||
132 | |||
133 | } | ||
134 | } | ||
135 | |||
136 | } | ||
137 | |||
138 | |||
139 | |||
140 | |||
141 | void gcanvas_erase(t_gcanvas* x,t_glist* glist) | ||
142 | { | ||
143 | int n,i; | ||
144 | delete_object(glist_getcanvas(glist),x,'a'); | ||
145 | for (i=1;i<x->x_xgrid;i++) | ||
146 | delete_object(glist_getcanvas(glist),x,'b'+ i); | ||
147 | for (i=1;i<x->x_ygrid;i++) | ||
148 | delete_object(glist_getcanvas(glist),x,'B'+ i); | ||
149 | |||
150 | n = 2; | ||
151 | while (n--) { | ||
152 | sys_vgui(".x%x.c delete %xo%d\n",glist_getcanvas(glist),x,n); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | |||
157 | |||
158 | /* ------------------------ gcanvas widgetbehaviour----------------------------- */ | ||
159 | |||
160 | |||
161 | static void gcanvas_getrect(t_gobj *z, t_glist *owner, | ||
162 | int *xp1, int *yp1, int *xp2, int *yp2) | ||
163 | { | ||
164 | int width, height; | ||
165 | t_gcanvas* s = (t_gcanvas*)z; | ||
166 | |||
167 | |||
168 | width = s->x_width; | ||
169 | height = s->x_height; | ||
170 | *xp1 = s->x_obj.te_xpix; | ||
171 | *yp1 = s->x_obj.te_ypix; | ||
172 | *xp2 = s->x_obj.te_xpix + width; | ||
173 | *yp2 = s->x_obj.te_ypix + height; | ||
174 | } | ||
175 | |||
176 | static void gcanvas_displace(t_gobj *z, t_glist *glist, | ||
177 | int dx, int dy) | ||
178 | { | ||
179 | t_gcanvas *x = (t_gcanvas *)z; | ||
180 | x->x_obj.te_xpix += dx; | ||
181 | x->x_obj.te_ypix += dy; | ||
182 | gcanvas_drawme(x, glist, 0); | ||
183 | canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x); | ||
184 | } | ||
185 | |||
186 | static void gcanvas_select(t_gobj *z, t_glist *glist, int state) | ||
187 | { | ||
188 | t_gcanvas *x = (t_gcanvas *)z; | ||
189 | color_object(glist,x,'a',state ? "blue" : BACKGROUNDCOLOR); | ||
190 | } | ||
191 | |||
192 | |||
193 | static void gcanvas_activate(t_gobj *z, t_glist *glist, int state) | ||
194 | { | ||
195 | /* t_text *x = (t_text *)z; | ||
196 | t_rtext *y = glist_findrtext(glist, x); | ||
197 | if (z->g_pd != gatom_class) rtext_activate(y, state);*/ | ||
198 | } | ||
199 | |||
200 | static void gcanvas_delete(t_gobj *z, t_glist *glist) | ||
201 | { | ||
202 | t_text *x = (t_text *)z; | ||
203 | canvas_deletelinesfor(glist_getcanvas(glist), x); | ||
204 | } | ||
205 | |||
206 | |||
207 | static void gcanvas_vis(t_gobj *z, t_glist *glist, int vis) | ||
208 | { | ||
209 | t_gcanvas* s = (t_gcanvas*)z; | ||
210 | if (vis) | ||
211 | gcanvas_drawme(s, glist, 1); | ||
212 | else | ||
213 | gcanvas_erase(s,glist); | ||
214 | } | ||
215 | |||
216 | /* can we use the normal text save function ?? */ | ||
217 | |||
218 | static void gcanvas_save(t_gobj *z, t_binbuf *b) | ||
219 | { | ||
220 | t_gcanvas *x = (t_gcanvas *)z; | ||
221 | binbuf_addv(b, "ssiisiiii", gensym("#X"),gensym("obj"), | ||
222 | (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix, | ||
223 | gensym("gcanvas"),x->x_width,x->x_height, | ||
224 | x->x_xgrid, | ||
225 | x->x_ygrid); | ||
226 | binbuf_addv(b, ";"); | ||
227 | } | ||
228 | |||
229 | |||
230 | t_widgetbehavior gcanvas_widgetbehavior; | ||
231 | |||
232 | static void gcanvas_motion(t_gcanvas *x, t_floatarg dx, t_floatarg dy) | ||
233 | { | ||
234 | x->x += dx; | ||
235 | x->y += dy; | ||
236 | outlet_float(x->out2,x->y); | ||
237 | outlet_float(x->x_obj.ob_outlet,x->x); | ||
238 | } | ||
239 | |||
240 | void gcanvas_key(t_gcanvas *x, t_floatarg f) | ||
241 | { | ||
242 | post("key"); | ||
243 | } | ||
244 | |||
245 | |||
246 | static void gcanvas_click(t_gcanvas *x, | ||
247 | t_floatarg xpos, t_floatarg ypos, t_floatarg shift, t_floatarg ctrl, | ||
248 | t_floatarg doit,int up) | ||
249 | { | ||
250 | glist_grab(x->x_glist, &x->x_obj.te_g, (t_glistmotionfn) gcanvas_motion, | ||
251 | (t_glistkeyfn) NULL, xpos, ypos); | ||
252 | |||
253 | x->x = xpos - x->x_obj.te_xpix; | ||
254 | x->y = ypos - x->x_obj.te_ypix; | ||
255 | outlet_float(x->out2,x->y); | ||
256 | outlet_float(x->x_obj.ob_outlet,x->x); | ||
257 | outlet_float(x->out3,0); | ||
258 | } | ||
259 | |||
260 | static int gcanvas_newclick(t_gobj *z, struct _glist *glist, | ||
261 | int xpix, int ypix, int shift, int alt, int dbl, int doit) | ||
262 | { | ||
263 | if (doit) | ||
264 | gcanvas_click((t_gcanvas *)z, (t_floatarg)xpix, (t_floatarg)ypix, | ||
265 | (t_floatarg)shift, 0, (t_floatarg)alt,dbl); | ||
266 | |||
267 | if (dbl) outlet_float(((t_gcanvas*)z)->out3,1); | ||
268 | return (1); | ||
269 | } | ||
270 | |||
271 | void gcanvas_size(t_gcanvas* x,t_floatarg w,t_floatarg h) { | ||
272 | x->x_width = w; | ||
273 | x->x_height = h; | ||
274 | gcanvas_drawme(x, x->x_glist, 0); | ||
275 | } | ||
276 | |||
277 | static void gcanvas_setwidget(void) | ||
278 | { | ||
279 | gcanvas_widgetbehavior.w_getrectfn = gcanvas_getrect; | ||
280 | gcanvas_widgetbehavior.w_displacefn = gcanvas_displace; | ||
281 | gcanvas_widgetbehavior.w_selectfn = gcanvas_select; | ||
282 | gcanvas_widgetbehavior.w_activatefn = gcanvas_activate; | ||
283 | gcanvas_widgetbehavior.w_deletefn = gcanvas_delete; | ||
284 | gcanvas_widgetbehavior.w_visfn = gcanvas_vis; | ||
285 | gcanvas_widgetbehavior.w_clickfn = gcanvas_newclick; | ||
286 | class_setsavefn(gcanvas_class,gcanvas_save); | ||
287 | } | ||
288 | |||
289 | |||
290 | static void *gcanvas_new(t_symbol* s,t_int ac,t_atom* at) | ||
291 | { | ||
292 | t_gcanvas *x = (t_gcanvas *)pd_new(gcanvas_class); | ||
293 | |||
294 | x->x_glist = (t_glist*) canvas_getcurrent(); | ||
295 | |||
296 | |||
297 | /* Fetch the width */ | ||
298 | |||
299 | x->x_width = DEFAULTSIZE; | ||
300 | if (ac-- > 0) { | ||
301 | if (at->a_type != A_FLOAT) | ||
302 | error("gcanvas: wrong argument type"); | ||
303 | else | ||
304 | x->x_width = atom_getfloat(at++); | ||
305 | |||
306 | if (x->x_width < 0 || x->x_width > 2000) { | ||
307 | error("gcanvas: unallowed width %f",x->x_width); | ||
308 | x->x_width = DEFAULTSIZE; | ||
309 | } | ||
310 | } | ||
311 | |||
312 | /* Fetch the height */ | ||
313 | |||
314 | x->x_height = DEFAULTSIZE; | ||
315 | if (ac-- > 0) { | ||
316 | if (at->a_type != A_FLOAT) | ||
317 | error("gcanvas: wrong argument type"); | ||
318 | else | ||
319 | x->x_height = atom_getfloat(at++); | ||
320 | |||
321 | if (x->x_height < 0 || x->x_height > 2000) { | ||
322 | error("gcanvas: unallowed height %f",x->x_height); | ||
323 | x->x_width = DEFAULTSIZE; | ||
324 | } | ||
325 | } | ||
326 | |||
327 | /* Fetch the xgrid */ | ||
328 | |||
329 | x->x_xgrid = 0; | ||
330 | if (ac-- > 0) { | ||
331 | if (at->a_type != A_FLOAT) | ||
332 | error("gcanvas: wrong argument type"); | ||
333 | else | ||
334 | x->x_xgrid = atom_getfloat(at++); | ||
335 | |||
336 | if (x->x_xgrid < 0 || x->x_xgrid > x->x_width/2) { | ||
337 | error("gcanvas: unallowed xgrid %f",x->x_xgrid); | ||
338 | x->x_xgrid = 0; | ||
339 | } | ||
340 | } | ||
341 | |||
342 | /* Fetch the ygrid */ | ||
343 | |||
344 | x->x_ygrid = 0; | ||
345 | if (ac-- > 0) { | ||
346 | if (at->a_type != A_FLOAT) | ||
347 | error("gcanvas: wrong argument type"); | ||
348 | else | ||
349 | x->x_ygrid = atom_getfloat(at++); | ||
350 | |||
351 | if (x->x_ygrid < 0 || x->x_ygrid > x->x_height/2) { | ||
352 | error("gcanvas: unallowed xgrid %f",x->x_ygrid); | ||
353 | x->x_ygrid = 0; | ||
354 | } | ||
355 | } | ||
356 | |||
357 | outlet_new(&x->x_obj, &s_float); | ||
358 | x->out2 = outlet_new(&x->x_obj, &s_float); | ||
359 | x->out3 = outlet_new(&x->x_obj, &s_float); | ||
360 | return (x); | ||
361 | } | ||
362 | |||
363 | |||
364 | |||
365 | void gcanvas_setup(void) | ||
366 | { | ||
367 | gcanvas_class = class_new(gensym("gcanvas"), (t_newmethod)gcanvas_new, 0, | ||
368 | sizeof(t_gcanvas),0, A_GIMME,0); | ||
369 | |||
370 | class_addmethod(gcanvas_class, (t_method)gcanvas_click, gensym("click"), | ||
371 | A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); | ||
372 | class_addmethod(gcanvas_class, (t_method)gcanvas_size, gensym("size"), | ||
373 | A_FLOAT, A_FLOAT, 0); | ||
374 | |||
375 | gcanvas_setwidget(); | ||
376 | class_setwidget(gcanvas_class,&gcanvas_widgetbehavior); | ||
377 | } | ||
378 | |||
379 | |||
380 | /* (C) Guenter Geiger <geiger@xdv.org> */ | ||
381 | |||
382 | |||
383 | #include "m_pd.h" | ||
384 | #include "g_canvas.h" | ||
385 | |||
386 | /* ------------------------ gcanvas ----------------------------- */ | ||
387 | |||
388 | |||
389 | #define BACKGROUNDCOLOR "grey" | ||
390 | |||
391 | #define DEFAULTSIZE 80 | ||
392 | |||
393 | static t_class *gcanvas_class; | ||
394 | |||
395 | typedef struct _gcanvas | ||
396 | { | ||
397 | t_object x_obj; | ||
398 | t_glist * x_glist; | ||
399 | t_outlet* out2; | ||
400 | t_outlet* out3; | ||
401 | int x_width; | ||
402 | int x_height; | ||
403 | int x; | ||
404 | int y; | ||
405 | int x_xgrid; | ||
406 | int x_ygrid; | ||
407 | } t_gcanvas; | ||
408 | |||
409 | |||
410 | static void rectangle(void* cv,void* o,char c,int x, int y,int w,int h,char* color) { | ||
411 | sys_vgui(".x%x.c create rectangle \ | ||
412 | %d %d %d %d -tags %x%c -fill %s\n",cv,x,y,x+w,y+h,o,c,color); | ||
413 | } | ||
414 | |||
415 | static void move_object(void* cv,void* o,char c,int x, int y,int w,int h) { | ||
416 | sys_vgui(".x%x.c coords %x%c %d %d %d %d\n", | ||
417 | cv,o,c,x,y,x+w,y+h); | ||
418 | |||
419 | } | ||
420 | |||
421 | static void color_object(void* cv,void* o,char c,char* color) { | ||
422 | sys_vgui(".x%x.c itemconfigure %x%c -fill %s\n", cv, | ||
423 | o, c,color); | ||
424 | } | ||
425 | |||
426 | static void delete_object(void* cv,void* o,char c) { | ||
427 | sys_vgui(".x%x.c delete %x%c\n", | ||
428 | cv, o,c); | ||
429 | } | ||
430 | |||
431 | static void line(void* cv,void* o,char c,int x,int y,int w,int h,char* color) { | ||
432 | sys_vgui(".x%x.c create line \ | ||
433 | %d %d %d %d -tags %x%c -fill %s\n",cv,x,y,x+w,y+h,o,c,color); | ||
434 | } | ||
435 | |||
436 | |||
437 | /* widget helper functions */ | ||
438 | |||
439 | void gcanvas_drawme(t_gcanvas *x, t_glist *glist, int firsttime) | ||
440 | { | ||
441 | int i; | ||
442 | if (firsttime) { | ||
443 | rectangle(glist_getcanvas(glist),x,'a', | ||
444 | x->x_obj.te_xpix, x->x_obj.te_ypix, | ||
445 | x->x_width, x->x_height,BACKGROUNDCOLOR); | ||
446 | for (i=1;i<x->x_xgrid;i++) | ||
447 | line(glist_getcanvas(glist),x,'b'+ i, | ||
448 | x->x_obj.te_xpix + x->x_width*i/x->x_xgrid, | ||
449 | x->x_obj.te_ypix, | ||
450 | 0, x->x_height,"red"); | ||
451 | for (i=1;i<x->x_ygrid;i++) | ||
452 | line(glist_getcanvas(glist),x,'B'+ i, | ||
453 | x->x_obj.te_xpix, | ||
454 | x->x_obj.te_ypix + x->x_height*i/x->x_ygrid, | ||
455 | x->x_width, 0,"blue"); | ||
456 | } | ||
457 | else { | ||
458 | move_object( | ||
459 | glist_getcanvas(glist),x,'a', | ||
460 | x->x_obj.te_xpix, x->x_obj.te_ypix, | ||
461 | x->x_width, x->x_height); | ||
462 | for (i=1;i<x->x_xgrid;i++) | ||
463 | move_object(glist_getcanvas(glist),x,'b'+ i, | ||
464 | x->x_obj.te_xpix + x->x_width*i/x->x_xgrid, | ||
465 | x->x_obj.te_ypix, | ||
466 | 0, x->x_height); | ||
467 | for (i=1;i<x->x_ygrid;i++) | ||
468 | move_object(glist_getcanvas(glist),x,'B'+ i, | ||
469 | x->x_obj.te_xpix, | ||
470 | x->x_obj.te_ypix + x->x_height*i/x->x_ygrid, | ||
471 | x->x_width, 0); | ||
472 | } | ||
473 | |||
474 | { | ||
475 | /* outlets */ | ||
476 | int n = 3; | ||
477 | int nplus, i; | ||
478 | nplus = (n == 1 ? 1 : n-1); | ||
479 | for (i = 0; i < n; i++) | ||
480 | { | ||
481 | int onset = x->x_obj.te_xpix + (x->x_width - IOWIDTH) * i / nplus; | ||
482 | if (firsttime) | ||
483 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xo%d\n", | ||
484 | glist_getcanvas(glist), | ||
485 | onset, x->x_obj.te_ypix + x->x_height - 1, | ||
486 | onset + IOWIDTH, x->x_obj.te_ypix + x->x_height, | ||
487 | x, i); | ||
488 | else | ||
489 | sys_vgui(".x%x.c coords %xo%d %d %d %d %d\n", | ||
490 | glist_getcanvas(glist), x, i, | ||
491 | onset, x->x_obj.te_ypix + x->x_height - 1, | ||
492 | onset + IOWIDTH, x->x_obj.te_ypix + x->x_height); | ||
493 | } | ||
494 | /* inlets */ | ||
495 | n = 0; | ||
496 | nplus = (n == 1 ? 1 : n-1); | ||
497 | for (i = 0; i < n; i++) | ||
498 | { | ||
499 | int onset = x->x_obj.te_xpix + (x->x_width - IOWIDTH) * i / nplus; | ||
500 | if (firsttime) | ||
501 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xi%d\n", | ||
502 | glist_getcanvas(glist), | ||
503 | onset, x->x_obj.te_ypix, | ||
504 | onset + IOWIDTH, x->x_obj.te_ypix + 1, | ||
505 | x, i); | ||
506 | else | ||
507 | sys_vgui(".x%x.c coords %xi%d %d %d %d %d\n", | ||
508 | glist_getcanvas(glist), x, i, | ||
509 | onset, x->x_obj.te_ypix, | ||
510 | onset + IOWIDTH, x->x_obj.te_ypix + 1); | ||
511 | |||
512 | } | ||
513 | } | ||
514 | |||
515 | } | ||
516 | |||
517 | |||
518 | |||
519 | |||
520 | void gcanvas_erase(t_gcanvas* x,t_glist* glist) | ||
521 | { | ||
522 | int n,i; | ||
523 | delete_object(glist_getcanvas(glist),x,'a'); | ||
524 | for (i=1;i<x->x_xgrid;i++) | ||
525 | delete_object(glist_getcanvas(glist),x,'b'+ i); | ||
526 | for (i=1;i<x->x_ygrid;i++) | ||
527 | delete_object(glist_getcanvas(glist),x,'B'+ i); | ||
528 | |||
529 | n = 2; | ||
530 | while (n--) { | ||
531 | sys_vgui(".x%x.c delete %xo%d\n",glist_getcanvas(glist),x,n); | ||
532 | } | ||
533 | } | ||
534 | |||
535 | |||
536 | |||
537 | /* ------------------------ gcanvas widgetbehaviour----------------------------- */ | ||
538 | |||
539 | |||
540 | static void gcanvas_getrect(t_gobj *z, t_glist *owner, | ||
541 | int *xp1, int *yp1, int *xp2, int *yp2) | ||
542 | { | ||
543 | int width, height; | ||
544 | t_gcanvas* s = (t_gcanvas*)z; | ||
545 | |||
546 | |||
547 | width = s->x_width; | ||
548 | height = s->x_height; | ||
549 | *xp1 = s->x_obj.te_xpix; | ||
550 | *yp1 = s->x_obj.te_ypix; | ||
551 | *xp2 = s->x_obj.te_xpix + width; | ||
552 | *yp2 = s->x_obj.te_ypix + height; | ||
553 | } | ||
554 | |||
555 | static void gcanvas_displace(t_gobj *z, t_glist *glist, | ||
556 | int dx, int dy) | ||
557 | { | ||
558 | t_gcanvas *x = (t_gcanvas *)z; | ||
559 | x->x_obj.te_xpix += dx; | ||
560 | x->x_obj.te_ypix += dy; | ||
561 | gcanvas_drawme(x, glist, 0); | ||
562 | canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x); | ||
563 | } | ||
564 | |||
565 | static void gcanvas_select(t_gobj *z, t_glist *glist, int state) | ||
566 | { | ||
567 | t_gcanvas *x = (t_gcanvas *)z; | ||
568 | color_object(glist,x,'a',state ? "blue" : BACKGROUNDCOLOR); | ||
569 | } | ||
570 | |||
571 | |||
572 | static void gcanvas_activate(t_gobj *z, t_glist *glist, int state) | ||
573 | { | ||
574 | /* t_text *x = (t_text *)z; | ||
575 | t_rtext *y = glist_findrtext(glist, x); | ||
576 | if (z->g_pd != gatom_class) rtext_activate(y, state);*/ | ||
577 | } | ||
578 | |||
579 | static void gcanvas_delete(t_gobj *z, t_glist *glist) | ||
580 | { | ||
581 | t_text *x = (t_text *)z; | ||
582 | canvas_deletelinesfor(glist_getcanvas(glist), x); | ||
583 | } | ||
584 | |||
585 | |||
586 | static void gcanvas_vis(t_gobj *z, t_glist *glist, int vis) | ||
587 | { | ||
588 | t_gcanvas* s = (t_gcanvas*)z; | ||
589 | if (vis) | ||
590 | gcanvas_drawme(s, glist, 1); | ||
591 | else | ||
592 | gcanvas_erase(s,glist); | ||
593 | } | ||
594 | |||
595 | /* can we use the normal text save function ?? */ | ||
596 | |||
597 | static void gcanvas_save(t_gobj *z, t_binbuf *b) | ||
598 | { | ||
599 | t_gcanvas *x = (t_gcanvas *)z; | ||
600 | binbuf_addv(b, "ssiisiiii", gensym("#X"),gensym("obj"), | ||
601 | (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix, | ||
602 | gensym("gcanvas"),x->x_width,x->x_height, | ||
603 | x->x_xgrid, | ||
604 | x->x_ygrid); | ||
605 | binbuf_addv(b, ";"); | ||
606 | } | ||
607 | |||
608 | |||
609 | t_widgetbehavior gcanvas_widgetbehavior; | ||
610 | |||
611 | static void gcanvas_motion(t_gcanvas *x, t_floatarg dx, t_floatarg dy) | ||
612 | { | ||
613 | x->x += dx; | ||
614 | x->y += dy; | ||
615 | outlet_float(x->out2,x->y); | ||
616 | outlet_float(x->x_obj.ob_outlet,x->x); | ||
617 | } | ||
618 | |||
619 | void gcanvas_key(t_gcanvas *x, t_floatarg f) | ||
620 | { | ||
621 | post("key"); | ||
622 | } | ||
623 | |||
624 | |||
625 | static void gcanvas_click(t_gcanvas *x, | ||
626 | t_floatarg xpos, t_floatarg ypos, t_floatarg shift, t_floatarg ctrl, | ||
627 | t_floatarg doit,int up) | ||
628 | { | ||
629 | glist_grab(x->x_glist, &x->x_obj.te_g, (t_glistmotionfn) gcanvas_motion, | ||
630 | (t_glistkeyfn) NULL, xpos, ypos); | ||
631 | |||
632 | x->x = xpos - x->x_obj.te_xpix; | ||
633 | x->y = ypos - x->x_obj.te_ypix; | ||
634 | outlet_float(x->out2,x->y); | ||
635 | outlet_float(x->x_obj.ob_outlet,x->x); | ||
636 | outlet_float(x->out3,0); | ||
637 | } | ||
638 | |||
639 | static int gcanvas_newclick(t_gobj *z, struct _glist *glist, | ||
640 | int xpix, int ypix, int shift, int alt, int dbl, int doit) | ||
641 | { | ||
642 | if (doit) | ||
643 | gcanvas_click((t_gcanvas *)z, (t_floatarg)xpix, (t_floatarg)ypix, | ||
644 | (t_floatarg)shift, 0, (t_floatarg)alt,dbl); | ||
645 | |||
646 | if (dbl) outlet_float(((t_gcanvas*)z)->out3,1); | ||
647 | return (1); | ||
648 | } | ||
649 | |||
650 | void gcanvas_size(t_gcanvas* x,t_floatarg w,t_floatarg h) { | ||
651 | x->x_width = w; | ||
652 | x->x_height = h; | ||
653 | gcanvas_drawme(x, x->x_glist, 0); | ||
654 | } | ||
655 | |||
656 | static void gcanvas_setwidget(void) | ||
657 | { | ||
658 | gcanvas_widgetbehavior.w_getrectfn = gcanvas_getrect; | ||
659 | gcanvas_widgetbehavior.w_displacefn = gcanvas_displace; | ||
660 | gcanvas_widgetbehavior.w_selectfn = gcanvas_select; | ||
661 | gcanvas_widgetbehavior.w_activatefn = gcanvas_activate; | ||
662 | gcanvas_widgetbehavior.w_deletefn = gcanvas_delete; | ||
663 | gcanvas_widgetbehavior.w_visfn = gcanvas_vis; | ||
664 | gcanvas_widgetbehavior.w_clickfn = gcanvas_newclick; | ||
665 | class_setsavefn(gcanvas_class,gcanvas_save); | ||
666 | } | ||
667 | |||
668 | |||
669 | static void *gcanvas_new(t_symbol* s,t_int ac,t_atom* at) | ||
670 | { | ||
671 | t_gcanvas *x = (t_gcanvas *)pd_new(gcanvas_class); | ||
672 | |||
673 | x->x_glist = (t_glist*) canvas_getcurrent(); | ||
674 | |||
675 | |||
676 | /* Fetch the width */ | ||
677 | |||
678 | x->x_width = DEFAULTSIZE; | ||
679 | if (ac-- > 0) { | ||
680 | if (at->a_type != A_FLOAT) | ||
681 | error("gcanvas: wrong argument type"); | ||
682 | else | ||
683 | x->x_width = atom_getfloat(at++); | ||
684 | |||
685 | if (x->x_width < 0 || x->x_width > 2000) { | ||
686 | error("gcanvas: unallowed width %f",x->x_width); | ||
687 | x->x_width = DEFAULTSIZE; | ||
688 | } | ||
689 | } | ||
690 | |||
691 | /* Fetch the height */ | ||
692 | |||
693 | x->x_height = DEFAULTSIZE; | ||
694 | if (ac-- > 0) { | ||
695 | if (at->a_type != A_FLOAT) | ||
696 | error("gcanvas: wrong argument type"); | ||
697 | else | ||
698 | x->x_height = atom_getfloat(at++); | ||
699 | |||
700 | if (x->x_height < 0 || x->x_height > 2000) { | ||
701 | error("gcanvas: unallowed height %f",x->x_height); | ||
702 | x->x_width = DEFAULTSIZE; | ||
703 | } | ||
704 | } | ||
705 | |||
706 | /* Fetch the xgrid */ | ||
707 | |||
708 | x->x_xgrid = 0; | ||
709 | if (ac-- > 0) { | ||
710 | if (at->a_type != A_FLOAT) | ||
711 | error("gcanvas: wrong argument type"); | ||
712 | else | ||
713 | x->x_xgrid = atom_getfloat(at++); | ||
714 | |||
715 | if (x->x_xgrid < 0 || x->x_xgrid > x->x_width/2) { | ||
716 | error("gcanvas: unallowed xgrid %f",x->x_xgrid); | ||
717 | x->x_xgrid = 0; | ||
718 | } | ||
719 | } | ||
720 | |||
721 | /* Fetch the ygrid */ | ||
722 | |||
723 | x->x_ygrid = 0; | ||
724 | if (ac-- > 0) { | ||
725 | if (at->a_type != A_FLOAT) | ||
726 | error("gcanvas: wrong argument type"); | ||
727 | else | ||
728 | x->x_ygrid = atom_getfloat(at++); | ||
729 | |||
730 | if (x->x_ygrid < 0 || x->x_ygrid > x->x_height/2) { | ||
731 | error("gcanvas: unallowed xgrid %f",x->x_ygrid); | ||
732 | x->x_ygrid = 0; | ||
733 | } | ||
734 | } | ||
735 | |||
736 | outlet_new(&x->x_obj, &s_float); | ||
737 | x->out2 = outlet_new(&x->x_obj, &s_float); | ||
738 | x->out3 = outlet_new(&x->x_obj, &s_float); | ||
739 | return (x); | ||
740 | } | ||
741 | |||
742 | |||
743 | |||
744 | void gcanvas_setup(void) | ||
745 | { | ||
746 | gcanvas_class = class_new(gensym("gcanvas"), (t_newmethod)gcanvas_new, 0, | ||
747 | sizeof(t_gcanvas),0, A_GIMME,0); | ||
748 | |||
749 | class_addmethod(gcanvas_class, (t_method)gcanvas_click, gensym("click"), | ||
750 | A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); | ||
751 | class_addmethod(gcanvas_class, (t_method)gcanvas_size, gensym("size"), | ||
752 | A_FLOAT, A_FLOAT, 0); | ||
753 | |||
754 | gcanvas_setwidget(); | ||
755 | class_setwidget(gcanvas_class,&gcanvas_widgetbehavior); | ||
756 | } | ||
757 | |||
758 | |||