Lost selection while handling the change signal



Hi!

I'd like to write an "autowordcomplete" function for a text
widget. Below is a simple trial to test it (the behavior of
a gtk_editable_select_region while I am in the change event
handler). It means:

1. Create a text widget
2. Connect the change signal
3. Type some text in at the end of the text widget.
4. It should complete my text (in this case a dumb "anytext"
will follow my letters).
5. It should select the completed part so my next typed
letter can overwrite it. (Or I can "accept" the completion
by pressing <END> for example).

But the selection will be lost!!! What to do not to lose it?
I also could see, that pressing the <DEL> before the last
letter in the text widget (eg. deleting the last letter by
<DEL>) the selection will work!!! It means that if the
pointer does not move, the selection is not lost. 

---------------------

Here is my dumb example (debian3rc1/XFree4.2.0/GTK1.2/):

/* Globally defined handler_id: */
gint autotext_handler_id;

/* Our autocompleter function: */
void autotext_handler (GtkWidget *widget, GdkEventKey *event)
{
gchar*  chars;
guint   where;
gchar*  autotext;

  gtk_text_freeze(GTK_TEXT(widget));
  
  chars = gtk_editable_get_chars(GTK_EDITABLE(widget), 0, -1);
  where = gtk_editable_get_position(GTK_EDITABLE(widget));

  autotext="anytext";
  /* Am I at the end of the text? */
  if(strlen(chars)==where) {
 
gtk_signal_handler_block(GTK_OBJECT(widget),autotext_handler_id);
    gtk_editable_insert_text(GTK_EDITABLE(widget), autotext,
strlen(autotext), &where);
    gtk_editable_set_position(GTK_EDITABLE(widget),
where-strlen(autotext));
   
gtk_editable_select_region(GTK_EDITABLE(widget),where-strlen(autotext),-1);
 
gtk_signal_handler_unblock(GTK_OBJECT(widget),autotext_handler_id);

  }

  free(chars);

  gtk_text_thaw(GTK_TEXT(widget));

}


/* Somewhere we connect to the handler above */

autotext_handler_id =
gtk_signal_connect_after(GTK_OBJECT(widget), "changed",
GTK_SIGNAL_FUNC(autotext_handler), NULL);







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