[maemo-developers] how to get the current item of GtkEntryCompletion list when up arrows moves throuth items

From: bocheng at redflag-linux.com bocheng at redflag-linux.com
Date: Tue Mar 13 11:12:14 EET 2007
main purpose of the code below is  when putting some characters such as 
to ,oa,and so on, the list of completion will show all strings  that contains 
the characters ,but when I tip Tab  key on the keyboard , it moves through 
all the match items in the list ,But what signal should I  connect to show 
current string on the entry when  tipping Tab continuous in the matching 
list ?
/* Entry Completion
gcc entry_completion.c `pkg-config gtk+-2.0 --cflags --libs`
*/

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
gboolean match_selected_cb(GtkEntryCompletion *completion,
                        GtkTreeModel * model , GtkTreeIter * iter,void * data)

{
        char * select = 0;
        gtk_tree_model_get(model,iter,0,&select,-1);
        gtk_entry_set_text(data,select);
}


gboolean  action_cb(GtkEntryCompletion* completion, int index,void * data)
{
        printf("SSSS %d\n",index);
        return TRUE;
}
gboolean match_rule(GtkEntryCompletion* completion , const char * 
key,GtkTreeIter * iter  , void * usr_data)
{
        GtkTreeModel *model;

        model = gtk_entry_completion_get_model (completion);
        char * context = NULL ;
        gtk_tree_model_get(model,iter,0,&context,-1);
        if(strstr(context,key)) return TRUE;
        else return FALSE;
}


static GtkWidget *window = NULL;
GdkEvent* synthesize_gdk_event(GdkEventType evtType, GtkWidget *widget, guint 
keyVal)
{
        GdkEvent *evt = NULL;
        GdkKeymapKey *keys;
        gint n_keys;
        GtkWidget *temp_widget;
        guint16 hardware_keycode;

        if (gdk_keymap_get_entries_for_keyval (gdk_keymap_get_default (),
                                keyVal, &keys, &n_keys))
        {
                temp_widget = GTK_WIDGET(widget);
                hardware_keycode = keys[0].keycode;

                g_free (keys);
                evt = gdk_event_new (evtType);
                evt->key.window = g_object_ref(temp_widget->window);
                evt->key.hardware_keycode = hardware_keycode;

                /* these are optional but possibly nice to have */
                //toevt->key.keyval = gdk_unicode_to_keyval (keyVal);
                evt->key.keyval = keyVal;

                /* the rest are no-ops */
                evt->key.send_event = FALSE;   /* seems unused except for dnd 
*/
                evt->key.time = GDK_CURRENT_TIME;
        }

        return evt;
}

tab_cb(GtkWidget *window, GdkEventKey *ev,gpointer data)
{
        if(ev->keyval == GDK_Tab)
        {
                GdkEvent * fake = synthesize_gdk_event(GDK_KEY_PRESS, 
GTK_WIDGET(data), GDK_Down);
        //      GdkEvent * fake1 = synthesize_gdk_event(GDK_KEY_PRESS, 
GTK_WIDGET(window), GDK_Return);
                gtk_main_do_event(fake);
        //      gtk_main_do_event(fake1);
                return 1;
        }


        return 0;

}
/* Creates a tree model containing the completions */
GtkTreeModel *
create_completion_model (void)
{
        GtkListStore *store;
        GtkTreeIter iter;

        store = gtk_list_store_new (1, G_TYPE_STRING);

        /* Append one word */
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, "GNOME", -1);

        /* Append another word */
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, "total", -1);

        /* And another word */
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, "totally", -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, "totallly", -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, "totalllly", -1);

        return GTK_TREE_MODEL (store);
}


GtkWidget *
do_entry_completion (GtkWidget *do_widget)
{
        GtkWidget *vbox;
        GtkWidget *label;
        GtkWidget *entry;
        GtkEntryCompletion *completion;
        GtkTreeModel *completion_model;

        if (!window)
        {
                window = gtk_dialog_new_with_buttons ("GtkEntryCompletion",
                                GTK_WINDOW (do_widget),
                                0,
                                GTK_STOCK_CLOSE,
                                GTK_RESPONSE_NONE,
                                NULL);
                gtk_window_set_resizable (GTK_WINDOW (window), FALSE);

                g_signal_connect (window, "response",
                                G_CALLBACK (gtk_widget_destroy), NULL);
                g_signal_connect (window, "destroy",
                                G_CALLBACK (gtk_widget_destroyed), &window);

                vbox = gtk_vbox_new (FALSE, 5);
                gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, 
TRUE, TRUE, 0);
                gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);

                label = gtk_label_new (NULL);
                gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try 
writing <b>total</b> or <b>gnome</b> for example.");
                gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

                /* Create our entry */
                entry = gtk_entry_new ();
                g_signal_connect(entry,"key_press_event",tab_cb,entry);
                gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);

                /* Create the completion object */
                completion = gtk_entry_completion_new ();

                /* Assign the completion to the entry */
                gtk_entry_set_completion (GTK_ENTRY (entry), completion);
                g_object_unref (completion);

                /* Create a tree model and use it as the completion model */
                completion_model = create_completion_model ();
                gtk_entry_completion_set_model (completion, completion_model);
                g_object_unref (completion_model);
                gtk_entry_completion_set_match_func(completion,match_rule,NULL,NULL);
                g_signal_connect (completion, "match_selected",          
G_CALLBACK (match_selected_cb),entry);
        //      g_signal_connect (completion, "action_activated" ,       
G_CALLBACK (action_cb),entry);
                /* Use model column 0 as the text column */
                gtk_entry_completion_set_text_column (completion, 0);
        }

        if (!GTK_WIDGET_VISIBLE (window))
                gtk_widget_show_all (window);
        else
                gtk_widget_destroy (window);

        return window;
}

main(int argc,char ** argv)
{
        gtk_init(&argc,&argv);
        GtkWidget * gg =do_entry_completion(NULL);
        gtk_widget_show_all(gg);
        gtk_main();
        return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maemo.org/pipermail/maemo-developers/attachments/20070313/dfcaffef/attachment.htm 
More information about the maemo-developers mailing list