Re: Modify behaviour to select more than one item in GtkListStore



On Thu, 20 Feb 2003, Emmanuel Di Pretoro wrote:

> I want to modify the behaviour of my GtkTreeView/GtkListStore. I can
> select more than one item, but I don't want to use 'Ctrl+Click' to
> select another item.

I'm not sure if this is exactly what you're after, but in my app I
have a liststore set up so the user can create a multiple selection
with a drag or "swipe" of the mouse across several items.  Sorry, I
don't have time to write this up properly, but the code fragments
(plus one full function) below may give you enough of a hint to be
going along with...

/* these things (among others) are done when the "list box" is
   initially set up
*/

GtkWidget *view;
GtkListStore *store;

view = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));

gtk_widget_set_events (view, GDK_POINTER_MOTION_MASK
		       | GDK_POINTER_MOTION_HINT_MASK);

g_signal_connect (G_OBJECT(view), "motion_notify_event",
		  G_CALLBACK(listbox_drag), NULL);

/* end of excerpts from set-up */

gboolean listbox_drag (GtkWidget *listbox, GdkEventMotion *event,
		       gpointer data)
/* allow the user to create an extended selection by dragging */
{
    gint x, y;
    GdkModifierType state;
    GtkTreeView *view = GTK_TREE_VIEW(listbox);
    GtkTreePath *path;

    if (event->is_hint) {
        gdk_window_get_pointer (event->window, &x, &y, &state);
    } else {
        x = event->x;
        y = event->y;
        state = event->state;
    }

    if ((state & GDK_BUTTON1_MASK) &&
	gtk_tree_view_get_path_at_pos(view, x, y, &path,
				      NULL, NULL, NULL)) {
	GtkTreeSelection *select = NULL;
	GtkTreePath *anchor_path = NULL;
	gchar *anchor_id = NULL;
	gint row;
	int anchor;
	static gint lastrow;

	/* "active_row" is set via g_object_set_data() when button 1
	   is clicked on a valid list box row */
	anchor = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(listbox),
						   "active_row"));
	row = tree_path_get_row_number(path);

	select = gtk_tree_view_get_selection(view);
	if (select == NULL) return FALSE;
	anchor_id = g_strdup_printf("%d", anchor);
	anchor_path = gtk_tree_path_new_from_string(anchor_id);
	g_free(anchor_id);

	if (row != lastrow) {
	    gtk_tree_selection_unselect_all(select);
	    gtk_tree_selection_select_range(select, anchor_path,
					    path);
	}

	gtk_tree_path_free(path);
	gtk_tree_path_free(anchor_path);

	lastrow = row;
    }

    return FALSE;
}

Allin Cottrell





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