[gtksourceview] Port to latest rendering API changes in gtk.



commit 6d91a3ea51e75c9aa6de97fdcd30b75741ecb091
Author: Garret Regier <alias301 gmail com>
Date:   Mon Sep 27 17:01:45 2010 -0400

    Port to latest rendering API changes in gtk.

 gtksourceview/gtksourcecompletioninfo.c |   22 ++----
 gtksourceview/gtksourcegutter.c         |   56 +++++++-------
 gtksourceview/gtksourceview.c           |  117 +++++++++++++++++--------------
 3 files changed, 100 insertions(+), 95 deletions(-)
---
diff --git a/gtksourceview/gtksourcecompletioninfo.c b/gtksourceview/gtksourcecompletioninfo.c
index 0dc2a02..c35ef98 100644
--- a/gtksourceview/gtksourcecompletioninfo.c
+++ b/gtksourceview/gtksourcecompletioninfo.c
@@ -288,26 +288,20 @@ gtk_source_completion_info_show (GtkWidget *widget)
 }
 
 static gboolean
-gtk_source_completion_info_expose (GtkWidget      *widget,
-                                   GdkEventExpose *expose)
+gtk_source_completion_info_draw (GtkWidget *widget,
+                                 cairo_t   *cr)
 {
-	GtkAllocation allocation;
+	GTK_WIDGET_CLASS (gtk_source_completion_info_parent_class)->draw (widget, cr);
 
-	GTK_WIDGET_CLASS (gtk_source_completion_info_parent_class)->expose_event (widget, expose);
-
-	gtk_widget_get_allocation (widget, &allocation);
-	
 	gtk_paint_shadow (gtk_widget_get_style (widget),
-			  gtk_widget_get_window (widget),
+			  cr,
 			  GTK_STATE_NORMAL,
 			  GTK_SHADOW_OUT,
-			  NULL,
 			  widget,
 			  NULL,
-			  allocation.x,
-			  allocation.y,
-			  allocation.width,
-			  allocation.height);
+			  0, 0,
+			  gtk_widget_get_allocated_width (widget),
+			  gtk_widget_get_allocated_height (widget));
 
 	return FALSE;
 }
@@ -323,7 +317,7 @@ gtk_source_completion_info_class_init (GtkSourceCompletionInfoClass *klass)
 	object_class->finalize = gtk_source_completion_info_finalize;
 	
 	widget_class->show = gtk_source_completion_info_show;
-	widget_class->expose_event = gtk_source_completion_info_expose;
+	widget_class->draw = gtk_source_completion_info_draw;
 	
 	/**
 	 * GtkSourceCompletionInfo::show-info:
diff --git a/gtksourceview/gtksourcegutter.c b/gtksourceview/gtksourcegutter.c
index 86b1766..4f0c608 100644
--- a/gtksourceview/gtksourcegutter.c
+++ b/gtksourceview/gtksourcegutter.c
@@ -91,7 +91,7 @@ typedef struct
 
 enum
 {
-	EXPOSE_EVENT,
+	DRAW,
 	MOTION_NOTIFY_EVENT,
 	BUTTON_PRESS_EVENT,
 	ENTER_NOTIFY_EVENT,
@@ -111,9 +111,9 @@ struct _GtkSourceGutterPrivate
 
 G_DEFINE_TYPE (GtkSourceGutter, gtk_source_gutter, G_TYPE_OBJECT)
 
-static gboolean on_view_expose_event (GtkSourceView   *view,
-                                      GdkEventExpose  *event,
-                                      GtkSourceGutter *gutter);
+static gboolean on_view_draw (GtkSourceView   *view,
+                              cairo_t         *cr,
+                              GtkSourceGutter *gutter);
 
 static gboolean on_view_motion_notify_event (GtkSourceView   *view,
                                              GdkEventMotion  *event,
@@ -242,10 +242,10 @@ set_view (GtkSourceGutter *gutter,
 	                   (GWeakNotify)view_notify,
 	                   gutter);
 
-	gutter->priv->signals[EXPOSE_EVENT] =
+	gutter->priv->signals[DRAW] =
 		g_signal_connect (view,
-	                  "expose-event",
-	                  G_CALLBACK (on_view_expose_event),
+	                  "draw",
+	                  G_CALLBACK (on_view_draw),
 	                  gutter);
 
 	gutter->priv->signals[MOTION_NOTIFY_EVENT] =
@@ -303,7 +303,7 @@ revalidate_size (GtkSourceGutter *gutter)
 	if (!window && gutter->priv->renderers)
 	{
 		/* Make window visible by setting its size to minimum size,
-		   actual size will be calculated in expose */
+		   actual size will be calculated in draw */
 		gtk_text_view_set_border_window_size (GTK_TEXT_VIEW (gutter->priv->view),
 		                                      gutter->priv->window_type,
 		                                      1);
@@ -318,7 +318,7 @@ revalidate_size (GtkSourceGutter *gutter)
 	}
 	else if (window)
 	{
-		/* Redraw the window. Actual size will be calculated in expose */
+		/* Redraw the window. Actual size will be calculated in draw */
 		do_redraw (gutter);
 	}
 }
@@ -727,14 +727,13 @@ calculate_size (GtkSourceGutter  *gutter,
 
 	if (width == -1 && renderer->size_func)
 	{
-		GtkRequisition min_size;
-
+                GtkRequisition req;
 		renderer->size_func (gutter, renderer->renderer, renderer->size_func_data);
 
-		gtk_cell_size_request_get_size (GTK_CELL_SIZE_REQUEST (renderer->renderer),
-		                                GTK_WIDGET (gutter->priv->view),
-		                                &min_size, NULL);
-		width = min_size.width;
+		gtk_cell_renderer_get_preferred_size (renderer->renderer,
+                                                      GTK_WIDGET (gutter->priv->view),
+                                                      &req, NULL);
+                width = req.width;
 	}
 
 	return width == -1 ? 1 : width;
@@ -860,29 +859,32 @@ get_lines (GtkTextView  *text_view,
 }
 
 static gboolean
-on_view_expose_event (GtkSourceView   *view,
-                      GdkEventExpose  *event,
-                      GtkSourceGutter *gutter)
+on_view_draw (GtkSourceView   *view,
+              cairo_t         *cr,
+              GtkSourceGutter *gutter)
 {
 	GdkWindow *window;
 	GtkTextView *text_view;
 	GArray *sizes;
+        GdkRectangle clip;
 	gint size;
 
 	window = gtk_source_gutter_get_window (gutter);
 
-	if (window == NULL || event->window != window)
+	if (window == NULL || !gtk_cairo_should_draw_window (cr, window))
 	{
 		return FALSE;
 	}
 
+        gtk_cairo_transform_to_window (cr, GTK_WIDGET (view), window);
+
 	text_view = GTK_TEXT_VIEW (view);
 	sizes = g_array_new (FALSE, FALSE, sizeof (gint));
 
 	/* This is fairly ugly, but we could not find a better way to
 	 * do it: renderers could have changed size and they do not have
-	 * a way to signal it. So on expose we revalidate the size and
-	 * requeue another expose.
+	 * a way to signal it. So on draw we revalidate the size and
+	 * requeue another draw.
 	 * To see if the size has changed we test the size of only the
 	 * gutter itself since the full border window size is not under
 	 * our control (see e.g bug #589382).
@@ -892,7 +894,6 @@ on_view_expose_event (GtkSourceView   *view,
 	size = calculate_sizes (gutter, sizes);
 	if (gutter->priv->size != size)
 	{
-
 		gint border_size;
 
 		border_size = gtk_text_view_get_border_window_size (text_view, gutter->priv->window_type);
@@ -904,10 +905,10 @@ on_view_expose_event (GtkSourceView   *view,
 
 		gutter->priv->size = size;
 
-		/* Will trigger a new expose */
+		/* Will trigger a new draw */
 		gtk_text_view_set_border_window_size (text_view, gutter->priv->window_type, border_size);
 	}
-	else
+	else if (gdk_cairo_get_clip_rectangle (cr, &clip))
 	{
 		GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
 		gint x, y;
@@ -923,8 +924,8 @@ on_view_expose_event (GtkSourceView   *view,
 
 		gdk_window_get_pointer (window, &x, &y, NULL);
 
-		y1 = event->area.y;
-		y2 = y1 + event->area.height;
+		y1 = clip.y;
+		y2 = y1 + clip.height;
 
 		/* get the extents of the line printing */
 		gtk_text_view_window_to_buffer_coords (text_view,
@@ -1007,11 +1008,10 @@ on_view_expose_event (GtkSourceView   *view,
 
 				/* Call render with correct area */
 				gtk_cell_renderer_render (renderer->renderer,
-					                  window,
+					                  cr,
 					                  GTK_WIDGET (view),
 					                  &cell_area,
 					                  &cell_area,
-					                  &cell_area,
 					                  state);
 
 				cell_area.x += cell_area.width;
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 655b601..41bf58a 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -234,8 +234,8 @@ static void 	gtk_source_view_get_lines 		(GtkTextView       *text_view,
 				       			 GArray            *line_heights,
 				       			 GArray            *numbers,
 				       			 gint              *countp);
-static gint     gtk_source_view_expose 			(GtkWidget         *widget,
-							 GdkEventExpose    *event);
+static gboolean gtk_source_view_draw 			(GtkWidget         *widget,
+							 cairo_t           *cr);
 static void	gtk_source_view_move_lines		(GtkSourceView     *view,
 							 gboolean           copy,
 							 gint               step);
@@ -303,7 +303,7 @@ gtk_source_view_class_init (GtkSourceViewClass *klass)
 	object_class->set_property = gtk_source_view_set_property;
 
 	widget_class->key_press_event = gtk_source_view_key_press_event;
-	widget_class->expose_event = gtk_source_view_expose;
+	widget_class->draw = gtk_source_view_draw;
 	widget_class->style_set = gtk_source_view_style_set;
 	widget_class->realize = gtk_source_view_realize;
 	gtk_object_class->destroy = gtk_source_view_destroy;
@@ -2392,7 +2392,7 @@ gtk_source_view_get_lines (GtkTextView  *text_view,
 
 static void
 gtk_source_view_paint_line_background (GtkTextView    *text_view,
-				       GdkEventExpose *event,
+				       cairo_t        *cr,
 				       int             y, /* in buffer coordinates */
 				       int             height,
 				       const GdkColor *color)
@@ -2401,7 +2401,6 @@ gtk_source_view_paint_line_background (GtkTextView    *text_view,
 	GdkRectangle line_rect;
 	gint win_y;
 	gint margin;
-	cairo_t *cr;
 	GtkAdjustment *hadjustment = gtk_text_view_get_hadjustment (text_view);
 
 	gtk_text_view_get_visible_rect (text_view, &visible_rect);
@@ -2426,21 +2425,20 @@ gtk_source_view_paint_line_background (GtkTextView    *text_view,
 
 	line_rect.x += MAX (0, margin - 1);
 
-	cr = gdk_cairo_create (event->window);
 	gdk_cairo_set_source_color (cr, (GdkColor *)color);
 	cairo_set_line_width (cr, 1);
 	cairo_rectangle (cr, line_rect.x + .5, line_rect.y + .5,
 			 line_rect.width - 1, line_rect.height - 1);
 	cairo_stroke_preserve (cr);
 	cairo_fill (cr);
-	cairo_destroy (cr);
 }
 
 static void
-gtk_source_view_paint_marks_background (GtkSourceView  *view,
-					GdkEventExpose *event)
+gtk_source_view_paint_marks_background (GtkSourceView *view,
+					cairo_t       *cr)
 {
 	GtkTextView *text_view;
+        GdkRectangle clip;
 	GArray *numbers;
 	GArray *pixels;
 	GArray *heights;
@@ -2448,13 +2446,14 @@ gtk_source_view_paint_marks_background (GtkSourceView  *view,
 	gint count;
 	gint i;
 
-	if (view->priv->source_buffer == NULL)
+	if (view->priv->source_buffer == NULL ||
+            !gdk_cairo_get_clip_rectangle (cr, &clip))
 		return;
 
 	text_view = GTK_TEXT_VIEW (view);
 
-	y1 = event->area.y;
-	y2 = y1 + event->area.height;
+	y1 = clip.y;
+	y2 = y1 + clip.height;
 
 	/* get the extents of the line printing */
 	gtk_text_view_window_to_buffer_coords (text_view,
@@ -2538,7 +2537,7 @@ gtk_source_view_paint_marks_background (GtkSourceView  *view,
 		}
 
 		if (background != NULL)
-			gtk_source_view_paint_line_background (text_view, event,
+			gtk_source_view_paint_line_background (text_view, cr,
 							       g_array_index (pixels, gint, i),
 							       g_array_index (heights, gint, i),
 							       background);
@@ -2781,22 +2780,26 @@ check_location (GtkSourceView *view,
 		return location & flags;
 	}
 }
+
 static void
-draw_tabs_and_spaces (GtkSourceView  *view,
-		      GdkEventExpose *event)
+draw_tabs_and_spaces (GtkSourceView *view,
+		      cairo_t       *cr)
 {
 	GtkTextView *text_view;
+        GdkRectangle clip;
 	gint x1, y1, x2, y2;
 	GtkTextIter s, e;
-	cairo_t *cr;
 	GtkTextIter leading, trailing;
 
+	if (!gdk_cairo_get_clip_rectangle (cr, &clip))
+		return;
+
 	text_view = GTK_TEXT_VIEW (view);
 
-	x1 = event->area.x;
-	y1 = event->area.y;
-	x2 = x1 + event->area.width;
-	y2 = y1 + event->area.height;
+	x1 = clip.x;
+	y1 = clip.y;
+	x2 = x1 + clip.width;
+	y2 = y1 + clip.height;
 
 	gtk_text_view_window_to_buffer_coords (text_view,
 					       GTK_TEXT_WINDOW_TEXT,
@@ -2819,8 +2822,6 @@ draw_tabs_and_spaces (GtkSourceView  *view,
                                              &e,
                                              x2, y2);
 
-	cr = gdk_cairo_create (event->window);
-
 	cairo_set_source_rgba (cr,
 			       view->priv->spaces_color->red / 65535.,
 			       view->priv->spaces_color->green / 65535.,
@@ -2883,16 +2884,14 @@ draw_tabs_and_spaces (GtkSourceView  *view,
 	} while (gtk_text_iter_compare (&s, &e) <= 0);
 
 	cairo_stroke (cr);
-	cairo_destroy (cr);
 }
 
 static void
-gtk_source_view_paint_right_margin (GtkSourceView  *view,
-                                    GdkEventExpose *event)
+gtk_source_view_paint_right_margin (GtkSourceView *view,
+                                    cairo_t       *cr)
 {
 	GdkRectangle visible_rect;
 	GdkRectangle redraw_rect;
-	cairo_t *cr;
 	double x;
 
 	GtkTextView *text_view = GTK_TEXT_VIEW (view);
@@ -2930,13 +2929,6 @@ gtk_source_view_paint_right_margin (GtkSourceView  *view,
 	redraw_rect.width = visible_rect.width;
 	redraw_rect.height = visible_rect.height;
 
-	cr = gdk_cairo_create (gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT));
-
-	/* Set a clip region for the expose event. */
-	cairo_rectangle (cr, event->area.x, event->area.y,
-			 event->area.width, event->area.height);
-	cairo_clip (cr);
-
 	/* Offset with 0.5 is needed for a sharp line. */
 	x = view->priv->cached_right_margin_pos -
 		visible_rect.x + redraw_rect.x + 0.5 +
@@ -2975,8 +2967,6 @@ gtk_source_view_paint_right_margin (GtkSourceView  *view,
 		cairo_fill (cr);
 	}
 
-	cairo_destroy (cr);
-
 	PROFILE ({
 		g_timer_stop (timer);
 		g_message ("Time to draw the margin: %g (sec * 1000)",
@@ -2984,26 +2974,32 @@ gtk_source_view_paint_right_margin (GtkSourceView  *view,
 	});
 }
 
-static gint
-gtk_source_view_expose (GtkWidget      *widget,
-			GdkEventExpose *event)
+static gboolean
+gtk_source_view_draw (GtkWidget *widget,
+	              cairo_t   *cr)
 {
 	GtkSourceView *view;
 	GtkTextView *text_view;
+	GdkWindow *window;
 	gboolean event_handled;
 
 	DEBUG ({
-		g_print ("> gtk_source_view_expose start\n");
+		g_print ("> gtk_source_view_draw start\n");
 	});
 
 	view = GTK_SOURCE_VIEW (widget);
 	text_view = GTK_TEXT_VIEW (widget);
 
+	window = gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT);
+
+        cairo_save (cr);
+        gtk_cairo_transform_to_window (cr, widget, window);
+
 	event_handled = FALSE;
 
-	/* check if the expose event is for the text window first, and
+	/* check if the draw is for the text window first, and
 	 * make sure the visible region is highlighted */
-	if (event->window == gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT) &&
+	if (gtk_cairo_should_draw_window (cr, window) &&
 	    view->priv->source_buffer != NULL)
 	{
 		GdkRectangle visible_rect;
@@ -3019,7 +3015,7 @@ gtk_source_view_expose (GtkWidget      *widget,
 		gtk_text_iter_forward_line (&iter2);
 
 		DEBUG ({
-			g_print ("    exposed area: %d - %d\n", visible_rect.y,
+			g_print ("    draw area: %d - %d\n", visible_rect.y,
 				 visible_rect.y + visible_rect.height);
 			g_print ("    lines to update: %d - %d\n",
 				 gtk_text_iter_get_line (&iter1),
@@ -3031,7 +3027,7 @@ gtk_source_view_expose (GtkWidget      *widget,
 	}
 
 	if (gtk_widget_is_sensitive (widget) && view->priv->highlight_current_line &&
-		    (event->window == gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT)))
+	    gtk_cairo_should_draw_window (cr, window))
 	{
 		GtkTextIter cur;
 		gint y, height;
@@ -3055,34 +3051,49 @@ gtk_source_view_expose (GtkWidget      *widget,
 			color = &style->bg[state];
 		}
 
-		gtk_source_view_paint_line_background (text_view, event, y, height, color);
+		gtk_source_view_paint_line_background (text_view, cr, y, height, color);
 	}
 
-	if (event->window == gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT))
-		gtk_source_view_paint_marks_background (view, event);
+	if (gtk_cairo_should_draw_window (cr, window))
+		gtk_source_view_paint_marks_background (view, cr);
 
 	/* Have GtkTextView draw the text first. */
-	if (GTK_WIDGET_CLASS (gtk_source_view_parent_class)->expose_event)
+	if (GTK_WIDGET_CLASS (gtk_source_view_parent_class)->draw)
+	{
+            /* Need to restore to original state here so the parent draw func
+             * gets the correct context. */
+		cairo_restore (cr);
+
+		cairo_save (cr);
+
 		event_handled =
-			GTK_WIDGET_CLASS (gtk_source_view_parent_class)->expose_event (widget, event);
+                    GTK_WIDGET_CLASS (gtk_source_view_parent_class)->draw (widget, cr);
+
+		cairo_restore (cr);
+
+		cairo_save (cr);
+		gtk_cairo_transform_to_window (cr, widget, window);
+	}
 
 	/* Draw the right margin vertical line + overlay. */
 	if (view->priv->show_right_margin &&
-	    (event->window == gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT)))
+	    gtk_cairo_should_draw_window (cr, window))
 	{
-		gtk_source_view_paint_right_margin (view, event);
+		gtk_source_view_paint_right_margin (view, cr);
 	}
 
 	if (view->priv->draw_spaces != 0 &&
-	    (event->window == gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT)))
+	    gtk_cairo_should_draw_window (cr, window))
 	{
-		draw_tabs_and_spaces (view, event);
+		draw_tabs_and_spaces (view, cr);
 	}
 
 	DEBUG ({
-		g_print ("> gtk_source_view_expose end\n");
+		g_print ("> gtk_source_view_draw end\n");
 	});
 
+        cairo_restore (cr);
+
 	return event_handled;
 }
 



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