From 2a320ac2fbd246be7b0844fe00b543bcd1bae271 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 21 Nov 2007 22:19:12 +0000 Subject: - created functions to generate all the various Tk IDs and tags and ported text.c to use those functions - started sketch for checkbutton.c svn path=/trunk/externals/tkwidgets/; revision=9020 --- checkbutton-help.pd | 2 + checkbutton.c | 167 +++++++++++++++++++++++++++++++++++ shared/tkwidgets.c | 111 +++++++++++++++++++---- shared/tkwidgets.h | 49 ++++++---- text.c | 250 ++++++++++++++++++++++++---------------------------- 5 files changed, 409 insertions(+), 170 deletions(-) create mode 100644 checkbutton-help.pd create mode 100644 checkbutton.c diff --git a/checkbutton-help.pd b/checkbutton-help.pd new file mode 100644 index 0000000..c4e0b7e --- /dev/null +++ b/checkbutton-help.pd @@ -0,0 +1,2 @@ +#N canvas 351 228 450 300 10; +#X obj 178 105 checkbutton; diff --git a/checkbutton.c b/checkbutton.c new file mode 100644 index 0000000..371ff48 --- /dev/null +++ b/checkbutton.c @@ -0,0 +1,167 @@ +/* [checkbutton] object for dislaying a check box + + Copyright (C) 2002-2004 Guenter Geiger + Copyright (C) 2007 Hans-Christoph Steiner + + This program 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. + + This program 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. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + This is part of the tkwidgets library for Pd. + +*/ + +#include "shared/tkwidgets.h" + +/* ------------------------ class variables --------------------------------- */ + +static t_class *checkbutton_class; +static t_widgetbehavior checkbutton_widgetbehavior; + +typedef struct _checkbutton +{ + t_object x_obj; + t_glist* x_glist; + int x_width; + int x_height; + + /* IDs for Tk widgets */ + t_symbol* tcl_namespace; + t_symbol* receive_name; /* name to bind to to receive callbacks */ + t_symbol* canvas_id; + t_symbol* widget_id; + t_symbol* handle_id; + t_symbol* all_tag; + + t_outlet* x_data_outlet; + t_outlet* x_status_outlet; +} t_checkbutton; + +static char *checkbutton_tk_options[] = { + "activebackground", + "activeforeground", + "anchor", + "background", + "bitmap", + "borderwidth", + "command", + "compound", + "cursor", + "disabledforeground", + "font", + "foreground", + "height", + "highlightbackground", + "highlightcolor", + "highlightthickness", + "image", + "indicatoron", + "justify", + "offrelief", + "offvalue", + "onvalue", + "overrelief", + "padx", + "pady", + "relief", + "selectcolor", + "selectimage", + "state", + "takefocus", + "text", + "textvariable", + "underline", + "variable", + "width", + "wraplength" +}; + +/* -------------------- widget helper functions------------------------------ */ + +static void checkbutton_drawme(t_checkbutton *x, t_glist *glist) +{ + sys_vgui("checkbutton %s\n", x->widget_id); +} + + +static void checkbutton_erase(t_checkbutton* x,t_glist* glist) +{ + sys_vgui("%s delete %s\n", x->canvas_id, x->widget_id); +} + + + +/* --------------------- checkbutton widgetbehaviour ------------------------ */ +static void checkbutton_getrect(t_gobj *z, t_glist *glist, + int *xp1, int *yp1, int *xp2, int *yp2) +{ + t_checkbutton* x = (t_checkbutton*)z; + + *xp1 = text_xpix(&x->x_obj, glist); + *yp1 = text_ypix(&x->x_obj, glist); + *xp2 = text_xpix(&x->x_obj, glist) + x->x_width; + *yp2 = text_ypix(&x->x_obj, glist) + x->x_height; +} + +static void checkbutton_delete(t_gobj *z, t_glist *glist) +{ + t_text *x = (t_text *)z; + canvas_deletelinesfor(glist_getcanvas(glist), x); +} + +static void checkbutton_vis(t_gobj *z, t_glist *glist, int vis) +{ + t_checkbutton* s = (t_checkbutton*)z; + if (vis) + checkbutton_drawme(s, glist); + else + checkbutton_erase(s, glist); +} + +/* --------------------------- standard class functions --------------------- */ + +static void checkbutton_free(t_checkbutton *x) +{ + pd_unbind(&x->x_obj.ob_pd, x->receive_name); +} + +static void *checkbutton_new(t_symbol* s, int argc, t_atom *argv) +{ + t_checkbutton *x = (t_checkbutton *)pd_new(checkbutton_class); + + x->x_glist = (t_glist*) canvas_getcurrent(); + + x->x_width = 15; + x->x_height = 15; + + outlet_new(&x->x_obj, &s_float); + return (x); +} + +void checkbutton_setup(void) +{ + checkbutton_class = class_new(gensym("checkbutton"), (t_newmethod)checkbutton_new, + (t_method)checkbutton_free, + sizeof(t_checkbutton), 0, A_GIMME,0); + + checkbutton_widgetbehavior.w_getrectfn = checkbutton_getrect; + checkbutton_widgetbehavior.w_displacefn = NULL; + checkbutton_widgetbehavior.w_selectfn = NULL; + checkbutton_widgetbehavior.w_activatefn = NULL; + checkbutton_widgetbehavior.w_deletefn = checkbutton_delete; + checkbutton_widgetbehavior.w_visfn = checkbutton_vis; + checkbutton_widgetbehavior.w_clickfn = NULL; + class_setwidget(checkbutton_class, &checkbutton_widgetbehavior); +} + + diff --git a/shared/tkwidgets.c b/shared/tkwidgets.c index 587f2d8..fd7f9c3 100644 --- a/shared/tkwidgets.c +++ b/shared/tkwidgets.c @@ -33,45 +33,120 @@ t_symbol *canvas_getname(t_canvas *canvas) } -void query_options(t_symbol *receive_name, char *widget_id, int argc, char** argv) +void tkwidgets_query_options(t_symbol *receive_name, t_symbol *widget_id, + int argc, char** argv) { int i; for(i = 0; i < argc; i++) sys_vgui("pd [concat %s query_callback %s [%s cget -%s] \\;]\n", - receive_name->s_name, argv[i], widget_id, argv[i]); + receive_name->s_name, argv[i], widget_id->s_name, argv[i]); } -/* -I think I'll probably have to follow Krzsztof and make structs to make this work -tkwidgets_setcallbackname(void *x, char *widget_name) +void tkwidgets_restore_options(t_symbol *receive_name, t_symbol *widget_id, + int argc, char** argv) +{ + int i; + for(i = 0; i < argc; i++) + { + // TODO parse out -flags and values, and set them here: + sys_vgui("%s configure %s\n", + widget_id->s_name, argv[i]); + } +} + +void tkwidgets_set_ids(t_object *x, t_tkwidgets *tkw, t_canvas *canvas) +{ + + tkw->canvas = canvas; +} + + +t_symbol* tkwidgets_gen_tcl_namespace(t_object* x, t_symbol* widget_name) { char buf[MAXPDSTRING]; + sprintf(buf,"%s%lx", widget_name->s_name, (long unsigned int)x); + return gensym(buf); +} - sprintf(buf,"%s%lx", widget_name, (long unsigned int)x); - x->tcl_namespace = getbytes(strlen(buf)); - strcpy(x->tcl_namespace, buf); +t_symbol* tkwidgets_gen_callback_name(t_symbol* tcl_namespace) +{ + char buf[MAXPDSTRING]; + sprintf(buf,"#%s", tcl_namespace->s_name); + return gensym(buf); +} - sprintf(buf,"#%s", x->tcl_namespace); - x->receive_name = gensym(buf); - pd_bind(&x->x_obj.ob_pd, x->receive_name); +t_symbol* tkwidgets_gen_canvas_id(t_canvas* canvas) +{ + char buf[MAXPDSTRING]; + sprintf(buf,".x%lx.c", (long unsigned int) canvas); + return gensym(buf); } -*/ -void draw_inlets(t_object *x, t_glist *glist, int firsttime, - int total_inlets, int total_outlets) +t_symbol* tkwidgets_gen_frame_id(t_object* x, t_symbol* canvas_id) { - // TODO perhaps I should try to use glist_drawiofor() from g_text.c + char buf[MAXPDSTRING]; + sprintf(buf,"%s.frame%lx", canvas_id->s_name, (long unsigned int)x); + return gensym(buf); } +t_symbol* tkwidgets_gen_widget_id(t_object* x, t_symbol* parent_id) +{ + char buf[MAXPDSTRING]; + sprintf(buf,"%s.widget%lx", parent_id->s_name, (long unsigned int)x); + return gensym(buf); +} +t_symbol* tkwidgets_gen_window_id(t_object* x, t_symbol* canvas_id) +{ + char buf[MAXPDSTRING]; + sprintf(buf,"%s.window%lx", canvas_id->s_name, (long unsigned int)x); + return gensym(buf); +} -void draw_handle() +t_symbol* tkwidgets_gen_handle_id(t_object *x, t_symbol* canvas_id) { - // TODO draw resize handle when selected in editmode + char buf[MAXPDSTRING]; + sprintf(buf,"%s.handle%lx", canvas_id->s_name, (long unsigned int)x); + return gensym(buf); +} + +t_symbol* tkwidgets_gen_scrollbar_id(t_object *x, t_symbol* frame_id) +{ + char buf[MAXPDSTRING]; + sprintf(buf,"%s.scrollbar%lx", frame_id->s_name, (long unsigned int)x); + return gensym(buf); +} + +t_symbol* tkwidgets_gen_all_tag(t_object *x) +{ + char buf[MAXPDSTRING]; + sprintf(buf,"all%lx", (long unsigned int)x); + return gensym(buf); +} + +void tkwidgets_draw_inlets(t_object *x, t_glist *glist, + int total_inlets, int total_outlets) +{ + // TODO perhaps I should try to use glist_drawiofor() from g_text.c } +void tkwidgets_draw_handle() +{ + // TODO draw resize handle when selected in editmode +} -void draw_resize_window() +void tkwidgets_draw_resize_window() { // TODO draw the resize window while resizing } + + +/* +void query_options() +{ + +} + + + +*/ diff --git a/shared/tkwidgets.h b/shared/tkwidgets.h index b7fdc00..aa1d097 100644 --- a/shared/tkwidgets.h +++ b/shared/tkwidgets.h @@ -27,6 +27,7 @@ #include "m_imp.h" #include "g_canvas.h" +/* I don't know what these do, but they seem to be everywhere */ #ifdef _MSC_VER #pragma warning( disable : 4244 ) #pragma warning( disable : 4305 ) @@ -40,30 +41,48 @@ /* sketch for a common struct */ typedef struct _tkwidgets { - t_symbol *canvas_id; /* the canvas that is showing this widget */ - t_symbol *receive_name; /* name to bind to, to receive callbacks */ - t_symbol *window_id; /* the window that contains the widget */ - t_symbol *widget_id; /* the core widget */ - t_symbol *all_tag; /* the tag for moving/deleting everything */ - int resizing; /* flag to tell when being resized */ - int selected; /* flag for when widget is selected */ + t_canvas* canvas; /* canvas/glist this widget is currently drawn in*/ + t_glist* glist; /* glist that owns this widget */ + t_binbuf* options_binbuf;/* binbuf to save options state in */ + t_symbol* receive_name; /* name to bind to, to receive callbacks */ + t_symbol* tcl_namespace; /* namespace to prevent name collisions */ + t_symbol* canvas_id; /* the canvas that is showing this widget */ + t_symbol* frame_id; /* the frame around the widget and supporters */ + t_symbol* window_id; /* the window that contains the widget */ + t_symbol* widget_id; /* the core widget */ + t_symbol* handle_id; /* the resizing handle */ + t_symbol* all_tag; /* the tag for moving/deleting everything */ + int resizing; /* flag to tell when being resized */ + int selected; /* flag for when widget is selected */ } t_tkwidgets; - - /* query a tk widget for the state of all its options */ -void query_options(t_symbol *receive_name, char *widget_id, int argc, char** argv); +void tkwidgets_query_options(t_symbol* receive_name, t_symbol *widget_id, + int argc, char** argv); +/* initialize things on new widget */ +void tkwidgets_new(t_tkwidgets* tkw); /* this should be part of the Pd API */ -t_symbol *canvas_getname(t_canvas *canvas); +t_symbol* canvas_getname(t_canvas *canvas); + void tkwidgets_setcallbackname(void *x, char *widget_name); +t_symbol* tkwidgets_gen_tcl_namespace(t_object* x, t_symbol* widget_name); +t_symbol* tkwidgets_gen_callback_name(t_symbol* tcl_namespace); +t_symbol* tkwidgets_gen_canvas_id(t_canvas* canvas); +t_symbol* tkwidgets_gen_frame_id(t_object* x, t_symbol* canvas_id); +t_symbol* tkwidgets_gen_widget_id(t_object* x, t_symbol* parent_id); +t_symbol* tkwidgets_gen_handle_id(t_object *x, t_symbol* parent_id); +t_symbol* tkwidgets_gen_window_tag(t_object* x, t_symbol* parent_id); +t_symbol* tkwidgets_gen_all_tag(t_object *x); + + // TODO perhaps I should try to use glist_drawiofor() from g_text.c -void draw_inlets(t_object *x, t_glist *glist, int firsttime, +void tkwidgets_draw_inlets(t_object *x, t_glist *glist, int total_inlets, int total_outlets); -void draw_handle(); // TODO draw resize handle when selected in editmode -void draw_resize_window(); // TODO draw the resize window while resizing +void tkwidgets_draw_handle(); // TODO draw resize handle when selected in editmode +void tkwidgets_draw_resize_window(); // TODO draw the resize window while resizing @@ -71,4 +90,4 @@ void draw_resize_window(); // TODO draw the resize window while resizing -#endif /* NOT g_TK_WIDGETS_H */ +#endif /* NOT __TK_WIDGETS_H */ diff --git a/text.c b/text.c index d420c6d..ddb9697 100644 --- a/text.c +++ b/text.c @@ -43,30 +43,28 @@ typedef struct _textwidget { - t_object x_obj; - t_canvas* x_canvas; - t_glist* x_glist; + t_object x_obj; + t_canvas* x_canvas; /* canvas/glist this widget is currently drawn in*/ + t_glist* x_glist; /* glist that owns this widget */ + t_binbuf* options_binbuf;/* binbuf to save options state in */ - t_symbol* receive_name; + int size_x; + int size_y; + int x_have_scrollbars; - t_binbuf* options_binbuf; - - int size_x; - int size_y; - int x_have_scrollbars; - - int x_resizing; - int x_selected; + int x_resizing; + int x_selected; /* IDs for Tk widgets */ - char* tcl_namespace; - char* canvas_id; - char* frame_id; - char* text_id; - char* scrollbar_id; - char* handle_id; - char* window_tag; - char* all_tag; + t_symbol* tcl_namespace; + t_symbol* receive_name; /* name to bind to to receive callbacks */ + t_symbol* canvas_id; + t_symbol* frame_id; + t_symbol* widget_id; + t_symbol* scrollbar_id; + t_symbol* handle_id; + t_symbol* window_id; + t_symbol* all_tag; t_outlet* x_data_outlet; t_outlet* x_status_outlet; @@ -113,7 +111,7 @@ static char *textwidget_tk_options[] = { }; -/* move these to tkwidgets.c */ +/* common symbols to preload */ static t_symbol *scrollbars_symbol; static t_symbol *size_symbol; static t_symbol *backspace_symbol; @@ -162,14 +160,14 @@ static void store_options(t_textwidget *x) { // TODO: only send if there is a value, not when blank sys_vgui("lappend ::%s::store_list -%s \n", - x->tcl_namespace, textwidget_tk_options[i]); + x->tcl_namespace->s_name, textwidget_tk_options[i]); sys_vgui("lappend ::%s::store_list [%s cget -%s] \n", - x->tcl_namespace, x->text_id, textwidget_tk_options[i]); + x->tcl_namespace->s_name, x->widget_id, textwidget_tk_options[i]); post("option %d: %s", i, textwidget_tk_options[i]); } sys_vgui("pd [concat %s store_callback $::%s::store_list \\;]\n", - x->receive_name->s_name, x->tcl_namespace); - sys_vgui("unset ::%s::store_list \n", x->tcl_namespace); + x->receive_name->s_name, x->tcl_namespace->s_name); + sys_vgui("unset ::%s::store_list \n", x->tcl_namespace->s_name); } static void restore_options(t_textwidget *x) @@ -177,42 +175,15 @@ static void restore_options(t_textwidget *x) // TODO restore options from x->options_binbuf } -static void set_tk_widget_ids(t_textwidget *x, t_canvas *canvas) +static void set_tkwidgets_ids(t_textwidget *x, t_canvas *canvas) { - char buf[MAXPDSTRING]; - x->x_canvas = canvas; - - /* Tk ID for the current canvas that this object is drawn in */ - sprintf(buf,".x%lx.c", (long unsigned int) canvas); - x->canvas_id = getbytes(strlen(buf)); - strcpy(x->canvas_id, buf); - - /* Tk ID for the "frame" the other things are drawn in */ - sprintf(buf,"%s.frame%lx", x->canvas_id, (long unsigned int)x); - x->frame_id = getbytes(strlen(buf)); - strcpy(x->frame_id, buf); - - sprintf(buf,"%s.text%lx", x->frame_id, (long unsigned int)x); - x->text_id = getbytes(strlen(buf)); - strcpy(x->text_id, buf); /* Tk ID for the "text", the meat! */ - - sprintf(buf,"%s.window%lx", x->canvas_id, (long unsigned int)x); - x->window_tag = getbytes(strlen(buf)); - strcpy(x->window_tag, buf); /* Tk ID for the resizing "window" */ - post(""); - - sprintf(buf,"%s.handle%lx", x->canvas_id, (long unsigned int)x); - x->handle_id = getbytes(strlen(buf)); - strcpy(x->handle_id, buf); /* Tk ID for the resizing "handle" */ - - sprintf(buf,"%s.scrollbar%lx", x->frame_id, (long unsigned int)x); - x->scrollbar_id = getbytes(strlen(buf)); - strcpy(x->scrollbar_id, buf); /* Tk ID for the optional "scrollbar" */ - - sprintf(buf,"all%lx", (long unsigned int)x); - x->all_tag = getbytes(strlen(buf)); - strcpy(x->all_tag, buf); /* Tk ID for the optional "scrollbar" */ + x->canvas_id = tkwidgets_gen_canvas_id(x->x_canvas); + x->frame_id = tkwidgets_gen_frame_id((t_object*)x, x->canvas_id); + x->widget_id = tkwidgets_gen_widget_id((t_object*)x, x->frame_id); + x->scrollbar_id = tkwidgets_gen_scrollbar_id((t_object*)x, x->frame_id); + x->window_id = tkwidgets_gen_window_id((t_object*)x, x->frame_id); + x->handle_id = tkwidgets_gen_handle_id((t_object *)x, x->canvas_id); } static int calculate_onset(t_textwidget *x, t_glist *glist, @@ -233,17 +204,17 @@ static void textwidget_draw_inlets(t_textwidget *x, t_glist *glist, int firsttim { onset = calculate_onset(x, glist, i, total_inlets); sys_vgui("%s create rectangle %d %d %d %d -tags {%xi%d %xi %s}\n", - x->canvas_id, onset, text_ypix(&x->x_obj, glist) - 2, + x->canvas_id->s_name, onset, text_ypix(&x->x_obj, glist) - 2, onset + IOWIDTH, text_ypix(&x->x_obj, glist), - x, i, x, x->all_tag); + x, i, x, x->all_tag->s_name); } for (i = 0; i < total_outlets; i++) /* outlets */ { onset = calculate_onset(x, glist, i, total_outlets); sys_vgui("%s create rectangle %d %d %d %d -tags {%xo%d %xo %s}\n", - x->canvas_id, onset, text_ypix(&x->x_obj, glist) + x->size_y, + x->canvas_id->s_name, onset, text_ypix(&x->x_obj, glist) + x->size_y, onset + IOWIDTH, text_ypix(&x->x_obj, glist) + x->size_y + 2, - x, i, x, x->all_tag); + x, i, x, x->all_tag->s_name); } } @@ -251,21 +222,21 @@ static void erase_inlets(t_textwidget *x) { DEBUG(post("erase_inlets");); /* Added tag for all inlets/outlets of one instance */ - sys_vgui("%s delete %xi\n", x->canvas_id, x); - sys_vgui("%s delete %xo\n", x->canvas_id, x); + sys_vgui("%s delete %xi\n", x->canvas_id->s_name, x); + sys_vgui("%s delete %xo\n", x->canvas_id->s_name, x); } static void draw_scrollbar(t_textwidget *x) { sys_vgui("pack %s -side right -fill y -before %s \n", - x->scrollbar_id, x->text_id); + x->scrollbar_id->s_name, x->widget_id->s_name); x->x_have_scrollbars = 1; } static void erase_scrollbar(t_textwidget *x) { - sys_vgui("pack forget %s \n", x->scrollbar_id); + sys_vgui("pack forget %s \n", x->scrollbar_id->s_name); x->x_have_scrollbars = 0; } @@ -273,14 +244,14 @@ static void bind_standard_keys(t_textwidget *x) { #ifdef __APPLE__ sys_vgui("bind %s {pdtk_canvas_ctrlkey %s %%K 0}\n", - x->text_id, x->canvas_id); + x->widget_id->s_name, x->canvas_id->s_name); sys_vgui("bind %s {pdtk_canvas_ctrlkey %s %%K 1}\n", - x->text_id, x->canvas_id); + x->widget_id->s_name, x->canvas_id->s_name); #else sys_vgui("bind %s {pdtk_canvas_ctrlkey %s %%K 0}\n", - x->text_id, x->canvas_id); + x->widget_id->s_name, x->canvas_id->s_name); sys_vgui("bind %s {pdtk_canvas_ctrlkey %s %%K 1}\n", - x->text_id, x->canvas_id); + x->widget_id->s_name, x->canvas_id->s_name); #endif } @@ -289,26 +260,33 @@ static void bind_button_events(t_textwidget *x) /* mouse buttons */ sys_vgui("bind %s