set focus problem



Hi all,

  I've a 2 entry widgets and a button. I want to check if
the text entered in the first entry widget corresponds
to a long number. So I connect the "focus_out_event"
to this entry and in the callback function (BackVerif), I want
to set the focus in the first entry if the an conversion
error appear.
  The problem is: the function gtk_window_set_focus
seems not work properly in this case, whereas if
I connect the "clicked" event's button to a callback
function (BackOK) which do the same thing, the focus is given
to my first entry.

Can anynone help me ?
Thanks in advance

Pascal
PS : Excuse-me for my bad english.

Here the code (Gtk version: 1.2.8)

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

gboolean BackOK(GtkWidget *button,GtkWidget **entries)
{
GtkWidget *window=gtk_widget_get_toplevel(button);

gtk_window_set_focus(GTK_WINDOW(window),entries[0]);

return TRUE;
}

gboolean BackVerif(GtkWidget *widget,GdkEventFocus *event,GtkWidget
**entries)
{
GtkWidget *window=gtk_widget_get_toplevel(widget);
char *err=NULL;
char *str=gtk_entry_get_text(GTK_ENTRY(entries[0]));
long nbre=strtol(str,&err,10);

if (*err!='\0')
  {
  puts("Error");
  gtk_window_set_focus(GTK_WINDOW(window),entries[0]);
  }

return FALSE;
}

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


gtk_init(&argc,&argv);

window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

gtk_signal_connect(GTK_OBJECT(window),"delete_event",\
                   (GtkSignalFunc)gtk_exit,NULL);
gtk_signal_connect(GTK_OBJECT(window),"destroy",\
                   (GtkSignalFunc)gtk_exit,NULL);

box=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),box);

entries=g_malloc(sizeof *entries * 2);

entries[0]=gtk_entry_new();
gtk_box_pack_start(GTK_BOX(box),entries[0],TRUE,TRUE,5);
gtk_signal_connect(GTK_OBJECT(entries[0]),"focus_out_event",\
                   (GtkSignalFunc)BackVerif,entries);

entries[1]=gtk_entry_new();
gtk_box_pack_start(GTK_BOX(box),entries[1],TRUE,TRUE,5);

button=gtk_button_new_with_label("OK");
gtk_signal_connect(GTK_OBJECT(button),"clicked",\
                   (GtkSignalFunc)BackOK,entries);
gtk_box_pack_start(GTK_BOX(box),button,TRUE,TRUE,5);

gtk_widget_show_all(window);

gtk_main();

return 0;
}



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