--- /dev/null
+/* -*-C-*-
+
+Copyright (C) 2013 Matthew Birkholz
+
+This file is part of MIT/GNU Scheme.
+
+MIT/GNU Scheme 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.
+
+MIT/GNU Scheme 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 MIT/GNU Scheme; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
+USA.
+
+*/
+
+/* A specialized GtkPaned. See documentation of <gtk-paned-view>. */
+
+#include "gtkpanedview.h"
+
+static void gtk_paned_view_get_preferred_width (GtkWidget *widget,
+ gint *minimum_size,
+ gint *natural_size);
+static void gtk_paned_view_get_preferred_height (GtkWidget *widget,
+ gint *minimum_size,
+ gint *natural_size);
+static void gtk_paned_view_get_preferred_height_for_width
+ (GtkWidget *layout,
+ gint width,
+ gint *minimum_height,
+ gint *natural_height);
+static void gtk_paned_view_get_preferred_width_for_height
+ (GtkWidget *layout,
+ gint width,
+ gint *minimum_height,
+ gint *natural_height);
+
+static void gtk_paned_view_size_allocate (GtkWidget *widget,
+ GtkAllocation *allocation);
+
+G_DEFINE_TYPE (GtkPanedView, gtk_paned_view,
+ GTK_TYPE_PANED)
+
+static void
+gtk_paned_view_class_init (GtkPanedViewClass *class)
+{
+ GtkWidgetClass *widget_class = (GtkWidgetClass*) class;
+
+ widget_class->get_preferred_width = gtk_paned_view_get_preferred_width;
+ widget_class->get_preferred_height = gtk_paned_view_get_preferred_height;
+ widget_class->get_preferred_height_for_width = gtk_paned_view_get_preferred_height_for_width;
+ widget_class->get_preferred_width_for_height = gtk_paned_view_get_preferred_width_for_height;
+ widget_class->size_allocate = gtk_paned_view_size_allocate;
+}
+
+static void
+gtk_paned_view_init (GtkPanedView *paned_view)
+{
+ g_assert (GTK_PANED (paned_view) ->priv != NULL);
+}
+
+/**
+ * gtk_paned_view_new:
+ * @orientation: GTK_ORIENTATION_VERTICAL or GTK_ORIENTATION_HORIZONTAL.
+ *
+ * Creates a new paned view.
+ *
+ * Returns: a new paned view.
+ */
+GtkWidget*
+gtk_paned_view_new (GtkOrientation orientation)
+{
+ return (g_object_new (GTK_TYPE_PANED_VIEW,
+ "orientation", orientation,
+ NULL));
+}
+\f
+/* The rest of this file was copied from gtkpaned.c v3.6.0 with
+ minimal modification. */
+
+enum {
+ CHILD1,
+ CHILD2
+};
+
+struct _GtkPanedPrivate
+{
+ GtkPaned *first_paned;
+ GtkWidget *child1;
+ GtkWidget *child2;
+ GdkWindow *child1_window;
+ GdkWindow *child2_window;
+ GtkWidget *last_child1_focus;
+ GtkWidget *last_child2_focus;
+ GtkWidget *saved_focus;
+ GtkOrientation orientation;
+
+ GdkCursorType cursor_type;
+ GdkDevice *grab_device;
+ GdkRectangle handle_pos;
+ GdkWindow *handle;
+
+ gint child1_size;
+ gint drag_pos;
+ gint last_allocation;
+ gint max_position;
+ gint min_position;
+ gint original_position;
+
+ guint32 grab_time;
+
+ guint handle_prelit : 1;
+ guint in_drag : 1;
+ guint in_recursion : 1;
+ guint child1_resize : 1;
+ guint child1_shrink : 1;
+ guint child2_resize : 1;
+ guint child2_shrink : 1;
+ guint position_set : 1;
+};
+
+static void
+flip_child (GtkWidget *widget,
+ GtkAllocation *child_pos)
+{
+ GtkAllocation allocation;
+ gint x, width;
+
+ gtk_widget_get_allocation (widget, &allocation);
+ x = allocation.x;
+ width = allocation.width;
+
+ child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
+}
+
+static void
+gtk_paned_set_child_visible (GtkPaned *paned,
+ guint id,
+ gboolean visible)
+{
+ GtkPanedPrivate *priv = paned->priv;
+ GtkWidget *child;
+
+ child = id == CHILD1 ? priv->child1 : priv->child2;
+
+ if (child == NULL)
+ return;
+
+ gtk_widget_set_child_visible (child, visible);
+
+ if (gtk_widget_get_mapped (GTK_WIDGET (paned)))
+ {
+ GdkWindow *window = id == CHILD1 ? priv->child1_window : priv->child2_window;
+
+ if (visible != gdk_window_is_visible (window))
+ {
+ if (visible)
+ gdk_window_show (window);
+ else
+ gdk_window_hide (window);
+ }
+ }
+}
+
+static void
+gtk_paned_child_allocate (GtkWidget *child,
+ GdkWindow *child_window, /* can be NULL */
+ const GtkAllocation *window_allocation,
+ GtkAllocation *child_allocation)
+{
+ if (child_window)
+ gdk_window_move_resize (child_window,
+ window_allocation->x, window_allocation->y,
+ window_allocation->width, window_allocation->height);
+
+ gtk_widget_size_allocate (child, child_allocation);
+}
+
+static void
+gtk_paned_calc_position (GtkPaned *paned,
+ gint allocation,
+ gint child1_min,
+ gint child1_nat,
+ gint child2_min,
+ gint child2_nat)
+{
+ GtkPanedPrivate *priv = paned->priv;
+ gint old_position;
+ gint old_min_position;
+ gint old_max_position;
+
+ old_position = priv->child1_size;
+ old_min_position = priv->min_position;
+ old_max_position = priv->max_position;
+
+ priv->min_position = priv->child1_shrink ? 0 : child1_min;
+
+ priv->max_position = allocation;
+ if (!priv->child2_shrink)
+ priv->max_position = MAX (1, priv->max_position - child2_min);
+ priv->max_position = MAX (priv->min_position, priv->max_position);
+
+ if (!priv->position_set)
+ {
+ if (priv->child1_resize && !priv->child2_resize)
+ priv->child1_size = MAX (0, allocation - child2_nat);
+ else if (!priv->child1_resize && priv->child2_resize)
+ priv->child1_size = child1_nat;
+ else if (child1_nat + child2_nat != 0)
+ priv->child1_size = allocation * ((gdouble)child1_nat / (child1_nat + child2_nat)) + 0.5;
+ else
+ priv->child1_size = allocation * 0.5 + 0.5;
+ }
+ else
+ {
+ /* If the position was set before the initial allocation.
+ * (priv->last_allocation <= 0) just clamp it and leave it.
+ */
+ if (priv->last_allocation > 0)
+ {
+ if (priv->child1_resize && !priv->child2_resize)
+ priv->child1_size += allocation - priv->last_allocation;
+ else if (!(!priv->child1_resize && priv->child2_resize))
+ priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
+ }
+ }
+
+ priv->child1_size = CLAMP (priv->child1_size,
+ priv->min_position,
+ priv->max_position);
+
+ if (priv->child1)
+ gtk_paned_set_child_visible (paned, 0, priv->child1_size != 0);
+
+ if (priv->child2)
+ gtk_paned_set_child_visible (paned, 1, priv->child1_size != allocation);
+
+ g_object_freeze_notify (G_OBJECT (paned));
+ if (priv->child1_size != old_position)
+ g_object_notify (G_OBJECT (paned), "position");
+ if (priv->min_position != old_min_position)
+ g_object_notify (G_OBJECT (paned), "min-position");
+ if (priv->max_position != old_max_position)
+ g_object_notify (G_OBJECT (paned), "max-position");
+ g_object_thaw_notify (G_OBJECT (paned));
+
+ priv->last_allocation = allocation;
+}
+
+static void
+gtk_paned_view_size_allocate (GtkWidget *widget,
+ GtkAllocation *allocation)
+{
+ GtkPaned *paned = GTK_PANED (widget);
+ GtkPanedPrivate *priv = paned->priv;
+
+ gtk_widget_set_allocation (widget, allocation);
+
+ if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
+ priv->child2 && gtk_widget_get_visible (priv->child2))
+ {
+ GtkAllocation child1_allocation, window1_allocation;
+ GtkAllocation child2_allocation, window2_allocation;
+ GtkAllocation priv_child1_allocation;
+ GdkRectangle old_handle_pos;
+ gint handle_size;
+
+ gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
+
+ old_handle_pos = priv->handle_pos;
+
+ if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+ {
+ gint child1_min_width, child1_nat_width;
+ gint child2_min_width, child2_nat_width;
+
+ gtk_widget_get_preferred_width_for_height (priv->child1,
+ allocation->height,
+ &child1_min_width, &child1_nat_width);
+ gtk_widget_get_preferred_width_for_height (priv->child2,
+ allocation->height,
+ &child2_min_width, &child2_nat_width);
+
+ gtk_paned_calc_position (paned,
+ MAX (1, allocation->width - handle_size),
+ child1_min_width, child1_nat_width,
+ child2_min_width, child2_nat_width);
+
+ priv->handle_pos.x = allocation->x + priv->child1_size;
+ priv->handle_pos.y = allocation->y;
+ priv->handle_pos.width = handle_size;
+ priv->handle_pos.height = allocation->height;
+
+ window1_allocation.height = window2_allocation.height = allocation->height;
+ window1_allocation.width = MAX (1, priv->child1_size);
+ window1_allocation.x = allocation->x;
+ window1_allocation.y = window2_allocation.y = allocation->y;
+
+ window2_allocation.x = window1_allocation.x + priv->child1_size + priv->handle_pos.width;
+ window2_allocation.width = MAX (1, allocation->x + allocation->width - window2_allocation.x);
+
+ if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
+ {
+ flip_child (widget, &(window2_allocation));
+ flip_child (widget, &(window1_allocation));
+ flip_child (widget, &(priv->handle_pos));
+ }
+
+ child1_allocation.x = child1_allocation.y = 0;
+ child1_allocation.width = window1_allocation.width;
+ child1_allocation.height = window1_allocation.height;
+ if (child1_min_width > child1_allocation.width)
+ {
+ if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_LTR)
+ child1_allocation.x -= child1_min_width - child1_allocation.width;
+ child1_allocation.width = child1_min_width;
+ }
+
+ child2_allocation.x = child2_allocation.y = 0;
+ child2_allocation.width = window2_allocation.width;
+ child2_allocation.height = window2_allocation.height;
+ if (child2_min_width > child2_allocation.width)
+ {
+ if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
+ child2_allocation.x -= child2_min_width - child2_allocation.width;
+ child2_allocation.width = child2_min_width;
+ }
+ }
+ else
+ {
+ gint child1_min_height, child1_nat_height;
+ gint child2_min_height, child2_nat_height;
+
+ gtk_widget_get_preferred_height_for_width (priv->child1,
+ allocation->width,
+ &child1_min_height,
+ &child1_nat_height);
+ gtk_widget_get_preferred_height_for_width (priv->child2,
+ allocation->width,
+ &child2_min_height,
+ &child2_nat_height);
+
+ gtk_paned_calc_position (paned,
+ MAX (1, allocation->height - handle_size),
+ child1_min_height, child1_nat_height,
+ child2_min_height, child2_nat_height);
+
+ priv->handle_pos.x = allocation->x;
+ priv->handle_pos.y = allocation->y + priv->child1_size;
+ priv->handle_pos.width = allocation->width;
+ priv->handle_pos.height = handle_size;
+
+ window1_allocation.width = window2_allocation.width = allocation->width;
+ window1_allocation.height = MAX (1, priv->child1_size);
+ window1_allocation.x = window2_allocation.x = allocation->x;
+ window1_allocation.y = allocation->y;
+
+ window2_allocation.y = window1_allocation.y + priv->child1_size + priv->handle_pos.height;
+ window2_allocation.height = MAX (1, allocation->y + allocation->height - window2_allocation.y);
+
+ child1_allocation.x = child1_allocation.y = 0;
+ child1_allocation.width = window1_allocation.width;
+ child1_allocation.height = window1_allocation.height;
+ if (child1_min_height > child1_allocation.height)
+ {
+ child1_allocation.y -= child1_min_height - child1_allocation.height;
+ child1_allocation.height = child1_min_height;
+ }
+
+ child2_allocation.x = child2_allocation.y = 0;
+ child2_allocation.width = window2_allocation.width;
+ child2_allocation.height = window2_allocation.height;
+ if (child2_min_height > child2_allocation.height)
+ child2_allocation.height = child2_min_height;
+ }
+
+ if (gtk_widget_get_mapped (widget) &&
+ (old_handle_pos.x != priv->handle_pos.x ||
+ old_handle_pos.y != priv->handle_pos.y ||
+ old_handle_pos.width != priv->handle_pos.width ||
+ old_handle_pos.height != priv->handle_pos.height))
+ {
+ GdkWindow *window;
+
+ window = gtk_widget_get_window (widget);
+ gdk_window_invalidate_rect (window, &old_handle_pos, FALSE);
+ gdk_window_invalidate_rect (window, &priv->handle_pos, FALSE);
+ }
+
+ if (gtk_widget_get_realized (widget))
+ {
+ if (gtk_widget_get_mapped (widget))
+ gdk_window_show (priv->handle);
+
+ if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+ {
+ gdk_window_move_resize (priv->handle,
+ priv->handle_pos.x,
+ priv->handle_pos.y,
+ handle_size,
+ priv->handle_pos.height);
+ }
+ else
+ {
+ gdk_window_move_resize (priv->handle,
+ priv->handle_pos.x,
+ priv->handle_pos.y,
+ priv->handle_pos.width,
+ handle_size);
+ }
+ }
+
+ /* Now allocate the childen, making sure, when resizing not to
+ * overlap the windows
+ */
+ gtk_widget_get_allocation (priv->child1, &priv_child1_allocation);
+ if (gtk_widget_get_mapped (widget) &&
+ ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+ priv_child1_allocation.width < child1_allocation.width) ||
+
+ (priv->orientation == GTK_ORIENTATION_VERTICAL &&
+ priv_child1_allocation.height < child1_allocation.height)))
+ {
+ gtk_paned_child_allocate (priv->child2,
+ priv->child2_window,
+ &window2_allocation,
+ &child2_allocation);
+ gtk_paned_child_allocate (priv->child1,
+ priv->child1_window,
+ &window1_allocation,
+ &child1_allocation);
+ }
+ else
+ {
+ gtk_paned_child_allocate (priv->child1,
+ priv->child1_window,
+ &window1_allocation,
+ &child1_allocation);
+ gtk_paned_child_allocate (priv->child2,
+ priv->child2_window,
+ &window2_allocation,
+ &child2_allocation);
+ }
+ }
+ else
+ {
+ GtkAllocation window_allocation, child_allocation;
+
+ if (gtk_widget_get_realized (widget))
+ gdk_window_hide (priv->handle);
+
+ window_allocation.x = allocation->x;
+ window_allocation.y = allocation->y;
+ window_allocation.width = allocation->width;
+ window_allocation.height = allocation->height;
+ child_allocation.x = child_allocation.y = 0;
+ child_allocation.width = allocation->width;
+ child_allocation.height = allocation->height;
+
+ if (priv->child1 && gtk_widget_get_visible (priv->child1))
+ {
+ gtk_paned_set_child_visible (paned, 0, TRUE);
+ if (priv->child2)
+ gtk_paned_set_child_visible (paned, 1, FALSE);
+
+ gtk_paned_child_allocate (priv->child1,
+ priv->child1_window,
+ &window_allocation,
+ &child_allocation);
+ }
+ else if (priv->child2 && gtk_widget_get_visible (priv->child2))
+ {
+ gtk_paned_set_child_visible (paned, 1, TRUE);
+ if (priv->child1)
+ gtk_paned_set_child_visible (paned, 0, FALSE);
+
+ gtk_paned_child_allocate (priv->child2,
+ priv->child2_window,
+ &window_allocation,
+ &child_allocation);
+ }
+ else
+ {
+ if (priv->child1)
+ gtk_paned_set_child_visible (paned, 0, FALSE);
+ if (priv->child2)
+ gtk_paned_set_child_visible (paned, 1, FALSE);
+ }
+ }
+}
+
+static void
+get_preferred_size_for_size (GtkWidget *widget,
+ GtkOrientation orientation,
+ gint size,
+ gint *minimum,
+ gint *natural)
+{
+ if (orientation == GTK_ORIENTATION_HORIZONTAL)
+ if (size < 0)
+ gtk_widget_get_preferred_width (widget, minimum, natural);
+ else
+ gtk_widget_get_preferred_width_for_height (widget, size, minimum, natural);
+ else
+ if (size < 0)
+ gtk_widget_get_preferred_height (widget, minimum, natural);
+ else
+ gtk_widget_get_preferred_height_for_width (widget, size, minimum, natural);
+}
+
+static void
+gtk_paned_view_get_preferred_size (GtkWidget *widget,
+ GtkOrientation orientation,
+ gint size,
+ gint *minimum_size,
+ gint *natural_size)
+{
+ GtkPaned *paned = GTK_PANED (widget);
+ GtkPanedPrivate *priv = paned->priv;
+ gint child_min, child_nat;
+ gint minimum, natural;
+
+ minimum = 0;
+ natural = 0;
+
+ if (priv->child1 && gtk_widget_get_visible (priv->child1))
+ {
+ get_preferred_size_for_size (priv->child1, orientation, size, &child_min, &child_nat);
+ if (priv->child1_shrink && priv->orientation == orientation)
+ minimum += 0;
+ else
+ minimum += child_min;
+ natural += child_nat;
+ }
+
+ if (priv->child2 && gtk_widget_get_visible (priv->child2))
+ {
+ get_preferred_size_for_size (priv->child2, orientation, size, &child_min, &child_nat);
+
+ if (priv->orientation == orientation)
+ {
+ if (!priv->child2_shrink)
+ minimum += child_min;
+ natural += child_nat;
+ }
+ else
+ {
+ minimum = MAX (minimum, child_min);
+ natural = MAX (natural, child_nat);
+ }
+ }
+
+ if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
+ priv->child2 && gtk_widget_get_visible (priv->child2))
+ {
+ gint handle_size;
+
+ gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
+
+ if (priv->orientation == orientation)
+ {
+ minimum += handle_size;
+ natural += handle_size;
+ }
+ }
+
+ if (minimum_size)
+ *minimum_size = minimum;
+ if (natural_size)
+ *natural_size = natural;
+}
+
+static void
+gtk_paned_view_get_preferred_width (GtkWidget *widget,
+ gint *minimum,
+ gint *natural)
+{
+ gtk_paned_view_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum, natural);
+}
+
+static void
+gtk_paned_view_get_preferred_height (GtkWidget *widget,
+ gint *minimum,
+ gint *natural)
+{
+ gtk_paned_view_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, -1, minimum, natural);
+}
+
+static void
+gtk_paned_view_get_preferred_width_for_height (GtkWidget *widget,
+ gint height,
+ gint *minimum,
+ gint *natural)
+{
+ gtk_paned_view_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
+}
+
+static void
+gtk_paned_view_get_preferred_height_for_width (GtkWidget *widget,
+ gint width,
+ gint *minimum,
+ gint *natural)
+{
+ gtk_paned_view_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
+}