aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/guitest/flgui.cpp
blob: 86d8e4ea20de10fb87a59f0a64eb5fa6301efb9a (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include "flgui.h"
#include "flguiobj.h"
#include "flinternal.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#ifdef PD
t_class *flext_gui::px_class = NULL;
t_class *flext_gui::pxkey_class = NULL;

struct flext_gui::px_object  // no virtual table!
{ 
	t_object obj;			// MUST reside at memory offset 0
	t_canvas *canv;

	void init(t_canvas *c) { canv = c; }
};

struct flext_gui::pxkey_object  // no virtual table!
{ 
	t_object obj;			// MUST reside at memory offset 0
	flext_gui *th;

	void init(flext_gui *t) { th = t; }
};
#endif


flext_gui::flext_gui(int xs,int ys):
	objs(NULL),
#ifdef PD
	xsize(xs),ysize(ys),
#endif
#ifdef MAXMSP
	curx(-1),cury(-1),curmod(-1),
	created(false),
#endif
	bindsym(NULL)
{
	canvas = new Canvas(thisCanvas());
	objs = new GuiGroup(canvas);

#ifdef PD
	AddCanvas();
#else
	t_box *b = (t_box *)gensym("#B")->s_thing;
	
	int x = b->b_rect.left,y = b->b_rect.top;
	t_pxbox *p = thisHdr();	
	box_new(&p->z_box, thisCanvas(), F_DRAWFIRSTIN | F_GROWBOTH | F_SAVVY,x,y,x+xs,y+ys);
	p->z_box.b_firstin = (void *)p;  /* it's not really an inlet */
	box_ready(&p->z_box);
#endif
}

flext_gui::~flext_gui()
{
#ifdef PD
	RmvCanvas();
#endif

	delete objs;
	delete canvas;
}


void flext_gui::setup(t_class *c)
{
#ifdef PD
	SetWidget(c);

    pxkey_class = class_new(gensym("flext_gui key proxy"),NULL,NULL,sizeof(pxkey_object),CLASS_PD|CLASS_NOINLET, A_NULL);
	add_anything(pxkey_class,pxkey_method); 
    pxkey = (pxkey_object *)pd_new(pxkey_class);

	pd_bind(&pxkey_class,gensym("#keyname"));
//	pd_bind(&pxkey_class,gensym("#key"));
//	pd_bind(&pxkey_class,gensym("#keyup"));

	gcanv = NULL;

#ifdef DIRECT_TK
    px_class = class_new(gensym("flext_gui proxy"),NULL,NULL,sizeof(px_object),CLASS_PD|CLASS_NOINLET, A_NULL);
	add_anything(px_class,px_method); 

	gcm_motion = MakeSymbol("_tk_motion");
	gcm_mousekey = MakeSymbol("_tk_mousekey");
	gcm_mousewheel = MakeSymbol("_tk_mousewheel");
	gcm_key = MakeSymbol("_tk_key");
	gcm_destroy = MakeSymbol("_tk_destroy");
#endif

	// this is wrong if a modifier key is pressed during creation of the first object.....
	curmod = 0;
#else
	addmess((method)sg_update, "update", A_CANT, A_NULL);
	addmess((method)sg_click, "click", A_CANT, A_NULL);
	addmess((method)sg_psave, "psave", A_CANT, A_NULL);
	addmess((method)sg_bfont, "bfont", A_CANT, A_NULL);
	addmess((method)sg_key, "key", A_CANT, A_NULL);
	addmess((method)sg_bidle, "bidle", A_CANT, A_NULL);
#endif
}

#ifdef PD

int flext_gui::evmask = evMotion|evMouseDown|evKeyDown|evKeyUp;
int flext_gui::curmod = 0;
flext_gui::pxkey_object *flext_gui::pxkey = NULL;
flext_gui::guicanv *flext_gui::gcanv = NULL;

#ifdef DIRECT_TK
const t_symbol *flext_gui::gcm_motion = NULL;
const t_symbol *flext_gui::gcm_mousekey = NULL;
const t_symbol *flext_gui::gcm_mousewheel = NULL;
const t_symbol *flext_gui::gcm_key = NULL;
const t_symbol *flext_gui::gcm_destroy = NULL;

void flext_gui::px_method(px_object *obj,const t_symbol *s,int argc,t_atom *argv)
{
	guicanv *ix = gcanv;
	for(; ix && ix->canv != obj->canv; ix = ix->nxt);

	if(ix) {
		CBParams parms;

		if(s == gcm_motion) {
			parms.kind = evMotion;
			parms.pMotion.x = GetAInt(argv[0]);
			parms.pMotion.y = GetAInt(argv[1]);
			parms.pMotion.mod = GetAInt(argv[2]);
		}
		else if(s == gcm_mousekey) {
			parms.kind = GetAInt(argv[0])?evMouseDown:evMouseUp;
			parms.pMouseKey.x = GetAInt(argv[1]);
			parms.pMouseKey.y = GetAInt(argv[2]);
			parms.pMouseKey.b = GetAInt(argv[3]);
			parms.pMouseKey.mod = GetAInt(argv[4]);
		}
		else if(s == gcm_mousewheel) {
			parms.kind = evMouseWheel;
			parms.pMouseWheel.x = GetAInt(argv[0]);
			parms.pMouseWheel.y = GetAInt(argv[1]);
			parms.pMouseWheel.mod = GetAInt(argv[2]);
			parms.pMouseWheel.delta = GetAInt(argv[3]);
		}
		else if(s == gcm_key) {
			parms.kind = GetAInt(argv[0])?evKeyDown:evKeyUp;
			parms.pKey.k = GetAInt(argv[1]);
			parms.pKey.a = GetAInt(argv[2]);
//			parms.pKey.n = GetAInt(argv[3]);
			parms.pKey.mod = GetAInt(argv[4]);
		}
		else if(s == gcm_destroy) {
//			post("TK destroy");
			DelCanvas(ix->canv);
		}

		if(parms.kind != evNone) {
			for(canvobj *co = ix->head; co; co = co->nxt) 
				co->guiobj->m_Method(parms);
		}

	}
	else 
		error("flext_gui: canvas not found!");
}

#endif

static const char *extkeys[] = {
	"Escape","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12",
	"Prior","Next","Home","End","Delete","Insert",""
};

void flext_gui::pxkey_method(pxkey_object *obj,const t_symbol *s,int argc,t_atom *argv)
{
/*
	if(s == sym_float && argc == 1) {
		lastkey = GetInt(argv[0]);
	}
	else 
*/
	if(s == sym_list && argc == 2) {
		CBParams p;

		bool down = GetABool(argv[0]);
		const char *str = GetString(argv[1]);
		int code = str[0];
		int asc = code;
		int mod = mod_None;
		if(code && str[1] != 0) { 
			code = asc = 0; 
			if(GetSymbol(argv[1]) == MakeSymbol("Shift_L") || GetSymbol(argv[1]) == MakeSymbol("Shift_R")) {
				code = 2001;
				mod = mod_Shift;
			}
			else if(GetSymbol(argv[1]) == MakeSymbol("Control_L") || GetSymbol(argv[1]) == MakeSymbol("Control_R")) {
				code = 2002;
				mod = mod_Ctrl;
			}
			else if(GetSymbol(argv[1]) == MakeSymbol("Alt_L") || GetSymbol(argv[1]) == MakeSymbol("Alt_R")) {
				code = 2003;
				mod = mod_Alt;
			}
			else {
				for(int i = 0;; ++i) {
					const char *ci = extkeys[i];
					if(!*ci) break;
					if(GetSymbol(argv[1]) == MakeSymbol(ci)) {
						code = 1000+i;
						break;
					}
				}
			}
#ifdef DEBUG
			else
				post("unknown modifier %s",str);
#endif
		}

		if(down) curmod |= mod;
		else curmod &= ~mod;

//		post("Key down=%i c=%c mod=%i",down?1:0,code,curmod);

		if(code || mod) {
			// button is pressed
			p.kind = down?evKeyDown:evKeyUp;
			p.ext = true;
			p.pKey.k = code; //lastkey;
			p.pKey.a = asc;
			p.pKey.mod = curmod;

			for(guicanv *ix = gcanv; ix; ix = ix->nxt)
				for(canvobj *ci = ix->head; ci; ci = ci->nxt)
					ci->guiobj->m_Method(p); 
		}
	}
	else
		post("flext_gui key proxy - unknown method");
}


flext_gui::guicanv::guicanv(t_canvas *c): 
	canv(c),nxt(NULL),ref(0),
	head(NULL),tail(NULL)
{
	char tmp[25];
	sprintf(tmp,"FLCANV%x",c);
	sym = MakeSymbol(tmp);

#ifdef DIRECT_TK
	// proxy for canvas messages
    (px = (px_object *)pd_new(px_class))->init(c);  
#endif
}

flext_gui::guicanv::~guicanv()
{
#ifdef DIRECT_TK
	if(px) pd_free(&px->obj.ob_pd);
#endif
}

void flext_gui::guicanv::Push(flext_gui *o) 
{ 
	canvobj *co = new canvobj(o);
	if(tail) tail->nxt = co;
	tail = co;
	if(!head) head = tail;

	++ref; 
}

void flext_gui::guicanv::Pop(flext_gui *o) 
{ 
	canvobj *prv = NULL,*ix = head;
	for(; ix && ix->guiobj != o; prv = ix,ix = ix->nxt);

	if(ix) {
		--ref; 
		if(prv) prv->nxt = ix->nxt;
		else head = ix->nxt;
		if(!ix->nxt) tail = prv;
	}
	else
		error("flext_gui: object not found in canvas!");
}


void flext_gui::AddCanvas()
{
	t_canvas *c = thisCanvas();
	guicanv *prv = NULL,*ix = gcanv;
	for(; ix && ix->canv != c; prv = ix,ix = ix->nxt);

	if(ix) {
		ix->Push(this);
	}
	else {
		guicanv *nc = new guicanv(c);
		if(prv) prv->nxt = nc;
		else gcanv = nc;

		nc->Push(this);

#ifdef DIRECT_TK
		pd_bind(&nc->px->obj.ob_pd,(t_symbol *)nc->sym); 

/*
		// initialize new canvas object
		sys_vgui("bind .x%x.c <Motion> {pd %s %s %%x %%y %%s \\;}\n",c,GetString(nc->sym),GetString(gcm_motion));

		sys_vgui("bind .x%x.c <ButtonPress> {pd %s %s 1 %%x %%y %%b %%s \\;}\n",c,GetString(nc->sym),GetString(gcm_mousekey));
		sys_vgui("bind .x%x.c <ButtonRelease> {pd %s %s 0 %%x %%y %%b %%s \\;}\n",c,GetString(nc->sym),GetString(gcm_mousekey));
		sys_vgui("bind .x%x.c <MouseWheel> {pd %s %s %%x %%y %%s %%D \\;}\n",c,GetString(nc->sym),GetString(gcm_mousewheel));
		sys_vgui("bind .x%x.c <KeyPress> {pd %s %s 1 %%k %%A %%N %%s \\;}\n",c,GetString(nc->sym),GetString(gcm_key));
		sys_vgui("bind .x%x.c <KeyRelease> {pd %s %s 0 %%k %%A %%N %%s \\;}\n",c,GetString(nc->sym),GetString(gcm_key));

		// what happend to objects in subpatchers?
		sys_vgui("bind .x%x.c <Destroy> {pd %s %s \\;}\n",c,GetString(nc->sym),GetString(gcm_destroy));

	//	sys_vgui("bind .x%x.c <Visibility> {pd %s %s %x %s \\;}\n",c,GetString(nc->sym),"_tk_visibility",this);
*/
#endif
	}
}

void flext_gui::RmvCanvas()
{
	guicanv *ix = gcanv;
	for(; ix && ix->canv != thisCanvas(); ix = ix->nxt);

	if(ix) {
		ix->Pop(this);
		if(!ix->Refs()) DelCanvas(thisCanvas());
	}
	else {
		error("flext_gui: Canvas not found!");
	}
}

void flext_gui::DelCanvas(t_canvas *c)
{
	guicanv *prv = NULL,*ix = gcanv;
	for(; ix && ix->canv != c; prv = ix,ix = ix->nxt);

	if(ix) {
#ifdef DIRECT_TK
		pd_unbind(&ix->px->obj.ob_pd,(t_symbol *)ix->sym); 
#endif

		if(prv) prv->nxt = ix->nxt;
		else gcanv = ix->nxt;
	}
	else {
		error("flext_gui: Canvas not found!");
	}
}

static GuiObj *GetGuiObj(const t_atom &a)
{
	GuiObj *th = NULL;
	sscanf(flext::GetString(a),"%x",&th);
	return th;
}

void flext_gui::g_Displace(int dx, int dy)
{
//	post("Displace");

	XLo(XLo()+dx);
	YLo(YLo()+dy);

	Group().MoveRel(dx,dy);
	FixLines();
}

void flext_gui::g_Delete()
{
	objs->Clear();
	DelLines();
}



t_widgetbehavior flext_gui::widgetbehavior; 

void flext_gui::SetWidget(t_class *c)
{
	// widgetbehavior struct MUST be resident... (static is just ok here)

    widgetbehavior.w_getrectfn =    sg_getrect;
    widgetbehavior.w_displacefn =   sg_displace;
    widgetbehavior.w_selectfn =     sg_select;
    widgetbehavior.w_activatefn =   NULL; //sg_activate;
    widgetbehavior.w_deletefn =     sg_delete;
    widgetbehavior.w_visfn =        sg_vis;
    widgetbehavior.w_clickfn =      sg_click;
    widgetbehavior.w_propertiesfn = sg_properties;
    widgetbehavior.w_savefn =       sg_save;
    class_setwidget(c, &widgetbehavior);
}

void flext_gui::sg_getrect(t_gobj *c, t_glist *,int *xp1, int *yp1, int *xp2, int *yp2) 
{ 
	flext_gui *th = thisObject(c); 
	/*th->g_GetRect(*xp1,*yp1,*xp2,*yp2);*/ 
	*xp1 = th->XLo(),*yp1 = th->YLo(),*xp2 = th->XHi(),*yp2 = th->YHi(); 
}

void flext_gui::sg_displace(t_gobj *c, t_glist *, int dx, int dy) 
{ 
	thisObject(c)->g_Displace(dx,dy); 
}

void flext_gui::sg_select(t_gobj *c, t_glist *, int selected) 
{ 
//	post("Select");

	flext_gui *th = thisObject(c); 
	th->g_Edit(th->selected = (selected != 0)); 
}

void flext_gui::sg_vis(t_gobj *c, t_glist *, int vis) 
{ 
	post("Visible %i",vis);
	
	if(vis) { 
		flext_gui *g = thisObject(c); 
		g->g_Create(); 
		g->Group().MoveRel(g->XLo(),g->YLo());
		g->FixLines(); 
	}
}

int flext_gui::sg_click(t_gobj *c, t_glist *,int xpix, int ypix, int shift, int alt, int dbl, int doit)
{
	flext_gui *g = thisObject(c);
	CBParams p;
	int x = xpix-g->XLo();
	int y = ypix-g->YLo();

	// PD bug: shift isn't reported for idle mousing
//	int mod = (alt?mod_Alt:0)+(shift?mod_Shift:0)+(dbl?mod_Double:0);

	if(doit) {
		// button is pressed
		p.kind = evMouseDown;
		p.pMouseKey.x = x;
		p.pMouseKey.y = y;
		p.pMouseKey.b = 1;
		p.pMouseKey.mod = curmod; //mod;
	}
	else {
		// only mouse position change
		p.kind = evMotion;
		p.pMotion.x = x;
		p.pMotion.y = y;
		p.pMotion.mod = curmod; //mod;
	}
	g->m_Method(p); 
	return 1;
}

void flext_gui::sg_delete(t_gobj *c, t_glist *) 
{ 
	thisObject(c)->g_Delete(); 
}

void flext_gui::sg_properties(t_gobj *c, t_glist *) 
{ 
	thisObject(c)->g_Properties(); 
}

void flext_gui::sg_save(t_gobj *c, t_binbuf *b) 
{ 
	thisObject(c)->g_Save(b); 
}

/*
bool flext_gui::sg_Key(flext_base *c,int argc,t_atom *argv)
{
	return true;
}

bool flext_gui::sg_KeyNum(flext_base *c,int &keynum)
{
	flext_gui *g = dynamic_cast<flext_gui *>(c);
	post("KeyNum %i",keynum);
	return true;
}

bool flext_gui::sg_KeyUp(flext_base *c,int &keynum)
{
	flext_gui *g = dynamic_cast<flext_gui *>(c);
	post("KeyUp %i",keynum);
	return true;
}
*/

#else // MAXMSP

int flext_gui::evmask = evMotion|evMouseDown|evKeyDown;

static void dragfun()
{
}

static void tmfun()
{
}

void flext_gui::sg_click(t_object *x, Point pt, short m) 
{ 
	flext_gui *g = thisObject(x);
	CBParams p(evMouseDown);
	p.pMouseKey.x = pt.h-g->XLo();
	p.pMouseKey.y = pt.v-g->YLo();
	p.pMouseKey.b = 0;
	p.pMouseKey.mod = (m&256?mod_Meta:0)+(m&512?mod_Shift:0)+(m&1024?mod_Caps:0)+(m&2048?mod_Alt:0)+(m&4096?mod_Ctrl:0);
	g->m_Method(p); 
}

void flext_gui::sg_update(t_object *x) 
{ 
	flext_gui *g = thisObject(x);
	if(!g->created) { g->g_Create(); g->created = true; }

	// draw elements
	g->Update();
}

void flext_gui::sg_psave (t_object *x, t_binbuf *dest) { thisObject(x)->g_Save(dest); }

void flext_gui::sg_bfont (t_object *x, short size, short font) {}

void flext_gui::sg_key (t_object *x, short keyvalue) 
{ 
	flext_gui *g = thisObject(x);

	CBParams p(evKeyDown);
	p.pKey.k = keyvalue;
	p.pKey.a = 0;
	p.pKey.mod = 0;
	g->m_Method(p); 
}

void flext_gui::sg_enter (t_object *x) {}

void flext_gui::sg_clipregion (t_object *x, RgnHandle *rgn, short *result) {}

void flext_gui::sg_bidle (t_object *x) 
{ 
	flext_gui *g = thisObject(x);
	Point pnt; GetMouse(&pnt);

	CBParams p(evMotion);
	p.pMotion.x = pnt.h-g->XLo();
	p.pMotion.y = pnt.v-g->YLo();
	p.pMotion.mod = 0;
	
	if(p.pMotion.x != g->curx || p.pMotion.y != g->cury || p.pMotion.mod != g->curmod) {
		g->m_Method(p); 
		g->curx = p.pMotion.x;
		g->cury = p.pMotion.y;
		g->curmod = p.pMotion.mod;
	}
}

void flext_gui::g_Displace(int dx, int dy)
{
}

void flext_gui::g_Delete()
{
	objs->Clear();
}

void flext_gui::Update()
{
	box_ready(&thisHdr()->z_box);

	if(Group().Canv().Pre(XLo(),YLo())) 
		Group().Draw();
	Group().Canv().Post();
}

#endif // PD / MAXMSP

void flext_gui::m_Method(const CBParams &p)
{
/*
	switch(p.kind) {
		case evMotion: {
		//	if(!g->Selected() || mod) 
			post("Motion: x=%i y=%i m=%i",p.pMotion.x,p.pMotion.y,p.pMotion.mod); 
			break;
		}
		case evMouseDown: {
			post("MouseDown: x=%i y=%i b=%i m=%i",p.pMouseKey.x,p.pMouseKey.y,p.pMouseKey.b,p.pMouseKey.mod);
			break;
		}
		case evMouseUp: {
			post("MouseUp: x=%i y=%i b=%i m=%i",p.pMouseKey.x,p.pMouseKey.y,p.pMouseKey.b,p.pMouseKey.mod);
			break;
		}
		case evMouseWheel: {
			post("Mousewheel: x=%i y=%i m=%i d=%i",p.pMouseWheel.x,p.pMouseWheel.y,p.pMouseWheel.mod,p.pMouseWheel.delta);
			break;
		}
		case evKeyDown: {
			post("KeyDown: k=%i a=%i m=%i",p.pKey.k,p.pKey.a,p.pKey.mod);
			break;
		}
		case evKeyUp: {
			post("KeyUp: k=%i a=%i m=%i",p.pKey.k,p.pKey.a,p.pKey.mod);
			break;
		}
	}
*/
	if(!Selected() || p.kind != evMotion || p.kind != evMouseDown || p.kind != evMouseUp)
		Group().Method(*this,p);
  }

bool flext_gui::BindEvent(GuiSingle &o,bool (*cb)(flext_gui &o,GuiSingle &obj,const CBParams &p),int evs)
{
	if((evs&EventMask()) == evs) {
		o.AddEvent(evs,cb);
		return true;
	}
	else 
		// not all requested events supported
		return false;
}

void flext_gui::UnbindEvent(GuiSingle &o,bool (*cb)(flext_gui &o,GuiSingle &obj,const CBParams &p),int evs)
{
	o.RmvEvent(evs,cb);
}