[gtksourceview] Fixed scrolling after an undo or redo



commit ee1af8178139ffe5d1ccd6ad6f882f25290a9e58
Author: Garrett Regier <alias301 gmail com>
Date:   Mon May 31 13:52:50 2010 -0700

    Fixed scrolling after an undo or redo
    
    Scrolling after an undo or redo was broken now we do the following:
    
    If the insert mark is hidden vertically then scroll to it
    and align it to the middle of the X and Y axis.
    
    Otherwise, if the insert mark is hidden horizontally then scroll to it
    and align it to the middle of the X axis. Because we cannot prevent it from
    also aligning to the Y axis we revert the vertical position of the view.

 gtksourceview/gseal-gtk-compat.h |    5 +++--
 gtksourceview/gtksourceview.c    |   31 ++++++++++++++++++++++++-------
 2 files changed, 27 insertions(+), 9 deletions(-)
---
diff --git a/gtksourceview/gseal-gtk-compat.h b/gtksourceview/gseal-gtk-compat.h
index 7ca287b..5946ca4 100644
--- a/gtksourceview/gseal-gtk-compat.h
+++ b/gtksourceview/gseal-gtk-compat.h
@@ -24,8 +24,9 @@
 G_BEGIN_DECLS
 
 #if !GTK_CHECK_VERSION (2, 21, 0)
-#define gtk_text_view_im_context_filter_keypress(view, event)   (gtk_im_context_filter_keypress (GTK_TEXT_VIEW (view)->im_context, event))
-#define gtk_text_view_get_hadjustment(view)                     (GTK_TEXT_VIEW (view)->hadjustment)
+#define gtk_text_view_im_context_filter_keypress(view, event)   (gtk_im_context_filter_keypress (view->im_context, event))
+#define gtk_text_view_get_hadjustment(view)                     (view->hadjustment)
+#define gtk_text_view_get_vadjustment(view)                     (view->vadjustment)
 #endif /* GTK < 2.22.0 */
 
 #if !GTK_CHECK_VERSION (2, 20, 0)
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index e76d4a7..3b02316 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -1879,26 +1879,43 @@ scroll_to_insert (GtkSourceView *view,
 	GtkTextMark *insert;
 	GtkTextIter iter;
 	GdkRectangle visible, location;
-	
+
 	insert = gtk_text_buffer_get_insert (buffer);
 	gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
 
 	gtk_text_view_get_visible_rect (GTK_TEXT_VIEW (view), &visible);
 	gtk_text_view_get_iter_location (GTK_TEXT_VIEW (view), &iter, &location);
 
-	if (location.x >= visible.x && location.x < visible.x + visible.width &&
-	    location.y >= visible.y && location.x < visible.y + visible.height)
+	if (location.y < visible.y || location.y > visible.y + visible.height)
 	{
 		gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
 					      insert,
 					      0.0,
 					      TRUE,
-					      0.0, 0.5);
+					      0.5, 0.5);
 	}
-	else
+	else if (location.x < visible.x || location.x > visible.x + visible.width)
 	{
-		gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (view),
-						    insert);
+		gdouble position;
+		GtkAdjustment *adjustment;
+
+		/* We revert the vertical position of the view because
+		 * _scroll_to_iter will cause it to move and the
+		 * insert mark is already visible vertically. */
+
+		adjustment = gtk_text_view_get_vadjustment (GTK_TEXT_VIEW (view));
+		position = gtk_adjustment_get_value (adjustment);
+
+		/* Must use _to_iter as _to_mark scrolls in an
+		 * idle handler and would prevent use from
+		 * reverting the vertical position of the view. */
+		gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (view),
+					      &iter,
+					      0.0,
+					      TRUE,
+					      0.5, 0.0);
+
+		gtk_adjustment_set_value (adjustment, position);
 	}
 }
 



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