diff options
Diffstat (limited to 'apps/plugins/pdbox/PDa/extra/fatom.h')
-rw-r--r-- | apps/plugins/pdbox/PDa/extra/fatom.h | 970 |
1 files changed, 970 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/extra/fatom.h b/apps/plugins/pdbox/PDa/extra/fatom.h new file mode 100644 index 0000000000..abaf9b91c1 --- /dev/null +++ b/apps/plugins/pdbox/PDa/extra/fatom.h | |||
@@ -0,0 +1,970 @@ | |||
1 | /* ------------------------ fatom ----------------------------- */ | ||
2 | |||
3 | #define x_val a_pos.a_w.w_float | ||
4 | #define DEBUG(x) | ||
5 | |||
6 | #include <string.h> | ||
7 | #include <stdio.h> | ||
8 | |||
9 | typedef struct _fatom | ||
10 | { | ||
11 | t_object x_obj; | ||
12 | t_atom a_pos; /* the value of the fatom */ | ||
13 | |||
14 | t_symbol* x_send; | ||
15 | t_symbol* x_receive; | ||
16 | t_glist * x_glist; /* value of the current canvas, intialized in _new */ | ||
17 | int x_rect_width; /* width of the widget */ | ||
18 | int x_rect_height; /* height of the widget */ | ||
19 | t_symbol* x_sym; /* symbol for receiving callbacks from GUI */ | ||
20 | t_symbol* x_type; /* type of fatom (vslider, hslider, checkbutton) */ | ||
21 | |||
22 | t_symbol* x_text; /* associated widget text */ | ||
23 | int x_max; /* maximum value of a_pos (x_val) */ | ||
24 | int x_min; /* minimum value of a_pos (x_val) */ | ||
25 | int x_width; /* width of widget (e.g x_rect_height + 15 for hslider, x_rect_width + 15 for slider) */ | ||
26 | t_symbol* x_color; | ||
27 | t_symbol* x_bgcolor; | ||
28 | } t_fatom; | ||
29 | |||
30 | /* widget helper functions */ | ||
31 | |||
32 | |||
33 | |||
34 | |||
35 | static void draw_inlets(t_fatom *x, t_glist *glist, int firsttime, int nin, int nout) | ||
36 | { | ||
37 | int n = nin; | ||
38 | int nplus, i; | ||
39 | nplus = (n == 1 ? 1 : n-1); | ||
40 | DEBUG(post("draw inlet");) | ||
41 | for (i = 0; i < n; i++) | ||
42 | { | ||
43 | int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH) * i / nplus; | ||
44 | if (firsttime) | ||
45 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xo%d\n", | ||
46 | glist_getcanvas(glist), | ||
47 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 1, | ||
48 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height, | ||
49 | x, i); | ||
50 | else | ||
51 | sys_vgui(".x%x.c coords %xo%d %d %d %d %d\n", | ||
52 | glist_getcanvas(glist), x, i, | ||
53 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 1, | ||
54 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height); | ||
55 | } | ||
56 | n = nout; | ||
57 | nplus = (n == 1 ? 1 : n-1); | ||
58 | for (i = 0; i < n; i++) | ||
59 | { | ||
60 | int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH) * i / nplus; | ||
61 | if (firsttime) | ||
62 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xi%d\n", | ||
63 | glist_getcanvas(glist), | ||
64 | onset, text_ypix(&x->x_obj, glist), | ||
65 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + 1, | ||
66 | x, i); | ||
67 | else | ||
68 | sys_vgui(".x%x.c coords %xi%d %d %d %d %d\n", | ||
69 | glist_getcanvas(glist), x, i, | ||
70 | onset, text_ypix(&x->x_obj, glist), | ||
71 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + 1); | ||
72 | |||
73 | } | ||
74 | DEBUG(post("draw inlet end");) | ||
75 | } | ||
76 | |||
77 | |||
78 | static void draw_handle(t_fatom *x, t_glist *glist, int firsttime) { | ||
79 | int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH+2); | ||
80 | |||
81 | if (firsttime) | ||
82 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xhandle\n", | ||
83 | glist_getcanvas(glist), | ||
84 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 12, | ||
85 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height-4, | ||
86 | x); | ||
87 | else | ||
88 | sys_vgui(".x%x.c coords %xhandle %d %d %d %d\n", | ||
89 | glist_getcanvas(glist), x, | ||
90 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 12, | ||
91 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height-4); | ||
92 | } | ||
93 | |||
94 | static void create_widget(t_fatom *x, t_glist *glist) | ||
95 | { | ||
96 | t_canvas *canvas=glist_getcanvas(glist); | ||
97 | |||
98 | if (!strcmp(x->x_type->s_name,"vslider")) { | ||
99 | x->x_rect_width = x->x_width+15; | ||
100 | x->x_rect_height = x->x_max-x->x_min+26; | ||
101 | |||
102 | sys_vgui("scale .x%x.c.s%x \ | ||
103 | -sliderlength 10 \ | ||
104 | -showvalue 0 \ | ||
105 | -length %d \ | ||
106 | -resolution 0.01 \ | ||
107 | -repeatinterval 20 \ | ||
108 | -from %d -to %d \ | ||
109 | -width %d \ | ||
110 | -bg %s \ | ||
111 | -activebackground %s \ | ||
112 | -troughcolor %s \ | ||
113 | -command fatom_cb%x\n",canvas,x, | ||
114 | x->x_max-x->x_min+14, | ||
115 | x->x_max, | ||
116 | x->x_min, | ||
117 | x->x_width, | ||
118 | x->x_color->s_name, | ||
119 | x->x_color->s_name, | ||
120 | x->x_bgcolor->s_name, | ||
121 | x); | ||
122 | } else if (!strcmp(x->x_type->s_name,"hslider")) { | ||
123 | x->x_rect_width = x->x_max-x->x_min + 24; | ||
124 | x->x_rect_height = x->x_width + 15; | ||
125 | sys_vgui("scale .x%x.c.s%x \ | ||
126 | -sliderlength 10 \ | ||
127 | -showvalue 0 \ | ||
128 | -length %d \ | ||
129 | -resolution 0.01 \ | ||
130 | -orient horizontal \ | ||
131 | -repeatinterval 20 \ | ||
132 | -from %d -to %d \ | ||
133 | -width %d \ | ||
134 | -bg %s \ | ||
135 | -activebackground %s \ | ||
136 | -troughcolor %s \ | ||
137 | -command fatom_cb%x\n",canvas,x, | ||
138 | x->x_max-x->x_min+14, | ||
139 | x->x_min, | ||
140 | x->x_max, | ||
141 | x->x_width, | ||
142 | x->x_color->s_name, | ||
143 | x->x_color->s_name, | ||
144 | x->x_bgcolor->s_name, | ||
145 | x); | ||
146 | } else if (!strcmp(x->x_type->s_name,"checkbutton")) { | ||
147 | x->x_rect_width = 32; | ||
148 | x->x_rect_height = 28; | ||
149 | sys_vgui("checkbutton .x%x.c.s%x \ | ||
150 | -command { fatom_cb%x $fatom_val%x} -variable fatom_val%x -text \"%s\" \ | ||
151 | -bg %s \ | ||
152 | -activebackground %s \ | ||
153 | \n",canvas,x,x,x,x, | ||
154 | x->x_text->s_name, | ||
155 | x->x_color->s_name, | ||
156 | x->x_bgcolor->s_name); | ||
157 | } else if (!strcmp(x->x_type->s_name,"hradio")) { | ||
158 | int i; | ||
159 | x->x_rect_width = 8*20; | ||
160 | x->x_rect_height = 25; | ||
161 | for (i=0;i<8;i++) { | ||
162 | sys_vgui("radiobutton .x%x.c.s%x%d \ | ||
163 | -command { fatom_cb%x $fatom_val%x} -variable fatom_val%x -value %d\n",canvas,x,i,x,x,x,i); | ||
164 | } | ||
165 | /* TODO pack them */ | ||
166 | } else if (!strcmp(x->x_type->s_name,"vradio")) { | ||
167 | int i; | ||
168 | x->x_rect_width = 30; | ||
169 | x->x_rect_height = 20*8+5; | ||
170 | for (i=0;i<8;i++) { | ||
171 | sys_vgui("radiobutton .x%x.c.s%x%d \ | ||
172 | -command { fatom_cb%x $fatom_val%x} -variable fatom_val%x -value %d\n",canvas,x,i,x,x,x,i); | ||
173 | } | ||
174 | /* TODO pack them */ | ||
175 | } else { | ||
176 | x->x_rect_width = 32; | ||
177 | x->x_rect_height = 140; | ||
178 | sys_vgui("scale .x%x.c.s%x \ | ||
179 | -sliderlength 10 \ | ||
180 | -showvalue 0 \ | ||
181 | -length 131 \ | ||
182 | -from 127 -to 0 \ | ||
183 | -command fatom_cb%x\n",canvas,x,x); | ||
184 | } | ||
185 | |||
186 | /* set the start value */ | ||
187 | if (!strcmp(x->x_type->s_name,"checkbutton")) { | ||
188 | if (x->x_val) | ||
189 | sys_vgui(".x%x.c.s%x select\n",canvas,x,x->x_val); | ||
190 | else | ||
191 | sys_vgui(".x%x.c.s%x deselect\n",canvas,x,x->x_val); | ||
192 | } else | ||
193 | sys_vgui(".x%x.c.s%x set %f\n",canvas,x,x->x_val); | ||
194 | |||
195 | } | ||
196 | |||
197 | |||
198 | |||
199 | |||
200 | |||
201 | static void fatom_drawme(t_fatom *x, t_glist *glist, int firsttime) | ||
202 | { | ||
203 | t_canvas *canvas=glist_getcanvas(glist);// x->x_glist;//glist_getcanvas(glist); | ||
204 | DEBUG(post("drawme %d",firsttime);) | ||
205 | if (firsttime) { | ||
206 | DEBUG(post("glist %x canvas %x",x->x_glist,canvas)); | ||
207 | create_widget(x,glist); | ||
208 | x->x_glist = canvas; | ||
209 | sys_vgui(".x%x.c create window %d %d -anchor nw -window .x%x.c.s%x -tags %xS\n", | ||
210 | canvas,text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)+2,x->x_glist,x,x); | ||
211 | |||
212 | } | ||
213 | else { | ||
214 | sys_vgui(".x%x.c coords %xS \ | ||
215 | %d %d\n", | ||
216 | canvas, x, | ||
217 | text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)+2); | ||
218 | } | ||
219 | draw_inlets(x, glist, firsttime, 1,1); | ||
220 | // draw_handle(x, glist, firsttime); | ||
221 | |||
222 | } | ||
223 | |||
224 | |||
225 | static void fatom_erase(t_fatom* x,t_glist* glist) | ||
226 | { | ||
227 | int n; | ||
228 | |||
229 | DEBUG(post("erase");) | ||
230 | sys_vgui("destroy .x%x.c.s%x\n",glist_getcanvas(glist),x); | ||
231 | |||
232 | sys_vgui(".x%x.c delete %xS\n",glist_getcanvas(glist), x); | ||
233 | |||
234 | /* inlets and outlets */ | ||
235 | |||
236 | sys_vgui(".x%x.c delete %xi%d\n",glist_getcanvas(glist),x,0); | ||
237 | sys_vgui(".x%x.c delete %xo%d\n",glist_getcanvas(glist),x,0); | ||
238 | sys_vgui(".x%x.c delete %xhandle\n",glist_getcanvas(glist),x,0); | ||
239 | } | ||
240 | |||
241 | |||
242 | |||
243 | /* ------------------------ fatom widgetbehaviour----------------------------- */ | ||
244 | |||
245 | |||
246 | static void fatom_getrect(t_gobj *z, t_glist *owner, | ||
247 | int *xp1, int *yp1, int *xp2, int *yp2) | ||
248 | { | ||
249 | int width, height; | ||
250 | t_fatom* s = (t_fatom*)z; | ||
251 | |||
252 | width = s->x_rect_width; | ||
253 | height = s->x_rect_height; | ||
254 | *xp1 = text_xpix(&s->x_obj, owner); | ||
255 | *yp1 = text_ypix(&s->x_obj, owner); | ||
256 | *xp2 = text_xpix(&s->x_obj, owner) + width; | ||
257 | *yp2 = text_ypix(&s->x_obj, owner) + height; | ||
258 | } | ||
259 | |||
260 | static void fatom_displace(t_gobj *z, t_glist *glist, | ||
261 | int dx, int dy) | ||
262 | { | ||
263 | t_fatom *x = (t_fatom *)z; | ||
264 | DEBUG(post("displace");) | ||
265 | x->x_obj.te_xpix += dx; | ||
266 | x->x_obj.te_ypix += dy; | ||
267 | if (glist_isvisible(glist)) | ||
268 | { | ||
269 | sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n", | ||
270 | glist_getcanvas(glist), x, | ||
271 | text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), | ||
272 | text_xpix(&x->x_obj, glist) + x->x_rect_width, text_ypix(&x->x_obj, glist) + x->x_rect_height); | ||
273 | |||
274 | fatom_drawme(x, glist, 0); | ||
275 | canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x); | ||
276 | } | ||
277 | DEBUG(post("displace end");) | ||
278 | } | ||
279 | |||
280 | static void fatom_select(t_gobj *z, t_glist *glist, int state) | ||
281 | { | ||
282 | t_fatom *x = (t_fatom *)z; | ||
283 | if (state) { | ||
284 | sys_vgui(".x%x.c create rectangle \ | ||
285 | %d %d %d %d -tags %xSEL -outline blue\n", | ||
286 | glist_getcanvas(glist), | ||
287 | text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), | ||
288 | text_xpix(&x->x_obj, glist) + x->x_rect_width, text_ypix(&x->x_obj, glist) + x->x_rect_height, | ||
289 | x); | ||
290 | } | ||
291 | else { | ||
292 | sys_vgui(".x%x.c delete %xSEL\n", | ||
293 | glist_getcanvas(glist), x); | ||
294 | } | ||
295 | |||
296 | |||
297 | |||
298 | } | ||
299 | |||
300 | |||
301 | static void fatom_activate(t_gobj *z, t_glist *glist, int state) | ||
302 | { | ||
303 | /* t_text *x = (t_text *)z; | ||
304 | t_rtext *y = glist_findrtext(glist, x); | ||
305 | if (z->g_pd != gatom_class) rtext_activate(y, state);*/ | ||
306 | } | ||
307 | |||
308 | static void fatom_delete(t_gobj *z, t_glist *glist) | ||
309 | { | ||
310 | t_text *x = (t_text *)z; | ||
311 | canvas_deletelinesfor(glist_getcanvas(glist), x); | ||
312 | } | ||
313 | |||
314 | |||
315 | static void fatom_vis(t_gobj *z, t_glist *glist, int vis) | ||
316 | { | ||
317 | t_fatom* s = (t_fatom*)z; | ||
318 | t_rtext *y; | ||
319 | DEBUG(post("vis: %d",vis);) | ||
320 | if (vis) { | ||
321 | #ifdef PD_MINOR_VERSION | ||
322 | y = (t_rtext *) rtext_new(glist, (t_text *)z); | ||
323 | #else | ||
324 | y = (t_rtext *) rtext_new(glist, (t_text *)z,0,0); | ||
325 | #endif | ||
326 | fatom_drawme(s, glist, 1); | ||
327 | } | ||
328 | else { | ||
329 | y = glist_findrtext(glist, (t_text *)z); | ||
330 | fatom_erase(s,glist); | ||
331 | rtext_free(y); | ||
332 | } | ||
333 | } | ||
334 | |||
335 | static void fatom_save(t_gobj *z, t_binbuf *b); | ||
336 | |||
337 | t_widgetbehavior fatom_widgetbehavior; | ||
338 | |||
339 | |||
340 | |||
341 | |||
342 | static void fatom_size(t_fatom* x,t_floatarg w,t_floatarg h) { | ||
343 | x->x_rect_width = w; | ||
344 | x->x_rect_height = h; | ||
345 | } | ||
346 | |||
347 | static void fatom_color(t_fatom* x,t_symbol* col) | ||
348 | { | ||
349 | |||
350 | } | ||
351 | |||
352 | |||
353 | static void fatom_f(t_fatom* x,t_floatarg f) | ||
354 | { | ||
355 | x->x_val = f; | ||
356 | if (x->x_send == &s_) | ||
357 | outlet_float(x->x_obj.ob_outlet,f); | ||
358 | else | ||
359 | if (x->x_send->s_thing) pd_float(x->x_send->s_thing,f); | ||
360 | } | ||
361 | |||
362 | |||
363 | static void fatom_float(t_fatom* x,t_floatarg f) | ||
364 | { | ||
365 | if (glist_isvisible(x->x_glist)) { | ||
366 | if (!strcmp(x->x_type->s_name,"checkbutton")) { | ||
367 | if (x->x_val) | ||
368 | sys_vgui(".x%x.c.s%x select\n",x->x_glist,x,f); | ||
369 | else | ||
370 | sys_vgui(".x%x.c.s%x deselect\n",x->x_glist,x,f); | ||
371 | } else | ||
372 | sys_vgui(".x%x.c.s%x set %f\n",x->x_glist,x,f); | ||
373 | } | ||
374 | fatom_f(x,f); | ||
375 | } | ||
376 | |||
377 | |||
378 | static void fatom_bang(t_fatom* x,t_floatarg f) | ||
379 | { | ||
380 | outlet_float(x->x_obj.ob_outlet,x->x_val); | ||
381 | } | ||
382 | |||
383 | |||
384 | static void fatom_properties(t_gobj *z, t_glist *owner) | ||
385 | { | ||
386 | post("N/I"); | ||
387 | } | ||
388 | |||
389 | |||
390 | static void fatom_save(t_gobj *z, t_binbuf *b) | ||
391 | { | ||
392 | |||
393 | t_fatom *x = (t_fatom *)z; | ||
394 | |||
395 | binbuf_addv(b, "ssiiss", gensym("#X"),gensym("obj"), | ||
396 | x->x_obj.te_xpix, x->x_obj.te_ypix , | ||
397 | gensym("fatom"),x->x_type); | ||
398 | binbuf_addv(b, ";"); | ||
399 | } | ||
400 | |||
401 | |||
402 | static void *fatom_new(t_fatom* x,int argc,t_atom* argv) | ||
403 | { | ||
404 | char buf[256]; | ||
405 | int n = 0; | ||
406 | x->x_glist = canvas_getcurrent(); | ||
407 | |||
408 | x->x_text = gensym(""); | ||
409 | x->x_max = 127; | ||
410 | x->x_min = 0; | ||
411 | x->x_width = 15; | ||
412 | x->x_color = gensym("grey"); | ||
413 | x->x_bgcolor = gensym("grey"); | ||
414 | x->x_send = &s_; | ||
415 | |||
416 | while (argc) { | ||
417 | if (argv->a_type == A_FLOAT) { | ||
418 | if (n==0) x->x_max = atom_getfloat(argv); | ||
419 | if (n==1) x->x_min = atom_getfloat(argv); | ||
420 | if (n==2) x->x_width = atom_getfloat(argv); | ||
421 | } | ||
422 | |||
423 | if (argv->a_type == A_SYMBOL) { | ||
424 | post("%d: symbol value %s",n,atom_getsymbol(argv)->s_name); | ||
425 | if (n==3) x->x_send = atom_getsymbol(argv); | ||
426 | if (n==4) x->x_color = atom_getsymbol(argv); | ||
427 | if (n==5) x->x_bgcolor = atom_getsymbol(argv); | ||
428 | } | ||
429 | argv++; | ||
430 | argc--; | ||
431 | n++; | ||
432 | } | ||
433 | |||
434 | /* bind to a symbol for slider callback (later make this based on the | ||
435 | filepath ??) */ | ||
436 | |||
437 | sprintf(buf,"fatom%x",(t_int)x); | ||
438 | x->x_sym = gensym(buf); | ||
439 | pd_bind(&x->x_obj.ob_pd, x->x_sym); | ||
440 | |||
441 | /* pipe startup code to tk */ | ||
442 | |||
443 | sys_vgui("proc fatom_cb%x {v} {\n pd [concat fatom%x f $v \\;]\n }\n",x,x); | ||
444 | |||
445 | outlet_new(&x->x_obj, &s_float); | ||
446 | return (x); | ||
447 | } | ||
448 | |||
449 | static void fatom_setup_common(t_class* class) | ||
450 | { | ||
451 | |||
452 | fatom_widgetbehavior.w_getrectfn = fatom_getrect; | ||
453 | fatom_widgetbehavior.w_displacefn = fatom_displace; | ||
454 | fatom_widgetbehavior.w_selectfn = fatom_select; | ||
455 | fatom_widgetbehavior.w_activatefn = fatom_activate; | ||
456 | fatom_widgetbehavior.w_deletefn = fatom_delete; | ||
457 | fatom_widgetbehavior.w_visfn = fatom_vis; | ||
458 | #if PD_MINOR_VERSION < 37 | ||
459 | fatom_widgetbehavior.w_savefn = fatom_save; | ||
460 | fatom_widgetbehavior.w_propertiesfn = NULL; | ||
461 | #endif | ||
462 | fatom_widgetbehavior.w_clickfn = NULL; | ||
463 | |||
464 | class_addfloat(class, (t_method)fatom_float); | ||
465 | class_addbang(class, (t_method)fatom_bang); | ||
466 | class_addmethod(class, (t_method)fatom_f, gensym("f"), | ||
467 | A_FLOAT, 0); | ||
468 | |||
469 | /* | ||
470 | class_addmethod(class, (t_method)fatom_size, gensym("size"), | ||
471 | A_FLOAT, A_FLOAT, 0); | ||
472 | |||
473 | class_addmethod(class, (t_method)fatom_color, gensym("color"), | ||
474 | A_SYMBOL, 0); | ||
475 | */ | ||
476 | /* | ||
477 | class_addmethod(class, (t_method)fatom_open, gensym("open"), | ||
478 | A_SYMBOL, 0); | ||
479 | */ | ||
480 | |||
481 | class_setwidget(class,&fatom_widgetbehavior); | ||
482 | #if PD_MINOR_VERSION >= 37 | ||
483 | class_setsavefn(class,&fatom_save); | ||
484 | #endif | ||
485 | } | ||
486 | /* ------------------------ fatom ----------------------------- */ | ||
487 | |||
488 | #define x_val a_pos.a_w.w_float | ||
489 | #define DEBUG(x) | ||
490 | |||
491 | #include <string.h> | ||
492 | #include <stdio.h> | ||
493 | |||
494 | typedef struct _fatom | ||
495 | { | ||
496 | t_object x_obj; | ||
497 | t_atom a_pos; /* the value of the fatom */ | ||
498 | |||
499 | t_symbol* x_send; | ||
500 | t_symbol* x_receive; | ||
501 | t_glist * x_glist; /* value of the current canvas, intialized in _new */ | ||
502 | int x_rect_width; /* width of the widget */ | ||
503 | int x_rect_height; /* height of the widget */ | ||
504 | t_symbol* x_sym; /* symbol for receiving callbacks from GUI */ | ||
505 | t_symbol* x_type; /* type of fatom (vslider, hslider, checkbutton) */ | ||
506 | |||
507 | t_symbol* x_text; /* associated widget text */ | ||
508 | int x_max; /* maximum value of a_pos (x_val) */ | ||
509 | int x_min; /* minimum value of a_pos (x_val) */ | ||
510 | int x_width; /* width of widget (e.g x_rect_height + 15 for hslider, x_rect_width + 15 for slider) */ | ||
511 | t_symbol* x_color; | ||
512 | t_symbol* x_bgcolor; | ||
513 | } t_fatom; | ||
514 | |||
515 | /* widget helper functions */ | ||
516 | |||
517 | |||
518 | |||
519 | |||
520 | static void draw_inlets(t_fatom *x, t_glist *glist, int firsttime, int nin, int nout) | ||
521 | { | ||
522 | int n = nin; | ||
523 | int nplus, i; | ||
524 | nplus = (n == 1 ? 1 : n-1); | ||
525 | DEBUG(post("draw inlet");) | ||
526 | for (i = 0; i < n; i++) | ||
527 | { | ||
528 | int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH) * i / nplus; | ||
529 | if (firsttime) | ||
530 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xo%d\n", | ||
531 | glist_getcanvas(glist), | ||
532 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 1, | ||
533 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height, | ||
534 | x, i); | ||
535 | else | ||
536 | sys_vgui(".x%x.c coords %xo%d %d %d %d %d\n", | ||
537 | glist_getcanvas(glist), x, i, | ||
538 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 1, | ||
539 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height); | ||
540 | } | ||
541 | n = nout; | ||
542 | nplus = (n == 1 ? 1 : n-1); | ||
543 | for (i = 0; i < n; i++) | ||
544 | { | ||
545 | int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH) * i / nplus; | ||
546 | if (firsttime) | ||
547 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xi%d\n", | ||
548 | glist_getcanvas(glist), | ||
549 | onset, text_ypix(&x->x_obj, glist), | ||
550 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + 1, | ||
551 | x, i); | ||
552 | else | ||
553 | sys_vgui(".x%x.c coords %xi%d %d %d %d %d\n", | ||
554 | glist_getcanvas(glist), x, i, | ||
555 | onset, text_ypix(&x->x_obj, glist), | ||
556 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + 1); | ||
557 | |||
558 | } | ||
559 | DEBUG(post("draw inlet end");) | ||
560 | } | ||
561 | |||
562 | |||
563 | static void draw_handle(t_fatom *x, t_glist *glist, int firsttime) { | ||
564 | int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH+2); | ||
565 | |||
566 | if (firsttime) | ||
567 | sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xhandle\n", | ||
568 | glist_getcanvas(glist), | ||
569 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 12, | ||
570 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height-4, | ||
571 | x); | ||
572 | else | ||
573 | sys_vgui(".x%x.c coords %xhandle %d %d %d %d\n", | ||
574 | glist_getcanvas(glist), x, | ||
575 | onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 12, | ||
576 | onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height-4); | ||
577 | } | ||
578 | |||
579 | static void create_widget(t_fatom *x, t_glist *glist) | ||
580 | { | ||
581 | t_canvas *canvas=glist_getcanvas(glist); | ||
582 | |||
583 | if (!strcmp(x->x_type->s_name,"vslider")) { | ||
584 | x->x_rect_width = x->x_width+15; | ||
585 | x->x_rect_height = x->x_max-x->x_min+26; | ||
586 | |||
587 | sys_vgui("scale .x%x.c.s%x \ | ||
588 | -sliderlength 10 \ | ||
589 | -showvalue 0 \ | ||
590 | -length %d \ | ||
591 | -resolution 0.01 \ | ||
592 | -repeatinterval 20 \ | ||
593 | -from %d -to %d \ | ||
594 | -width %d \ | ||
595 | -bg %s \ | ||
596 | -activebackground %s \ | ||
597 | -troughcolor %s \ | ||
598 | -command fatom_cb%x\n",canvas,x, | ||
599 | x->x_max-x->x_min+14, | ||
600 | x->x_max, | ||
601 | x->x_min, | ||
602 | x->x_width, | ||
603 | x->x_color->s_name, | ||
604 | x->x_color->s_name, | ||
605 | x->x_bgcolor->s_name, | ||
606 | x); | ||
607 | } else if (!strcmp(x->x_type->s_name,"hslider")) { | ||
608 | x->x_rect_width = x->x_max-x->x_min + 24; | ||
609 | x->x_rect_height = x->x_width + 15; | ||
610 | sys_vgui("scale .x%x.c.s%x \ | ||
611 | -sliderlength 10 \ | ||
612 | -showvalue 0 \ | ||
613 | -length %d \ | ||
614 | -resolution 0.01 \ | ||
615 | -orient horizontal \ | ||
616 | -repeatinterval 20 \ | ||
617 | -from %d -to %d \ | ||
618 | -width %d \ | ||
619 | -bg %s \ | ||
620 | -activebackground %s \ | ||
621 | -troughcolor %s \ | ||
622 | -command fatom_cb%x\n",canvas,x, | ||
623 | x->x_max-x->x_min+14, | ||
624 | x->x_min, | ||
625 | x->x_max, | ||
626 | x->x_width, | ||
627 | x->x_color->s_name, | ||
628 | x->x_color->s_name, | ||
629 | x->x_bgcolor->s_name, | ||
630 | x); | ||
631 | } else if (!strcmp(x->x_type->s_name,"checkbutton")) { | ||
632 | x->x_rect_width = 32; | ||
633 | x->x_rect_height = 28; | ||
634 | sys_vgui("checkbutton .x%x.c.s%x \ | ||
635 | -command { fatom_cb%x $fatom_val%x} -variable fatom_val%x -text \"%s\" \ | ||
636 | -bg %s \ | ||
637 | -activebackground %s \ | ||
638 | \n",canvas,x,x,x,x, | ||
639 | x->x_text->s_name, | ||
640 | x->x_color->s_name, | ||
641 | x->x_bgcolor->s_name); | ||
642 | } else if (!strcmp(x->x_type->s_name,"hradio")) { | ||
643 | int i; | ||
644 | x->x_rect_width = 8*20; | ||
645 | x->x_rect_height = 25; | ||
646 | for (i=0;i<8;i++) { | ||
647 | sys_vgui("radiobutton .x%x.c.s%x%d \ | ||
648 | -command { fatom_cb%x $fatom_val%x} -variable fatom_val%x -value %d\n",canvas,x,i,x,x,x,i); | ||
649 | } | ||
650 | /* TODO pack them */ | ||
651 | } else if (!strcmp(x->x_type->s_name,"vradio")) { | ||
652 | int i; | ||
653 | x->x_rect_width = 30; | ||
654 | x->x_rect_height = 20*8+5; | ||
655 | for (i=0;i<8;i++) { | ||
656 | sys_vgui("radiobutton .x%x.c.s%x%d \ | ||
657 | -command { fatom_cb%x $fatom_val%x} -variable fatom_val%x -value %d\n",canvas,x,i,x,x,x,i); | ||
658 | } | ||
659 | /* TODO pack them */ | ||
660 | } else { | ||
661 | x->x_rect_width = 32; | ||
662 | x->x_rect_height = 140; | ||
663 | sys_vgui("scale .x%x.c.s%x \ | ||
664 | -sliderlength 10 \ | ||
665 | -showvalue 0 \ | ||
666 | -length 131 \ | ||
667 | -from 127 -to 0 \ | ||
668 | -command fatom_cb%x\n",canvas,x,x); | ||
669 | } | ||
670 | |||
671 | /* set the start value */ | ||
672 | if (!strcmp(x->x_type->s_name,"checkbutton")) { | ||
673 | if (x->x_val) | ||
674 | sys_vgui(".x%x.c.s%x select\n",canvas,x,x->x_val); | ||
675 | else | ||
676 | sys_vgui(".x%x.c.s%x deselect\n",canvas,x,x->x_val); | ||
677 | } else | ||
678 | sys_vgui(".x%x.c.s%x set %f\n",canvas,x,x->x_val); | ||
679 | |||
680 | } | ||
681 | |||
682 | |||
683 | |||
684 | |||
685 | |||
686 | static void fatom_drawme(t_fatom *x, t_glist *glist, int firsttime) | ||
687 | { | ||
688 | t_canvas *canvas=glist_getcanvas(glist);// x->x_glist;//glist_getcanvas(glist); | ||
689 | DEBUG(post("drawme %d",firsttime);) | ||
690 | if (firsttime) { | ||
691 | DEBUG(post("glist %x canvas %x",x->x_glist,canvas)); | ||
692 | create_widget(x,glist); | ||
693 | x->x_glist = canvas; | ||
694 | sys_vgui(".x%x.c create window %d %d -anchor nw -window .x%x.c.s%x -tags %xS\n", | ||
695 | canvas,text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)+2,x->x_glist,x,x); | ||
696 | |||
697 | } | ||
698 | else { | ||
699 | sys_vgui(".x%x.c coords %xS \ | ||
700 | %d %d\n", | ||
701 | canvas, x, | ||
702 | text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)+2); | ||
703 | } | ||
704 | draw_inlets(x, glist, firsttime, 1,1); | ||
705 | // draw_handle(x, glist, firsttime); | ||
706 | |||
707 | } | ||
708 | |||
709 | |||
710 | static void fatom_erase(t_fatom* x,t_glist* glist) | ||
711 | { | ||
712 | int n; | ||
713 | |||
714 | DEBUG(post("erase");) | ||
715 | sys_vgui("destroy .x%x.c.s%x\n",glist_getcanvas(glist),x); | ||
716 | |||
717 | sys_vgui(".x%x.c delete %xS\n",glist_getcanvas(glist), x); | ||
718 | |||
719 | /* inlets and outlets */ | ||
720 | |||
721 | sys_vgui(".x%x.c delete %xi%d\n",glist_getcanvas(glist),x,0); | ||
722 | sys_vgui(".x%x.c delete %xo%d\n",glist_getcanvas(glist),x,0); | ||
723 | sys_vgui(".x%x.c delete %xhandle\n",glist_getcanvas(glist),x,0); | ||
724 | } | ||
725 | |||
726 | |||
727 | |||
728 | /* ------------------------ fatom widgetbehaviour----------------------------- */ | ||
729 | |||
730 | |||
731 | static void fatom_getrect(t_gobj *z, t_glist *owner, | ||
732 | int *xp1, int *yp1, int *xp2, int *yp2) | ||
733 | { | ||
734 | int width, height; | ||
735 | t_fatom* s = (t_fatom*)z; | ||
736 | |||
737 | width = s->x_rect_width; | ||
738 | height = s->x_rect_height; | ||
739 | *xp1 = text_xpix(&s->x_obj, owner); | ||
740 | *yp1 = text_ypix(&s->x_obj, owner); | ||
741 | *xp2 = text_xpix(&s->x_obj, owner) + width; | ||
742 | *yp2 = text_ypix(&s->x_obj, owner) + height; | ||
743 | } | ||
744 | |||
745 | static void fatom_displace(t_gobj *z, t_glist *glist, | ||
746 | int dx, int dy) | ||
747 | { | ||
748 | t_fatom *x = (t_fatom *)z; | ||
749 | DEBUG(post("displace");) | ||
750 | x->x_obj.te_xpix += dx; | ||
751 | x->x_obj.te_ypix += dy; | ||
752 | if (glist_isvisible(glist)) | ||
753 | { | ||
754 | sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n", | ||
755 | glist_getcanvas(glist), x, | ||
756 | text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), | ||
757 | text_xpix(&x->x_obj, glist) + x->x_rect_width, text_ypix(&x->x_obj, glist) + x->x_rect_height); | ||
758 | |||
759 | fatom_drawme(x, glist, 0); | ||
760 | canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x); | ||
761 | } | ||
762 | DEBUG(post("displace end");) | ||
763 | } | ||
764 | |||
765 | static void fatom_select(t_gobj *z, t_glist *glist, int state) | ||
766 | { | ||
767 | t_fatom *x = (t_fatom *)z; | ||
768 | if (state) { | ||
769 | sys_vgui(".x%x.c create rectangle \ | ||
770 | %d %d %d %d -tags %xSEL -outline blue\n", | ||
771 | glist_getcanvas(glist), | ||
772 | text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist), | ||
773 | text_xpix(&x->x_obj, glist) + x->x_rect_width, text_ypix(&x->x_obj, glist) + x->x_rect_height, | ||
774 | x); | ||
775 | } | ||
776 | else { | ||
777 | sys_vgui(".x%x.c delete %xSEL\n", | ||
778 | glist_getcanvas(glist), x); | ||
779 | } | ||
780 | |||
781 | |||
782 | |||
783 | } | ||
784 | |||
785 | |||
786 | static void fatom_activate(t_gobj *z, t_glist *glist, int state) | ||
787 | { | ||
788 | /* t_text *x = (t_text *)z; | ||
789 | t_rtext *y = glist_findrtext(glist, x); | ||
790 | if (z->g_pd != gatom_class) rtext_activate(y, state);*/ | ||
791 | } | ||
792 | |||
793 | static void fatom_delete(t_gobj *z, t_glist *glist) | ||
794 | { | ||
795 | t_text *x = (t_text *)z; | ||
796 | canvas_deletelinesfor(glist_getcanvas(glist), x); | ||
797 | } | ||
798 | |||
799 | |||
800 | static void fatom_vis(t_gobj *z, t_glist *glist, int vis) | ||
801 | { | ||
802 | t_fatom* s = (t_fatom*)z; | ||
803 | t_rtext *y; | ||
804 | DEBUG(post("vis: %d",vis);) | ||
805 | if (vis) { | ||
806 | #ifdef PD_MINOR_VERSION | ||
807 | y = (t_rtext *) rtext_new(glist, (t_text *)z); | ||
808 | #else | ||
809 | y = (t_rtext *) rtext_new(glist, (t_text *)z,0,0); | ||
810 | #endif | ||
811 | fatom_drawme(s, glist, 1); | ||
812 | } | ||
813 | else { | ||
814 | y = glist_findrtext(glist, (t_text *)z); | ||
815 | fatom_erase(s,glist); | ||
816 | rtext_free(y); | ||
817 | } | ||
818 | } | ||
819 | |||
820 | static void fatom_save(t_gobj *z, t_binbuf *b); | ||
821 | |||
822 | t_widgetbehavior fatom_widgetbehavior; | ||
823 | |||
824 | |||
825 | |||
826 | |||
827 | static void fatom_size(t_fatom* x,t_floatarg w,t_floatarg h) { | ||
828 | x->x_rect_width = w; | ||
829 | x->x_rect_height = h; | ||
830 | } | ||
831 | |||
832 | static void fatom_color(t_fatom* x,t_symbol* col) | ||
833 | { | ||
834 | |||
835 | } | ||
836 | |||
837 | |||
838 | static void fatom_f(t_fatom* x,t_floatarg f) | ||
839 | { | ||
840 | x->x_val = f; | ||
841 | if (x->x_send == &s_) | ||
842 | outlet_float(x->x_obj.ob_outlet,f); | ||
843 | else | ||
844 | if (x->x_send->s_thing) pd_float(x->x_send->s_thing,f); | ||
845 | } | ||
846 | |||
847 | |||
848 | static void fatom_float(t_fatom* x,t_floatarg f) | ||
849 | { | ||
850 | if (glist_isvisible(x->x_glist)) { | ||
851 | if (!strcmp(x->x_type->s_name,"checkbutton")) { | ||
852 | if (x->x_val) | ||
853 | sys_vgui(".x%x.c.s%x select\n",x->x_glist,x,f); | ||
854 | else | ||
855 | sys_vgui(".x%x.c.s%x deselect\n",x->x_glist,x,f); | ||
856 | } else | ||
857 | sys_vgui(".x%x.c.s%x set %f\n",x->x_glist,x,f); | ||
858 | } | ||
859 | fatom_f(x,f); | ||
860 | } | ||
861 | |||
862 | |||
863 | static void fatom_bang(t_fatom* x,t_floatarg f) | ||
864 | { | ||
865 | outlet_float(x->x_obj.ob_outlet,x->x_val); | ||
866 | } | ||
867 | |||
868 | |||
869 | static void fatom_properties(t_gobj *z, t_glist *owner) | ||
870 | { | ||
871 | post("N/I"); | ||
872 | } | ||
873 | |||
874 | |||
875 | static void fatom_save(t_gobj *z, t_binbuf *b) | ||
876 | { | ||
877 | |||
878 | t_fatom *x = (t_fatom *)z; | ||
879 | |||
880 | binbuf_addv(b, "ssiiss", gensym("#X"),gensym("obj"), | ||
881 | x->x_obj.te_xpix, x->x_obj.te_ypix , | ||
882 | gensym("fatom"),x->x_type); | ||
883 | binbuf_addv(b, ";"); | ||
884 | } | ||
885 | |||
886 | |||
887 | static void *fatom_new(t_fatom* x,int argc,t_atom* argv) | ||
888 | { | ||
889 | char buf[256]; | ||
890 | int n = 0; | ||
891 | x->x_glist = canvas_getcurrent(); | ||
892 | |||
893 | x->x_text = gensym(""); | ||
894 | x->x_max = 127; | ||
895 | x->x_min = 0; | ||
896 | x->x_width = 15; | ||
897 | x->x_color = gensym("grey"); | ||
898 | x->x_bgcolor = gensym("grey"); | ||
899 | x->x_send = &s_; | ||
900 | |||
901 | while (argc) { | ||
902 | if (argv->a_type == A_FLOAT) { | ||
903 | if (n==0) x->x_max = atom_getfloat(argv); | ||
904 | if (n==1) x->x_min = atom_getfloat(argv); | ||
905 | if (n==2) x->x_width = atom_getfloat(argv); | ||
906 | } | ||
907 | |||
908 | if (argv->a_type == A_SYMBOL) { | ||
909 | post("%d: symbol value %s",n,atom_getsymbol(argv)->s_name); | ||
910 | if (n==3) x->x_send = atom_getsymbol(argv); | ||
911 | if (n==4) x->x_color = atom_getsymbol(argv); | ||
912 | if (n==5) x->x_bgcolor = atom_getsymbol(argv); | ||
913 | } | ||
914 | argv++; | ||
915 | argc--; | ||
916 | n++; | ||
917 | } | ||
918 | |||
919 | /* bind to a symbol for slider callback (later make this based on the | ||
920 | filepath ??) */ | ||
921 | |||
922 | sprintf(buf,"fatom%x",(t_int)x); | ||
923 | x->x_sym = gensym(buf); | ||
924 | pd_bind(&x->x_obj.ob_pd, x->x_sym); | ||
925 | |||
926 | /* pipe startup code to tk */ | ||
927 | |||
928 | sys_vgui("proc fatom_cb%x {v} {\n pd [concat fatom%x f $v \\;]\n }\n",x,x); | ||
929 | |||
930 | outlet_new(&x->x_obj, &s_float); | ||
931 | return (x); | ||
932 | } | ||
933 | |||
934 | static void fatom_setup_common(t_class* class) | ||
935 | { | ||
936 | |||
937 | fatom_widgetbehavior.w_getrectfn = fatom_getrect; | ||
938 | fatom_widgetbehavior.w_displacefn = fatom_displace; | ||
939 | fatom_widgetbehavior.w_selectfn = fatom_select; | ||
940 | fatom_widgetbehavior.w_activatefn = fatom_activate; | ||
941 | fatom_widgetbehavior.w_deletefn = fatom_delete; | ||
942 | fatom_widgetbehavior.w_visfn = fatom_vis; | ||
943 | #if PD_MINOR_VERSION < 37 | ||
944 | fatom_widgetbehavior.w_savefn = fatom_save; | ||
945 | fatom_widgetbehavior.w_propertiesfn = NULL; | ||
946 | #endif | ||
947 | fatom_widgetbehavior.w_clickfn = NULL; | ||
948 | |||
949 | class_addfloat(class, (t_method)fatom_float); | ||
950 | class_addbang(class, (t_method)fatom_bang); | ||
951 | class_addmethod(class, (t_method)fatom_f, gensym("f"), | ||
952 | A_FLOAT, 0); | ||
953 | |||
954 | /* | ||
955 | class_addmethod(class, (t_method)fatom_size, gensym("size"), | ||
956 | A_FLOAT, A_FLOAT, 0); | ||
957 | |||
958 | class_addmethod(class, (t_method)fatom_color, gensym("color"), | ||
959 | A_SYMBOL, 0); | ||
960 | */ | ||
961 | /* | ||
962 | class_addmethod(class, (t_method)fatom_open, gensym("open"), | ||
963 | A_SYMBOL, 0); | ||
964 | */ | ||
965 | |||
966 | class_setwidget(class,&fatom_widgetbehavior); | ||
967 | #if PD_MINOR_VERSION >= 37 | ||
968 | class_setsavefn(class,&fatom_save); | ||
969 | #endif | ||
970 | } | ||