[gnome-photos] tool-filters: Use a separate derived class for the buttons



commit 5ac749cd428ae22055b0066f07d466cc5c3bef2f
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Dec 3 16:51:20 2015 +0100

    tool-filters: Use a separate derived class for the buttons

 src/Makefile.am                 |    2 +
 src/photos-tool-filter-button.c |  255 +++++++++++++++++++++++++++++++++++++++
 src/photos-tool-filter-button.h |   50 ++++++++
 src/photos-tool-filters.c       |   59 ++--------
 4 files changed, 315 insertions(+), 51 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 5bd946b..15c858f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -202,6 +202,8 @@ gnome_photos_SOURCES = \
        photos-tool-crop.h \
        photos-tool-enhance.c \
        photos-tool-enhance.h \
+       photos-tool-filter-button.c \
+       photos-tool-filter-button.h \
        photos-tool-filters.c \
        photos-tool-filters.h \
        photos-tracker-change-event.c \
diff --git a/src/photos-tool-filter-button.c b/src/photos-tool-filter-button.c
new file mode 100644
index 0000000..ddacaac
--- /dev/null
+++ b/src/photos-tool-filter-button.c
@@ -0,0 +1,255 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2015 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+
+#include "config.h"
+
+#include <gdk/gdk.h>
+#include <gio/gio.h>
+
+#include "photos-application.h"
+#include "photos-icons.h"
+#include "photos-tool-filter-button.h"
+#include "photos-utils.h"
+
+
+struct _PhotosToolFilterButton
+{
+  GtkBin parent_instance;
+  GtkWidget *button;
+  gchar *label;
+};
+
+struct _PhotosToolFilterButtonClass
+{
+  GtkBinClass parent_class;
+};
+
+enum
+{
+  PROP_0,
+  PROP_LABEL,
+
+  /* GtkActionable properties */
+  PROP_ACTION_NAME,
+  PROP_ACTION_TARGET
+};
+
+static void photos_tool_filter_button_actionable_iface_init (GtkActionableInterface *iface);
+
+
+G_DEFINE_TYPE_EXTENDED (PhotosToolFilterButton, photos_tool_filter_button, GTK_TYPE_BIN, 0,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE,
+                                               photos_tool_filter_button_actionable_iface_init));
+
+
+static const gchar *
+photos_tool_filter_button_get_action_name (GtkActionable *actionable)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (actionable);
+  return gtk_actionable_get_action_name (GTK_ACTIONABLE (self->button));
+}
+
+
+static GVariant *
+photos_tool_filter_button_get_action_target_value (GtkActionable *actionable)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (actionable);
+  return gtk_actionable_get_action_target_value (GTK_ACTIONABLE (self->button));
+}
+
+
+static void
+photos_tool_filter_button_set_action_name (GtkActionable *actionable, const gchar *action_name)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (actionable);
+  gtk_actionable_set_action_name (GTK_ACTIONABLE (self->button), action_name);
+}
+
+
+static void
+photos_tool_filter_button_set_action_target_value (GtkActionable *actionable, GVariant *action_target)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (actionable);
+  gtk_actionable_set_action_target_value (GTK_ACTIONABLE (self->button), action_target);
+}
+
+
+static void
+photos_tool_filter_button_constructed (GObject *object)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (object);
+  GApplication *app;
+  GdkPixbuf *preview_icon = NULL;
+  GtkWidget *image;
+  cairo_surface_t *preview_icon_surface = NULL;
+  gint scale;
+
+  G_OBJECT_CLASS (photos_tool_filter_button_parent_class)->constructed (object);
+
+  app = g_application_get_default ();
+  scale = photos_application_get_scale_factor (PHOTOS_APPLICATION (app));
+  preview_icon = photos_utils_create_placeholder_icon_for_scale (PHOTOS_ICON_CONTENT_LOADING_SYMBOLIC, 96, 
scale);
+  if (preview_icon != NULL)
+    preview_icon_surface = gdk_cairo_surface_create_from_pixbuf (preview_icon, scale, NULL);
+
+  image = gtk_image_new_from_surface (preview_icon_surface);
+  self->button = gtk_button_new_with_label (self->label);
+  gtk_button_set_always_show_image (GTK_BUTTON (self->button), TRUE);
+  gtk_button_set_image (GTK_BUTTON (self->button), image);
+  gtk_button_set_image_position (GTK_BUTTON (self->button), GTK_POS_TOP);
+  gtk_button_set_relief (GTK_BUTTON (self->button), GTK_RELIEF_NONE);
+  gtk_container_add (GTK_CONTAINER (self), self->button);
+
+  g_clear_object (&preview_icon);
+  g_clear_pointer (&preview_icon_surface, (GDestroyNotify) cairo_surface_destroy);
+}
+
+
+static void
+photos_tool_filter_button_finalize (GObject *object)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (object);
+
+  g_free (self->label);
+
+  G_OBJECT_CLASS (photos_tool_filter_button_parent_class)->finalize (object);
+}
+
+
+static void
+photos_tool_filter_button_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACTION_NAME:
+      {
+        const gchar *action_name;
+
+        action_name = photos_tool_filter_button_get_action_name (GTK_ACTIONABLE (self));
+        g_value_set_string (value, action_name);
+        break;
+      }
+
+    case PROP_ACTION_TARGET:
+      {
+        GVariant *action_target;
+
+        action_target = photos_tool_filter_button_get_action_target_value (GTK_ACTIONABLE (self));
+        g_value_set_variant (value, action_target);
+        break;
+      }
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+static void
+photos_tool_filter_button_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec 
*pspec)
+{
+  PhotosToolFilterButton *self = PHOTOS_TOOL_FILTER_BUTTON (object);
+
+  switch (prop_id)
+    {
+    case PROP_LABEL:
+      self->label = g_value_dup_string (value);
+      break;
+
+    case PROP_ACTION_NAME:
+      {
+        const gchar *action_name;
+
+        action_name = g_value_get_string (value);
+        photos_tool_filter_button_set_action_name (GTK_ACTIONABLE (self), action_name);
+        break;
+      }
+
+    case PROP_ACTION_TARGET:
+      {
+        GVariant *action_target;
+
+        action_target = g_value_get_variant (value);
+        photos_tool_filter_button_set_action_target_value (GTK_ACTIONABLE (self), action_target);
+        break;
+      }
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+static void
+photos_tool_filter_button_init (PhotosToolFilterButton *self)
+{
+}
+
+
+static void
+photos_tool_filter_button_class_init (PhotosToolFilterButtonClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->constructed = photos_tool_filter_button_constructed;
+  object_class->finalize = photos_tool_filter_button_finalize;
+  object_class->get_property = photos_tool_filter_button_get_property;
+  object_class->set_property = photos_tool_filter_button_set_property;
+
+  g_object_class_install_property (object_class,
+                                   PROP_LABEL,
+                                   g_param_spec_string ("label",
+                                                        "Label",
+                                                        "The human-readable name of the filter",
+                                                        NULL,
+                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+
+  g_object_class_override_property (object_class, PROP_ACTION_NAME, "action-name");
+  g_object_class_override_property (object_class, PROP_ACTION_TARGET, "action-target");
+}
+
+
+static void
+photos_tool_filter_button_actionable_iface_init (GtkActionableInterface *iface)
+{
+  iface->get_action_name = photos_tool_filter_button_get_action_name;
+  iface->get_action_target_value = photos_tool_filter_button_get_action_target_value;
+  iface->set_action_name = photos_tool_filter_button_set_action_name;
+  iface->set_action_target_value = photos_tool_filter_button_set_action_target_value;
+}
+
+
+GtkWidget *
+photos_tool_filter_button_new (const gchar *label)
+{
+  return g_object_new (PHOTOS_TYPE_TOOL_FILTER_BUTTON, "label", label, NULL);
+}
+
+
+void
+photos_tool_filter_button_set_image (PhotosToolFilterButton *self, GtkWidget *image)
+{
+  gtk_button_set_image (GTK_BUTTON (self->button), image);
+}
diff --git a/src/photos-tool-filter-button.h b/src/photos-tool-filter-button.h
new file mode 100644
index 0000000..d5de5f6
--- /dev/null
+++ b/src/photos-tool-filter-button.h
@@ -0,0 +1,50 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2015 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef PHOTOS_TOOL_FILTER_BUTTON_H
+#define PHOTOS_TOOL_FILTER_BUTTON_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_TOOL_FILTER_BUTTON (photos_tool_filter_button_get_type ())
+
+#define PHOTOS_TOOL_FILTER_BUTTON(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   PHOTOS_TYPE_TOOL_FILTER_BUTTON, PhotosToolFilterButton))
+
+#define PHOTOS_IS_TOOL_FILTER_BUTTON(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   PHOTOS_TYPE_TOOL_FILTER_BUTTON))
+
+typedef struct _PhotosToolFilterButton      PhotosToolFilterButton;
+typedef struct _PhotosToolFilterButtonClass PhotosToolFilterButtonClass;
+
+GType                  photos_tool_filter_button_get_type               (void) G_GNUC_CONST;
+
+GtkWidget             *photos_tool_filter_button_new                    (const gchar *label);
+
+void                   photos_tool_filter_button_set_image              (PhotosToolFilterButton *self,
+                                                                         GtkWidget *image);
+
+G_END_DECLS
+
+#endif /* PHOTOS_TOOL_FILTER_BUTTON_H */
diff --git a/src/photos-tool-filters.c b/src/photos-tool-filters.c
index 3a17ae4..7589245 100644
--- a/src/photos-tool-filters.c
+++ b/src/photos-tool-filters.c
@@ -29,6 +29,7 @@
 #include "photos-icons.h"
 #include "photos-operation-insta-common.h"
 #include "photos-tool.h"
+#include "photos-tool-filter-button.h"
 #include "photos-tool-filters.h"
 #include "photos-utils.h"
 
@@ -85,7 +86,7 @@ photos_tool_filters_create_preview_idle (gpointer user_data)
                                              "preset", preset,
                                              NULL);
   image = gtk_image_new_from_surface (surface);
-  gtk_button_set_image (GTK_BUTTON (button), image);
+  photos_tool_filter_button_set_image (PHOTOS_TOOL_FILTER_BUTTON (button), image);
 
   gtk_widget_show (image);
 
@@ -156,95 +157,51 @@ photos_tool_filters_finalize (GObject *object)
 static void
 photos_tool_filters_init (PhotosToolFilters *self)
 {
-  GApplication *app;
-  GdkPixbuf *preview_icon = NULL;
   GtkWidget *button;
-  GtkWidget *image;
-  cairo_surface_t *preview_icon_surface = NULL;
-  gint scale;
   guint row = 0;
 
-  app = g_application_get_default ();
-  scale = photos_application_get_scale_factor (PHOTOS_APPLICATION (app));
-  preview_icon = photos_utils_create_placeholder_icon_for_scale (PHOTOS_ICON_CONTENT_LOADING_SYMBOLIC, 96, 
scale);
-  if (preview_icon != NULL)
-    preview_icon_surface = gdk_cairo_surface_create_from_pixbuf (preview_icon, scale, NULL);
-
   self->grid = g_object_ref_sink (gtk_grid_new ());
 
-  image = gtk_image_new_from_surface (preview_icon_surface);
-  button = gtk_button_new_with_label (_("None"));
+  button = photos_tool_filter_button_new (_("None"));
   gtk_actionable_set_action_name (GTK_ACTIONABLE (button), "app.insta-current");
   gtk_actionable_set_action_target (GTK_ACTIONABLE (button), "n", (gint16) 
PHOTOS_OPERATION_INSTA_PRESET_NONE);
-  gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
-  gtk_button_set_image (GTK_BUTTON (button), image);
-  gtk_button_set_image_position (GTK_BUTTON (button), GTK_POS_TOP);
-  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
   gtk_grid_attach (GTK_GRID (self->grid), button, 0, row, 1, 1);
   self->buttons = g_list_prepend (self->buttons, button);
 
-  image = gtk_image_new_from_surface (preview_icon_surface);
-  button = gtk_button_new_with_label (_("1977"));
+  button = photos_tool_filter_button_new (_("1977"));
   gtk_actionable_set_action_name (GTK_ACTIONABLE (button), "app.insta-current");
   gtk_actionable_set_action_target (GTK_ACTIONABLE (button), "n", (gint16) 
PHOTOS_OPERATION_INSTA_PRESET_1977);
-  gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
-  gtk_button_set_image (GTK_BUTTON (button), image);
-  gtk_button_set_image_position (GTK_BUTTON (button), GTK_POS_TOP);
-  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
   gtk_grid_attach (GTK_GRID (self->grid), button, 1, row, 1, 1);
   self->buttons = g_list_prepend (self->buttons, button);
   row++;
 
-  image = gtk_image_new_from_surface (preview_icon_surface);
-  button = gtk_button_new_with_label (_("Brannan"));
+  button = photos_tool_filter_button_new (_("Brannan"));
   gtk_actionable_set_action_name (GTK_ACTIONABLE (button), "app.insta-current");
   gtk_actionable_set_action_target (GTK_ACTIONABLE (button), "n", (gint16) 
PHOTOS_OPERATION_INSTA_PRESET_BRANNAN);
-  gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
-  gtk_button_set_image (GTK_BUTTON (button), image);
-  gtk_button_set_image_position (GTK_BUTTON (button), GTK_POS_TOP);
-  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
   gtk_grid_attach (GTK_GRID (self->grid), button, 0, row, 1, 1);
   self->buttons = g_list_prepend (self->buttons, button);
 
-  image = gtk_image_new_from_surface (preview_icon_surface);
-  button = gtk_button_new_with_label (_("Gotham"));
+  button = photos_tool_filter_button_new (_("Gotham"));
   gtk_actionable_set_action_name (GTK_ACTIONABLE (button), "app.insta-current");
   gtk_actionable_set_action_target (GTK_ACTIONABLE (button), "n", (gint16) 
PHOTOS_OPERATION_INSTA_PRESET_GOTHAM);
-  gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
-  gtk_button_set_image (GTK_BUTTON (button), image);
-  gtk_button_set_image_position (GTK_BUTTON (button), GTK_POS_TOP);
-  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
   gtk_grid_attach (GTK_GRID (self->grid), button, 1, row, 1, 1);
   self->buttons = g_list_prepend (self->buttons, button);
   row++;
 
-  image = gtk_image_new_from_surface (preview_icon_surface);
-  button = gtk_button_new_with_label (_("Gray"));
+  button = photos_tool_filter_button_new (_("Gray"));
   gtk_actionable_set_action_name (GTK_ACTIONABLE (button), "app.insta-current");
   gtk_actionable_set_action_target (GTK_ACTIONABLE (button), "n", (gint16) 
PHOTOS_OPERATION_INSTA_PRESET_GRAY);
-  gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
-  gtk_button_set_image (GTK_BUTTON (button), image);
-  gtk_button_set_image_position (GTK_BUTTON (button), GTK_POS_TOP);
-  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
   gtk_grid_attach (GTK_GRID (self->grid), button, 0, row, 1, 1);
   self->buttons = g_list_prepend (self->buttons, button);
 
-  image = gtk_image_new_from_surface (preview_icon_surface);
-  button = gtk_button_new_with_label (_("Nashville"));
+  button = photos_tool_filter_button_new (_("Nashville"));
   gtk_actionable_set_action_name (GTK_ACTIONABLE (button), "app.insta-current");
   gtk_actionable_set_action_target (GTK_ACTIONABLE (button), "n", (gint16) 
PHOTOS_OPERATION_INSTA_PRESET_NASHVILLE);
-  gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
-  gtk_button_set_image (GTK_BUTTON (button), image);
-  gtk_button_set_image_position (GTK_BUTTON (button), GTK_POS_TOP);
-  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
   gtk_grid_attach (GTK_GRID (self->grid), button, 1, row, 1, 1);
   self->buttons = g_list_prepend (self->buttons, button);
   row++;
 
   self->buttons = g_list_reverse (self->buttons);
-
-  g_clear_object (&preview_icon);
-  g_clear_pointer (&preview_icon_surface, (GDestroyNotify) cairo_surface_destroy);
 }
 
 


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