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

GtkEntry prevents grabbing focus?



Hi,

I'm writing a large order entry program and one of the capabilities I want
to have is to warp focus to any invalid fields when the user does something
that would cause a record to be committed to a data-base.

Often the 'something' is moving to a GtkEntry in another screen area.  At that
point I attempt to do a gtk_widget_grab_focus (or alternatively
gtk_window_set_focus) to attempt to warp the focus back to the problem
field.  Unfortunately the grab focus seems to fail in this circumstance.

Here is a demo program.  The way I would expect it to work is that if you
do a mouse button press in one of the GtkEntry's, it should warp focus
to the other.

Help (Thanks)
Kent

#include <stdio.h>
#include <gtk/gtk.h>

static GtkWidget  *entry1,*entry2;

static void set_focus_child_cb(GtkWidget *w, GtkWidget *child, gpointer data) {
   gtk_signal_handler_block_by_func(GTK_OBJECT(w),set_focus_child_cb,data);

   if (child == entry1) {
      fprintf(stderr,"Moving to entry2\n");
      gtk_widget_grab_focus(entry2);
   } else {
      fprintf(stderr,"Moving to entry1\n");
      gtk_widget_grab_focus(entry1);
   }

   gtk_signal_handler_unblock_by_func(GTK_OBJECT(w),set_focus_child_cb,data);
}

int main(int argc, char *argv[]) {
   GtkWidget   *box,*window;

   gtk_init(&argc,&argv);

   window   = gtk_window_new(GTK_WINDOW_TOPLEVEL);

   gtk_signal_connect(GTK_OBJECT(window),"delete_event",
      GTK_SIGNAL_FUNC(gtk_main_quit),NULL);

   box      = gtk_vbox_new(0,5);
   entry1   = gtk_entry_new();
   entry2   = gtk_entry_new();

   gtk_entry_set_text(GTK_ENTRY(entry1),"Entry1");
   gtk_entry_set_text(GTK_ENTRY(entry2),"Entry2");

   gtk_container_add(GTK_CONTAINER(box),entry1);
   gtk_container_add(GTK_CONTAINER(box),entry2);

   gtk_signal_connect_after(GTK_OBJECT(box),"set-focus-child",
      GTK_SIGNAL_FUNC(set_focus_child_cb),NULL);

   gtk_container_add(GTK_CONTAINER(window),box);

   gtk_widget_show_all(window);

   gtk_main();

   return(0);
}




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