[gitg] Fix diff line renderer sizing issues



commit c92aa7e049f20414c334fc3bf0da4682f1325cce
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Sun Jul 24 10:59:00 2011 +0200

    Fix diff line renderer sizing issues

 gitg/gitg-diff-line-renderer.c |  105 ++++++++++++++-------------------------
 gitg/gitg-diff-line-renderer.h |    2 +
 gitg/gitg-diff-view.c          |   18 ++++++-
 3 files changed, 56 insertions(+), 69 deletions(-)
---
diff --git a/gitg/gitg-diff-line-renderer.c b/gitg/gitg-diff-line-renderer.c
index a94891e..366119e 100644
--- a/gitg/gitg-diff-line-renderer.c
+++ b/gitg/gitg-diff-line-renderer.c
@@ -248,12 +248,12 @@ render_lines (GtkSourceGutterRenderer      *renderer,
               GtkSourceGutterRendererState  renderer_state)
 {
 	GitgDiffLineRenderer *lr = GITG_DIFF_LINE_RENDERER (renderer);
-	/* Render new/old in the cell area */
 	gchar old_str[16];
 	gchar new_str[16];
 	PangoLayout *layout;
 	GtkWidget *widget;
 	GtkStyleContext *style_context;
+	guint xpad = 0;
 
 	widget = GTK_WIDGET (gtk_source_gutter_renderer_get_view (renderer));
 	layout = lr->priv->cached_layout;
@@ -280,12 +280,14 @@ render_lines (GtkSourceGutterRenderer      *renderer,
 		*new_str = '\0';
 	}
 
+	g_object_get (renderer, "xpad", &xpad, NULL);
+
 	pango_layout_set_text (layout, old_str, -1);
 	style_context = gtk_widget_get_style_context (widget);
 
 	gtk_render_layout (style_context,
 	                   ctx,
-	                   cell_area->x + cell_area->width / 2 - 1,
+	                   cell_area->x + cell_area->width / 2 - xpad,
 	                   cell_area->y,
 	                   layout);
 
@@ -410,87 +412,48 @@ measure_text (GitgDiffLineRenderer *lr,
 static void
 recalculate_size (GitgDiffLineRenderer *lr)
 {
-	/* Get size of this rendering */
-	gint num_digits, num;
+	gchar *markup;
+	gint size;
+	gint num = 1;
+	gint i;
 
-	num_digits = 0;
-	num = lr->priv->line_old;
-
-	while (num > 0)
+	for (i = 1; i < lr->priv->num_digits; ++i)
 	{
-		num /= 10;
-		++num_digits;
+		num *= 10;
 	}
 
-	num = lr->priv->line_new;
+	markup = g_strdup_printf ("<b>%d %d</b>",
+	                          num,
+	                          num);
 
-	while (num > 0)
-	{
-		num /= 10;
-		++num_digits;
-	}
+	measure_text (lr, markup, NULL, &size, NULL);
+	g_free (markup);
 
-	num_digits = MAX (num_digits, 2);
-
-	if (num_digits != lr->priv->num_digits)
-	{
-		gchar *markup;
-		gint size;
-
-		lr->priv->num_digits = num_digits;
-
-		markup = g_strdup_printf ("<b>%d   %d</b>",
-		                          lr->priv->line_old,
-		                          lr->priv->line_new);
-
-		measure_text (lr, markup, NULL, &size, NULL);
-		g_free (markup);
-
-		gtk_source_gutter_renderer_set_size (GTK_SOURCE_GUTTER_RENDERER (lr),
-		                                     size);
-	}
+	gtk_source_gutter_renderer_set_size (GTK_SOURCE_GUTTER_RENDERER (lr),
+	                                     size);
 }
 
 static void
-on_buffer_changed (GtkSourceBuffer      *buffer,
-                   GitgDiffLineRenderer *renderer)
+update_num_digits (GitgDiffLineRenderer *renderer,
+                   guint                 max_line_count)
 {
-	recalculate_size (renderer);
-}
-
-static void
-gitg_diff_line_renderer_change_buffer (GtkSourceGutterRenderer *renderer,
-                                       GtkTextBuffer           *old_buffer)
-{
-	GitgDiffLineRenderer *lr;
-	GtkTextView *view;
+	/* Get size of this rendering */
+	gint num_digits;
 
-	lr = GITG_DIFF_LINE_RENDERER (renderer);
+	num_digits = 0;
 
-	if (old_buffer)
+	while (max_line_count > 0)
 	{
-		g_signal_handler_disconnect (old_buffer,
-		                             lr->priv->changed_handler_id);
+		max_line_count /= 10;
+		++num_digits;
 	}
 
-	view = gtk_source_gutter_renderer_get_view (renderer);
+	num_digits = MAX (num_digits, 2);
 
-	if (view)
+	if (num_digits != renderer->priv->num_digits)
 	{
-		GtkTextBuffer *buffer;
-
-		buffer = gtk_text_view_get_buffer (view);
-
-		if (buffer)
-		{
-			lr->priv->changed_handler_id =
-				g_signal_connect (buffer,
-				                  "changed",
-				                  G_CALLBACK (on_buffer_changed),
-				                  lr);
-
-			recalculate_size (lr);
-		}
+		renderer->priv->num_digits = num_digits;
+		recalculate_size (renderer);
 	}
 }
 
@@ -503,7 +466,6 @@ gitg_diff_line_renderer_class_init (GitgDiffLineRendererClass *klass)
 	renderer_class->begin = gitg_diff_line_renderer_begin;
 	renderer_class->draw = gitg_diff_line_renderer_draw;
 	renderer_class->end= gitg_diff_line_renderer_end;
-	renderer_class->change_buffer = gitg_diff_line_renderer_change_buffer;
 
 	object_class->set_property = gitg_diff_line_renderer_set_property;
 	object_class->get_property = gitg_diff_line_renderer_get_property;
@@ -551,3 +513,12 @@ gitg_diff_line_renderer_new ()
 {
 	return g_object_new (GITG_TYPE_DIFF_LINE_RENDERER, NULL);
 }
+
+void
+gitg_diff_line_renderer_set_max_line_count (GitgDiffLineRenderer *renderer,
+                                            guint                 max_line_count)
+{
+	g_return_if_fail (GITG_IS_DIFF_LINE_RENDERER (renderer));
+
+	update_num_digits (renderer, max_line_count);
+}
diff --git a/gitg/gitg-diff-line-renderer.h b/gitg/gitg-diff-line-renderer.h
index deffacb..1ffdad5 100644
--- a/gitg/gitg-diff-line-renderer.h
+++ b/gitg/gitg-diff-line-renderer.h
@@ -52,6 +52,8 @@ struct _GitgDiffLineRendererClass {
 GType gitg_diff_line_renderer_get_type (void) G_GNUC_CONST;
 GitgDiffLineRenderer *gitg_diff_line_renderer_new (void);
 
+void gitg_diff_line_renderer_set_max_line_count (GitgDiffLineRenderer *renderer,
+                                                 guint max_line_count);
 
 G_END_DECLS
 
diff --git a/gitg/gitg-diff-view.c b/gitg/gitg-diff-view.c
index bc49e37..e7665a5 100644
--- a/gitg/gitg-diff-view.c
+++ b/gitg/gitg-diff-view.c
@@ -184,6 +184,19 @@ region_free (Region   *region,
 }
 
 static void
+set_max_line_count (GitgDiffView *view,
+                    guint         max_line_count)
+{
+	view->priv->max_line_count = max_line_count;
+
+	if (view->priv->line_renderer)
+	{
+		gitg_diff_line_renderer_set_max_line_count (view->priv->line_renderer,
+		                                            max_line_count);
+	}
+}
+
+static void
 regions_free (GitgDiffView *view)
 {
 	region_free (view->priv->regions, TRUE);
@@ -194,7 +207,8 @@ regions_free (GitgDiffView *view)
 	view->priv->regions = NULL;
 	view->priv->last_region = NULL;
 	view->priv->last_scan_line = 0;
-	view->priv->max_line_count = 99;
+
+	set_max_line_count (view, 99);
 }
 
 static void
@@ -499,7 +513,7 @@ ensure_max_line (GitgDiffView *view, Hunk *hunk)
 
 	if (m > view->priv->max_line_count)
 	{
-		view->priv->max_line_count = m;
+		set_max_line_count (view, m);
 
 		gtk_source_gutter_queue_draw (gtk_source_view_get_gutter (GTK_SOURCE_VIEW (view), GTK_TEXT_WINDOW_LEFT));
 	}



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