aboutsummaryrefslogtreecommitdiff
path: root/src/menubutton.c
blob: 5ad264cc569ca74b2a3ea7594d67d24b5e5f9548 (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
/* menubutton widget for PD                                              *
 * Based on:
 * menubutton by Ben Bogart         
 * and button from GGEE by Guenter Geiger                                *

 * This program is distributed under the terms of the GNU General Public *
 * License                                                               *

 * menubutton is free software; you can redistribute it and/or modify         *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation; either version 2 of the License, or     *
 * (at your option) any later version.                                   *

 * menubutton is distributed in the hope that it will be useful,              *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          */




/* Append " x " to the following line to show debugging messages */
#define DEBUG(x)


#include <m_pd.h>
#include <g_canvas.h>
#include <stdio.h>
#include <string.h>


#define HANDLEWIDTH 10

#ifndef IOWIDTH 
#define IOWIDTH 4
#endif

static t_symbol* COMMA;


typedef struct _menubutton
{
     t_object x_obj;

     t_glist * x_glist;
     //t_outlet* out2;
     //int x_rect_width;
     //int x_rect_height;
     t_symbol*  x_sym;
	
     int x_height;
     int x_width;
	 
     int current_selection;
     int x_num_options;	 
     t_symbol* bg_color;
     t_symbol* fg_color;
     t_symbol* hi_color;
     t_symbol* co_color;
     int saveitems;
     int halign;
     
     t_symbol* send;
     t_symbol* receive;
     
     int send_set;
     int receive_set;
     
     t_symbol* s_empty;
     t_symbol* s_;
     //t_symbol* x_name;
	
     t_symbol** x_options;
     int        x_maxoptions;
     
     //t_symbol** current_options;

     int initialized; /* 1 when we are allowed to draw, 0 otherwise */
     int x_disabled; /* when disabled, graphical chosing is prohibited */
} t_menubutton;


#include "menubutton_w.h"



static void menubutton_output(t_menubutton* x)
{
  
  
  t_atom atoms[2];
  SETFLOAT(atoms,x->current_selection);
  SETSYMBOL(atoms+1,x->x_options[x->current_selection]);
  outlet_list(x->x_obj.ob_outlet, &s_list, 2, atoms);
  if ( x->send != x->s_empty && x->send->s_thing) pd_forwardmess(x->send->s_thing, 2,atoms);

}


static void menubutton_callback(t_menubutton* x, t_floatarg f)
{
  //DEBUG(post("output start");)
  x->current_selection= f;
 menubutton_output(x);
  
}
static void menubutton_save(t_gobj *z, t_binbuf *b)
{
        DEBUG(post("save start");)
    
	
    t_menubutton *x = (t_menubutton *)z;
    
    t_atom* creation = binbuf_getvec(x->x_obj.te_binbuf);
       
    
    t_symbol* send = x->s_empty;
    t_symbol* receive = x->s_empty;
    char buf[80];
    if ( x->send_set ) {
        atom_string(creation + 3, buf, 80);
        send = gensym(buf);
	}
	
    if ( x->receive_set ) {
		atom_string(creation + 4, buf, 80);
		receive = gensym(buf);
	}
    
	DEBUG(post("send: %s receive: %s",send->s_name,receive->s_name);)
	
    binbuf_addv(b, "ssiisiississssi", gensym("#X"), gensym("obj"),
                x->x_obj.te_xpix, x->x_obj.te_ypix ,  
                atom_getsymbol(creation),
                x->x_width, x->x_height, send,receive,x->saveitems,
                x->bg_color, x->fg_color,x->hi_color,x->co_color,x->halign);
                
	// Loop for menu items
	int i;
	if ( x->saveitems) {
		//binbuf_addv(b, "s", gensym(","));
		for(i=0 ; i<x->x_num_options ; i++)
		{
			binbuf_addv(b, "s", gensym(","));
			DEBUG(post("saving option: %s",x->x_options[i]->s_name);)
			binbuf_addv(b, "s", x->x_options[i]);
			
		}
	}	
    binbuf_addv(b, ";");
    
    DEBUG(post("save end");)
}


static void menubutton_clear(t_menubutton* x) {
	
	x->x_num_options = 0;
	x->current_selection = -1;
	if ( menubutton_w_is_visible(x) ) {
		menubutton_w_clear(x);
	}
}

static void menubutton_size(t_menubutton* x,t_symbol *s, int argc, t_atom *argv) {
	
	if (argc>2) argc =2;
	switch (argc) {
		case 2: if ( (argv+1)->a_type == A_FLOAT) x->x_height = atom_getfloat(argv+1);
		case 1: if ( argv->a_type == A_FLOAT) x->x_width = atom_getfloat(argv);
		break;
	}
	
	if ( x->x_width < 10) x->x_width = 10;
	if ( x->x_height < 10) x->x_height = 10;
	
	if ( menubutton_w_is_visible(x) ) {
		menubutton_w_resize(x);
	}
}



static void menubutton_add(t_menubutton* x, t_symbol *s, int argc, t_atom *argv) {
	
	int visible = menubutton_w_is_visible(x);
	
	// resize the options-array if it is too small
	if((argc + x->x_num_options) > x->x_maxoptions){
          
          x->x_options = resizebytes( x->x_options, x->x_maxoptions*sizeof(*(x->x_options)), 
				(argc + x->x_num_options+10)*sizeof(*(x->x_options)));
          x->x_maxoptions=argc + x->x_num_options+10;
    }
	
	int i;
	t_symbol* label;
	for  ( i=0;i<argc;i++) {
		if ((argv+i)->a_type==A_SYMBOL) {
			
		    label = atom_getsymbol(argv+i);
		    DEBUG(post("adding option: %s",label->s_name);)
			 x->x_options[x->x_num_options] = label;
			if ( visible ) {
				menubutton_w_additem( x,label,x->x_num_options);
			}
			x->x_num_options = x->x_num_options + 1;
		 }
		
	}
	
    
}



// function to change the colors

static void menubutton_colors(t_menubutton* x, t_symbol* s, int argc, t_atom* argv)
{
    
	DEBUG(post("bgcolour start");)
    
    if ( argc && argv->a_type==A_SYMBOL && atom_getsymbol(argv)==gensym("default")) {
		x->bg_color = gensym("grey90");
		x->fg_color = gensym("black");
		x->hi_color = gensym("grey95");
		x->co_color = gensym("black");
	} else {
    
		if (argc > 4) argc = 4;
		switch (argc) {
			case 4: if ((argv+3)->a_type==A_SYMBOL) x->co_color = atom_getsymbol(argv+3);
			case 3: if ((argv+2)->a_type==A_SYMBOL) x->hi_color = atom_getsymbol(argv+2);
			case 2: if ((argv+1)->a_type==A_SYMBOL) x->fg_color = atom_getsymbol(argv+1);
			case 1: if ((argv)->a_type==A_SYMBOL) x->bg_color = atom_getsymbol(argv);
			break;
		}
    }
    
	if(menubutton_w_is_visible(x)) {
		//sys_vgui(".x%x.c.s%x configure -background \"%s\" -foreground \"%s\"\n", x->x_glist, x, x->bg_color->s_name,x->fg_color->s_name);
	    menubutton_w_apply_colors(x);
	}
}


static int menubutton_set_float(t_menubutton* x, t_floatarg item) {
	
	    
	int i=(int)item;
	if( (i < x->x_num_options) && (i >= 0)) {
		x->current_selection = i;
		if(menubutton_w_is_visible(x)) menubutton_w_text(x,x->x_options[x->current_selection]);
         return 1;
	} else {
         return 0;
   }
	
}

/* Function to select a menu option by inlet */
static void menubutton_float(t_menubutton* x, t_floatarg item)
{
	DEBUG(post("iselect start");)
        
	if ( menubutton_set_float(x,item) ) {
		menubutton_output(x);
	} else {
		pd_error(x,"menubutton: expecting value between 0 and %i",x->x_num_options);
	}

	DEBUG(post("iselect end");)
}

static int menubutton_set_symbol(t_menubutton* x, t_symbol *s) {
	

	int i;
        
	/* Compare inlet symbol to each option */
	for(i=0; i < x->x_num_options; i++) {
	  if(x->x_options[i]->s_name == s->s_name) {
		    x->current_selection = i;
			if(menubutton_w_is_visible(x)) menubutton_w_text(x,s);
	        return 1;
       }
	}
	return 0;
}


/* Function to choose value via symbol name */
static void menubutton_symbol(t_menubutton* x, t_symbol *s)
{
	if(menubutton_set_symbol(x,s)) {
	   menubutton_output(x);
  } else {
	   post("menubutton: '%s' is not an available option.", s->s_name);
  }
	
}

/* Function to choose value via name/index but without outputting it*/
static void menubutton_set(t_menubutton* x, t_symbol *S, int argc, t_atom*argv)
{
  if(!argc)return;

  if(argv->a_type==A_FLOAT) {
      menubutton_set_float(x,atom_getfloat(argv));
    } else if(argv->a_type==A_SYMBOL) {
		menubutton_set_symbol(x,atom_getsymbol(argv));
     } else {
      pd_error(x, "menubutton: can only 'set' symbols or floats");
    }
}






static t_class *menubutton_class;


static void menubutton_receive(t_menubutton* x, t_symbol* s) {
	if ( x->receive != x->s_empty ) pd_unbind(&x->x_obj.ob_pd, x->receive);
	if ( s == x->s_empty || s ==  x->s_) {
		x->receive = x->s_empty;
	} else {
		x->receive = s;
		pd_bind(&x->x_obj.ob_pd, x->receive);
	}
	
}

static void menubutton_send(t_menubutton* x, t_symbol* s) {

	if ( s == x->s_empty || s ==  x->s_ ) {
		x->send = x->s_empty;
	} else {
		x->send = s;
	}
	
	
	
}

static void menubutton_free(t_menubutton*x)
{
  if(x->x_options)freebytes(x->x_options, sizeof(t_symbol*)*x->x_maxoptions);
   pd_unbind(&x->x_obj.ob_pd, x->x_sym);
   
   if ( x->receive != x->s_empty ) pd_unbind(&x->x_obj.ob_pd, x->receive);
}

static void menubutton_saveitems( t_menubutton* x, t_float f) {
	x->saveitems = (f != 0);
}

static void menubutton_align( t_menubutton* x, t_float f) {
	if ( f > 0 ) {
		x->halign = 1;
	} else if ( f == 0) {
		x->halign = 0;
	} else {
		x->halign = -1;
	}
	if(menubutton_w_is_visible(x)) {
		//sys_vgui(".x%x.c.s%x configure -background \"%s\" -foreground \"%s\"\n", x->x_glist, x, x->bg_color->s_name,x->fg_color->s_name);
	    menubutton_w_set_align(x);
	}
}

static void *menubutton_new(t_symbol *s, int argc, t_atom *argv)
{
    DEBUG(post("menubutton new start");)

    t_menubutton *x = (t_menubutton *)pd_new(menubutton_class);
    int i;
	char buf[256];

    x->x_glist = (t_glist*)NULL;

    x->x_height = 25;
    x->current_selection = -1;

    x->x_maxoptions=10;
    x->x_options=(t_symbol**)getbytes(sizeof(t_symbol*)*x->x_maxoptions);
    //x->current_options=(t_symbol**)getbytes(sizeof(t_symbol*)*x->x_maxoptions);
    x->x_num_options = 0 ;
     //x->x_options[0] = gensym("");
     
    x->x_width = 124;
    x->x_height = 25;
    
    // These should match the default colors in menubutton_colors
    x->bg_color = gensym("grey90");
    x->fg_color = gensym("black");
    x->hi_color = gensym("grey95");
    x->co_color = gensym("black");
    x->saveitems = 0;
    x->halign = -1;
    
    x->s_empty = gensym("empty");
    x->s_ = gensym("");
    
    x->send = x->s_empty;
    x->receive = x->s_empty;
    x->send_set =0;
    x->receive_set =0;
    
    x->initialized=0;

    x->x_disabled=0;
    
    
    int conf_argc;
    t_atom* item_argv;
    int item_argc;
    
    // loop through arguments and search for a comma
    for ( conf_argc = 0; conf_argc < argc; conf_argc++) {
		if ((argv+conf_argc)->a_type == A_SYMBOL && atom_getsymbol(argv+conf_argc) == COMMA) {
			break;
		}
	}
	
	
	
	DEBUG(post("conf_argc: %i", conf_argc);)
	item_argc = argc - (conf_argc + 1); // The +1 is to skip the comma
	if (item_argc < 0) item_argc = 0;
	item_argv = argv + conf_argc + 1; // Point to the start of the items
	DEBUG(post("item_argc: %i", item_argc);)
	
    
    // The maximum number of configuration arguments is 10
    if (conf_argc > 10) conf_argc = 10;
    
    switch(conf_argc){ 
	case 10: if ((argv+9)->a_type == A_FLOAT) x->halign = atom_getfloat(argv+9);	
    case 9: if ((argv+8)->a_type == A_SYMBOL) x->co_color = atom_getsymbol(argv+8);
    case 8: if ((argv+7)->a_type==A_SYMBOL) x->hi_color = atom_getsymbol(argv+7);
    case 7: if ((argv+6)->a_type==A_SYMBOL) x->fg_color = atom_getsymbol(argv+6);
    case 6: if ((argv+5)->a_type==A_SYMBOL) x->bg_color = atom_getsymbol(argv+5);
    case 5: if ((argv+4)->a_type==A_FLOAT) x->saveitems = atom_getfloat(argv+4);
    case 4: if ((argv+3)->a_type==A_SYMBOL) {
		x->receive = atom_getsymbol(argv+3);
		x->receive_set =1;
		}
	case 3: if ((argv+2)->a_type==A_SYMBOL) {
		x->send = atom_getsymbol(argv+2);
		x->send_set =1;
		}
	case 2: if ((argv+1)->a_type==A_FLOAT) x->x_height = atom_getfloat(argv+1);  
	case 1: if ((argv)->a_type==A_FLOAT) x->x_width = atom_getfloat(argv);  
      break;
    }

   DEBUG(post("send: %s receive: %s",x->send->s_name,x->receive->s_name);)
   
   
   if ( x->x_width < 10) x->x_width = 10;
   if ( x->x_height < 10) x->x_height = 10;
   
   // Bind receiver
   if ( x->receive != x->s_empty ) pd_bind(&x->x_obj.ob_pd, x->receive);


      /* Bind the recieve "menubutton%p" to the widget outlet*/
      sprintf(buf,"menubutton%p",x);
      x->x_sym = gensym(buf);
      pd_bind(&x->x_obj.ob_pd, x->x_sym);

      /* define proc in tcl/tk where "menubutton%p" is the receive, "callback" is the method, and "$index" is an argument. */
    sys_vgui("proc select%x {index} {\n pd [concat menubutton%p callback $index \\;]\n }\n",x,x); 


    outlet_new(&x->x_obj, &s_symbol);



    // PARSE ITEMS ALONG COMMAS
    
   unsigned int buffer_size = 0;
   char buffer[MAXPDSTRING];
   buffer[0] = '\0';
   char* bp = buffer;
   t_atom tempatom; 
   int word_length;
   	  
	i = 0;
	while (item_argc && i <= item_argc) {
	   if (((item_argv+i)->a_type == A_SYMBOL && atom_getsymbol(item_argv+i) == COMMA)|| i == item_argc ) {
			
			if ( buffer_size ) {
				SETSYMBOL(&tempatom,gensym(buffer));
				menubutton_add(x,&s_list,1,&tempatom);
				DEBUG(post("buffer: %s",buffer);)
			}
			buffer_size = 0;
			bp = buffer; 
			buffer[0] = '\0';
			
		} else {
			if ( buffer_size > 0) {
			  // Add a space separator
			  *bp = ' ';
			  bp++;
			  *bp = '\0';
			  buffer_size++;
			}
			atom_string(item_argv+i, bp , MAXPDSTRING-buffer_size);
			word_length = strlen(bp);
			DEBUG(post("word_length: %i",word_length);)
			DEBUG(post("bp: %s",bp);)
			bp = bp + word_length;
			buffer_size = buffer_size + word_length;
			}
	   i++;
	}
		
	//menubutton_add(x,&s_list,item_argc,item_argv);
	


DEBUG(post("menubutton new end");)

    return (x);
}

void menubutton_setup(void) {

    DEBUG(post("setup start");)

    COMMA = gensym(",");


      menubutton_class = class_new(gensym("menubutton"), (t_newmethod)menubutton_new, (t_method)menubutton_free,
				sizeof(t_menubutton),0,A_GIMME,0);
				
	class_addmethod(menubutton_class, (t_method)menubutton_callback,
								  gensym("callback"),A_DEFFLOAT,0);
								  
	class_addmethod(menubutton_class, (t_method)menubutton_send,
								  gensym("send"),A_DEFSYMBOL,0);
								  
	class_addmethod(menubutton_class, (t_method)menubutton_receive,
								  gensym("receive"),A_DEFSYMBOL,0);
								  
	class_addmethod(menubutton_class, (t_method)menubutton_saveitems,
								  gensym("saveitems"),A_FLOAT,0);
				  
	class_addmethod(menubutton_class, (t_method)menubutton_add,
								  gensym("add"), A_GIMME,0);
	
	class_addmethod(menubutton_class, (t_method)menubutton_clear,
								  gensym("clear"), 0);
								  
	class_addmethod(menubutton_class, (t_method)menubutton_colors,
								  gensym("colors"),A_GIMME,0);
								 
    class_addmethod(menubutton_class, (t_method)menubutton_size,
								  gensym("size"),A_GIMME,0);

        class_addmethod(menubutton_class, (t_method)menubutton_set,
                                    gensym("set"),A_GIMME,0);
                                    
class_addmethod(menubutton_class, (t_method)menubutton_align,
                                    gensym("align"),A_FLOAT,0);


	class_addsymbol(menubutton_class, (t_method)menubutton_symbol);

	class_addfloat(menubutton_class, (t_method)menubutton_float);



    class_setwidget(menubutton_class,&menubutton_widgetbehavior);

    class_setsavefn(menubutton_class,&menubutton_save);


	post("menubutton v0.12 tof, based on popup by Ben Bogart and button by ggee");
}