cursor position handling by GtkEntry



 Hello, list!

 I'm having a problem with implementing "changed" signal of GtkEntry that will
do what I wish. I wish to update content of the gtkentry on the fly as it's
changed  (final code will canonalize typed numeric values - ie when user types 
1234, the thousands separator will be inserted between 1 and 234, and when, 
say, 4 is removed, that thousand separator is removed automagically too)). I've
implemented all functions for doing this, and now I'm fighting with gtk+ now.

 It seems that it's impossible to change cursor position in the handler for
"changed" - even after deleting entire content of it and then setting new
cursor position old position is used for some reason unknown to me. Here is a
sample code (it doesn't normalize values, but appends "123" on each change of
GtkEntry).
 Could anybody recommend me how to solve this problem? May be it's a bug in
gtk?

I'm using stock RH6.0's gtk+1.2.1-10 on x86 box

 Thanks in advance.

 Best regards,
  -Vlad
------------------------------------
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>


void sigchanged1(GtkEntry* e)
{
    static int recursive;
    char buf[100];    
    char* str;
    int curpos;
    
    if (recursive)
	return;
	
    str= gtk_entry_get_text(e);    strcpy(buf,str);
    curpos= gtk_editable_get_position(GTK_EDITABLE(e));
    strcat(buf,"123");
    recursive = 1; 

    gtk_entry_set_text(e,"");/*it should forget about cursor position now*/
    gtk_entry_set_text(e,buf);
	/*-1 means position at the end. But arbitrary value in range
		[0,strlen(buf)] won't work too  - cursor will still be
	placed at offset where it was during "real" editing*/
    gtk_editable_set_position(GTK_EDITABLE(e),-1); 
    recursive = 0;    
};



int main(int argc,char** argv)
{
    GtkWidget *window,*e;
    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    e = gtk_entry_new();

	/*using 'connect' doesn't help too*/
    gtk_signal_connect_after(GTK_OBJECT(e),"changed",
	GTK_SIGNAL_FUNC(sigchanged1),e);

    gtk_container_add(GTK_CONTAINER(window),e);
    gtk_widget_show_all(window);
    gtk_main ();    
};



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