aboutsummaryrefslogtreecommitdiff
path: root/shared/hammer/gui.c
blob: 131ddfe71b6fcaeef881628f7df1e210d3a77336 (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* Copyright (c) 2003-2004 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* FIXME use guiconnect */

#include <stdio.h>
#include "m_pd.h"
#include "g_canvas.h"
#include "hammer/gui.h"

//#define HAMMERGUI_DEBUG

static t_class *hammergui_class = 0;
static t_hammergui *sink = 0;
static t_symbol *ps__up;
static t_symbol *ps__focus;
static t_symbol *ps__vised;

static void hammergui_anything(t_hammergui *snk,
			       t_symbol *s, int ac, t_atom *av)
{
    /* Dummy method, filtering out messages from gui to the masters.  This is
       needed in order to keep Pd's message system happy in a ``gray period''
       -- after last master is unbound, and before gui bindings are cleared. */
#ifdef HAMMERGUI_DEBUG
    startpost("%s", s->s_name);
    postatom(ac, av);
    endpost();
#endif
}

/* filtering out redundant "_up" messages */
static void hammergui__up(t_hammergui *snk, t_floatarg f)
{
#ifdef HAMMERGUI_DEBUG
    post("_up %g", f);
#endif
    if ((int)f)
    {
	if (!snk->g_up)
	{
	    snk->g_up = 1;
	    if (snk->g_mouse->s_thing)
	    {
		t_atom at;
		SETFLOAT(&at, 1);
		pd_typedmess(snk->g_mouse->s_thing, ps__up, 1, &at);
	    }
	}
    }
    else
    {
	if (snk->g_up)
	{
	    snk->g_up = 0;
	    if (snk->g_mouse->s_thing)
	    {
		t_atom at;
		SETFLOAT(&at, 0);
		pd_typedmess(snk->g_mouse->s_thing, ps__up, 1, &at);
	    }
	}
    }
}

static void hammergui__focus(t_hammergui *snk, t_symbol *s, t_floatarg f)
{
#ifdef HAMMERGUI_DEBUG
    if (s) post("_focus %s %g", s->s_name, f);
#endif
    if (snk->g_focus->s_thing)
    {
	t_atom at[2];
	SETSYMBOL(&at[0], s);
	SETFLOAT(&at[1], f);
	pd_typedmess(snk->g_focus->s_thing, ps__focus, 2, at);
    }
}

static void hammergui__vised(t_hammergui *snk, t_symbol *s, t_floatarg f)
{
#ifdef HAMMERGUI_DEBUG
    if (s) post("_vised %s %g", s->s_name, f);
#endif
    if (snk->g_vised->s_thing)
    {
	t_atom at[2];
	SETSYMBOL(&at[0], s);
	SETFLOAT(&at[1], f);
	pd_typedmess(snk->g_vised->s_thing, ps__vised, 2, at);
    }
#if 0
    /* How to be notified about changes of button state, prior to gui objects
       in a canvas?  LATER find a reliable way -- delete if failed */
    sys_vgui("bindtags %s {hammertag %s Canvas . all}\n",
	     s->s_name, s->s_name);
#endif
}


static void hammergui_dobindmouse(t_hammergui *snk)
{
#if 0
    /* How to be notified about changes of button state, prior to gui objects
       in a canvas?  LATER find a reliable way -- delete if failed */
    sys_vgui("bind hammertag <<hammerdown>> {pd [concat %s _up 0 \\;]}\n",
	     snk->g_gui->s_name);
    sys_vgui("bind hammertag <<hammerup>> {pd [concat %s _up 1 \\;]}\n",
	     snk->g_gui->s_name);
#endif
    sys_vgui("bind all <<hammerdown>> {pd [concat %s _up 0 \\;]}\n",
	     snk->g_gui->s_name);
    sys_vgui("bind all <<hammerup>> {pd [concat %s _up 1 \\;]}\n",
	     snk->g_gui->s_name);
}

static void hammergui__remouse(t_hammergui *snk)
{
    if (snk->g_mouse->s_thing)
    {
	/* if a new master was bound in a gray period, we need to
	   restore gui bindings */
#if 1
	post("rebinding mouse...");
#endif
	hammergui_dobindmouse(snk);
    }
}

static void hammergui_dobindfocus(t_hammergui *snk)
{
    sys_vgui("bind Canvas <<hammerfocusin>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _focus %%W 1 \\;]}}\n", snk->g_gui->s_name);
    sys_vgui("bind Canvas <<hammerfocusout>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _focus %%W 0 \\;]}}\n", snk->g_gui->s_name);
}

static void hammergui__refocus(t_hammergui *snk)
{
    if (snk->g_focus->s_thing)
    {
	/* if a new master was bound in a gray period, we need to
	   restore gui bindings */
#if 1
	post("rebinding focus...");
#endif
	hammergui_dobindfocus(snk);
    }
}

static void hammergui_dobindvised(t_hammergui *snk)
{
    sys_vgui("bind Canvas <<hammervised>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _vised %%W 1 \\;]}}\n", snk->g_gui->s_name);
    sys_vgui("bind Canvas <<hammerunvised>> \
 {if {[hammergui_ispatcher %%W]} \
  {pd [concat %s _vised %%W 0 \\;]}}\n", snk->g_gui->s_name);
}

static void hammergui__revised(t_hammergui *snk)
{
    if (snk->g_vised->s_thing)
    {
	/* if a new master was bound in a gray period, we need to
	   restore gui bindings */
#if 1
	post("rebinding vised events...");
#endif
	hammergui_dobindvised(snk);
    }
}

static void hammergui_setup(void)
{
    hammergui_class = class_new(gensym("_hammergui"), 0, 0,
				sizeof(t_hammergui),
				CLASS_PD | CLASS_NOINLET, 0);
    class_addanything(hammergui_class, hammergui_anything);
    class_addmethod(hammergui_class, (t_method)hammergui__remouse,
		    gensym("_remouse"), 0);
    class_addmethod(hammergui_class, (t_method)hammergui__refocus,
		    gensym("_refocus"), 0);
    class_addmethod(hammergui_class, (t_method)hammergui__revised,
		    gensym("_revised"), 0);
    ps__up = gensym("_up");
    class_addmethod(hammergui_class, (t_method)hammergui__up,
		    ps__up, A_FLOAT, 0);
    ps__focus = gensym("_focus");
    class_addmethod(hammergui_class, (t_method)hammergui__focus,
		    ps__focus, A_SYMBOL, A_FLOAT, 0);
    ps__vised = gensym("_vised");
    class_addmethod(hammergui_class, (t_method)hammergui__vised,
		    ps__vised, A_SYMBOL, A_FLOAT, 0);

    /* Protect against pdCmd being called (via "Canvas <Destroy>" binding)
       during Tcl_Finalize().  FIXME this should be a standard exit handler. */
    sys_gui("proc hammergui_exithook {cmd op} {proc pd {} {}}\n");
    sys_gui("trace add execution exit enter hammergui_exithook\n");

    sys_gui("proc hammergui_ispatcher {cv} {\n");
    sys_gui(" if {[string range $cv 0 1] == \".x\"");
    sys_gui("  && [string range $cv end-1 end] == \".c\"} {\n");
    sys_gui("  return 1} else {return 0}\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_remouse {} {\n");
    sys_gui(" bind all <<hammerdown>> {}\n");
    sys_gui(" bind all <<hammerup>> {}\n");
    sys_gui(" pd [concat #hammergui _remouse \\;]\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_mousexy {target} {\n");
    sys_gui(" set x [winfo pointerx .]\n");
    sys_gui(" set y [winfo pointery .]\n");
    sys_gui(" pd [concat #hammermouse $target $x $y \\;]\n");
    sys_gui("}\n");

    /* visibility hack for msw, LATER rethink */
    sys_gui("global hammergui_ispolling\n");
    sys_gui("global hammergui_x\n");
    sys_gui("global hammergui_y\n");
    sys_gui("set hammergui_ispolling 0\n");
    sys_gui("set hammergui_x 0\n");
    sys_gui("set hammergui_y 0\n");

    sys_gui("proc hammergui_poll {} {\n");
    sys_gui(" global hammergui_ispolling\n");
    sys_gui(" global hammergui_x\n");
    sys_gui(" global hammergui_y\n");
    sys_gui(" if {$hammergui_ispolling == 1} {\n");
    sys_gui("  set x [winfo pointerx .]\n");
    sys_gui("  set y [winfo pointery .]\n");
    sys_gui("  if {$hammergui_x != $x || $hammergui_y != $y} {\n");
    sys_gui("   pd [concat #hammermouse _poll $x $y \\;]\n");
    sys_gui("   set hammergui_x $x\n");
    sys_gui("   set hammergui_y $y\n");
    sys_gui("  }\n");
    sys_gui("  after 50 hammergui_poll\n");
    sys_gui(" }\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_refocus {} {\n");
    sys_gui(" bind Canvas <<hammerfocusin>> {}\n");
    sys_gui(" bind Canvas <<hammerfocusout>> {}\n");
    sys_gui(" pd [concat #hammergui _refocus \\;]\n");
    sys_gui("}\n");

    sys_gui("proc hammergui_revised {} {\n");
    sys_gui(" bind Canvas <<hammervised>> {}\n");
    sys_gui(" bind Canvas <<hammerunvised>> {}\n");
    sys_gui(" pd [concat #hammergui _revised \\;]\n");
    sys_gui("}\n");
}

static int hammergui_validate(int dosetup)
{
    if (dosetup)
    {
	if (!hammergui_class) hammergui_setup();
	if (!sink)
	{
	    sink = (t_hammergui *)pd_new(hammergui_class);
	    sink->g_gui = gensym("#hammergui");
	    pd_bind((t_pd *)sink, sink->g_gui);
	}
    }
    if (hammergui_class && sink)
	return (1);
    else
    {
	bug("hammergui_validate");
	return (0);
    }
}

static int hammergui_mousevalidate(int dosetup)
{
    if (dosetup && !sink->g_mouse)
    {
	sink->g_mouse = gensym("#hammermouse");
	sys_gui("event add <<hammerdown>> <ButtonPress>\n");
	sys_gui("event add <<hammerup>> <ButtonRelease>\n");
    }
    if (sink->g_mouse)
	return (1);
    else
    {
	bug("hammergui_mousevalidate");
	return (0);
    }
}

static int hammergui_pollvalidate(int dosetup)
{
    if (dosetup && !sink->g_poll)
    {
	sink->g_poll = gensym("#hammerpoll");
	pd_bind((t_pd *)sink, sink->g_poll);  /* never unbound */
    }
    if (sink->g_poll)
	return (1);
    else
    {
	bug("hammergui_pollvalidate");
	return (0);
    }
}

static int hammergui_focusvalidate(int dosetup)
{
    if (dosetup && !sink->g_focus)
    {
	sink->g_focus = gensym("#hammerfocus");
	sys_gui("event add <<hammerfocusin>> <FocusIn>\n");
	sys_gui("event add <<hammerfocusout>> <FocusOut>\n");
    }
    if (sink->g_focus)
	return (1);
    else
    {
	bug("hammergui_focusvalidate");
	return (0);
    }
}

static int hammergui_visedvalidate(int dosetup)
{
    if (dosetup && !sink->g_vised)
    {
	sink->g_vised = gensym("#hammervised");
	/* subsequent map events have to be filtered out at the caller's side,
	   LATER investigate */
	sys_gui("event add <<hammervised>> <Map>\n");
	sys_gui("event add <<hammerunvised>> <Destroy>\n");
    }
    if (sink->g_vised)
	return (1);
    else
    {
	bug("hammergui_visedvalidate");
	return (0);
    }
}

void hammergui_bindmouse(t_pd *master)
{
    hammergui_validate(1);
    hammergui_mousevalidate(1);
    if (!sink->g_mouse->s_thing)
	hammergui_dobindmouse(sink);
    pd_bind(master, sink->g_mouse);
}

void hammergui_unbindmouse(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_mousevalidate(0)
	&& sink->g_mouse->s_thing)
    {
	pd_unbind(master, sink->g_mouse);
	if (!sink->g_mouse->s_thing)
	    sys_gui("hammergui_remouse\n");
    }
    else bug("hammergui_unbindmouse");
}

void hammergui_mousexy(t_symbol *s)
{
    if (hammergui_validate(0))
	sys_vgui("hammergui_mousexy %s\n", s->s_name);
}

void hammergui_willpoll(void)
{
    hammergui_validate(1);
    hammergui_pollvalidate(1);
}

void hammergui_startpolling(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_pollvalidate(0))
    {
	int doinit = (sink->g_poll->s_thing == (t_pd *)sink);
	pd_bind(master, sink->g_poll);
	if (doinit)
	{
	    /* visibility hack for msw, LATER rethink */
	    sys_gui("global hammergui_ispolling\n");
	    sys_gui("set hammergui_ispolling 1\n");
	    sys_gui("hammergui_poll\n");
	}
    }
}

void hammergui_stoppolling(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_pollvalidate(0))
    {
	pd_unbind(master, sink->g_poll);
	if (sink->g_poll->s_thing == (t_pd *)sink)
	{
	    sys_gui("after cancel hammergui_poll\n");
	    /* visibility hack for msw, LATER rethink */
	    sys_gui("global hammergui_ispolling\n");
	    sys_gui("set hammergui_ispolling 0\n");
	}
    }
}

void hammergui_bindfocus(t_pd *master)
{
    hammergui_validate(1);
    hammergui_focusvalidate(1);
    if (!sink->g_focus->s_thing)
	hammergui_dobindfocus(sink);
    pd_bind(master, sink->g_focus);
}

void hammergui_unbindfocus(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_focusvalidate(0)
	&& sink->g_focus->s_thing)
    {
	pd_unbind(master, sink->g_focus);
	if (!sink->g_focus->s_thing)
	    sys_gui("hammergui_refocus\n");
    }
    else bug("hammergui_unbindfocus");
}

void hammergui_bindvised(t_pd *master)
{
    hammergui_validate(1);
    hammergui_visedvalidate(1);
    if (!sink->g_vised->s_thing)
	hammergui_dobindvised(sink);
    pd_bind(master, sink->g_vised);
}

void hammergui_unbindvised(t_pd *master)
{
    if (hammergui_validate(0) && hammergui_visedvalidate(0)
	&& sink->g_vised->s_thing)
    {
	pd_unbind(master, sink->g_vised);
	if (!sink->g_vised->s_thing)
	    sys_gui("hammergui_revised\n");
    }
    else bug("hammergui_unbindvised");
}