about button's problem......



hi,hello:
Thanks for your helps.
now i want to do:my condition is being no  mouse,the only terminal controlling  tool is  the keyboard.(i am doing a STB project.). when a button get focus(with keyboard) , setting  the button's  background to be a color. then when the button lost focus,setting the button's background to be another color.i do the following.
i rewrite the testing code,but it's still not working like i expected.


#include<gtk/gtk.h>
#include<gdk/gdkx.h>

#define WIDTH 30
#define Height 20

/* drawing a colorful rectangle on the button,the button will look like with a colorful background*/
void DrawColorRectOnButton(GtkWidget *object,gpointer data)
{
  static GdkColormap *colormap = NULL;
  static GdkGC *gc = NULL;
  GdkColor color;
  GtkWidget *widget = GTK_WIDGET(data);
  gint x = 0;
  gint y=0;
  gint width = 30;     //the rectangle's width,height equal to the button's width,height
  gint height = 20;   
  
  if(NULL == colormap)
  {
     colormap = gdk_color_get_system();
     gc = gdk_gc_new(widget->window);
  }

  color.red  = 61696;
  color.green = 52736;
  color.blue  = 45312;
  gdk_color_alloc(colormap,&color);

  gdk_gc_set_foreground(gc,&color);
  
  gdk_draw_rectangle(widget->window,gc,TRUE,x,y,width,height);

}

int main(int argc,char *argv[])
{
  GtkWidget *window;
  GttkWidget *button1;
  GtkWidget *area1;
  GtkWidget *button2;
  GtkWidget *area2;
  GtkWidget *vbox;

  gtk_init(&argc,&argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(gtk_exit),NULL);

  vbox = gtk_vbox_new(TRUE,2);
  gtk_container_add(GTK_CONTAINER(window),vbox);
 
  button1= gtk_button_new_with_label("1");
  gtk_widget_set_usize(button1,WIDTH,HEIGHT);
  gtk_box_pack_start(GTK_BOX(vbox),button1,TRUE,FALSE,0);
  gtk_window_set_focus(GTK_WINDOW(window),button1);
  area1 = gtk_drawing_area_new();
  gtk_widget_set_usize(area1,WIDTH,HEIGHT);
  gtk_signal_connect(GTK_OBJECT(button1),"focus_in_event",GTK_SIGNAL_FUNC(DrawColorRectButton),area1);
  
   button2= gtk_button_new_with_label("2");
  gtk_widget_set_usize(button2,WIDTH,HEIGHT);
  gtk_box_pack_start(GTK_BOX(vbox),button2,TRUE,FALSE,0);
  gtk_window_set_focus(GTK_WINDOW(window),button2);
  area2 = gtk_drawing_area_new();
  gtk_widget_set_usize(area2,WIDTH,HEIGHT);
  gtk_signal_connect(GTK_OBJECT(button2),"focus_in_event",GTK_SIGNAL_FUNC(DrawColorRectButton),area2);

  gtk_widget_show(area1);
  gtk_widget_show(area2);
  gtk_widget_show(button1);
  gtk_widget_show(button2);
  gtk_widget_show(vbox);
  gtk_widget_show(window);

  gtk_main();
  return 1;
}
PS: i don't know how to set a button's background  color when the user enviorment without mouse supporting( i know when  the mouse available,using bg[GTK_STATE_PRELIGHT]=color, the button's color will show some color after  the mouse pointer put on the button).



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