Re: Creating widgets with auto-repeat



>Perhaps someone with a bit more experience of this sort of thing could point
>out a neat way to avoid all the globals here, so this code could be reused 
>in a more generic way...  
>
>Perosnally, I *like* globals... :-)

you are right about needing a bit more experience :)

>guint32 timer_id = 0;
>gint timer_state = 0;
>gint timer_count = 0;
>gint value = 0;

typedef struct timer_func_info {
       guint32 timer_id;
       gint timer_state;
       gint timer_count;
       gint value;
} timer_func_info_t;

>/* Timer callback function! */
>static gint timer_func (gpointer data )
>{
    timer_func_info_t *info = (timer_func_info_t *) data;
    	
	... now use info-> instead of the globals.

>   return TRUE;
>
>}
>
>void button_press_proc( GtkWidget *widget, gpointer data )
>{

    timer_func_info_t *info = (timer_func_info_t *) data;

>   /* Increment value */
>   printf("VALUE: %d\n", value++);
>
>   /* Setup a timer */

    info->value++;
    info->count = 0;
    info->state = 0;
>   info->timer_id = gtk_timeout_add(REPEAT_STAGE0_DELAY, 
		                      (GtkFunction)timer_func, info);

>}
>
>void button_release_proc( GtkWidget *widget, gpointer data )
>{
>
>   /* Remove timer */

    timer_func_info_t *info = (timer_func_info_t *) data;
    gtk_timeout_remove(info->timer_id);


>}
>
>int main( int   argc, char *argv[] )
>{
>   GtkWidget *window;
>   GtkWidget *button;
    timer_func_info_t info;

>
>   gtk_init(&argc, &argv);

    info->value = 0;

>   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
>   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
>   button = gtk_button_new_with_label (" INCREMENT ");
    gtk_signal_connect (GTK_OBJECT (button), "button_press_event",
                        GTK_SIGNAL_FUNC (button_press_proc), &info);
    gtk_signal_connect (GTK_OBJECT (button), "button_release_event",
                        GTK_SIGNAL_FUNC (button_release_proc), &info);
>   gtk_container_add (GTK_CONTAINER (window), button);
>   gtk_widget_show (button);
>   gtk_widget_show (window);
>   gtk_main ();
>
>   return(0);
>}

all clear?

--p



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]