Re: Changing window color



On Fri, 31 Jan 2003, Liviu BURCUSEL wrote:

On Thu, 30 Jan 2003 13:31:20 +0100 (CET)
TORRI Vincent wrote:



Hello,

  you could use the style of a widget :


  GtkWidget *w;
  GdkColor col = {0, 0xffff, 0x0000, 0x0000};
  GtkStyle *style;

  w = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  style = gtk_widget_get_style (w); 
  style->bg[GTK_STATE_NORMAL] = col;
  gtk_widget_set_style (w, style);


and the background is red.  

regards

Vincent TORRI

Thanks again !
Now my window is all red and I want to turn it back gray. I followed the
same steps with the color changed and the window still remains red. How
can I make it back ? I'm a little bit lost




Hello, 
  I've tried to make a callback wich changes the color and, indeed, the 
background is not changed. It seems that the widget has to be redrawn. So 
I use gtk_widget_queue_draw to update it. You can try this (Perhaps there 
is some more efficient code): 

#include <gtk/gtk.h>

guint n;

GdkColor red   = {0, 0xffff, 0x0000, 0x0000};
GdkColor gray = {1, 0x9999, 0x9999, 0x9999};
GtkStyle *style;

void change_color(GtkWidget *b, GtkWidget *ww)
{
  GtkStyle *w_style;
  GdkColor c;

  n=1-n;

  if (n==1)
    {
      c = gray;
    }
  else
    {
      c = red;
    }
  
  gtk_widget_queue_draw (ww);
  w_style = gtk_style_copy (style);
  w_style->bg[GTK_STATE_NORMAL] = c;
  style = gtk_style_copy (w_style);
  gtk_widget_set_style (ww, style);
}

int main(int argc, char *argv[])
{
  GtkWidget *w, *b;

  gtk_init(&argc, &argv);

  w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width(GTK_CONTAINER(w), 15);
  b = gtk_button_new_with_label("Change color");
  gtk_signal_connect(GTK_OBJECT(b), "clicked", 
(GtkSignalFunc)change_color, (gpointer)w);
  gtk_container_add(GTK_CONTAINER(w), b);
  
  n = 0;
  style = gtk_widget_get_style(w); 
  
  gtk_widget_show_all(w);

  gtk_main();

  return 0;
}


regards

Vincent TORRI


-- 
TORRI Vincent
Mathematiques Appliquees Bordeaux                                                
Institut de Mathematiques                                                       
Universite Bordeaux 1                                                           
351 cours de la liberation                                                      
33 405 Talence cedex - France                                                   
                                                                                
Tel : 33 (0)5 57 96 21 42                                                       
Fax : 33 (0)5 56 84 26 26                                                       
--




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