[gnome-photos] embed: Move the preview widget into its separate class



commit 939270d4383d595992e194a15f7acb078d5a1bd0
Author: Debarshi Ray <debarshir gnome org>
Date:   Sat Mar 23 18:58:42 2013 +0100

    embed: Move the preview widget into its separate class
    
    Restore the following constraints:
    + smaller images are not expanded
    + bigger ones are scaled down
    + images are always centered
    
    These were lost when moving away from Clutter.

 src/Makefile.am           |    2 +
 src/photos-embed.c        |   31 +-------
 src/photos-preview-view.c |  195 +++++++++++++++++++++++++++++++++++++++++++++
 src/photos-preview-view.h |   78 ++++++++++++++++++
 4 files changed, 278 insertions(+), 28 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 9a76554..aef6cc3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -94,6 +94,8 @@ gnome_photos_SOURCES = \
        photos-organize-collection-model.h \
        photos-organize-collection-view.c \
        photos-organize-collection-view.h \
+       photos-preview-view.c \
+       photos-preview-view.h \
        photos-properties-dialog.c \
        photos-properties-dialog.h \
        photos-query.c \
diff --git a/src/photos-embed.c b/src/photos-embed.c
index ffc0b9d..dcbe4df 100644
--- a/src/photos-embed.c
+++ b/src/photos-embed.c
@@ -27,7 +27,6 @@
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <gegl.h>
-#include <gegl-gtk.h>
 
 #include "photos-embed.h"
 #include "photos-empty-results-box.h"
@@ -38,6 +37,7 @@
 #include "photos-mode-controller.h"
 #include "photos-notification-manager.h"
 #include "photos-offset-overview-controller.h"
+#include "photos-preview-view.h"
 #include "photos-selection-toolbar.h"
 #include "photos-spinner-box.h"
 #include "photos-tracker-change-monitor.h"
@@ -91,7 +91,7 @@ photos_embed_item_load (GObject *source_object, GAsyncResult *res, gpointer user
   if (node == NULL)
     return;
 
-  gegl_gtk_view_set_node (GEGL_GTK_VIEW (priv->preview), node);
+  photos_preview_view_set_node (PHOTOS_PREVIEW_VIEW (priv->preview), node);
 
   /* TODO: set toolbar model */
 
@@ -249,22 +249,6 @@ photos_embed_prepare_for_preview (PhotosEmbed *self)
 }
 
 
-static void
-photos_embed_preview_draw_background (PhotosEmbed *self, cairo_t *cr, GdkRectangle *rect)
-{
-  PhotosEmbedPrivate *priv = self->priv;
-  GtkStyleContext *context;
-  GtkStateFlags flags;
-
-  context = gtk_widget_get_style_context (priv->preview);
-  flags = gtk_widget_get_state_flags (priv->preview);
-  gtk_style_context_save (context);
-  gtk_style_context_set_state (context, flags);
-  gtk_render_background (context, cr, 0, 0, rect->width, rect->height);
-  gtk_style_context_restore (context);
-}
-
-
 void
 photos_embed_set_error (PhotosEmbed *self, const gchar *primary, const gchar *secondary)
 {
@@ -379,16 +363,7 @@ photos_embed_init (PhotosEmbed *self)
   priv->favorites = photos_view_container_new (PHOTOS_WINDOW_MODE_FAVORITES);
   priv->favorites_page = gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->favorites, NULL);
 
-  priv->preview = GTK_WIDGET (gegl_gtk_view_new ());
-  context = gtk_widget_get_style_context (priv->preview);
-  gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
-  gtk_style_context_add_class (context, "content-view");
-  gegl_gtk_view_set_autoscale_policy (GEGL_GTK_VIEW (priv->preview), GEGL_GTK_VIEW_AUTOSCALE_DISABLED);
-  g_signal_connect_swapped (priv->preview,
-                            "draw-background",
-                            G_CALLBACK (photos_embed_preview_draw_background),
-                            self);
-  gtk_widget_show (priv->preview);
+  priv->preview = photos_preview_view_new ();
   priv->preview_page = gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->preview, NULL);
 
   priv->spinner_box = photos_spinner_box_new ();
diff --git a/src/photos-preview-view.c b/src/photos-preview-view.c
new file mode 100644
index 0000000..66e7823
--- /dev/null
+++ b/src/photos-preview-view.c
@@ -0,0 +1,195 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2013 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.
+ */
+
+/* Based on code from:
+ *   + Documents
+ */
+
+
+#include "config.h"
+
+#include <gegl-gtk.h>
+#include <glib.h>
+#include <gtk/gtk.h>
+#include <glib/gi18n.h>
+
+#include "photos-preview-view.h"
+
+
+struct _PhotosPreviewViewPrivate
+{
+  GeglNode *node;
+  GtkWidget *view;
+};
+
+
+G_DEFINE_TYPE (PhotosPreviewView, photos_preview_view, GTK_TYPE_SCROLLED_WINDOW);
+
+
+static void
+photos_preview_view_draw_background (PhotosPreviewView *self, cairo_t *cr, GdkRectangle *rect)
+{
+  PhotosPreviewViewPrivate *priv = self->priv;
+  GtkStyleContext *context;
+  GtkStateFlags flags;
+
+  context = gtk_widget_get_style_context (priv->view);
+  flags = gtk_widget_get_state_flags (priv->view);
+  gtk_style_context_save (context);
+  gtk_style_context_set_state (context, flags);
+  gtk_render_background (context, cr, 0, 0, rect->width, rect->height);
+  gtk_style_context_restore (context);
+}
+
+
+static void
+photos_preview_view_scale_and_align_image (PhotosPreviewView *self)
+{
+  PhotosPreviewViewPrivate *priv = self->priv;
+  GeglRectangle bbox;
+  GtkAllocation alloc;
+  float delta_x;
+  float delta_y;
+  float scale = 1.0;
+
+  /* Reset these properties, otherwise values from the previous node
+   * will interfere with the current one.
+   */
+  gegl_gtk_view_set_autoscale_policy (GEGL_GTK_VIEW (priv->view), GEGL_GTK_VIEW_AUTOSCALE_DISABLED);
+  gegl_gtk_view_set_scale (GEGL_GTK_VIEW (priv->view), 1.0);
+  gegl_gtk_view_set_x (GEGL_GTK_VIEW (priv->view), 0.0);
+  gegl_gtk_view_set_y (GEGL_GTK_VIEW (priv->view), 0.0);
+
+  bbox = gegl_node_get_bounding_box (priv->node);
+  gtk_widget_get_allocation (priv->view, &alloc);
+
+  if (bbox.width > alloc.width || bbox.height > alloc.height)
+    {
+      float height_ratio;
+      float max_ratio;
+      float width_ratio;
+
+      gegl_gtk_view_set_autoscale_policy (GEGL_GTK_VIEW (priv->view), GEGL_GTK_VIEW_AUTOSCALE_CONTENT);
+
+      /* TODO: since gegl_gtk_view_get_scale is not giving the
+       *       correct value of scale, we calculate it ourselves.
+       */
+      height_ratio = (float) bbox.height / alloc.height;
+      width_ratio = (float) bbox.width / alloc.width;
+      max_ratio = width_ratio >= height_ratio ? width_ratio : height_ratio;
+      scale = 1.0 / max_ratio;
+
+      bbox.width = (gint) (scale * bbox.width + 0.5);
+      bbox.height = (gint) (scale * bbox.height + 0.5);
+    }
+
+
+  /* At this point, alloc is definitely bigger than bbox. */
+  delta_x = (alloc.width - bbox.width) / 2.0;
+  delta_y = (alloc.height - bbox.height) / 2.0;
+  gegl_gtk_view_set_x (GEGL_GTK_VIEW (priv->view), -delta_x);
+  gegl_gtk_view_set_y (GEGL_GTK_VIEW (priv->view), -delta_y);
+}
+
+
+static void
+photos_preview_view_dispose (GObject *object)
+{
+  PhotosPreviewView *self = PHOTOS_PREVIEW_VIEW (object);
+  PhotosPreviewViewPrivate *priv = self->priv;
+
+  g_clear_object (&priv->node);
+
+  G_OBJECT_CLASS (photos_preview_view_parent_class)->dispose (object);
+}
+
+
+static void
+photos_preview_view_init (PhotosPreviewView *self)
+{
+  PhotosPreviewViewPrivate *priv;
+  GtkStyleContext *context;
+
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, PHOTOS_TYPE_PREVIEW_VIEW, PhotosPreviewViewPrivate);
+  priv = self->priv;
+
+  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
+  gtk_widget_set_vexpand (GTK_WIDGET (self), TRUE);
+  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (self), GTK_SHADOW_IN);
+  context = gtk_widget_get_style_context (GTK_WIDGET (self));
+  gtk_style_context_add_class (context, "documents-scrolledwin");
+
+  priv->view = GTK_WIDGET (gegl_gtk_view_new ());
+  context = gtk_widget_get_style_context (priv->view);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
+  gtk_style_context_add_class (context, "content-view");
+  g_signal_connect_swapped (priv->view,
+                            "size-allocate",
+                            G_CALLBACK (photos_preview_view_scale_and_align_image),
+                            self);
+  g_signal_connect_swapped (priv->view,
+                            "draw-background",
+                            G_CALLBACK (photos_preview_view_draw_background),
+                            self);
+  gtk_container_add (GTK_CONTAINER (self), priv->view);
+
+  gtk_widget_show_all (GTK_WIDGET (self));
+}
+
+
+static void
+photos_preview_view_class_init (PhotosPreviewViewClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->dispose = photos_preview_view_dispose;
+
+  g_type_class_add_private (class, sizeof (PhotosPreviewViewPrivate));
+}
+
+
+GtkWidget *
+photos_preview_view_new (void)
+{
+  return g_object_new (PHOTOS_TYPE_PREVIEW_VIEW, NULL);
+}
+
+
+void
+photos_preview_view_set_node (PhotosPreviewView *self, GeglNode *node)
+{
+  PhotosPreviewViewPrivate *priv = self->priv;
+  GeglRectangle bbox;
+  GtkAllocation alloc;
+  float delta_x;
+  float delta_y;
+  float scale = 1.0;
+
+  if (priv->node == node)
+    return;
+
+  g_clear_object (&priv->node);
+  if (node == NULL)
+    return;
+
+  priv->node = g_object_ref (node);
+  photos_preview_view_scale_and_align_image (self);
+  gegl_gtk_view_set_node (GEGL_GTK_VIEW (priv->view), priv->node);
+}
diff --git a/src/photos-preview-view.h b/src/photos-preview-view.h
new file mode 100644
index 0000000..656994b
--- /dev/null
+++ b/src/photos-preview-view.h
@@ -0,0 +1,78 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2013 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.
+ */
+
+/* Based on code from:
+ *   + Documents
+ */
+
+#ifndef PHOTOS_PREVIEW_VIEW_H
+#define PHOTOS_PREVIEW_VIEW_H
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_PREVIEW_VIEW (photos_preview_view_get_type ())
+
+#define PHOTOS_PREVIEW_VIEW(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   PHOTOS_TYPE_PREVIEW_VIEW, PhotosPreviewView))
+
+#define PHOTOS_PREVIEW_VIEW_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+   PHOTOS_TYPE_PREVIEW_VIEW, PhotosPreviewViewClass))
+
+#define PHOTOS_IS_PREVIEW_VIEW(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   PHOTOS_TYPE_PREVIEW_VIEW))
+
+#define PHOTOS_IS_PREVIEW_VIEW_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+   PHOTOS_TYPE_PREVIEW_VIEW))
+
+#define PHOTOS_PREVIEW_VIEW_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+   PHOTOS_TYPE_PREVIEW_VIEW, PhotosPreviewViewClass))
+
+typedef struct _PhotosPreviewView        PhotosPreviewView;
+typedef struct _PhotosPreviewViewClass   PhotosPreviewViewClass;
+typedef struct _PhotosPreviewViewPrivate PhotosPreviewViewPrivate;
+
+struct _PhotosPreviewView
+{
+  GtkScrolledWindow parent_instance;
+  PhotosPreviewViewPrivate *priv;
+};
+
+struct _PhotosPreviewViewClass
+{
+  GtkScrolledWindowClass parent_class;
+};
+
+GType                  photos_preview_view_get_type               (void) G_GNUC_CONST;
+
+GtkWidget             *photos_preview_view_new                    (void);
+
+void                   photos_preview_view_set_node               (PhotosPreviewView *self, GeglNode *node);
+
+G_END_DECLS
+
+#endif /* PHOTOS_PREVIEW_VIEW_H */


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