[eog] EogScrollView: Add function to check if an event's source if over the image



commit 5fcbe0c8d1048c233de70b75724a6eb427c2d55c
Author: Felix Riemann <friemann gnome org>
Date:   Fri Apr 6 17:45:15 2012 +0200

    EogScrollView: Add function to check if an event's source if over the image
    
    Useful to check if click events are over the image itself.

 doc/reference/eog-sections.txt |    1 +
 src/eog-scroll-view.c          |  106 ++++++++++++++++++++++++++++++++++------
 src/eog-scroll-view.h          |    3 +
 3 files changed, 95 insertions(+), 15 deletions(-)
---
diff --git a/doc/reference/eog-sections.txt b/doc/reference/eog-sections.txt
index b6a9b81..ee7139f 100644
--- a/doc/reference/eog-sections.txt
+++ b/doc/reference/eog-sections.txt
@@ -433,6 +433,7 @@ eog_scroll_view_override_bg_color
 eog_scroll_view_set_background_color
 eog_scroll_view_set_transparency_color
 eog_scroll_view_set_use_bg_color
+eog_scroll_view_event_is_over_image
 <SUBSECTION Standard>
 EOG_SCROLL_VIEW
 EOG_IS_SCROLL_VIEW
diff --git a/src/eog-scroll-view.c b/src/eog-scroll-view.c
index 335b42f..516ab3b 100644
--- a/src/eog-scroll-view.c
+++ b/src/eog-scroll-view.c
@@ -174,6 +174,10 @@ static void view_on_drag_data_get_cb (GtkWidget *widget,
 				      GtkSelectionData *data, guint info,
 				      guint time, gpointer user_data);
 static void _set_zoom_mode_internal (EogScrollView *view, EogZoomMode mode);
+static gboolean eog_scroll_view_get_image_coords (EogScrollView *view, gint *x,
+                                                  gint *y, gint *width,
+                                                  gint *height);
+
 
 #define EOG_SCROLL_VIEW_GET_PRIVATE(object) \
 	(G_TYPE_INSTANCE_GET_PRIVATE ((object), EOG_TYPE_SCROLL_VIEW, EogScrollViewPrivate))
@@ -1791,21 +1795,8 @@ display_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
 	if (priv->pixbuf == NULL)
 		return TRUE;
 
-	compute_scaled_size (view, priv->zoom, &scaled_width, &scaled_height);
-
-	gtk_widget_get_allocation (GTK_WIDGET (priv->display), &allocation);
-
-	/* Compute image offsets with respect to the window */
-
-	if (scaled_width <= allocation.width)
-		xofs = (allocation.width - scaled_width) / 2;
-	else
-		xofs = -priv->xofs;
-
-	if (scaled_height <= allocation.height)
-		yofs = (allocation.height - scaled_height) / 2;
-	else
-		yofs = -priv->yofs;
+	eog_scroll_view_get_image_coords (view, &xofs, &yofs,
+	                                  &scaled_width, &scaled_height);
 
 	eog_debug_message (DEBUG_WINDOW, "zoom %.2f, xofs: %i, yofs: %i scaled w: %i h: %i\n",
 			   priv->zoom, xofs, yofs, scaled_width, scaled_height);
@@ -3102,3 +3093,88 @@ eog_scroll_view_get_zoom_mode (EogScrollView *view)
 
 	return view->priv->zoom_mode;
 }
+
+static gboolean
+eog_scroll_view_get_image_coords (EogScrollView *view, gint *x, gint *y,
+                                  gint *width, gint *height)
+{
+	EogScrollViewPrivate *priv = view->priv;
+	GtkAllocation allocation;
+	gint scaled_width, scaled_height, xofs, yofs;
+
+	compute_scaled_size (view, priv->zoom, &scaled_width, &scaled_height);
+
+	if (G_LIKELY (width))
+		*width = scaled_width;
+	if (G_LIKELY (height))
+		*height = scaled_height;
+
+	/* If only width and height are needed stop here. */
+	if (x == NULL && y == NULL)
+		return TRUE;
+
+	gtk_widget_get_allocation (GTK_WIDGET (priv->display), &allocation);
+
+	/* Compute image offsets with respect to the window */
+
+	if (scaled_width <= allocation.width)
+		xofs = (allocation.width - scaled_width) / 2;
+	else
+		xofs = -priv->xofs;
+
+	if (scaled_height <= allocation.height)
+		yofs = (allocation.height - scaled_height) / 2;
+	else
+		yofs = -priv->yofs;
+
+	if (G_LIKELY (x))
+		*x = xofs;
+	if (G_LIKELY (y))
+		*y = yofs;
+
+	return TRUE;
+}
+
+/**
+ * eog_scroll_view_event_is_over_image:
+ * @view: An #EogScrollView that has an image loaded.
+ * @ev: A #GdkEvent which must have window-relative coordinates.
+ *
+ * Tells if @ev's originates from inside the image area. @view must be
+ * realized and have an image set for this to work.
+ *
+ * It only works with #GdkEvent<!-- -->s that supply coordinate data,
+ * i.e. #GdkEventButton.
+ *
+ * Returns: %TRUE if @ev originates from over the image, %FALSE otherwise.
+ */
+gboolean
+eog_scroll_view_event_is_over_image (EogScrollView *view, const GdkEvent *ev)
+{
+	EogScrollViewPrivate *priv;
+	GdkWindow *window;
+	gdouble evx, evy;
+	gint x, y, width, height;
+
+	g_return_val_if_fail (EOG_IS_SCROLL_VIEW (view), FALSE);
+	g_return_val_if_fail (gtk_widget_get_realized(GTK_WIDGET(view)), FALSE);
+	g_return_val_if_fail (ev != NULL, FALSE);
+
+	priv = view->priv;
+	window = gtk_widget_get_window (GTK_WIDGET (priv->display));
+
+	if (G_UNLIKELY (priv->pixbuf == NULL 
+	    || window != ((GdkEventAny*) ev)->window))
+		return FALSE;
+
+	if (G_UNLIKELY (!gdk_event_get_coords (ev, &evx, &evy)))
+		return FALSE;
+
+	if (!eog_scroll_view_get_image_coords (view, &x, &y, &width, &height))
+		return FALSE;
+
+	if (evx < x || evy < y || evx > (x + width) || evy > (y + height))
+		return FALSE;
+
+	return TRUE;
+}
diff --git a/src/eog-scroll-view.h b/src/eog-scroll-view.h
index f8c51aa..528d3b2 100644
--- a/src/eog-scroll-view.h
+++ b/src/eog-scroll-view.h
@@ -94,6 +94,9 @@ gboolean eog_scroll_view_get_zoom_is_max  (EogScrollView *view);
 void     eog_scroll_view_show_cursor      (EogScrollView *view);
 void     eog_scroll_view_hide_cursor      (EogScrollView *view);
 
+gboolean eog_scroll_view_event_is_over_image	(EogScrollView *view,
+						 const GdkEvent *ev);
+
 G_END_DECLS
 
 #endif /* _EOG_SCROLL_VIEW_H_ */



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