RE: (gtk1.2) Making autocomplete with a GTK_COMBO(widget)->entry



(Oops, replied directly to Martyn at first, sorry about that)

Martyn,

Thanks a lot for your suggestions!

On Tue, 2002-05-07 at 10:34, martyn 2 russell bt com wrote:
Note: I have not used these functions, but it is what I would try
(shown
below).  I agree with you, the method of setting a flag to stop
recursion
(as you have done it and so have I in the past) doesnt seem the best
way.

It's the quick and easy way, nothing more ;)

 
Try the following (found at:
http://developer.gnome.org/doc/API/gtk/gtk-signals.html)

[...]

OR 
      gtk_signal_handler_block(...);
      gtk_signal_handler_unblock(...);

I used these, seems more future compatible :)
 
Also, I noticed you use strcmp (i.e. basic C functions) but you can
also use
GLIB's equivilants.. g_strcasecmp() and g_strncasecmp(): more about
this can
be found here:

http://developer.gnome.org/doc/API/glib/glib-string-utility-functions.html

Done, thanks!

[Original message SNIPed]

However, it still does not work as expected. To make matters worse, I
downloaded the source code for Galeon, and took a look at the function
that does the autocompletion of URLs in the browser window. The code is
almost exactly the same! The only difference is that theirs work, mine
doesn't ;|

For those who might be kind enough to take another look, here is the
function again, updated with a cleaner flow trough what the function
needs to do;

-----
gint me_addmatch_autocomplete_cb( GtkWidget *entry, gpointer userdata )
{
        gchar *partialname, *completion;
        gint curpos, length, cid;

        if( !entry )
                return( FALSE );

        partialname = gtk_editable_get_chars( GTK_EDITABLE(entry), 0, -1
);
        curpos = gtk_editable_get_position( GTK_EDITABLE(entry) );
        length = strlen( partialname );
        if( length )
        {
                completion = me_find_autocomplete_alternative(
partialname );
                if( (completion != NULL) && (g_strcasecmp(partialname,
completion)!=0 ))
                {
                        cid = (gint) gtk_object_get_user_data(
GTK_OBJECT(entry) );
                        gtk_signal_handler_block( GTK_OBJECT(entry), cid
);
                
                        gtk_entry_set_text( GTK_ENTRY(entry), completion
);
                        gtk_editable_select_region( GTK_EDITABLE(entry),
length, -1 );
                        gtk_editable_set_position( GTK_EDITABLE(entry),
-1 );

                        gtk_signal_handler_unblock( GTK_OBJECT(entry),
cid );
                }
        }
        g_free( partialname );
        return( TRUE );
}
-----

Notice if you will that just like in Galeon, I now moved the code that
searches 
for an alternative out into its' own function,
me_find_autocomplete_alternative().
This function returns a pointer to the first string that matches what
the user has typed
in so far, or NULL if nothing matches.

The blocking of the signal seems to work, using some debugging printf()
statements show 
that the gtk_entry_set_text() no longer triggers the "changed" signal to
call the same 
function again.

Now here's the b***h of it; The only real difference as I see it between
my code and
Galeon's, is that mine blocks the signals, and theirs work! :| And
believe me, I _have_
tried unblocking the signals to see if that has any effect..

For those brave enough to try to figure out what is going on, it does
seem to work fine
under certain circuimstances. For instance, if I let it autocomplete a
word, then go
to the end of that word (i.e. to the right of the last character), and
press backspace,
THEN the last character of the word is highlighted, just like you'd
expect! However, when
I first type in the first character, the position remains to the right
of the first
character, and nothing is selected..

Is there anyone out there who can wrap their heads around this one? I
sure can't... :(

Many thanks,

Rune Jacobsen





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