Re: [gtk-list] GtkCList focus



Anders Melchiorsen wrote:
> 
> I am trying to get GtkCList to work like e.g. Windows lists where
> pressing a key will move the focus to a row starting with that letter.
> If the focus is already on such a row, the focus should move to the
> next row starting with that letter.
> 
> I have a hard time doing this. For one thing, is the ->focus_row
> member public? I see no accessor function for getting the contents of
> the focused row (not the selection). Thus, I think that I need to use
> focus_row combined with gtk_clist_get_text() calls to get the
> functionality that I want.
> 
> Is there an easier and/or better way?

I have done that in an application.  Here are some pieces of the code,
it should be fairly decifrable, though not complete.

This is an utility function to set the current focus row.

void
gtk_clist_set_focus_row(GtkCList *list, guint row)
{
    g_return_if_fail(list != NULL);
    g_return_if_fail(row >= 0);
    g_return_if_fail(row < list->rows);

    list->focus_row = row;
}

This is the key_press event handler for the clist.  Ignore the
PersonSelector stuff, it is structure that contains the clist, among
other things.

gboolean
on_person_selector_list_key_press_event (GtkWidget       *widget,
                                        GdkEventKey     *event,
                                        gpointer         user_data)
{
    PersonSelector *sel;

#ifdef DEBUG
    g_print("on_person_selector_list_key_press_event\n");
#endif

    if (event->state != 0)
	return FALSE;

    sel = widget_get_person_selector(GTK_WIDGET(user_data));
    return person_selector_search(sel,
gdk_keyval_to_lower(event->keyval));
}


gboolean
person_selector_search(PersonSelector *sel, guint keyval)
{
    GtkCList *list = sel->list;
    guint row;
    gchar *name;

    keyval = (guchar)keyval;
    if (! isprint(keyval))
	return FALSE;

#ifdef DEBUG
    g_print("person_selector_search: key %c\n", keyval);
#endif /* DEBUG */

    row = (list->focus_row + 1) % list->rows;
    while (row != list->focus_row) {
	gtk_clist_get_text(list, row, 0, &name);

#ifdef DEBUG
	g_print("person_selector_search: row %d match %.20s\n", row, name);
#endif /* DEBUG */

	if (tolower(*name) == keyval) {
	    gtk_clist_set_focus_row(list, row);

	    if (gtk_clist_row_is_visible(list, row) != GTK_VISIBILITY_FULL)
		gtk_clist_moveto(list, row, 0, 0.5, 0.0);
	    gtk_widget_queue_draw(GTK_WIDGET(list));

#ifdef DEBUG
	    g_print("person_selector_search: got row %d\n", row);
#endif /* DEBUG */
	    return TRUE;
	}
	row = (row + 1) % list->rows;
    }

#ifdef DEBUG
    g_print("person_selector_search: not found\n");
#endif /* DEBUG */

    return FALSE;
}

-- 
René Seindal (rene@seindal.dk)			http://www.seindal.dk/rene/



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