diff options
author | Michael Natterer <mitch@gimp.org> | 2008-11-07 16:39:50 +0000 |
---|---|---|
committer | Michael Natterer <mitch@src.gnome.org> | 2008-11-07 16:39:50 +0000 |
commit | 59f0703855d6066aaef0a4e9b8dd2f2edd3fbdf2 (patch) | |
tree | 2b2722da77749249f22a35c957a1a0ba6945939b /gtk/gtkpaned.c | |
parent | 3e72ccbcdc75422010100354ac3d226746195626 (diff) | |
download | gtk+-59f0703855d6066aaef0a4e9b8dd2f2edd3fbdf2.tar.gz |
Bug 553586 – Add orientation API to GtkPaned
2008-11-07 Michael Natterer <mitch@gimp.org>
Bug 553586 – Add orientation API to GtkPaned
* gtk/gtkpaned.[ch]: implement the GtkOrientable interface
and swallow all code from GtkHPaned and GtkVPaned. Add
gtk_paned_new() which takes a GtkOrientation argument. Deprecate
gtk_paned_compute_position() for good (also for GTK_COMPILATION).
* gtk/gtkhpaned.[ch]
* gtk/gtkvpaned.[ch]: remove all code except the constructor and
call gtk_orientable_set_orientation() in init().
* gtk/gtk.symbols: add gtk_box_new().
svn path=/trunk/; revision=21772
Diffstat (limited to 'gtk/gtkpaned.c')
-rw-r--r-- | gtk/gtkpaned.c | 344 |
1 files changed, 317 insertions, 27 deletions
diff --git a/gtk/gtkpaned.c b/gtk/gtkpaned.c index 4f2db528a5..a1ed20c1bc 100644 --- a/gtk/gtkpaned.c +++ b/gtk/gtkpaned.c @@ -21,22 +21,25 @@ * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS * file for a list of people on the GTK+ Team. See the ChangeLog * files for a list of changes. These files are distributed with - * GTK+ at ftp://ftp.gtk.org/pub/gtk/. + * GTK+ at ftp://ftp.gtk.org/pub/gtk/. */ #include "config.h" -#include "gtkintl.h" -#include "gtkpaned.h" -#include "gtkbindings.h" + #include "gdk/gdkkeysyms.h" -#include "gtkwindow.h" +#include "gtkbindings.h" #include "gtkmain.h" #include "gtkmarshalers.h" +#include "gtkorientable.h" +#include "gtkpaned.h" +#include "gtkwindow.h" #include "gtkprivate.h" +#include "gtkintl.h" #include "gtkalias.h" enum { PROP_0, + PROP_ORIENTATION, PROP_POSITION, PROP_POSITION_SET, PROP_MIN_POSITION, @@ -78,6 +81,11 @@ static void gtk_paned_get_child_property (GtkContainer *container, GValue *value, GParamSpec *pspec); static void gtk_paned_finalize (GObject *object); + +static void gtk_paned_size_request (GtkWidget *widget, + GtkRequisition *requisition); +static void gtk_paned_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); static void gtk_paned_realize (GtkWidget *widget); static void gtk_paned_unrealize (GtkWidget *widget); static void gtk_paned_map (GtkWidget *widget); @@ -108,6 +116,10 @@ static void gtk_paned_forall (GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data); +static void gtk_paned_calc_position (GtkPaned *paned, + gint allocation, + gint child1_req, + gint child2_req); static void gtk_paned_set_focus_child (GtkContainer *container, GtkWidget *child); static void gtk_paned_set_saved_focus (GtkPaned *paned, @@ -134,15 +146,20 @@ static void gtk_paned_grab_notify (GtkWidget *widget, struct _GtkPanedPrivate { - GtkWidget *saved_focus; - GtkPaned *first_paned; - guint32 grab_time; + GtkOrientation orientation; + GtkWidget *saved_focus; + GtkPaned *first_paned; + guint32 grab_time; }; -G_DEFINE_ABSTRACT_TYPE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER) + +G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER, + G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, + NULL)) static guint signals[LAST_SIGNAL] = { 0 }; + static void add_tab_bindings (GtkBindingSet *binding_set, GdkModifierType modifiers) @@ -182,6 +199,8 @@ gtk_paned_class_init (GtkPanedClass *class) object_class->get_property = gtk_paned_get_property; object_class->finalize = gtk_paned_finalize; + widget_class->size_request = gtk_paned_size_request; + widget_class->size_allocate = gtk_paned_size_allocate; widget_class->realize = gtk_paned_realize; widget_class->unrealize = gtk_paned_unrealize; widget_class->map = gtk_paned_map; @@ -196,7 +215,7 @@ gtk_paned_class_init (GtkPanedClass *class) widget_class->grab_broken_event = gtk_paned_grab_broken; widget_class->grab_notify = gtk_paned_grab_notify; widget_class->state_changed = gtk_paned_state_changed; - + container_class->add = gtk_paned_add; container_class->remove = gtk_paned_remove; container_class->forall = gtk_paned_forall; @@ -212,6 +231,10 @@ gtk_paned_class_init (GtkPanedClass *class) paned_class->accept_position = gtk_paned_accept_position; paned_class->cancel_position = gtk_paned_cancel_position; + g_object_class_override_property (object_class, + PROP_ORIENTATION, + "orientation"); + g_object_class_install_property (object_class, PROP_POSITION, g_param_spec_int ("position", @@ -221,6 +244,7 @@ gtk_paned_class_init (GtkPanedClass *class) G_MAXINT, 0, GTK_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_POSITION_SET, g_param_spec_boolean ("position-set", @@ -520,7 +544,7 @@ gtk_paned_class_init (GtkPanedClass *class) add_move_binding (binding_set, GDK_End, 0, GTK_SCROLL_END); add_move_binding (binding_set, GDK_KP_End, 0, GTK_SCROLL_END); - g_type_class_add_private (object_class, sizeof (GtkPanedPrivate)); + g_type_class_add_private (object_class, sizeof (GtkPanedPrivate)); } static GType @@ -538,9 +562,15 @@ gtk_paned_init (GtkPaned *paned) GTK_WIDGET_SET_FLAGS (paned, GTK_NO_WINDOW | GTK_CAN_FOCUS); /* We only need to redraw when the handle position moves, which is - * independent of the overall allocation of the GtkPaned */ + * independent of the overall allocation of the GtkPaned + */ gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE); + paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate); + + paned->priv->orientation = GTK_ORIENTATION_HORIZONTAL; + paned->cursor_type = GDK_SB_H_DOUBLE_ARROW; + paned->child1 = NULL; paned->child2 = NULL; paned->handle = NULL; @@ -553,7 +583,6 @@ gtk_paned_init (GtkPaned *paned) paned->last_allocation = -1; paned->in_drag = FALSE; - paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate); paned->last_child1_focus = NULL; paned->last_child2_focus = NULL; paned->in_recursion = FALSE; @@ -573,9 +602,22 @@ gtk_paned_set_property (GObject *object, GParamSpec *pspec) { GtkPaned *paned = GTK_PANED (object); - + switch (prop_id) { + case PROP_ORIENTATION: + paned->priv->orientation = g_value_get_enum (value); + paned->orientation = !paned->priv->orientation; + + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL) + paned->cursor_type = GDK_SB_H_DOUBLE_ARROW; + else + paned->cursor_type = GDK_SB_V_DOUBLE_ARROW; + + /* state_changed updates the cursor */ + gtk_paned_state_changed (GTK_WIDGET (paned), GTK_WIDGET (paned)->state); + gtk_widget_queue_resize (GTK_WIDGET (paned)); + break; case PROP_POSITION: gtk_paned_set_position (paned, g_value_get_int (value)); break; @@ -596,9 +638,12 @@ gtk_paned_get_property (GObject *object, GParamSpec *pspec) { GtkPaned *paned = GTK_PANED (object); - + switch (prop_id) { + case PROP_ORIENTATION: + g_value_set_enum (value, paned->priv->orientation); + break; case PROP_POSITION: g_value_set_int (value, paned->child1_size); break; @@ -708,6 +753,224 @@ gtk_paned_finalize (GObject *object) } static void +gtk_paned_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + GtkPaned *paned = GTK_PANED (widget); + GtkRequisition child_requisition; + + requisition->width = 0; + requisition->height = 0; + + if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1)) + { + gtk_widget_size_request (paned->child1, &child_requisition); + + requisition->height = child_requisition.height; + requisition->width = child_requisition.width; + } + + if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2)) + { + gtk_widget_size_request (paned->child2, &child_requisition); + + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + requisition->height = MAX (requisition->height, + child_requisition.height); + requisition->width += child_requisition.width; + } + else + { + requisition->width = MAX (requisition->width, + child_requisition.width); + requisition->height += child_requisition.height; + } + } + + requisition->width += GTK_CONTAINER (paned)->border_width * 2; + requisition->height += GTK_CONTAINER (paned)->border_width * 2; + + if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) && + paned->child2 && GTK_WIDGET_VISIBLE (paned->child2)) + { + gint handle_size; + + gtk_widget_style_get (widget, "handle-size", &handle_size, NULL); + + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL) + requisition->width += handle_size; + else + requisition->height += handle_size; + } +} + +static void +flip_child (GtkWidget *widget, + GtkAllocation *child_pos) +{ + gint x = widget->allocation.x; + gint width = widget->allocation.width; + + child_pos->x = 2 * x + width - child_pos->x - child_pos->width; +} + +static void +gtk_paned_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GtkPaned *paned = GTK_PANED (widget); + gint border_width = GTK_CONTAINER (paned)->border_width; + + widget->allocation = *allocation; + + if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) && + paned->child2 && GTK_WIDGET_VISIBLE (paned->child2)) + { + GtkRequisition child1_requisition; + GtkRequisition child2_requisition; + GtkAllocation child1_allocation; + GtkAllocation child2_allocation; + GdkRectangle old_handle_pos; + gint handle_size; + + gtk_widget_style_get (widget, "handle-size", &handle_size, NULL); + + gtk_widget_get_child_requisition (paned->child1, &child1_requisition); + gtk_widget_get_child_requisition (paned->child2, &child2_requisition); + + old_handle_pos = paned->handle_pos; + + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + gtk_paned_compute_position (paned, + MAX (1, widget->allocation.width + - handle_size + - 2 * border_width), + child1_requisition.width, + child2_requisition.width); + + paned->handle_pos.x = widget->allocation.x + paned->child1_size + border_width; + paned->handle_pos.y = widget->allocation.y + border_width; + paned->handle_pos.width = handle_size; + paned->handle_pos.height = MAX (1, widget->allocation.height - 2 * border_width); + + child1_allocation.height = child2_allocation.height = MAX (1, (gint) allocation->height - border_width * 2); + child1_allocation.width = MAX (1, paned->child1_size); + child1_allocation.x = widget->allocation.x + border_width; + child1_allocation.y = child2_allocation.y = widget->allocation.y + border_width; + + child2_allocation.x = child1_allocation.x + paned->child1_size + paned->handle_pos.width; + child2_allocation.width = MAX (1, widget->allocation.x + widget->allocation.width - child2_allocation.x - border_width); + + if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL) + { + flip_child (widget, &(child2_allocation)); + flip_child (widget, &(child1_allocation)); + flip_child (widget, &(paned->handle_pos)); + } + } + else + { + gtk_paned_compute_position (paned, + MAX (1, widget->allocation.height + - handle_size + - 2 * border_width), + child1_requisition.height, + child2_requisition.height); + + paned->handle_pos.x = widget->allocation.x + border_width; + paned->handle_pos.y = widget->allocation.y + paned->child1_size + border_width; + paned->handle_pos.width = MAX (1, (gint) widget->allocation.width - 2 * border_width); + paned->handle_pos.height = handle_size; + + child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2); + child1_allocation.height = MAX (1, paned->child1_size); + child1_allocation.x = child2_allocation.x = widget->allocation.x + border_width; + child1_allocation.y = widget->allocation.y + border_width; + + child2_allocation.y = child1_allocation.y + paned->child1_size + paned->handle_pos.height; + child2_allocation.height = MAX (1, widget->allocation.y + widget->allocation.height - child2_allocation.y - border_width); + } + + if (GTK_WIDGET_MAPPED (widget) && + (old_handle_pos.x != paned->handle_pos.x || + old_handle_pos.y != paned->handle_pos.y || + old_handle_pos.width != paned->handle_pos.width || + old_handle_pos.height != paned->handle_pos.height)) + { + gdk_window_invalidate_rect (widget->window, &old_handle_pos, FALSE); + gdk_window_invalidate_rect (widget->window, &paned->handle_pos, FALSE); + } + + if (GTK_WIDGET_REALIZED (widget)) + { + if (GTK_WIDGET_MAPPED (widget)) + gdk_window_show (paned->handle); + + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL) + { + gdk_window_move_resize (paned->handle, + paned->handle_pos.x, + paned->handle_pos.y, + handle_size, + paned->handle_pos.height); + } + else + { + gdk_window_move_resize (paned->handle, + paned->handle_pos.x, + paned->handle_pos.y, + paned->handle_pos.width, + handle_size); + } + } + + /* Now allocate the childen, making sure, when resizing not to + * overlap the windows + */ + if (GTK_WIDGET_MAPPED (widget) && + + ((paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL && + paned->child1->allocation.width < child1_allocation.width) || + + (paned->priv->orientation == GTK_ORIENTATION_VERTICAL && + paned->child1->allocation.height < child1_allocation.height))) + { + gtk_widget_size_allocate (paned->child2, &child2_allocation); + gtk_widget_size_allocate (paned->child1, &child1_allocation); + } + else + { + gtk_widget_size_allocate (paned->child1, &child1_allocation); + gtk_widget_size_allocate (paned->child2, &child2_allocation); + } + } + else + { + GtkAllocation child_allocation; + + if (GTK_WIDGET_REALIZED (widget)) + gdk_window_hide (paned->handle); + + if (paned->child1) + gtk_widget_set_child_visible (paned->child1, TRUE); + if (paned->child2) + gtk_widget_set_child_visible (paned->child2, TRUE); + + child_allocation.x = widget->allocation.x + border_width; + child_allocation.y = widget->allocation.y + border_width; + child_allocation.width = MAX (1, allocation->width - 2 * border_width); + child_allocation.height = MAX (1, allocation->height - 2 * border_width); + + if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1)) + gtk_widget_size_allocate (paned->child1, &child_allocation); + else if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2)) + gtk_widget_size_allocate (paned->child2, &child_allocation); + } +} + +static void gtk_paned_realize (GtkWidget *widget) { GtkPaned *paned; @@ -824,7 +1087,7 @@ gtk_paned_expose (GtkWidget *widget, &paned->handle_pos, widget, "paned", paned->handle_pos.x, paned->handle_pos.y, paned->handle_pos.width, paned->handle_pos.height, - paned->orientation); + !paned->priv->orientation); } /* Chain up to draw children */ @@ -836,7 +1099,7 @@ gtk_paned_expose (GtkWidget *widget, static gboolean is_rtl (GtkPaned *paned) { - if (paned->orientation == GTK_ORIENTATION_VERTICAL && + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL && gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL) { return TRUE; @@ -851,11 +1114,11 @@ update_drag (GtkPaned *paned) gint pos; gint handle_size; gint size; - - if (paned->orientation == GTK_ORIENTATION_HORIZONTAL) - gtk_widget_get_pointer (GTK_WIDGET (paned), NULL, &pos); - else + + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL) gtk_widget_get_pointer (GTK_WIDGET (paned), &pos, NULL); + else + gtk_widget_get_pointer (GTK_WIDGET (paned), NULL, &pos); pos -= paned->drag_pos; @@ -964,11 +1227,11 @@ gtk_paned_button_press (GtkWidget *widget, paned->in_drag = TRUE; paned->priv->grab_time = event->time; - if (paned->orientation == GTK_ORIENTATION_HORIZONTAL) - paned->drag_pos = event->y; - else + if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL) paned->drag_pos = event->x; - + else + paned->drag_pos = event->y; + return TRUE; } @@ -1061,6 +1324,24 @@ gtk_paned_motion (GtkWidget *widget, return FALSE; } +/** + * gtk_paned_new: + * @orientation: the paned's orientation. + * + * Creates a new #GtkPaned widget. + * + * Return value: a new #GtkPaned. + * + * Since: 2.16 + **/ +GtkWidget * +gtk_paned_new (GtkOrientation orientation) +{ + return g_object_new (GTK_TYPE_PANED, + "orientation", orientation, + NULL); +} + void gtk_paned_add1 (GtkPaned *paned, GtkWidget *widget) @@ -1289,11 +1570,20 @@ gtk_paned_compute_position (GtkPaned *paned, gint child1_req, gint child2_req) { + g_return_if_fail (GTK_IS_PANED (paned)); + + gtk_paned_calc_position (paned, allocation, child1_req, child2_req); +} + +static void +gtk_paned_calc_position (GtkPaned *paned, + gint allocation, + gint child1_req, + gint child2_req) +{ gint old_position; gint old_min_position; gint old_max_position; - - g_return_if_fail (GTK_IS_PANED (paned)); old_position = paned->child1_size; old_min_position = paned->min_position; |