Re: Complete newbie question about colors




Joakim Hove <hove@phys.ntnu.no> writes:

> So far, so good. Now i want the drawingare displayed with a different
> color. I have tried several varieties of :
>  
>   .... 
>   colormap = gtk_widget_get_colormap(...)
>   colormap = gdk_colomap_new (...)
>   gdk_color_alloc(..)	
>   gdk_window_set_background(..)
>   ....
> 
> Some varieties won't compile at all, some generate compiler
> warnings, some run-time warnings and some don't complain at all. But
> unfortunately they *all* produce the same gray drawingare.

The problem you are having is that you can't set the background
on a GdkWindow until it is exists. And the GdkWindow for the
GtkDrawingArea (drawing_area->window) doesn't exist until the
widget is realized.

So, basically, you want to do:

  colormap = gtk_widget_get_colormap(drawing_area);
  gdk_color_alloc (colormap (...);

  gdk_widget_realize (drawing_area);
  gdk_window_set_background (drawing_area->window, ...);

But, be warned that this will produce bad results when
(using features that will be present in GTK+-1.2), the user
changes their color scheme at runtime - at that point the
drawing area background will change to whatever the new background 
color is.

So, you may want to use an alternate method:

One fairly simple way is to make a copy of the drawing_area's
style and set the background for that.

  GtkStyle *new_style;

  gtk_widget_ensure_style (drawing_area);
  new_style = gtk_style_copy (drawing_area->style);

  new_style->bg[GTK_STATE_NORMAL].red = 0xFFFF;
  new_style->bg[GTK_STATE_NORMAL].green = 0;
  new_style->bg[GTK_STATE_NORMAL].blue = 0;
   
  gtk_widget_set_style (drawing_area, new_style);
  gtk_style_unref (new_style);  /* We don't need it any more ourself */

The widget's style will now be completely unaffected when the user
changes color scheme changes. 

Hope this helps,
                                        Owen



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