Refreshing gtk window without any mouse/keyboard intervention

2006-02-13 Thread Nisha P Kurur


Hi,

We are trying to create a gtk application which should run without much of 
manual intervention. Few buttons are placed in a row and each button has 
an image at the top which changes to red on selection. This image changes 
to green when the button goes out of selection. So the whole process has 
to be done in a scan mode without any manual intervention.


The problem with our code (attached with this mail) is that the events are 
generated and the callback functions are called. But the window is not 
refreshed properly. The window gets updated only when there is any

mouse/keyboard movement.

Thanks in Advance,

Regards
Nisha
---
Nisha P Kurur
DON Lab (BSB 328)
Dept. of CSE
IITM, Chennai - 36
Phone (044) 2257 5364/9804/9853
--#include common.h

GtkWidget *window;
GtkWidget *table;
GtkWidget *button[9];
GtkWidget *image_enable[9];

static void button_pressed_callback(GtkWidget *widget, GdkEvent *event, 
gpointer image);
static void button_released_callback(GtkWidget *widget, GdkEvent *event, 
gpointer image);
void button_selection();

/* Create a new hbox with an image and a label packed into it
 * and return the box. */

static GtkWidget *xpm_label_box(gchar *label_text,  gchar *xpm_filename)
{
GtkWidget *box;
GtkWidget *label;
GtkWidget *image;

/* Create box for image and label */
box = gtk_vbox_new (FALSE, 0);

gtk_container_set_border_width (GTK_CONTAINER (box), 2);

/* Now on to the image stuff */
image = gtk_image_new_from_file (xpm_filename);

/* Create a label for the button */
label = gtk_label_new (label_text);

/* Pack the image and label into the box */
gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 3);
gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 3);

gtk_widget_show (label);
gtk_widget_show (image);

return box;
}

/*
static void hiding_showing_images(GtkWidget *widget, gint index)
{
GtkWidget *image;
//  guint index = (guint *)img;
g_print(INDEX = %d, FLAG = %d \n,index ,flag);
if(flag == 1)
{
g_print(INSIDE LEAVE\n);
gtk_widget_hide(image_enable[index-1]);
image = gtk_image_new_from_file(radio_green.jpg);
gtk_table_attach_defaults (GTK_TABLE (table), image, index-1, 
index, 0, 1);
gtk_widget_show (image);
image_enable[index-1] = image;
flag = 0;
}
else
{
g_print(INSIDE ENTER\n);
gtk_widget_hide(image_enable[index-1]);
image = gtk_image_new_from_file(radio_red.jpg);
gtk_table_attach_defaults (GTK_TABLE (table), image, index-1, 
index, 0, 1);
gtk_widget_show (image);
image_enable[index-1] = image;
flag = 1;
}
}
*/

/* Callback method */

static void button_released_callback(GtkWidget *widget, GdkEvent *event, 
gpointer img)
{
GtkWidget *image;
guint index = (guint *)img;
g_print(INSIDE LEAVE\n);
gtk_widget_hide(image_enable[index-1]);
image = gtk_image_new_from_file(radio_green.jpg);
gtk_table_attach_defaults (GTK_TABLE (table), image, index-1, index, 0, 
1);
gtk_widget_show (image);
image_enable[index-1] = image;
}
static void button_pressed_callback(GtkWidget *widget, GdkEvent *event, 
gpointer img)
{
GtkWidget *image;
guint index = (guint *)img;
g_print(INSIDE ENTER\n);
gtk_widget_hide(image_enable[index-1]);
image = gtk_image_new_from_file(radio_red.jpg);
gtk_table_attach_defaults (GTK_TABLE (table), image, index-1, index, 0, 
1);
gtk_widget_show (image);
image_enable[index-1] = image;
}

static GtkWidget *create_button(guint index)
{
GtkWidget *box;
GtkWidget *button;
GtkWidget *image;

image = gtk_image_new_from_file(radio_green.jpg);
button = gtk_button_new();
switch(index)
{
case 1: 
 {
  box = xpm_label_box(   ABC   , soccer.gif);
  gtk_container_add (GTK_CONTAINER (button), box);
  gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 1, 2);
  gtk_table_attach_defaults (GTK_TABLE (table), image, 0, 1, 0, 1);
  image_enable[0] = image;
  g_signal_connect(G_OBJECT(button), button_press_event,
   G_CALLBACK (button_pressed_callback), (gpointer)index);
  g_signal_connect(G_OBJECT(button), button_release_event,
   G_CALLBACK(button_released_callback), (gpointer)index);

  break;
}
case 2:
{
  box = xpm_label_box(   DEF   , soccer.gif);
  gtk_container_add (GTK_CONTAINER (button), box);

Re: Refreshing gtk window without any mouse/keyboard intervention

2006-02-13 Thread Gus Koppel
Nisha P Kurur wrote:

 We are trying to create a gtk application which should run without much of 
 manual intervention. Few buttons are placed in a row and each button has 
 an image at the top which changes to red on selection. This image changes 
 to green when the button goes out of selection. So the whole process has 
 to be done in a scan mode without any manual intervention.
 
 The problem with our code (attached with this mail) is that the events are 
 generated and the callback functions are called. But the window is not 
 refreshed properly. The window gets updated only when there is any
 mouse/keyboard movement.

1. using the sleep() function in a GUI application is very bad
   programming style by all means, even if taking place in a custom
   thread. It's a relict of shell programming. You should rather use
   gtk_timeout_add() or g_timeout_add(). Probably you can even do without
   multiple threads then, which may avoid some hard to find bugs in the
   future and makes debugging much easier. See:

http://developer.gnome.org/doc/API/2.0/gtk/gtk-General.html#gtk-timeout-add
http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#g-timeout-add

2. you have to make sure that GTK+ can pass through its main loop for
   changes to be drawn. In a single-threaded application this would
   have to happen after your g_signal_emit_by_name() calls. See:

http://developer.gnome.org/doc/API/2.0/gtk/gtk-General.html#gtk-main-iteration-do

3. you probably know that the switch-case tree of your example is  
   bloated, i.e. contains much redundancy which could (and probably
   should) be avoided. Of 7 statements, 3 are completely identical
   and the other ones could be unified by simply using your index
   variable instead of distinct constants to pass to the respective
   functions. Add a little list of const strings for the xpm_label_box()
   call and you can do completely without a switch-case, that is, with
   only 1 instead of 9 blocks of that code.

Golden rule of programming: _never_ use the copy  paste feature of your
text editor for more than three or four lines of code, especially not
multiple times! Write sub functions (or in this case: just restructure a
code block) instead.
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list