From 7fc61f47a6133047135b01190ad3dc8e9eed4489 Mon Sep 17 00:00:00 2001 From: Vitaly Minko Date: Wed, 27 Oct 2010 23:39:32 +0400 Subject: Add auto scrolling when drag-and-dropping contacts (#619858). --- libempathy-gtk/empathy-individual-view.c | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/libempathy-gtk/empathy-individual-view.c b/libempathy-gtk/empathy-individual-view.c index 2ad0fc0e0..2e88ac24d 100644 --- a/libempathy-gtk/empathy-individual-view.c +++ b/libempathy-gtk/empathy-individual-view.c @@ -90,6 +90,15 @@ typedef struct guint timeout_id; } DragMotionData; +typedef struct +{ + EmpathyIndividualView *view; + guint timeout_id; + /* Distance between mouse pointer and the nearby border. Negative when + scrolling updards.*/ + gint distance; +} AutoScrollData; + typedef struct { EmpathyIndividualView *view; @@ -549,6 +558,32 @@ individual_view_drag_motion_cb (DragMotionData *data) return FALSE; } +/* Minimum distance between the mouse pointer and a horizontal border when we + start auto scrolling. */ +#define AUTO_SCROLL_MARGIN_SIZE 20 +/* How far to scroll per one tick. */ +#define AUTO_SCROLL_PITCH 10 + +static gboolean +individual_view_auto_scroll_cb (AutoScrollData *data) +{ + GtkAdjustment *adj; + gdouble new_value; + + adj = gtk_scrollable_get_vadjustment (GTK_TREE_VIEW (data->view)); + + if (data->distance < 0) + new_value = adj->value - AUTO_SCROLL_PITCH; + else + new_value = adj->value + AUTO_SCROLL_PITCH; + + new_value = CLAMP (new_value, adj->lower, adj->upper - adj->page_size); + + gtk_adjustment_set_value (adj, new_value); + + return TRUE; +} + static gboolean individual_view_drag_motion (GtkWidget *widget, GdkDragContext *context, @@ -561,6 +596,7 @@ individual_view_drag_motion (GtkWidget *widget, GdkAtom target; GtkTreeIter iter; static DragMotionData *dm = NULL; + static AutoScrollData as; GtkTreePath *path; gboolean is_row; gboolean is_different = FALSE; @@ -570,6 +606,24 @@ individual_view_drag_motion (GtkWidget *widget, priv = GET_PRIV (EMPATHY_INDIVIDUAL_VIEW (widget)); model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget)); + if (!as.view) + as.view = EMPATHY_INDIVIDUAL_VIEW (widget); + + if (as.timeout_id) + g_source_remove (as.timeout_id); + + if (y < AUTO_SCROLL_MARGIN_SIZE || + y > (widget->allocation.height - AUTO_SCROLL_MARGIN_SIZE)) + { + if (y < AUTO_SCROLL_MARGIN_SIZE) + as.distance = MIN (-y, -1); + else + as.distance = MAX (widget->allocation.height - y, 1); + + as.timeout_id = g_timeout_add (10 * ABS (as.distance), + (GSourceFunc) individual_view_auto_scroll_cb, &as); + } + is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget), x, y, &path, NULL, NULL, NULL); -- cgit v1.2.3