style usage



Hi,

I'm writing a C++ library with the GTK+ and
I'd like to be able to set the foreground and
background colour directly, not using resources.
As I haven't managed to do this without segfaulting
sooner or later, I have written a mini-program
that reproduces the segv with GTK+ 1.0.6. What
I do is to create a new style with 
  gtk_style_copy()
give it a new colour and assign it to a button.
No problem.

But when I then reuse this style to change another
colour of the style and assign the style to
the button it will give a segv upon destruction
of the button. I tried to use gtk_style_ref()
but that didn't change anything. It is possble to
do what I want by using gtk_style_copy() everytime,
but I wonder if that is not a memory leak.

Also, in order to set the colour of the text of
a button, I have to change the label's style as 
well. As you can easily test with my program, it
is possible without segfaulting to assign the
same style to two widgets, the label and the
button. Is that correct or do I have to fear
memory garbage later on?

  Thanks and regards,

    Robert Roebling

#include <gtk/gtk.h>

int main (int argc, char *argv[])
{
    GtkWidget     *window;
    GtkWidget     *menu;
    GtkWidget     *button;
    GdkColor       color;
    GdkColormap   *colormap;
    GtkStyle      *style;

    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Style usage?");

    gtk_container_border_width (GTK_CONTAINER (window), 10);
    gtk_widget_realize(window);

    /*
       create simple button
     */
      
    button = gtk_button_new_with_label( "Hallo" );
    
    gtk_container_add (GTK_CONTAINER (window), button );
    
    gtk_widget_realize( button );
    gtk_widget_show( button );
    
    /*
      create style by copying an existing one
    */
    
    style = gtk_style_copy( gtk_widget_get_style( button ) );

    /*
      create color and assign it to style's background
    */
        
    color.red   = 40000;
    color.green = 40000;
    color.blue  = 40000;
    colormap = gdk_window_get_colormap( button->window );
    gdk_color_alloc( colormap, &color );
    style->bg[GTK_STATE_NORMAL] = color;
    
    /*
      assign style to button
    */
    
    gtk_widget_set_style( button, style );
    
    /*
      assign same color to style's foreground
      if this is not done -> no SEGFAULT
      if I do another gtk_style_copy(...) -> no SEGFAULT
    */
    
    style->fg[GTK_STATE_NORMAL] = color;
    gtk_widget_set_style( button, style );
    
    
    gtk_widget_show (window);
    
    /*
       destruction will produce SEGFAULT
    */
    
    gtk_widget_destroy( window );
    
    return 0;
}




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