Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / extra / fatom.h
blobabaf9b91c1438b1ae707887f18f4b5a6b5410d79
1 /* ------------------------ fatom ----------------------------- */
3 #define x_val a_pos.a_w.w_float
4 #define DEBUG(x)
6 #include <string.h>
7 #include <stdio.h>
9 typedef struct _fatom
11 t_object x_obj;
12 t_atom a_pos; /* the value of the fatom */
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) */
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;
30 /* widget helper functions */
35 static void draw_inlets(t_fatom *x, t_glist *glist, int firsttime, int nin, int nout)
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++)
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);
56 n = nout;
57 nplus = (n == 1 ? 1 : n-1);
58 for (i = 0; i < n; i++)
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);
74 DEBUG(post("draw inlet end");)
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);
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);
94 static void create_widget(t_fatom *x, t_glist *glist)
96 t_canvas *canvas=glist_getcanvas(glist);
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;
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,
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,
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);
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);
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);
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);
201 static void fatom_drawme(t_fatom *x, t_glist *glist, int firsttime)
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);
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);
219 draw_inlets(x, glist, firsttime, 1,1);
220 // draw_handle(x, glist, firsttime);
225 static void fatom_erase(t_fatom* x,t_glist* glist)
227 int n;
229 DEBUG(post("erase");)
230 sys_vgui("destroy .x%x.c.s%x\n",glist_getcanvas(glist),x);
232 sys_vgui(".x%x.c delete %xS\n",glist_getcanvas(glist), x);
234 /* inlets and outlets */
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);
243 /* ------------------------ fatom widgetbehaviour----------------------------- */
246 static void fatom_getrect(t_gobj *z, t_glist *owner,
247 int *xp1, int *yp1, int *xp2, int *yp2)
249 int width, height;
250 t_fatom* s = (t_fatom*)z;
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;
260 static void fatom_displace(t_gobj *z, t_glist *glist,
261 int dx, int dy)
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))
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);
274 fatom_drawme(x, glist, 0);
275 canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x);
277 DEBUG(post("displace end");)
280 static void fatom_select(t_gobj *z, t_glist *glist, int state)
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,
291 else {
292 sys_vgui(".x%x.c delete %xSEL\n",
293 glist_getcanvas(glist), x);
301 static void fatom_activate(t_gobj *z, t_glist *glist, int state)
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);*/
308 static void fatom_delete(t_gobj *z, t_glist *glist)
310 t_text *x = (t_text *)z;
311 canvas_deletelinesfor(glist_getcanvas(glist), x);
315 static void fatom_vis(t_gobj *z, t_glist *glist, int vis)
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);
328 else {
329 y = glist_findrtext(glist, (t_text *)z);
330 fatom_erase(s,glist);
331 rtext_free(y);
335 static void fatom_save(t_gobj *z, t_binbuf *b);
337 t_widgetbehavior fatom_widgetbehavior;
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;
347 static void fatom_color(t_fatom* x,t_symbol* col)
353 static void fatom_f(t_fatom* x,t_floatarg f)
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);
363 static void fatom_float(t_fatom* x,t_floatarg f)
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);
374 fatom_f(x,f);
378 static void fatom_bang(t_fatom* x,t_floatarg f)
380 outlet_float(x->x_obj.ob_outlet,x->x_val);
384 static void fatom_properties(t_gobj *z, t_glist *owner)
386 post("N/I");
390 static void fatom_save(t_gobj *z, t_binbuf *b)
393 t_fatom *x = (t_fatom *)z;
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, ";");
402 static void *fatom_new(t_fatom* x,int argc,t_atom* argv)
404 char buf[256];
405 int n = 0;
406 x->x_glist = canvas_getcurrent();
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_;
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);
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);
429 argv++;
430 argc--;
431 n++;
434 /* bind to a symbol for slider callback (later make this based on the
435 filepath ??) */
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);
441 /* pipe startup code to tk */
443 sys_vgui("proc fatom_cb%x {v} {\n pd [concat fatom%x f $v \\;]\n }\n",x,x);
445 outlet_new(&x->x_obj, &s_float);
446 return (x);
449 static void fatom_setup_common(t_class* class)
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;
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);
470 class_addmethod(class, (t_method)fatom_size, gensym("size"),
471 A_FLOAT, A_FLOAT, 0);
473 class_addmethod(class, (t_method)fatom_color, gensym("color"),
474 A_SYMBOL, 0);
477 class_addmethod(class, (t_method)fatom_open, gensym("open"),
478 A_SYMBOL, 0);
481 class_setwidget(class,&fatom_widgetbehavior);
482 #if PD_MINOR_VERSION >= 37
483 class_setsavefn(class,&fatom_save);
484 #endif
486 /* ------------------------ fatom ----------------------------- */
488 #define x_val a_pos.a_w.w_float
489 #define DEBUG(x)
491 #include <string.h>
492 #include <stdio.h>
494 typedef struct _fatom
496 t_object x_obj;
497 t_atom a_pos; /* the value of the fatom */
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) */
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;
515 /* widget helper functions */
520 static void draw_inlets(t_fatom *x, t_glist *glist, int firsttime, int nin, int nout)
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++)
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);
541 n = nout;
542 nplus = (n == 1 ? 1 : n-1);
543 for (i = 0; i < n; i++)
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);
559 DEBUG(post("draw inlet end");)
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);
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,
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);
579 static void create_widget(t_fatom *x, t_glist *glist)
581 t_canvas *canvas=glist_getcanvas(glist);
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;
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,
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,
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);
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);
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);
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);
686 static void fatom_drawme(t_fatom *x, t_glist *glist, int firsttime)
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);
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);
704 draw_inlets(x, glist, firsttime, 1,1);
705 // draw_handle(x, glist, firsttime);
710 static void fatom_erase(t_fatom* x,t_glist* glist)
712 int n;
714 DEBUG(post("erase");)
715 sys_vgui("destroy .x%x.c.s%x\n",glist_getcanvas(glist),x);
717 sys_vgui(".x%x.c delete %xS\n",glist_getcanvas(glist), x);
719 /* inlets and outlets */
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);
728 /* ------------------------ fatom widgetbehaviour----------------------------- */
731 static void fatom_getrect(t_gobj *z, t_glist *owner,
732 int *xp1, int *yp1, int *xp2, int *yp2)
734 int width, height;
735 t_fatom* s = (t_fatom*)z;
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;
745 static void fatom_displace(t_gobj *z, t_glist *glist,
746 int dx, int dy)
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))
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);
759 fatom_drawme(x, glist, 0);
760 canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x);
762 DEBUG(post("displace end");)
765 static void fatom_select(t_gobj *z, t_glist *glist, int state)
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,
776 else {
777 sys_vgui(".x%x.c delete %xSEL\n",
778 glist_getcanvas(glist), x);
786 static void fatom_activate(t_gobj *z, t_glist *glist, int state)
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);*/
793 static void fatom_delete(t_gobj *z, t_glist *glist)
795 t_text *x = (t_text *)z;
796 canvas_deletelinesfor(glist_getcanvas(glist), x);
800 static void fatom_vis(t_gobj *z, t_glist *glist, int vis)
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);
813 else {
814 y = glist_findrtext(glist, (t_text *)z);
815 fatom_erase(s,glist);
816 rtext_free(y);
820 static void fatom_save(t_gobj *z, t_binbuf *b);
822 t_widgetbehavior fatom_widgetbehavior;
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;
832 static void fatom_color(t_fatom* x,t_symbol* col)
838 static void fatom_f(t_fatom* x,t_floatarg f)
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);
848 static void fatom_float(t_fatom* x,t_floatarg f)
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);
859 fatom_f(x,f);
863 static void fatom_bang(t_fatom* x,t_floatarg f)
865 outlet_float(x->x_obj.ob_outlet,x->x_val);
869 static void fatom_properties(t_gobj *z, t_glist *owner)
871 post("N/I");
875 static void fatom_save(t_gobj *z, t_binbuf *b)
878 t_fatom *x = (t_fatom *)z;
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, ";");
887 static void *fatom_new(t_fatom* x,int argc,t_atom* argv)
889 char buf[256];
890 int n = 0;
891 x->x_glist = canvas_getcurrent();
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_;
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);
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);
914 argv++;
915 argc--;
916 n++;
919 /* bind to a symbol for slider callback (later make this based on the
920 filepath ??) */
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);
926 /* pipe startup code to tk */
928 sys_vgui("proc fatom_cb%x {v} {\n pd [concat fatom%x f $v \\;]\n }\n",x,x);
930 outlet_new(&x->x_obj, &s_float);
931 return (x);
934 static void fatom_setup_common(t_class* class)
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;
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);
955 class_addmethod(class, (t_method)fatom_size, gensym("size"),
956 A_FLOAT, A_FLOAT, 0);
958 class_addmethod(class, (t_method)fatom_color, gensym("color"),
959 A_SYMBOL, 0);
962 class_addmethod(class, (t_method)fatom_open, gensym("open"),
963 A_SYMBOL, 0);
966 class_setwidget(class,&fatom_widgetbehavior);
967 #if PD_MINOR_VERSION >= 37
968 class_setsavefn(class,&fatom_save);
969 #endif