[shotwell] map: Create Clutter.Image icon loader method



commit 25f98eeb28ca236b5193e6ba3a82486f5bfc256f
Author: Andreas Brauchli <a brauchli elementarea net>
Date:   Sun May 29 23:04:27 2016 +0200

    map: Create Clutter.Image icon loader method

 src/MapWidget.vala | 33 ++++++++-------------------------
 src/Resources.vala | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 25 deletions(-)
---
diff --git a/src/MapWidget.vala b/src/MapWidget.vala
index b949ecad..cca9e5af 100644
--- a/src/MapWidget.vala
+++ b/src/MapWidget.vala
@@ -256,31 +256,14 @@ private class MapWidget : Gtk.Bin {
         button_press_event.connect(map_zoom_handler);
         set_size_request(200, 200);
 
-        // Load gdk pixbuf via Resources class
-        Gdk.Pixbuf gdk_marker = Resources.get_icon(Resources.ICON_GPS_MARKER);
-        Gdk.Pixbuf gdk_marker_selected = Resources.get_icon(Resources.ICON_GPS_MARKER_SELECTED);
-        try {
-            // this is what GtkClutter.Texture.set_from_pixmap does
-            marker_image = new Clutter.Image();
-            marker_image.set_data(gdk_marker.get_pixels(),
-                    gdk_marker.get_has_alpha() ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888,
-                    gdk_marker.get_width(),
-                    gdk_marker.get_height(),
-                    gdk_marker.get_rowstride());
-
-            marker_selected_image = new Clutter.Image();
-            marker_selected_image.set_data(gdk_marker_selected.get_pixels(),
-                    gdk_marker_selected.get_has_alpha() ? Cogl.PixelFormat.RGBA_8888 : 
Cogl.PixelFormat.RGB_888,
-                    gdk_marker_selected.get_width(),
-                    gdk_marker_selected.get_height(),
-                    gdk_marker_selected.get_rowstride());
-
-            marker_image_width = gdk_marker.get_width();
-            marker_image_height = gdk_marker.get_height();
-        } catch (GLib.Error e) {
-            // Fall back to the generic champlain marker
-            marker_image = null;
-            marker_selected_image = null;
+        // Load icons
+        float w, h;
+        marker_image = Resources.get_icon_as_clutter_image(
+                Resources.ICON_GPS_MARKER, out w, out h);
+        marker_image_width = w;
+        marker_image_height = h;
+        marker_selected_image = Resources.get_icon_as_clutter_image(
+                Resources.ICON_GPS_MARKER_SELECTED, out w, out h);
         }
     }
 
diff --git a/src/Resources.vala b/src/Resources.vala
index 54c109c6..c260a5a0 100644
--- a/src/Resources.vala
+++ b/src/Resources.vala
@@ -1068,6 +1068,27 @@ along with Shotwell; if not, write to the Free Software Foundation, Inc.,
         return (scale > 0) ? scale_pixbuf(pixbuf, scale, Gdk.InterpType.BILINEAR, false) : pixbuf;
     }
     
+    // Helper method for loading the result of get_icon into a Clutter.Image
+    // Returns null on error, in which case width and height are set to 0.
+    public static Clutter.Image? get_icon_as_clutter_image(string name, out float width, out float height) {
+        Gdk.Pixbuf pixbuf = Resources.get_icon(name);
+        Clutter.Image clutter_image = new Clutter.Image();
+        try {
+            clutter_image.set_data(pixbuf.get_pixels(),
+                pixbuf.get_has_alpha() ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888,
+                pixbuf.get_width(),
+                pixbuf.get_height(),
+                pixbuf.get_rowstride());
+            width = pixbuf.get_width();
+            height = pixbuf.get_height();
+        } catch (GLib.Error e) {
+            width = 0;
+            height = 0;
+            return null;
+        }
+        return clutter_image;
+    }
+
     // Get the directory where our help files live.  Returns a string
     // describing the help path we want, or, if we're installed system
     // -wide already, returns null.


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