1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
#include <m_pd.h>
#include <g_canvas.h>
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif
#ifndef IOWIDTH
#define IOWIDTH 4
#endif
typedef struct _button
{
t_object x_obj;
t_atom a_pos;
t_glist * x_glist;
int x_rect_width;
int x_rect_height;
t_symbol* x_sym;
t_symbol* x_text;
int x_height;
int x_width;
} t_button;
/* widget helper functions */
#define DEBUG(x)
static void draw_inlets(t_button *x, t_glist *glist, int firsttime, int nin, int nout)
{
int n = nin;
int nplus, i;
nplus = (n == 1 ? 1 : n-1);
DEBUG(post("draw inlet");)
for (i = 0; i < n; i++)
{
int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH) * i / nplus;
if (firsttime)
sys_vgui(".x%lx.c create rectangle %d %d %d %d -tags %lxo%d\n",
glist_getcanvas(glist),
onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 2,
onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height-1,
x, i);
else
sys_vgui(".x%lx.c coords %lxo%d %d %d %d %d\n",
glist_getcanvas(glist), x, i,
onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 2,
onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->x_rect_height-1);
}
n = nout;
nplus = (n == 1 ? 1 : n-1);
for (i = 0; i < n; i++)
{
int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH) * i / nplus;
if (firsttime)
sys_vgui(".x%lx.c create rectangle %d %d %d %d -tags %lxi%d\n",
glist_getcanvas(glist),
onset, text_ypix(&x->x_obj, glist),
onset + IOWIDTH, text_ypix(&x->x_obj, glist)+5,
x, i);
else
sys_vgui(".x%lx.c coords %lxi%d %d %d %d %d\n",
glist_getcanvas(glist), x, i,
onset, text_ypix(&x->x_obj, glist),
onset + IOWIDTH, text_ypix(&x->x_obj, glist)+5);
}
DEBUG(post("draw inlet end");)
}
static void draw_handle(t_button *x, t_glist *glist, int firsttime) {
int onset = text_xpix(&x->x_obj, glist) + (x->x_rect_width - IOWIDTH+2);
if (firsttime)
sys_vgui(".x%lx.c create rectangle %d %d %d %d -tags %lxhandle\n",
glist_getcanvas(glist),
onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 12,
onset + IOWIDTH-2, text_ypix(&x->x_obj, glist) + x->x_rect_height-4,
x);
else
sys_vgui(".x%lx.c coords %lxhandle %d %d %d %d\n",
glist_getcanvas(glist), x,
onset, text_ypix(&x->x_obj, glist) + x->x_rect_height - 12,
onset + IOWIDTH-2, text_ypix(&x->x_obj, glist) + x->x_rect_height-4);
}
static void create_widget(t_button *x, t_glist *glist)
{
char text[MAXPDSTRING];
int len,i;
t_canvas *canvas=glist_getcanvas(glist);
x->x_rect_width = x->x_width+10;
x->x_rect_height = x->x_height*20+12;
strncpy(text,x->x_text->s_name,MAXPDSTRING);
len = strlen(text);
for (i=0;i<len;i++) {
if (text[i] == '_')
text[i] = ' ';
}
sys_vgui("destroy .x%lx.c.s%lx\n",glist_getcanvas(glist),x);
sys_vgui("button .x%lx.c.s%lx -height %d -text \"%s\" -command button_cb%lx\n",canvas,x,
x->x_height,text,
x);
}
static void button_drawme(t_button *x, t_glist *glist, int firsttime)
{
t_canvas *canvas=glist_getcanvas(glist);
DEBUG(post("drawme %d",firsttime);)
if (firsttime) {
DEBUG(post("glist %lx canvas %lx",x->x_glist,canvas);)
// if (x->x_glist != canvas) {
create_widget(x,glist);
x->x_glist = canvas;
// }
sys_vgui(".x%lx.c create window %d %d -anchor nw -window .x%lx.c.s%lx -tags %lxS\n",
canvas,text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x->x_glist,x,x);
}
else {
sys_vgui(".x%lx.c coords %lxS \
%d %d\n",
canvas, x,
text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist));
}
draw_inlets(x, glist, firsttime, 1,1);
// draw_handle(x, glist, firsttime);
}
static void button_erase(t_button* x,t_glist* glist)
{
int n;
DEBUG(post("erase");)
sys_vgui("destroy .x%lx.c.s%lx\n",glist_getcanvas(glist),x);
sys_vgui(".x%lx.c delete %lxS\n",glist_getcanvas(glist), x);
/* inlets and outlets */
sys_vgui(".x%lx.c delete %lxi%d\n",glist_getcanvas(glist),x,0);
sys_vgui(".x%lx.c delete %lxo%d\n",glist_getcanvas(glist),x,0);
sys_vgui(".x%lx.c delete %lxhandle\n",glist_getcanvas(glist),x,0);
}
/* ------------------------ button widgetbehaviour----------------------------- */
static void button_getrect(t_gobj *z, t_glist *owner,
int *xp1, int *yp1, int *xp2, int *yp2)
{
int width, height;
t_button* s = (t_button*)z;
width = s->x_rect_width;
height = s->x_rect_height;
*xp1 = text_xpix(&s->x_obj, owner);
*yp1 = text_ypix(&s->x_obj, owner) - 1;
*xp2 = text_xpix(&s->x_obj, owner) + width;
*yp2 = text_ypix(&s->x_obj, owner) + height;
}
static void button_displace(t_gobj *z, t_glist *glist,
int dx, int dy)
{
t_button *x = (t_button *)z;
DEBUG(post("displace");)
x->x_obj.te_xpix += dx;
x->x_obj.te_ypix += dy;
if (glist_isvisible(glist))
{
sys_vgui(".x%lx.c coords %lxSEL %d %d %d %d\n",
glist_getcanvas(glist), x,
text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)-1,
text_xpix(&x->x_obj, glist) + x->x_rect_width, text_ypix(&x->x_obj, glist) + x->x_rect_height-2);
button_drawme(x, glist, 0);
canvas_fixlinesfor(glist,(t_text*) x);
}
DEBUG(post("displace end");)
}
static void button_select(t_gobj *z, t_glist *glist, int state)
{
t_button *x = (t_button *)z;
if (state) {
sys_vgui(".x%lx.c create rectangle \
%d %d %d %d -tags %lxSEL -outline blue\n",
glist_getcanvas(glist),
text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist)-1,
text_xpix(&x->x_obj, glist) + x->x_rect_width, text_ypix(&x->x_obj, glist) + x->x_rect_height-2,
x);
}
else {
sys_vgui(".x%lx.c delete %lxSEL\n",
glist_getcanvas(glist), x);
}
}
static void button_activate(t_gobj *z, t_glist *glist, int state)
{
/* t_text *x = (t_text *)z;
t_rtext *y = glist_findrtext(glist, x);
if (z->g_pd != gatom_class) rtext_activate(y, state);*/
}
static void button_delete(t_gobj *z, t_glist *glist)
{
t_text *x = (t_text *)z;
canvas_deletelinesfor(glist, x);
}
static void button_vis(t_gobj *z, t_glist *glist, int vis)
{
t_button* s = (t_button*)z;
DEBUG(post("vis: %d",vis);)
if (vis) {
button_drawme(s, glist, 1);
}
else {
button_erase(s,glist);
sys_unqueuegui(z);
}
}
static void button_save(t_gobj *z, t_binbuf *b);
t_widgetbehavior button_widgetbehavior;
void button_size(t_button* x,t_floatarg w,t_floatarg h) {
x->x_width = w;
x->x_height = h;
}
void button_color(t_button* x,t_symbol* col)
{
/* outlet_bang(x->x_obj.ob_outlet); only bang if there was a bang ..
so color black does the same as bang, but doesn't forward the bang
*/
}
static void button_bang(t_button* x)
{
sys_vgui(".x%lx.c.s%lx flash\n",x->x_glist,x);
outlet_bang(x->x_obj.ob_outlet);
}
static void button_b(t_button* x)
{
outlet_bang(x->x_obj.ob_outlet);
}
static void button_save(t_gobj *z, t_binbuf *b)
{
t_button *x = (t_button *)z;
binbuf_addv(b, "ssiiss", gensym("#X"), gensym("obj"),
x->x_obj.te_xpix, x->x_obj.te_ypix ,
atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
x->x_text);
binbuf_addv(b, ";");
}
static t_class *button_class;
static void *button_new(t_symbol* text)
{
t_button *x = (t_button *)pd_new(button_class);
char buf[256];
x->x_glist = (t_glist*)NULL;
x->x_width = 30;
x->x_height = 1;
if (text == &s_)
x->x_text = gensym("OK");
else
x->x_text = text;
/* TODO .. ask the button for its width */
x->x_width += strlen(x->x_text->s_name)*5.2;
sprintf(buf,"button%lx", (long unsigned int)x);
x->x_sym = gensym(buf);
pd_bind(&x->x_obj.ob_pd, x->x_sym);
/* pipe startup code to tk */
sys_vgui("proc button_cb%lx {} {pdsend {%s b}}\n", x, buf);
outlet_new(&x->x_obj, &s_float);
return (x);
}
void button_setup(void) {
button_class = class_new(gensym("button"), (t_newmethod)button_new, 0,
sizeof(t_button),0,A_DEFSYM,0);
class_addbang(button_class, (t_method)button_bang);
class_addmethod(button_class, (t_method)button_b,gensym("b"),0);
button_widgetbehavior.w_getrectfn= button_getrect,
button_widgetbehavior.w_displacefn= button_displace,
button_widgetbehavior.w_selectfn= button_select,
button_widgetbehavior.w_activatefn= button_activate,
button_widgetbehavior.w_deletefn= button_delete,
button_widgetbehavior.w_visfn= button_vis,
#if PD_MINOR_VERSION < 37
button_widgetbehavior.w_savefn= button_save,
#endif
button_widgetbehavior.w_clickfn= NULL,
#if PD_MINOR_VERSION < 37
button_widgetbehavior.w_propertiesfn= NULL,
#endif
class_setwidget(button_class,&button_widgetbehavior);
#if PD_MINOR_VERSION >= 37
class_setsavefn(button_class,&button_save);
#endif
}
|