aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas O Fredericks <mrtof@users.sourceforge.net>2009-12-20 18:48:29 +0000
committerThomas O Fredericks <mrtof@users.sourceforge.net>2009-12-20 18:48:29 +0000
commit53aae6f1dc9497e94f384b1a7a38ca1876bba923 (patch)
treec1e42bb12e70bf4ccc84bf872a2d11a04c08fe31
parentc833dce1781c722cd9bf14f08cef87056ebdf280 (diff)
Added support for spaces and alignement in menubutton
svn path=/trunk/externals/tof/; revision=12841
-rw-r--r--help/menubutton-help.pd156
-rw-r--r--src/menubutton.c106
-rw-r--r--src/menubutton_w.h52
3 files changed, 238 insertions, 76 deletions
diff --git a/help/menubutton-help.pd b/help/menubutton-help.pd
index 538514f..367577b 100644
--- a/help/menubutton-help.pd
+++ b/help/menubutton-help.pd
@@ -1,72 +1,104 @@
-#N canvas 1323 101 834 600 10;
-#X obj 54 358 tof/menubutton 132 22 empty empty 1 #000000 #ffffff #ff0000
-#3a12ff a b c d e f;
-#X msg 80 33 add a b c d e f;
-#X msg 52 11 clear;
-#X msg 142 124 saveitems \$1;
-#X obj 142 106 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0
+#N canvas 1181 230 1012 664 10;
+#X obj 26 538 tof/menubutton 132 22 \$0s \$0r 1 green red orange purple
+0 \, a \, b \, c \, d \, e \, f \, with spaces;
+#X msg 43 66 add a b c d e f;
+#X msg 28 46 clear;
+#X msg 118 159 saveitems \$1;
+#X obj 118 132 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0
1;
-#X floatatom 208 509 5 0 0 0 - - -;
-#X text 95 10 Clear menu items;
-#X text 184 31 Add menu items;
-#X obj 59 477 r \$0s;
-#X obj 208 538 send \$0r;
-#X text 188 368 [tof/menubutton] creation arguments:;
-#X text 415 373 1: width;
-#X text 415 387 2: height;
-#X text 415 400 3: send name;
-#X text 415 412 4: receive name;
-#X text 229 111 Set the saving of the menu items in the patch. Off
+#X floatatom 392 560 5 0 0 0 - - -;
+#X text 71 45 Clear menu items;
+#X text 147 64 Add menu items;
+#X obj 242 540 r \$0s;
+#X obj 392 589 send \$0r;
+#X text 542 335 1: width;
+#X text 542 349 2: height;
+#X text 542 362 3: send name;
+#X text 542 374 4: receive name;
+#X text 139 130 Set the saving of the menu items in the patch. Off
by default.;
-#X text 313 324 To move the menubutton \, switch to edit mode and draw
-a selection rectangle around it. A blue handle will appear. Drag this
-handle to move the menubutton.;
-#X floatatom 264 243 5 0 0 0 - - -;
-#X obj 54 405 unpack f s;
-#X floatatom 54 428 5 0 0 0 - - -;
-#X symbolatom 113 426 10 0 0 0 - - -;
-#X floatatom 59 544 5 0 0 0 - - -;
-#X symbolatom 116 542 10 0 0 0 - - -;
-#X obj 59 513 unpack f s;
-#X text 304 242 Select and output a menu item;
-#X msg 289 268 set b;
-#X text 338 270 Select an item without outputing it;
-#X msg 308 292 set 2;
-#X text 459 174 values can be tk colors(red \, blue \, etc) or html
+#X floatatom 188 303 5 0 0 0 - - -;
+#X obj 26 574 unpack f s;
+#X floatatom 26 597 5 0 0 0 - - -;
+#X symbolatom 85 595 10 0 0 0 - - -;
+#X floatatom 243 595 5 0 0 0 - - -;
+#X symbolatom 300 593 10 0 0 0 - - -;
+#X obj 243 564 unpack f s;
+#X msg 189 327 set b;
+#X msg 189 351 set 2;
+#X text 124 193 values can be tk colors(red \, blue \, etc) or html
colors (#ffffff);
-#X msg 260 215 colors default;
-#X text 199 147 colors: background foreground highlight contour;
-#X text 415 422 5: save menu items;
-#X text 414 434 6: background color;
-#X text 414 447 7: foreground color;
-#X text 414 458 8: highlight color;
-#X text 413 480 10+: menu items;
-#X text 415 469 9: contour color;
-#X msg 202 170 colors green red orange purple;
-#X msg 222 195 colors #000000 #ffffff #ff0000 #3a12ff;
-#X text 182 55 Set its size;
-#X msg 103 56 size 102 32;
-#X text 202 77 Set its size;
-#X msg 123 78 size 132 22;
-#N canvas 0 0 742 557 test 1;
-#X coords 0 -1 1 1 300 200 1;
-#X restore 556 538 pd test;
-#X connect 0 0 18 0;
+#X msg 184 275 colors default;
+#X text 125 180 colors: background foreground highlight contour;
+#X msg 126 230 colors green red orange purple;
+#X msg 146 255 colors #000000 #ffffff #ff0000 #3a12ff;
+#X text 145 88 Set its size;
+#X msg 66 89 size 102 32;
+#X text 165 110 Set its size;
+#X msg 86 111 size 132 22;
+#X msg 186 421 align -1;
+#X msg 188 401 align 0;
+#X msg 187 380 align 1;
+#X obj 542 244 cnv 15 400 60 empty empty empty 20 12 0 14 -4034 -66577
+0;
+#X text 547 252 To move the [menubutton] \, switch to edit mode and
+draw a selection rectangle around it. The [menubutton] will temporarly
+be disabled allowing you to drag it.;
+#X obj 540 315 cnv 15 400 15 empty empty empty 20 12 0 14 -257985 -66577
+0;
+#X text 543 313 Creation arguments:;
+#X text 542 385 5: save menu items flag;
+#X msg 183 510 add \$1;
+#X obj 183 487 cyclone/tosymbol;
+#X text 543 422 8: highlight color (#000000 html or tk colors);
+#X text 543 411 7: foreground color (#000000 hmtl or tk colors);
+#X text 541 398 6: background color (#000000 html or tk colors);
+#X text 542 435 9: contour color (#000000 html or tk colors);
+#X text 541 448 10: horizontal alignement;
+#X text 537 463 \, : anything after a comma will be parsed as the menu
+items. Each menu item must be separated by a comma.;
+#X text 234 324 Select an item by value without outputing it;
+#X text 229 348 Select an item by index without outputing it;
+#X text 242 380 align right;
+#X text 245 398 align center;
+#X text 249 418 align left;
+#X text 228 302 Select and output a menu item by index;
+#X obj 28 9 cnv 15 400 30 empty empty empty 20 12 0 14 -260097 -66577
+0;
+#X text 30 8 description: a button with a popup menu;
+#X text 31 21 tags: ui;
+#X msg 183 464 with spaces;
+#X text 105 572 output: index value;
+#X text 180 444 Adding items with spaces;
+#X text 339 445;
+#X obj 540 541 cnv 15 400 30 empty empty empty 20 12 0 14 -232576 -66577
+0;
+#X text 544 519 The [menubutton] at left was created with the following:
+;
+#X text 546 539 [tof/menubutton 132 22 \$0s \$0r 1 green red orange
+purple 0 \, a \, b \, c \, d \, e \, f \, with spaces];
+#X connect 0 0 16 0;
#X connect 1 0 0 0;
#X connect 2 0 0 0;
#X connect 3 0 0 0;
#X connect 4 0 3 0;
#X connect 5 0 9 0;
-#X connect 8 0 23 0;
-#X connect 17 0 0 0;
-#X connect 18 0 19 0;
-#X connect 18 1 20 0;
-#X connect 23 0 21 0;
-#X connect 23 1 22 0;
+#X connect 8 0 21 0;
+#X connect 15 0 0 0;
+#X connect 16 0 17 0;
+#X connect 16 1 18 0;
+#X connect 21 0 19 0;
+#X connect 21 1 20 0;
+#X connect 22 0 0 0;
+#X connect 23 0 0 0;
#X connect 25 0 0 0;
#X connect 27 0 0 0;
-#X connect 29 0 0 0;
-#X connect 37 0 0 0;
-#X connect 38 0 0 0;
-#X connect 40 0 0 0;
-#X connect 42 0 0 0;
+#X connect 28 0 0 0;
+#X connect 30 0 0 0;
+#X connect 32 0 0 0;
+#X connect 33 0 0 0;
+#X connect 34 0 0 0;
+#X connect 35 0 0 0;
+#X connect 41 0 0 0;
+#X connect 42 0 41 0;
+#X connect 58 0 42 0;
diff --git a/src/menubutton.c b/src/menubutton.c
index a96d81d..5ad264c 100644
--- a/src/menubutton.c
+++ b/src/menubutton.c
@@ -35,7 +35,7 @@
#define IOWIDTH 4
#endif
-
+static t_symbol* COMMA;
typedef struct _menubutton
@@ -58,6 +58,7 @@ typedef struct _menubutton
t_symbol* hi_color;
t_symbol* co_color;
int saveitems;
+ int halign;
t_symbol* send;
t_symbol* receive;
@@ -128,19 +129,22 @@ static void menubutton_save(t_gobj *z, t_binbuf *b)
DEBUG(post("send: %s receive: %s",send->s_name,receive->s_name);)
- binbuf_addv(b, "ssiisiississss", gensym("#X"), gensym("obj"),
+ 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->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, ";");
@@ -354,6 +358,19 @@ 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)
{
@@ -383,6 +400,7 @@ static void *menubutton_new(t_symbol *s, int argc, t_atom *argv)
x->hi_color = gensym("grey95");
x->co_color = gensym("black");
x->saveitems = 0;
+ x->halign = -1;
x->s_empty = gensym("empty");
x->s_ = gensym("");
@@ -396,10 +414,33 @@ static void *menubutton_new(t_symbol *s, int argc, t_atom *argv)
x->x_disabled=0;
- int conf_argc = argc;
- if (conf_argc > 9) conf_argc = 9;
+
+ 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 9: if ((argv+8)->a_type==A_SYMBOL) x->co_color = atom_getsymbol(argv+8);
+ 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);
@@ -439,7 +480,49 @@ static void *menubutton_new(t_symbol *s, int argc, t_atom *argv)
outlet_new(&x->x_obj, &s_symbol);
- if (argc > 9) menubutton_add(x,&s_list,argc-9,argv+9);
+
+ // 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");)
@@ -451,6 +534,9 @@ 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);
@@ -480,6 +566,10 @@ void menubutton_setup(void) {
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);
@@ -492,7 +582,7 @@ void menubutton_setup(void) {
class_setsavefn(menubutton_class,&menubutton_save);
- post("menubutton v0.01 tof, based on popup by Ben Bogart and button by ggee");
+ post("menubutton v0.12 tof, based on popup by Ben Bogart and button by ggee");
}
diff --git a/src/menubutton_w.h b/src/menubutton_w.h
index b519463..f683c4a 100644
--- a/src/menubutton_w.h
+++ b/src/menubutton_w.h
@@ -36,6 +36,20 @@ static void menubutton_w_additem(t_menubutton* x, t_symbol *label, int i) {
}
+//-anchor %s
+
+static void menubutton_w_set_align(t_menubutton* x) {
+
+ if ( x->halign == -1 ) {
+ sys_vgui(".x%x.c.s%x configure -anchor w\n", x->x_glist, x);
+ } else if ( x->halign == 0 ) {
+ sys_vgui(".x%x.c.s%x configure -anchor center\n", x->x_glist, x);
+ } else {
+ sys_vgui(".x%x.c.s%x configure -anchor e\n", x->x_glist, x);
+ }
+
+}
+
static void menubutton_w_apply_colors(t_menubutton* x) {
sys_vgui(".x%x.c.s%x configure -background \"%s\" -foreground \"%s\" -activeforeground \"%s\" -activebackground \"%s\"\n",
@@ -104,19 +118,33 @@ static void menubutton_w_draw_inlets(t_menubutton *x, t_glist *glist, int draw,
static void menubutton_w_draw_handle(t_menubutton *x, t_glist *glist, int draw) {
DEBUG(post("draw_handle start: %i",draw);)
- int onset = text_xpix(&x->x_obj, glist) + (x->x_width) ;
-
+ //int onset = text_xpix(&x->x_obj, glist) + (x->x_width) ;
+ int onset = text_xpix(&x->x_obj, glist) ;
+
if (draw==CREATE) {
+ sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xhandle -outline blue -fill blue\n",
+ glist_getcanvas(glist),
+ onset, text_ypix(&x->x_obj, glist) ,
+ onset + (x->x_width) , text_ypix(&x->x_obj, glist) + x->x_height ,
+ x);
+ /*
sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xhandle -outline blue -fill blue\n",
glist_getcanvas(glist),
onset, text_ypix(&x->x_obj, glist) ,
onset + HANDLEWIDTH, text_ypix(&x->x_obj, glist) + x->x_height ,
x);
+ */
} else if (draw==UPDATE) {
+ sys_vgui(".x%x.c coords %xhandle %d %d %d %d\n",
+ glist_getcanvas(glist), x,
+ onset, text_ypix(&x->x_obj, glist) ,
+ onset + (x->x_width) , text_ypix(&x->x_obj, glist) + x->x_height );
+ /*
sys_vgui(".x%x.c coords %xhandle %d %d %d %d\n",
glist_getcanvas(glist), x,
onset, text_ypix(&x->x_obj, glist) ,
onset + HANDLEWIDTH, text_ypix(&x->x_obj, glist) + x->x_height );
+ */
} else {
sys_vgui(".x%x.c delete %xhandle\n",glist_getcanvas(glist),x,0);
}
@@ -155,10 +183,18 @@ static void menubutton_w_create_widget(t_menubutton *x)
// Create menubutton and menu
- sys_vgui("set %xw .x%x.c.s%x ; menubutton $%xw -justify left -relief flat -anchor e -indicatoron 0 -text \"%s\" -direction flush -menu $%xw.menu ; menu $%xw.menu -relief solid -tearoff 0 \n",
+ sys_vgui("set %xw .x%x.c.s%x ; menubutton $%xw -justify left -relief flat -indicatoron 0 -text \"%s\" -direction flush -menu $%xw.menu ; menu $%xw.menu -relief solid -tearoff 0 \n",
x,x->x_glist,x,x,temp_name->s_name,x,x);
+ menubutton_w_set_align(x);
+
menubutton_w_apply_colors(x);
+
+ // Add a binding
+ //post("Trying to bind");
+ //sys_vgui("bind Menubutton <ButtonPress-1> {} \n", x);
+ //sys_vgui("bind $%xw <ButtonPress-1> { } \n", x);
+
for(i=0 ; i<x->x_num_options ; i++)
{
// Add menu itmes
@@ -265,7 +301,8 @@ static void menubutton_w_getrect(t_gobj *z, t_glist *owner,
height = s->x_height;
*xp1 = text_xpix(&s->x_obj, owner) ;
*yp1 = text_ypix(&s->x_obj, owner) ;
- *xp2 = text_xpix(&s->x_obj, owner) + width + HANDLEWIDTH ;
+ //*xp2 = text_xpix(&s->x_obj, owner) + width + HANDLEWIDTH ;
+ *xp2 = text_xpix(&s->x_obj, owner) + width ;
*yp2 = text_ypix(&s->x_obj, owner) + height ;
// DEBUG(post("getrect end");)
@@ -292,6 +329,7 @@ static void menubutton_w_select(t_gobj *z, t_glist *glist, int state)
{
DEBUG(post("select start");)
+
t_menubutton *x = (t_menubutton *)z;
if(x->initialized){
/*
@@ -301,12 +339,14 @@ static void menubutton_w_select(t_gobj *z, t_glist *glist, int state)
if (state) {
menubutton_w_draw_handle(x,glist,CREATE);
menubutton_w_draw_inlets(x, glist, CREATE, 1,1);
- menubutton_w_disable(x,1);
+ //menubutton_w_disable(x,1);
+ menubutton_w_drawme(x, glist, DESTROY);
}
else {
- menubutton_w_disable(x,0);
+ //menubutton_w_disable(x,0);
menubutton_w_draw_handle(x,glist,DESTROY);
menubutton_w_draw_inlets(x,glist,DESTROY,1,1);
+ menubutton_w_drawme(x, glist, CREATE);
}
}