[evince/gtk-style-context: 5/8] [view] Make EvView inherit from GtkFixed and implement scrollable interface



commit 79bb743121bd1cba02be49aa447fef5460d5848b
Author: Carlos Garcia Campos <carlosgc gnome org>
Date:   Sun Oct 31 16:39:21 2010 +0100

    [view] Make EvView inherit from GtkFixed and implement scrollable interface
    
    GtkLayout implements scrollable interface while GtkFixed doesn't, so
    using GtkFixed we can still put/move child widgets while having our own
    scrollable implementation.

 libview/ev-view-private.h |    4 +
 libview/ev-view.c         |  230 +++++++++++++++++++++++++--------------------
 2 files changed, 132 insertions(+), 102 deletions(-)
---
diff --git a/libview/ev-view-private.h b/libview/ev-view-private.h
index 5b5fd19..0387642 100644
--- a/libview/ev-view-private.h
+++ b/libview/ev-view-private.h
@@ -132,6 +132,10 @@ struct _EvView {
 	/* Scrolling */
 	GtkAdjustment *hadjustment;
 	GtkAdjustment *vadjustment;
+	/* GtkScrollablePolicy needs to be checked when
+	 * driving the scrollable adjustment values */
+	guint hscroll_policy : 1;
+	guint vscroll_policy : 1;
 
 	gint scroll_x;
 	gint scroll_y;	
diff --git a/libview/ev-view.c b/libview/ev-view.c
index 024153c..5edf89a 100644
--- a/libview/ev-view.c
+++ b/libview/ev-view.c
@@ -67,6 +67,14 @@ enum {
 	TARGET_DND_IMAGE
 };
 
+enum {
+	PROP_0,
+	PROP_HADJUSTMENT,
+	PROP_VADJUSTMENT,
+	PROP_HSCROLL_POLICY,
+	PROP_VSCROLL_POLICY
+};
+
 static guint signals[N_SIGNALS];
 
 typedef enum {
@@ -81,9 +89,6 @@ typedef enum {
 
 /*** Scrolling ***/
 static void       view_update_range_and_current_page         (EvView             *view);
-static void       set_scroll_adjustment                      (EvView             *view,
-							      GtkOrientation      orientation,
-							      GtkAdjustment      *adjustment);
 static void       add_scroll_binding_keypad                  (GtkBindingSet      *binding_set,
 							      guint               keyval,
 							      GdkModifierType modifiers,
@@ -151,7 +156,6 @@ static void       ev_view_size_request                       (GtkWidget
 							      GtkRequisition     *requisition);
 static void       ev_view_size_allocate                      (GtkWidget          *widget,
 							      GtkAllocation      *allocation);
-static void       ev_view_realize                            (GtkWidget          *widget);
 static gboolean   ev_view_scroll_event                       (GtkWidget          *widget,
 							      GdkEventScroll     *event);
 static gboolean   ev_view_draw                               (GtkWidget          *widget,
@@ -279,7 +283,8 @@ static void       ev_view_primary_clear_cb                   (GtkClipboard
 							      gpointer            data);
 static void       ev_view_update_primary_selection           (EvView             *ev_view);
 
-G_DEFINE_TYPE (EvView, ev_view, GTK_TYPE_LAYOUT)
+G_DEFINE_TYPE_WITH_CODE (EvView, ev_view, GTK_TYPE_FIXED,
+			 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
 
 /* HeightToPage cache */
 #define EV_HEIGHT_TO_PAGE_CACHE_KEY "ev-height-to-page-cache"
@@ -550,8 +555,8 @@ ev_view_scroll_to_page_position (EvView *view, GtkOrientation orientation)
 }
 
 static void
-view_set_adjustment_values (EvView         *view,
-			    GtkOrientation  orientation)
+ev_view_set_adjustment_values (EvView         *view,
+			       GtkOrientation  orientation)
 {
 	GtkWidget *widget = GTK_WIDGET (view);
 	GtkAdjustment *adjustment;
@@ -741,53 +746,40 @@ view_update_range_and_current_page (EvView *view)
 }
 
 static void
-set_scroll_adjustment (EvView *view,
-		       GtkOrientation  orientation,
-		       GtkAdjustment  *adjustment)
+ev_view_set_scroll_adjustment (EvView         *view,
+			       GtkOrientation  orientation,
+			       GtkAdjustment  *adjustment)
 {
 	GtkAdjustment **to_set;
+	const gchar    *prop_name;
 
-	if (orientation == GTK_ORIENTATION_HORIZONTAL)
+	if (orientation == GTK_ORIENTATION_HORIZONTAL) {
 		to_set = &view->hadjustment;
-	else
+		prop_name = "hadjustment";
+	} else {
 		to_set = &view->vadjustment;
+		prop_name = "vadjustment";
+	}
 
-	if (*to_set != adjustment) {
-		if (*to_set) {
-			g_signal_handlers_disconnect_by_func (*to_set,
-							      (gpointer) on_adjustment_value_changed,
-							      view);
-			g_object_unref (*to_set);
-		}
-
-		*to_set = adjustment;
-		view_set_adjustment_values (view, orientation);
+	if (adjustment && adjustment == *to_set)
+		return;
 
-		if (*to_set) {
-			g_object_ref (*to_set);
-			g_signal_connect (*to_set, "value_changed",
-					  G_CALLBACK (on_adjustment_value_changed), view);
-		}
+	if (*to_set) {
+		g_signal_handlers_disconnect_by_func (*to_set,
+						      (gpointer) on_adjustment_value_changed,
+						      view);
+		g_object_unref (*to_set);
 	}
-}
-
-static void
-on_hadjustment_notify (EvView *view,
-                       GParamSpec *pspec,
-                       gpointer user_data)
-{
-        set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL, gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (view)));
-        on_adjustment_value_changed (NULL, view);
-}
 
+	if (!adjustment)
+		adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
+	g_signal_connect (adjustment, "value_changed",
+			  G_CALLBACK (on_adjustment_value_changed),
+			  view);
+	*to_set = g_object_ref_sink (adjustment);
+	ev_view_set_adjustment_values (view, orientation);
 
-static void
-on_vadjustment_notify (EvView *view,
-                       GParamSpec *pspec,
-                       gpointer user_data)
-{
-        set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL, gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view)));
-        on_adjustment_value_changed (NULL, view);
+	g_object_notify (G_OBJECT (view), prop_name);
 }
 
 static void
@@ -2423,7 +2415,7 @@ ev_view_handle_form_field (EvView      *view,
 				       form_field_mapping,
 				       field, &view_area);
 
-	gtk_layout_put (GTK_LAYOUT (view), field_widget, view_area.x, view_area.y);
+	gtk_fixed_put (GTK_FIXED (view), field_widget, view_area.x, view_area.y);
 	gtk_widget_show (field_widget);
 	gtk_widget_grab_focus (field_widget);
 }
@@ -3167,8 +3159,8 @@ ev_view_size_allocate (GtkWidget      *widget,
 		view->internal_size_request = FALSE;
 	}
 	
-	view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
-	view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
+	ev_view_set_adjustment_values (view, GTK_ORIENTATION_HORIZONTAL);
+	ev_view_set_adjustment_values (view, GTK_ORIENTATION_VERTICAL);
 
 	if (view->document)
 		view_update_range_and_current_page (view);
@@ -3209,7 +3201,7 @@ ev_view_size_allocate (GtkWidget      *widget,
 					 NULL);
 		if (child_allocation.x != view_area.x ||
 		    child_allocation.y != view_area.y) {
-			gtk_layout_move (GTK_LAYOUT (widget), child, view_area.x, view_area.y);
+			gtk_fixed_move (GTK_FIXED (widget), child, view_area.x, view_area.y);
 		}
 	}
 	g_list_free (children);
@@ -3242,35 +3234,6 @@ ev_view_size_allocate (GtkWidget      *widget,
 	}
 }
 
-static void
-ev_view_realize (GtkWidget *widget)
-{
-	EvView    *view = EV_VIEW (widget);
-	GdkWindow *bin_window;
-	GtkStyle  *style;
-
-	if (GTK_WIDGET_CLASS (ev_view_parent_class)->realize)
-		(* GTK_WIDGET_CLASS (ev_view_parent_class)->realize) (widget);
-
-	bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
-	gdk_window_set_events (bin_window,
-			       (gdk_window_get_events (bin_window) |
-				GDK_EXPOSURE_MASK |
-				GDK_BUTTON_PRESS_MASK |
-				GDK_BUTTON_RELEASE_MASK |
-				GDK_SCROLL_MASK |
-				GDK_KEY_PRESS_MASK |
-				GDK_POINTER_MOTION_MASK |
-				GDK_POINTER_MOTION_HINT_MASK |
-				GDK_ENTER_NOTIFY_MASK |
-				GDK_LEAVE_NOTIFY_MASK));
-
-	style = gtk_widget_get_style (widget);
-	gdk_window_set_background (bin_window, &style->mid[GTK_STATE_NORMAL]);
-
-	on_adjustment_value_changed (NULL, view);
-}
-
 static gboolean
 ev_view_scroll_event (GtkWidget *widget, GdkEventScroll *event)
 {
@@ -3871,15 +3834,15 @@ ev_view_motion_notify_event (GtkWidget      *widget,
 			     GdkEventMotion *event)
 {
 	EvView    *view = EV_VIEW (widget);
-	GdkWindow *bin_window;
+	GdkWindow *window;
 	gint       x, y;
 
 	if (!view->document)
 		return FALSE;
 
-	bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
+	window = gtk_widget_get_window (widget);
 
-        if (event->is_hint || event->window != bin_window) {
+        if (event->is_hint || event->window != window) {
 	    gtk_widget_get_pointer (widget, &x, &y);
         } else {
 	    x = event->x;
@@ -4590,6 +4553,64 @@ ev_view_dispose (GObject *object)
 	G_OBJECT_CLASS (ev_view_parent_class)->dispose (object);
 }
 
+static void
+ev_view_get_property (GObject     *object,
+		      guint        prop_id,
+		      GValue      *value,
+		      GParamSpec  *pspec)
+{
+	EvView *view = EV_VIEW (object);
+
+	switch (prop_id) {
+	case PROP_HADJUSTMENT:
+		g_value_set_object (value, view->hadjustment);
+		break;
+	case PROP_VADJUSTMENT:
+		g_value_set_object (value, view->vadjustment);
+		break;
+	case PROP_HSCROLL_POLICY:
+		g_value_set_enum (value, view->hscroll_policy);
+		break;
+	case PROP_VSCROLL_POLICY:
+		g_value_set_enum (value, view->vscroll_policy);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+static void
+ev_view_set_property (GObject      *object,
+		      guint         prop_id,
+		      const GValue *value,
+		      GParamSpec   *pspec)
+{
+	EvView *view = EV_VIEW (object);
+
+	switch (prop_id) {
+	case PROP_HADJUSTMENT:
+		ev_view_set_scroll_adjustment (view, GTK_ORIENTATION_HORIZONTAL,
+					       (GtkAdjustment *) g_value_get_object (value));
+		break;
+	case PROP_VADJUSTMENT:
+		ev_view_set_scroll_adjustment (view, GTK_ORIENTATION_VERTICAL,
+					       (GtkAdjustment *) g_value_get_object (value));
+		break;
+	case PROP_HSCROLL_POLICY:
+		view->hscroll_policy = g_value_get_enum (value);
+		gtk_widget_queue_resize (GTK_WIDGET (view));
+		break;
+	case PROP_VSCROLL_POLICY:
+		view->vscroll_policy = g_value_get_enum (value);
+		gtk_widget_queue_resize (GTK_WIDGET (view));
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
 static AtkObject *
 ev_view_get_accessible (GtkWidget *widget)
 {
@@ -4630,6 +4651,8 @@ ev_view_class_init (EvViewClass *class)
 	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
 	GtkBindingSet *binding_set;
 
+	object_class->get_property = ev_view_get_property;
+	object_class->set_property = ev_view_set_property;
         object_class->dispose = ev_view_dispose;
 	object_class->finalize = ev_view_finalize;
 
@@ -4643,7 +4666,6 @@ ev_view_class_init (EvViewClass *class)
  	widget_class->get_accessible = ev_view_get_accessible;
 	widget_class->size_request = ev_view_size_request;
 	widget_class->size_allocate = ev_view_size_allocate;
-	widget_class->realize = ev_view_realize;
 	widget_class->scroll_event = ev_view_scroll_event;
 	widget_class->enter_notify_event = ev_view_enter_notify_event;
 	widget_class->leave_notify_event = ev_view_leave_notify_event;
@@ -4655,6 +4677,12 @@ ev_view_class_init (EvViewClass *class)
 
 	class->binding_activated = ev_view_scroll;
 
+	/* Scrollable interface */
+	g_object_class_override_property (object_class, PROP_HADJUSTMENT, "hadjustment");
+	g_object_class_override_property (object_class, PROP_VADJUSTMENT, "vadjustment");
+	g_object_class_override_property (object_class, PROP_HSCROLL_POLICY, "hscroll-policy");
+	g_object_class_override_property (object_class, PROP_VSCROLL_POLICY, "vscroll-policy");
+
 	signals[SIGNAL_BINDING_ACTIVATED] = g_signal_new ("binding_activated",
 	  	         G_TYPE_FROM_CLASS (object_class),
 		         G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
@@ -4745,8 +4773,20 @@ ev_view_class_init (EvViewClass *class)
 static void
 ev_view_init (EvView *view)
 {
+	gtk_widget_set_has_window (GTK_WIDGET (view), TRUE);
 	gtk_widget_set_can_focus (GTK_WIDGET (view), TRUE);
 
+	gtk_widget_set_events (GTK_WIDGET (view),
+			       GDK_EXPOSURE_MASK |
+			       GDK_BUTTON_PRESS_MASK |
+			       GDK_BUTTON_RELEASE_MASK |
+			       GDK_SCROLL_MASK |
+			       GDK_KEY_PRESS_MASK |
+			       GDK_POINTER_MOTION_MASK |
+			       GDK_POINTER_MOTION_HINT_MASK |
+			       GDK_ENTER_NOTIFY_MASK |
+			       GDK_LEAVE_NOTIFY_MASK);
+
 	view->start_page = -1;
 	view->end_page = -1;
 	view->spacing = 5;
@@ -4767,12 +4807,6 @@ ev_view_init (EvView *view)
 	view->pending_scroll = SCROLL_TO_KEEP_POSITION;
 	view->jump_to_find_result = TRUE;
 	view->highlight_find_results = FALSE;
-
-        g_signal_connect (view, "notify::hadjustment", G_CALLBACK (on_hadjustment_notify), NULL);
-        g_signal_connect (view, "notify::vadjustment", G_CALLBACK (on_vadjustment_notify), NULL);
-
-        gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (view), NULL);
-        gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (view), NULL);
 }
 
 /*** Callbacks ***/
@@ -4800,10 +4834,7 @@ job_finished_cb (EvPixbufCache  *pixbuf_cache,
 		 EvView         *view)
 {
 	if (region) {
-		GdkWindow *bin_window;
-
-		bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
-		gdk_window_invalidate_region (bin_window, region, TRUE);
+		gdk_window_invalidate_region (gtk_widget_get_window (GTK_WIDGET (view)), region, TRUE);
 	} else {
 		gtk_widget_queue_draw (GTK_WIDGET (view));
 	}
@@ -4865,7 +4896,7 @@ on_adjustment_value_changed (GtkAdjustment *adjustment,
 					 "x", &child_x,
 					 "y", &child_y,
 					 NULL);
-		gtk_layout_move (GTK_LAYOUT (view), child, child_x + dx, child_y + dy);
+		gtk_fixed_move (GTK_FIXED (view), child, child_x + dx, child_y + dy);
 	}
 	g_list_free (children);
 
@@ -4880,10 +4911,7 @@ on_adjustment_value_changed (GtkAdjustment *adjustment,
 	if (view->pending_resize) {
 		gtk_widget_queue_draw (GTK_WIDGET (view));
 	} else {
-		GdkWindow *bin_window;
-
-		bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
-		gdk_window_scroll (bin_window, dx, dy);
+		gdk_window_scroll (gtk_widget_get_window (GTK_WIDGET (view)), dx, dy);
 	}
 
 	gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
@@ -5948,16 +5976,14 @@ merge_selection_region (EvView *view,
 
 		/* Redraw the damaged region! */
 		if (region) {
-			GdkWindow   *bin_window;
 			GdkRectangle page_area;
 			GtkBorder    border;
 
-			bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
 			ev_view_get_page_extents (view, cur_page, &page_area, &border);
 			cairo_region_translate (region,
 					   page_area.x + border.left - view->scroll_x,
 					   page_area.y + border.top - view->scroll_y);
-			gdk_window_invalidate_region (bin_window, region, TRUE);
+			gdk_window_invalidate_region (gtk_widget_get_window (GTK_WIDGET (view)), region, TRUE);
 			cairo_region_destroy (region);
 		}
 	}
@@ -6195,7 +6221,7 @@ ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
 {
 	GdkCursor *cursor = NULL;
 	GtkWidget *widget;
-	GdkWindow *bin_window;
+	GdkWindow *window;
 
 	if (view->cursor == new_cursor) {
 		return;
@@ -6203,10 +6229,10 @@ ev_view_set_cursor (EvView *view, EvViewCursor new_cursor)
 
 	view->cursor = new_cursor;
 
-	bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (view));
+	window = gtk_widget_get_window (GTK_WIDGET (view));
 	widget = gtk_widget_get_toplevel (GTK_WIDGET (view));
 	cursor = ev_view_cursor_new (gtk_widget_get_display (widget), new_cursor);
-	gdk_window_set_cursor (bin_window, cursor);
+	gdk_window_set_cursor (window, cursor);
 	gdk_flush ();
 	if (cursor)
 		gdk_cursor_unref (cursor);



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