[gthumb] image viewer: hide the horizontal scrollbar in fit_width mode



commit 1788137c6c5b2c78f0ba4896794441a3acf6cbdc
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Mon Nov 7 15:00:41 2011 +0100

    image viewer: hide the horizontal scrollbar in fit_width mode

 gthumb/gth-image-navigator.c |   73 +++++++++++++++++++++++++++--------------
 gthumb/gth-image-viewer.c    |   62 ++++++++++++++++++++++++++++++-----
 gthumb/gth-image-viewer.h    |    8 +++-
 3 files changed, 107 insertions(+), 36 deletions(-)
---
diff --git a/gthumb/gth-image-navigator.c b/gthumb/gth-image-navigator.c
index 2275416..6c9082e 100644
--- a/gthumb/gth-image-navigator.c
+++ b/gthumb/gth-image-navigator.c
@@ -49,7 +49,8 @@ struct _GthImageNavigatorPrivate {
 	GtkWidget *hscrollbar;
 	GtkWidget *navigator_event_area;
 	gboolean   automatic_scrollbars;
-	gboolean   scrollbars_visible;
+	gboolean   hscrollbar_visible;
+	gboolean   vscrollbar_visible;
 };
 
 
@@ -119,21 +120,37 @@ gth_image_navigator_size_allocate (GtkWidget     *widget,
 				   GtkAllocation *allocation)
 {
 	GthImageNavigator *self = (GthImageNavigator *) widget;
-	gboolean           needs_scrollbars;
+	gboolean           hscrollbar_visible;
+	gboolean           vscrollbar_visible;
 	GtkAllocation      viewer_allocation;
 
 	gtk_widget_set_allocation (widget, allocation);
 
-	needs_scrollbars = self->priv->automatic_scrollbars && gth_image_viewer_needs_scrollbars (GTH_IMAGE_VIEWER (self->priv->viewer), allocation);
-	if (self->priv->scrollbars_visible != needs_scrollbars) {
-		self->priv->scrollbars_visible = needs_scrollbars;
-		gtk_widget_set_child_visible (self->priv->vscrollbar, self->priv->scrollbars_visible);
-		gtk_widget_set_child_visible (self->priv->hscrollbar, self->priv->scrollbars_visible);
-		gtk_widget_set_child_visible (self->priv->navigator_event_area, self->priv->scrollbars_visible);
+	if (self->priv->automatic_scrollbars) {
+		gth_image_viewer_needs_scrollbars (GTH_IMAGE_VIEWER (self->priv->viewer),
+						   allocation,
+						   self->priv->hscrollbar,
+						   self->priv->vscrollbar,
+						   &hscrollbar_visible,
+						   &vscrollbar_visible);
+
+		if (self->priv->vscrollbar_visible != vscrollbar_visible)
+			self->priv->vscrollbar_visible = vscrollbar_visible;
+
+		if (self->priv->hscrollbar_visible != hscrollbar_visible)
+			self->priv->hscrollbar_visible = hscrollbar_visible;
 	}
+	else {
+		self->priv->vscrollbar_visible = FALSE;
+		self->priv->hscrollbar_visible = FALSE;
+	}
+
+	gtk_widget_set_child_visible (self->priv->vscrollbar, self->priv->vscrollbar_visible);
+	gtk_widget_set_child_visible (self->priv->hscrollbar, self->priv->hscrollbar_visible);
+	gtk_widget_set_child_visible (self->priv->navigator_event_area, self->priv->hscrollbar_visible || self->priv->vscrollbar_visible);
 
 	viewer_allocation = *allocation;
-	if (self->priv->scrollbars_visible) {
+	if (self->priv->hscrollbar_visible || self->priv->vscrollbar_visible) {
 		GtkRequisition vscrollbar_requisition;
 		GtkRequisition hscrollbar_requisition;
 		GtkAllocation  child_allocation;
@@ -141,24 +158,29 @@ gth_image_navigator_size_allocate (GtkWidget     *widget,
 		gtk_widget_get_preferred_size (self->priv->vscrollbar, &vscrollbar_requisition, NULL);
 		gtk_widget_get_preferred_size (self->priv->hscrollbar, &hscrollbar_requisition, NULL);
 
-		viewer_allocation.width -= vscrollbar_requisition.width;
-		viewer_allocation.height -= hscrollbar_requisition.height;
+		if (self->priv->vscrollbar_visible) {
+			viewer_allocation.width -= vscrollbar_requisition.width;
 
-		/* vertical scrollbar */
+			/* vertical scrollbar */
 
-		child_allocation.x = allocation->x + allocation->width - vscrollbar_requisition.width;
-		child_allocation.y = allocation->y;
-		child_allocation.width = vscrollbar_requisition.width;
-		child_allocation.height = allocation->height - hscrollbar_requisition.height;
-		gtk_widget_size_allocate (self->priv->vscrollbar, &child_allocation);
+			child_allocation.x = allocation->x + allocation->width - vscrollbar_requisition.width;
+			child_allocation.y = allocation->y;
+			child_allocation.width = vscrollbar_requisition.width;
+			child_allocation.height = allocation->height - hscrollbar_requisition.height;
+			gtk_widget_size_allocate (self->priv->vscrollbar, &child_allocation);
+		}
 
-		/* horizontal scrollbar */
+		if (self->priv->hscrollbar_visible) {
+			viewer_allocation.height -= hscrollbar_requisition.height;
 
-		child_allocation.x = allocation->x;
-		child_allocation.y = allocation->y + allocation->height - hscrollbar_requisition.height;
-		child_allocation.width = allocation->width - vscrollbar_requisition.width;
-		child_allocation.height = hscrollbar_requisition.height;
-		gtk_widget_size_allocate (self->priv->hscrollbar, &child_allocation);
+			/* horizontal scrollbar */
+
+			child_allocation.x = allocation->x;
+			child_allocation.y = allocation->y + allocation->height - hscrollbar_requisition.height;
+			child_allocation.width = allocation->width - vscrollbar_requisition.width;
+			child_allocation.height = hscrollbar_requisition.height;
+			gtk_widget_size_allocate (self->priv->hscrollbar, &child_allocation);
+		}
 
 		/* event area */
 
@@ -168,6 +190,7 @@ gth_image_navigator_size_allocate (GtkWidget     *widget,
 		child_allocation.height = hscrollbar_requisition.height;
 		gtk_widget_size_allocate (self->priv->navigator_event_area, &child_allocation);
 	}
+
 	gtk_widget_size_allocate (self->priv->viewer, &viewer_allocation);
 }
 
@@ -178,7 +201,6 @@ typedef struct {
 } DrawData;
 
 
-
 static void
 gth_image_navigator_draw_child (GtkWidget *child,
                                 gpointer   user_data)
@@ -627,7 +649,8 @@ gth_image_navigator_init (GthImageNavigator *self)
 	gtk_container_set_reallocate_redraws (GTK_CONTAINER (self), TRUE);
 
 	self->priv->automatic_scrollbars = TRUE;
-	self->priv->scrollbars_visible = FALSE;
+	self->priv->hscrollbar_visible = FALSE;
+	self->priv->vscrollbar_visible = FALSE;
 
 	/* horizonal scrollbar */
 
diff --git a/gthumb/gth-image-viewer.c b/gthumb/gth-image-viewer.c
index 1585918..fed6706 100644
--- a/gthumb/gth-image-viewer.c
+++ b/gthumb/gth-image-viewer.c
@@ -2347,21 +2347,65 @@ gth_image_viewer_get_reset_scrollbars (GthImageViewer *self)
 }
 
 
-gboolean
+void
 gth_image_viewer_needs_scrollbars (GthImageViewer *self,
-				   GtkAllocation  *allocation)
-{
-	int    visible_width;
-	int    visible_height;
-	double zoom_level;
-	int    zoomed_width;
-	int    zoomed_height;
+				   GtkAllocation  *allocation,
+				   GtkWidget      *hscrollbar,
+				   GtkWidget      *vscrollbar,
+				   gboolean       *hscrollbar_visible_p,
+				   gboolean       *vscrollbar_visible_p)
+{
+	double   zoom_level;
+	int      zoomed_width;
+	int      zoomed_height;
+	int      visible_width;
+	int      visible_height;
+	gboolean hscrollbar_visible;
+	gboolean vscrollbar_visible;
 
 	zoom_level = get_zoom_level_for_allocation (self, allocation);
 	_gth_image_viewer_get_zoomed_size_for_zoom (self, &zoomed_width, &zoomed_height, zoom_level);
 	_gth_image_viewer_get_visible_area_size_for_allocation (self, &visible_width, &visible_height, allocation);
 
-	return ((zoomed_width > visible_width) || (zoomed_height > visible_height));
+	hscrollbar_visible = (zoomed_width > visible_width);
+	vscrollbar_visible = (zoomed_height > visible_height);
+
+	switch (self->priv->fit) {
+	case GTH_FIT_SIZE:
+	case GTH_FIT_SIZE_IF_LARGER:
+		hscrollbar_visible = FALSE;
+		vscrollbar_visible = FALSE;
+		break;
+
+	case GTH_FIT_WIDTH:
+	case GTH_FIT_WIDTH_IF_LARGER:
+		hscrollbar_visible = FALSE;
+		break;
+
+	case GTH_FIT_NONE:
+		if (hscrollbar_visible != vscrollbar_visible) {
+			if (vscrollbar_visible) {
+				GtkRequisition vscrollbar_requisition;
+
+				gtk_widget_get_preferred_size (vscrollbar, &vscrollbar_requisition, NULL);
+				visible_width -= vscrollbar_requisition.width;
+				hscrollbar_visible = (zoomed_width > visible_width);
+			}
+			else if (hscrollbar_visible) {
+				GtkRequisition hscrollbar_requisition;
+
+				gtk_widget_get_preferred_size (hscrollbar, &hscrollbar_requisition, NULL);
+				visible_height -= hscrollbar_requisition.height;
+				vscrollbar_visible = (zoomed_height > visible_height);
+			}
+		}
+		break;
+	}
+
+	if (hscrollbar_visible_p != NULL)
+		*hscrollbar_visible_p = hscrollbar_visible;
+	if (vscrollbar_visible_p != NULL)
+		*vscrollbar_visible_p = vscrollbar_visible;
 }
 
 
diff --git a/gthumb/gth-image-viewer.h b/gthumb/gth-image-viewer.h
index bd459db..2dae86e 100644
--- a/gthumb/gth-image-viewer.h
+++ b/gthumb/gth-image-viewer.h
@@ -266,8 +266,12 @@ void           gth_image_viewer_get_scroll_offset        (GthImageViewer
 void           gth_image_viewer_set_reset_scrollbars     (GthImageViewer        *viewer,
   							  gboolean               reset);
 gboolean       gth_image_viewer_get_reset_scrollbars     (GthImageViewer        *viewer);
-gboolean       gth_image_viewer_needs_scrollbars         (GthImageViewer        *viewer,
-							  GtkAllocation         *allocation);
+void           gth_image_viewer_needs_scrollbars         (GthImageViewer        *self,
+							  GtkAllocation         *allocation,
+							  GtkWidget             *hscrollbar,
+							  GtkWidget             *vscrollbar,
+							  gboolean              *hscrollbar_visible,
+							  gboolean              *vscrollbar_visible);
 
 /* Cursor. */
 



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