Re: numerical buttons labels cont'd



On Wed, 2002-11-27 at 22:37, Pierre CHATEL wrote:
> Hi,
> 
> i have changed my code the way indicated in the mailing list but it's
> not working...
> 
> The on_button_click_event2 callback decrements the label (numerical
> value) of a button by 1, but the new value is not showing in 
> the button ! The line bellow is probably incriminated:
> 
> gtk_label_set(GTK_LABEL(GTK_BIN(button)->child), str);
> 
> But when i use a constant string in place of str it's working:
> 
> gtk_label_set(GTK_LABEL(GTK_BIN(button)->child), "xx");
> The above line change the label of the button to xx ON THE SCREEN
> 
> How can i use a char* in order to manipulate the label ?
> 
> /* Code Segment */
> 
> button1 = gtk_button_new_with_label (_("4"));
> gtk_widget_ref (button1);
> gtk_object_set_data_full (GTK_OBJECT (window1), "button1",
> button1,(GtkDestroyNotify) gtk_widget_unref);
> gtk_widget_show (button1);
> gtk_fixed_put (GTK_FIXED (fixed1), button1, 48, 144);
> gtk_widget_set_uposition (button1, 48, 144);
> gtk_widget_set_usize (button1, 47, 22);
> 
> gtk_signal_connect (GTK_OBJECT (button1), "clicked",
>                     GTK_SIGNAL_FUNC (on_button_click_event2),
>                     NULL);
> 
> /* The callback: decrement the label of the button (default "4") by 1 */
> 
> void
> on_button_click_event2 (GtkWidget *button, gpointer user_data)
> {
>   char *str;
>   int i;
>   gtk_label_get(GTK_LABEL(GTK_BIN(button)->child), &str);
>   i = atoi(str);
>   i--;
>   sprintf(str,"%i",i);
>   g_print("%s",str); /* The new value is displayed for debugging */
>   gtk_label_set(GTK_LABEL(GTK_BIN(button)->child), str);
> }

gtk_label_set() is an alias for:
 void gtk_label_set_text(GtkLabel *label, const char *str)

Since *str is declared const, you need to declare the pointer you pass
into it as const also.

This is for gtk2 BTW.
-- 
Stephen




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