[perl-Gtk2] Gtk2::Gdk::Drawable->draw_lines() and draw_points(): allow empty input



commit 6ea16bfef0c2a1c8408c811fc4740c140c312b5e
Author: Kevin Ryde <user42 zip com au>
Date:   Sun Nov 28 10:38:06 2010 +1100

    Gtk2::Gdk::Drawable->draw_lines() and draw_points(): allow empty input
    
    Handy when passing an array to those methods so you don't have to
    check if it's empty.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=622774

 t/GdkDrawable.t   |    2 ++
 xs/GdkDrawable.xs |   42 ++++++++++++++++++++++++++----------------
 2 files changed, 28 insertions(+), 16 deletions(-)
---
diff --git a/t/GdkDrawable.t b/t/GdkDrawable.t
index ea47a21..d0d76f2 100644
--- a/t/GdkDrawable.t
+++ b/t/GdkDrawable.t
@@ -62,8 +62,10 @@ my $gc = Gtk2::Gdk::GC -> new_with_values($window -> window(), $values);
 my $layout = $window -> create_pango_layout("Bla!");
 
 $win -> draw_point($gc, 10, 10);
+$win -> draw_points($gc);  # no points
 $win -> draw_points($gc, 10, 10, 11, 11, 12, 12, 13, 13);
 $win -> draw_line($gc, 5, 5, 10, 10);
+$win -> draw_lines($gc);  # no lines
 $win -> draw_lines($gc, 5, 5, 10, 10, 15, 15, 20, 20);
 $win -> draw_segments($gc);
 $win -> draw_segments($gc, 1, 2, 3, 4, 10, 11, 12, 13);
diff --git a/xs/GdkDrawable.xs b/xs/GdkDrawable.xs
index 9ad6181..0fb9256 100644
--- a/xs/GdkDrawable.xs
+++ b/xs/GdkDrawable.xs
@@ -21,6 +21,10 @@
 
 #include "gtk2perl.h"
 
+#ifndef G_LIKELY  /* new in glib 2.2 */
+#define G_LIKELY(cond)  (cond)  /* fallback */
+#endif
+
 /*
 NOTE:
 GdkDrawable descends directly from GObject, so be sure to use GdkDrawable_noinc
@@ -185,18 +189,20 @@ gdk_draw_point (drawable, gc, x, y)
  ## void gdk_draw_lines (GdkDrawable *drawable, GdkGC *gc, GdkPoint *points, gint npoints)
 
 =for apidoc Gtk2::Gdk::Drawable::draw_lines
-=for arg x1 (integer) the x coordinate of the first point
-=for arg y1 (integer) the y coordinate of the first point
-=for arg ... pairs of x and y coordinates
+=for arg ... integer x,y coordinates (possibly none)
+For example
+
+    $win->draw_lines ($gc, 0,0, 20,30, 40,20);
 =cut
 
 =for apidoc
-=for arg x1 (integer) the x coordinate of the first point
-=for arg y1 (integer) the y coordinate of the first point
-=for arg ... (__hide__)
+=for arg ... integer x,y coordinates (possibly none)
+For example three points
+
+    $win->draw_points ($gc, 0,0, 10,10, 20,20);
 =cut
 void
-gdk_draw_points (drawable, gc, x1, y1, ...)
+gdk_draw_points (drawable, gc, ...)
 	GdkDrawable *drawable
 	GdkGC *gc
     ALIAS:
@@ -207,16 +213,20 @@ gdk_draw_points (drawable, gc, x1, y1, ...)
 	gint i, j;
     CODE:
 	npoints = (items-2)/2;
-	points = g_new (GdkPoint, npoints);
-	for (i = 0, j = 2; i < npoints ; i++, j+=2) {
-		points[i].x = SvIV (ST (j));
-		points[i].y = SvIV (ST (j+1));
+	/* gdk_draw_points() and gdk_draw_lines() both accept npoints==0 but
+	   can skip entirely with a couple of bytes of code. */
+	if (G_LIKELY (npoints != 0)) {
+		points = g_new (GdkPoint, npoints);
+		for (i = 0, j = 2; i < npoints ; i++, j+=2) {
+			points[i].x = SvIV (ST (j));
+			points[i].y = SvIV (ST (j+1));
+		}
+		if (ix == 1)
+			gdk_draw_lines (drawable, gc, points, npoints);
+		else
+			gdk_draw_points (drawable, gc, points, npoints);
+		g_free (points);
 	}
-	if (ix == 1)
-		gdk_draw_lines (drawable, gc, points, npoints);
-	else
-		gdk_draw_points (drawable, gc, points, npoints);
-	g_free (points);
 
  #### void gdk_draw_segments (GdkDrawable *drawable, GdkGC *gc, GdkSegment *segs, gint nsegs)
 =for apidoc



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