X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtkhandlebox.c;h=e5f535163d9ed241f3585a63e5576933d4aeccf8;hb=15f5226ed6a9d61fbb1de592fc0d4ef2a600c171;hp=de2c8566944b99589441ffa5307de0254e560905;hpb=90408c4641e6493913eb859a67b3965bca259558;p=~andy%2Fgtk diff --git a/gtk/gtkhandlebox.c b/gtk/gtkhandlebox.c index de2c85669..e5f535163 100644 --- a/gtk/gtkhandlebox.c +++ b/gtk/gtkhandlebox.c @@ -3,47 +3,117 @@ * Copyright (C) 1998 Elliot Lee * * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public + * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 - * Library General Public License for more details. + * Lesser General Public License for more details. * - * You should have received a copy of the GNU Library General Public + * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ +/* + * 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/. + */ +#include #include -#include "gdk/gdkx.h" #include "gtkhandlebox.h" +#include "gtkinvisible.h" #include "gtkmain.h" -#include "gtksignal.h" +#include "gtkmarshalers.h" #include "gtkwindow.h" +#include "gtkprivate.h" +#include "gtkintl.h" +#include "gtkalias.h" + +typedef struct _GtkHandleBoxPrivate GtkHandleBoxPrivate; + +struct _GtkHandleBoxPrivate +{ + gint orig_x; + gint orig_y; +}; +enum { + PROP_0, + PROP_SHADOW, + PROP_SHADOW_TYPE, + PROP_HANDLE_POSITION, + PROP_SNAP_EDGE, + PROP_SNAP_EDGE_SET +}; #define DRAG_HANDLE_SIZE 10 #define CHILDLESS_SIZE 25 #define GHOST_HEIGHT 3 +#define TOLERANCE 5 -enum -{ +enum { SIGNAL_CHILD_ATTACHED, SIGNAL_CHILD_DETACHED, SIGNAL_LAST }; -typedef void (*SignalChildAttached) (GtkObject *object, - GtkWidget *widget, - gpointer func_data); +/* The algorithm for docking and redocking implemented here + * has a couple of nice properties: + * + * 1) During a single drag, docking always occurs at the + * the same cursor position. This means that the users + * motions are reversible, and that you won't + * undock/dock oscillations. + * + * 2) Docking generally occurs at user-visible features. + * The user, once they figure out to redock, will + * have useful information about doing it again in + * the future. + * + * Please try to preserve these properties if you + * change the algorithm. (And the current algorithm + * is far from ideal). Briefly, the current algorithm + * for deciding whether the handlebox is docked or not: + * + * 1) The decision is done by comparing two rectangles - the + * allocation if the widget at the start of the drag, + * and the boundary of hb->bin_window at the start of + * of the drag offset by the distance that the cursor + * has moved. + * + * 2) These rectangles must have one edge, the "snap_edge" + * of the handlebox, aligned within TOLERANCE. + * + * 3) On the other dimension, the extents of one rectangle + * must be contained in the extents of the other, + * extended by tolerance. That is, either we can have: + * + * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE-> + * <--------float_window--------------------> + * + * or we can have: + * + * <-TOLERANCE-|------float_window--------------|-TOLERANCE-> + * <--------bin_window--------------------> + */ static void gtk_handle_box_class_init (GtkHandleBoxClass *klass); static void gtk_handle_box_init (GtkHandleBox *handle_box); +static void gtk_handle_box_set_property (GObject *object, + guint param_id, + const GValue *value, + GParamSpec *pspec); +static void gtk_handle_box_get_property (GObject *object, + guint param_id, + GValue *value, + GParamSpec *pspec); static void gtk_handle_box_destroy (GtkObject *object); static void gtk_handle_box_map (GtkWidget *widget); static void gtk_handle_box_unmap (GtkWidget *widget); @@ -63,8 +133,6 @@ static void gtk_handle_box_draw_ghost (GtkHandleBox *hb); static void gtk_handle_box_paint (GtkWidget *widget, GdkEventExpose *event, GdkRectangle *area); -static void gtk_handle_box_draw (GtkWidget *widget, - GdkRectangle *area); static gint gtk_handle_box_expose (GtkWidget *widget, GdkEventExpose *event); static gint gtk_handle_box_button_changed (GtkWidget *widget, @@ -73,81 +141,102 @@ static gint gtk_handle_box_motion (GtkWidget *widget, GdkEventMotion *event); static gint gtk_handle_box_delete_event (GtkWidget *widget, GdkEventAny *event); +static void gtk_handle_box_reattach (GtkHandleBox *hb); +static void gtk_handle_box_end_drag (GtkHandleBox *hb, + guint32 time); static GtkBinClass *parent_class; static guint handle_box_signals[SIGNAL_LAST] = { 0 }; -guint +GType gtk_handle_box_get_type (void) { - static guint handle_box_type = 0; + static GType handle_box_type = 0; if (!handle_box_type) { - GtkTypeInfo handle_box_info = + static const GTypeInfo handle_box_info = { - "GtkHandleBox", - sizeof (GtkHandleBox), sizeof (GtkHandleBoxClass), - (GtkClassInitFunc) gtk_handle_box_class_init, - (GtkObjectInitFunc) gtk_handle_box_init, - /* reserved_1 */ NULL, - /* reserved_2 */ NULL, - (GtkClassInitFunc) NULL, + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) gtk_handle_box_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (GtkHandleBox), + 0, /* n_preallocs */ + (GInstanceInitFunc) gtk_handle_box_init, }; - handle_box_type = gtk_type_unique (gtk_bin_get_type (), &handle_box_info); + handle_box_type = g_type_register_static (GTK_TYPE_BIN, I_("GtkHandleBox"), + &handle_box_info, 0); } return handle_box_type; } -static void -gtk_handle_box_marshal_child_attached (GtkObject *object, - GtkSignalFunc func, - gpointer func_data, - GtkArg *args) -{ - SignalChildAttached sfunc = (SignalChildAttached) func; - - (* sfunc) (object, - (GtkWidget*) GTK_VALUE_OBJECT (args[0]), - func_data); -} - static void gtk_handle_box_class_init (GtkHandleBoxClass *class) { + GObjectClass *gobject_class; GtkObjectClass *object_class; GtkWidgetClass *widget_class; GtkContainerClass *container_class; + gobject_class = (GObjectClass *) class; object_class = (GtkObjectClass *) class; widget_class = (GtkWidgetClass *) class; container_class = (GtkContainerClass *) class; - parent_class = gtk_type_class (gtk_bin_get_type ()); + parent_class = g_type_class_peek_parent (class); - handle_box_signals[SIGNAL_CHILD_ATTACHED] = - gtk_signal_new ("child_attached", - GTK_RUN_FIRST, - object_class->type, - GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_attached), - gtk_handle_box_marshal_child_attached, - GTK_TYPE_NONE, 1, - GTK_TYPE_WIDGET); - handle_box_signals[SIGNAL_CHILD_DETACHED] = - gtk_signal_new ("child_detached", - GTK_RUN_FIRST, - object_class->type, - GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_detached), - gtk_handle_box_marshal_child_attached, - GTK_TYPE_NONE, 1, - GTK_TYPE_WIDGET); - gtk_object_class_add_signals (object_class, handle_box_signals, SIGNAL_LAST); + gobject_class->set_property = gtk_handle_box_set_property; + gobject_class->get_property = gtk_handle_box_get_property; + g_object_class_install_property (gobject_class, + PROP_SHADOW, + g_param_spec_enum ("shadow", NULL, + P_("Deprecated property, use shadow_type instead"), + GTK_TYPE_SHADOW_TYPE, + GTK_SHADOW_ETCHED_OUT, + GTK_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, + PROP_SHADOW_TYPE, + g_param_spec_enum ("shadow-type", + P_("Shadow type"), + P_("Appearance of the shadow that surrounds the container"), + GTK_TYPE_SHADOW_TYPE, + GTK_SHADOW_ETCHED_OUT, + GTK_PARAM_READWRITE)); + + g_object_class_install_property (gobject_class, + PROP_HANDLE_POSITION, + g_param_spec_enum ("handle-position", + P_("Handle position"), + P_("Position of the handle relative to the child widget"), + GTK_TYPE_POSITION_TYPE, + GTK_POS_LEFT, + GTK_PARAM_READWRITE)); + + g_object_class_install_property (gobject_class, + PROP_SNAP_EDGE, + g_param_spec_enum ("snap-edge", + P_("Snap edge"), + P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"), + GTK_TYPE_POSITION_TYPE, + GTK_POS_TOP, + GTK_PARAM_READWRITE)); + + g_object_class_install_property (gobject_class, + PROP_SNAP_EDGE_SET, + g_param_spec_boolean ("snap-edge-set", + P_("Snap edge set"), + P_("Whether to use the value from the snap_edge property or a value derived from handle_position"), + FALSE, + GTK_PARAM_READWRITE)); + object_class->destroy = gtk_handle_box_destroy; widget_class->map = gtk_handle_box_map; @@ -157,11 +246,8 @@ gtk_handle_box_class_init (GtkHandleBoxClass *class) widget_class->style_set = gtk_handle_box_style_set; widget_class->size_request = gtk_handle_box_size_request; widget_class->size_allocate = gtk_handle_box_size_allocate; - widget_class->draw = gtk_handle_box_draw; widget_class->expose_event = gtk_handle_box_expose; widget_class->button_press_event = gtk_handle_box_button_changed; - widget_class->button_release_event = gtk_handle_box_button_changed; - widget_class->motion_notify_event = gtk_handle_box_motion; widget_class->delete_event = gtk_handle_box_delete_event; container_class->add = gtk_handle_box_add; @@ -169,41 +255,134 @@ gtk_handle_box_class_init (GtkHandleBoxClass *class) class->child_attached = NULL; class->child_detached = NULL; + + handle_box_signals[SIGNAL_CHILD_ATTACHED] = + g_signal_new (I_("child_attached"), + G_OBJECT_CLASS_TYPE (gobject_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached), + NULL, NULL, + _gtk_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + GTK_TYPE_WIDGET); + handle_box_signals[SIGNAL_CHILD_DETACHED] = + g_signal_new (I_("child_detached"), + G_OBJECT_CLASS_TYPE (gobject_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached), + NULL, NULL, + _gtk_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + GTK_TYPE_WIDGET); +} + +static GtkHandleBoxPrivate * +gtk_handle_box_get_private (GtkHandleBox *hb) +{ + GtkHandleBoxPrivate *private; + static GQuark private_quark = 0; + + if (!private_quark) + private_quark = g_quark_from_static_string ("gtk-handle-box-private"); + + private = g_object_get_qdata (G_OBJECT (hb), private_quark); + + if (!private) + { + private = g_new0 (GtkHandleBoxPrivate, 1); + g_object_set_qdata_full (G_OBJECT (hb), private_quark, + private, g_free); + } + + return private; } static void gtk_handle_box_init (GtkHandleBox *handle_box) { GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW); - GTK_WIDGET_SET_FLAGS (handle_box, GTK_BASIC); /* FIXME: are we really a basic widget? */ handle_box->bin_window = NULL; handle_box->float_window = NULL; + handle_box->shadow_type = GTK_SHADOW_OUT; handle_box->handle_position = GTK_POS_LEFT; handle_box->float_window_mapped = FALSE; handle_box->child_detached = FALSE; handle_box->in_drag = FALSE; handle_box->shrink_on_detach = TRUE; - handle_box->dragoff_x = 0; - handle_box->dragoff_y = 0; + handle_box->snap_edge = -1; } +static void +gtk_handle_box_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GtkHandleBox *handle_box = GTK_HANDLE_BOX (object); + + switch (prop_id) + { + case PROP_SHADOW: + case PROP_SHADOW_TYPE: + gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value)); + break; + case PROP_HANDLE_POSITION: + gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value)); + break; + case PROP_SNAP_EDGE: + gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value)); + break; + case PROP_SNAP_EDGE_SET: + if (!g_value_get_boolean (value)) + gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gtk_handle_box_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + GtkHandleBox *handle_box = GTK_HANDLE_BOX (object); + + switch (prop_id) + { + case PROP_SHADOW: + case PROP_SHADOW_TYPE: + g_value_set_enum (value, handle_box->shadow_type); + break; + case PROP_HANDLE_POSITION: + g_value_set_enum (value, handle_box->handle_position); + break; + case PROP_SNAP_EDGE: + g_value_set_enum (value, + (handle_box->snap_edge == -1 ? + GTK_POS_TOP : handle_box->snap_edge)); + break; + case PROP_SNAP_EDGE_SET: + g_value_set_boolean (value, handle_box->snap_edge != -1); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + GtkWidget* gtk_handle_box_new (void) { - return GTK_WIDGET (gtk_type_new (gtk_handle_box_get_type ())); + return g_object_new (GTK_TYPE_HANDLE_BOX, NULL); } static void gtk_handle_box_destroy (GtkObject *object) { - GtkHandleBox *hb; - - g_return_if_fail (object != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (object)); - - hb = GTK_HANDLE_BOX (object); - if (GTK_OBJECT_CLASS (parent_class)->destroy) (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); } @@ -214,16 +393,15 @@ gtk_handle_box_map (GtkWidget *widget) GtkBin *bin; GtkHandleBox *hb; - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED); bin = GTK_BIN (widget); hb = GTK_HANDLE_BOX (widget); - gdk_window_show (hb->bin_window); - gdk_window_show (widget->window); + if (bin->child && + GTK_WIDGET_VISIBLE (bin->child) && + !GTK_WIDGET_MAPPED (bin->child)) + gtk_widget_map (bin->child); if (hb->child_detached && !hb->float_window_mapped) { @@ -231,10 +409,8 @@ gtk_handle_box_map (GtkWidget *widget) hb->float_window_mapped = TRUE; } - if (bin->child && - GTK_WIDGET_VISIBLE (bin->child) && - !GTK_WIDGET_MAPPED (bin->child)) - gtk_widget_map (bin->child); + gdk_window_show (hb->bin_window); + gdk_window_show (widget->window); } static void @@ -242,9 +418,6 @@ gtk_handle_box_unmap (GtkWidget *widget) { GtkHandleBox *hb; - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED); hb = GTK_HANDLE_BOX (widget); @@ -264,9 +437,6 @@ gtk_handle_box_realize (GtkWidget *widget) gint attributes_mask; GtkHandleBox *hb; - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - hb = GTK_HANDLE_BOX (widget); GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); @@ -290,11 +460,11 @@ gtk_handle_box_realize (GtkWidget *widget) attributes.width = widget->allocation.width; attributes.height = widget->allocation.height; attributes.window_type = GDK_WINDOW_CHILD; - attributes.event_mask |= (gtk_widget_get_events (widget) | - GDK_EXPOSURE_MASK | - GDK_BUTTON1_MOTION_MASK | - GDK_POINTER_MOTION_HINT_MASK | - GDK_BUTTON_PRESS_MASK | + attributes.event_mask = (gtk_widget_get_events (widget) | + GDK_EXPOSURE_MASK | + GDK_BUTTON1_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK | + GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask); @@ -317,25 +487,23 @@ gtk_handle_box_realize (GtkWidget *widget) GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK); attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; - hb->float_window = gdk_window_new (NULL, &attributes, attributes_mask); + hb->float_window = gdk_window_new (gtk_widget_get_root_window (widget), + &attributes, attributes_mask); gdk_window_set_user_data (hb->float_window, widget); gdk_window_set_decorations (hb->float_window, 0); + gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR); widget->style = gtk_style_attach (widget->style, widget->window); gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb)); gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb)); gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb)); + gdk_window_set_back_pixmap (widget->window, NULL, TRUE); } static void gtk_handle_box_unrealize (GtkWidget *widget) { - GtkHandleBox *hb; - - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - - hb = GTK_HANDLE_BOX (widget); + GtkHandleBox *hb = GTK_HANDLE_BOX (widget); gdk_window_set_user_data (hb->bin_window, NULL); gdk_window_destroy (hb->bin_window); @@ -352,41 +520,60 @@ static void gtk_handle_box_style_set (GtkWidget *widget, GtkStyle *previous_style) { - GtkHandleBox *hb; - - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - - hb = GTK_HANDLE_BOX (widget); + GtkHandleBox *hb = GTK_HANDLE_BOX (widget); if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_NO_WINDOW (widget)) { gtk_style_set_background (widget->style, widget->window, -widget->state); + widget->state); gtk_style_set_background (widget->style, hb->bin_window, widget->state); gtk_style_set_background (widget->style, hb->float_window, widget->state); - if (GTK_WIDGET_DRAWABLE (widget)) - gdk_window_clear (widget->window); } } +static int +effective_handle_position (GtkHandleBox *hb) +{ + int handle_position; + + if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR) + handle_position = hb->handle_position; + else + { + switch (hb->handle_position) + { + case GTK_POS_LEFT: + handle_position = GTK_POS_RIGHT; + break; + case GTK_POS_RIGHT: + handle_position = GTK_POS_LEFT; + break; + default: + handle_position = hb->handle_position; + break; + } + } + + return handle_position; +} + static void gtk_handle_box_size_request (GtkWidget *widget, GtkRequisition *requisition) { GtkBin *bin; GtkHandleBox *hb; - - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - g_return_if_fail (requisition != NULL); + GtkRequisition child_requisition; + gint handle_position; bin = GTK_BIN (widget); hb = GTK_HANDLE_BOX (widget); - if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) + handle_position = effective_handle_position (hb); + + if (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) { requisition->width = DRAG_HANDLE_SIZE; requisition->height = 0; @@ -401,25 +588,31 @@ gtk_handle_box_size_request (GtkWidget *widget, * won't have any usefull hint for our size otherwise. */ if (bin->child) - gtk_widget_size_request (bin->child, &bin->child->requisition); + gtk_widget_size_request (bin->child, &child_requisition); + else + { + child_requisition.width = 0; + child_requisition.height = 0; + } if (hb->child_detached) { + /* FIXME: This doesn't work currently */ if (!hb->shrink_on_detach) { - if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) - requisition->height += bin->child->requisition.height; + if (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) + requisition->height += child_requisition.height; else - requisition->width += bin->child->requisition.width; + requisition->width += child_requisition.width; } else { - if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) - requisition->height += widget->style->klass->ythickness; + if (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) + requisition->height += widget->style->ythickness; else - requisition->width += widget->style->klass->xthickness; + requisition->width += widget->style->xthickness; } } else @@ -429,8 +622,8 @@ gtk_handle_box_size_request (GtkWidget *widget, if (bin->child) { - requisition->width += bin->child->requisition.width; - requisition->height += bin->child->requisition.height; + requisition->width += child_requisition.width; + requisition->height += child_requisition.height; } else { @@ -446,41 +639,23 @@ gtk_handle_box_size_allocate (GtkWidget *widget, { GtkBin *bin; GtkHandleBox *hb; - - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - g_return_if_fail (allocation != NULL); + GtkRequisition child_requisition; + gint handle_position; bin = GTK_BIN (widget); hb = GTK_HANDLE_BOX (widget); - widget->allocation.x = allocation->x; + handle_position = effective_handle_position (hb); - if (hb->child_detached) - { - guint max_req_height; - guint max_req_width; - - max_req_height = MAX (widget->requisition.height, - bin->child->requisition.height + - 2 * widget->style->klass->ythickness); - max_req_width = MAX (widget->requisition.width, - bin->child->requisition.width + - 2 * widget->style->klass->xthickness); - - if (allocation->height > max_req_height) - widget->allocation.y = allocation->y + - (allocation->height - max_req_height) / 2; - else - widget->allocation.y = allocation->y; - - widget->allocation.height = MIN (allocation->height, max_req_height); - widget->allocation.width = MIN (allocation->width, max_req_width); - } + if (bin->child) + gtk_widget_get_child_requisition (bin->child, &child_requisition); else { - widget->allocation = *allocation; - } + child_requisition.width = 0; + child_requisition.height = 0; + } + + widget->allocation = *allocation; if (GTK_WIDGET_REALIZED (hb)) gdk_window_move_resize (widget->window, @@ -492,18 +667,16 @@ gtk_handle_box_size_allocate (GtkWidget *widget, if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) { - GtkWidget *child; GtkAllocation child_allocation; guint border_width; - child = bin->child; border_width = GTK_CONTAINER (widget)->border_width; child_allocation.x = border_width; child_allocation.y = border_width; - if (hb->handle_position == GTK_POS_LEFT) + if (handle_position == GTK_POS_LEFT) child_allocation.x += DRAG_HANDLE_SIZE; - else if (hb->handle_position == GTK_POS_TOP) + else if (handle_position == GTK_POS_TOP) child_allocation.y += DRAG_HANDLE_SIZE; if (hb->child_detached) @@ -511,14 +684,14 @@ gtk_handle_box_size_allocate (GtkWidget *widget, guint float_width; guint float_height; - child_allocation.width = child->requisition.width; - child_allocation.height = child->requisition.height; + child_allocation.width = child_requisition.width; + child_allocation.height = child_requisition.height; float_width = child_allocation.width + 2 * border_width; float_height = child_allocation.height + 2 * border_width; - if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) + if (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) float_width += DRAG_HANDLE_SIZE; else float_height += DRAG_HANDLE_SIZE; @@ -537,11 +710,11 @@ gtk_handle_box_size_allocate (GtkWidget *widget, } else { - child_allocation.width = MAX (1, widget->allocation.width - 2 * border_width); - child_allocation.height = MAX (1, widget->allocation.height - 2 * border_width); + child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width); + child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width); - if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) + if (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) child_allocation.width -= DRAG_HANDLE_SIZE; else child_allocation.height -= DRAG_HANDLE_SIZE; @@ -566,13 +739,15 @@ gtk_handle_box_draw_ghost (GtkHandleBox *hb) guint y; guint width; guint height; + gint handle_position; widget = GTK_WIDGET (hb); - - if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) + + handle_position = effective_handle_position (hb); + if (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) { - x = hb->handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width; + x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE; y = 0; width = DRAG_HANDLE_SIZE; height = widget->allocation.height; @@ -580,194 +755,223 @@ gtk_handle_box_draw_ghost (GtkHandleBox *hb) else { x = 0; - y = hb->handle_position == GTK_POS_TOP ? 0 : widget->allocation.height; + y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE; width = widget->allocation.width; height = DRAG_HANDLE_SIZE; } - gtk_draw_shadow (widget->style, - widget->window, - GTK_WIDGET_STATE (widget), - GTK_SHADOW_ETCHED_IN, - x, - y, - width, - height); - /* - if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) - gtk_draw_hline (widget->style, + gtk_paint_shadow (widget->style, widget->window, GTK_WIDGET_STATE (widget), - hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : widget->allocation.width - DRAG_HANDLE_SIZE, - widget->allocation.width - DRAG_HANDLE_SIZE, - widget->allocation.height / 2); - else - gtk_draw_vline (widget->style, - widget->window, - GTK_WIDGET_STATE (widget), - hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : widget->allocation.height - DRAG_HANDLE_SIZE, - widget->allocation.height - DRAG_HANDLE_SIZE, - widget->allocation.width / 2); - */ + GTK_SHADOW_ETCHED_IN, + NULL, widget, "handle", + x, + y, + width, + height); + if (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) + gtk_paint_hline (widget->style, + widget->window, + GTK_WIDGET_STATE (widget), + NULL, widget, "handlebox", + handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0, + handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE, + widget->allocation.height / 2); + else + gtk_paint_vline (widget->style, + widget->window, + GTK_WIDGET_STATE (widget), + NULL, widget, "handlebox", + handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0, + handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE, + widget->allocation.width / 2); } static void draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow, GdkRectangle *clip) { - int x, y; - int xthick, ythick; - GdkGC *light_gc, *dark_gc; - GdkRectangle dest; + gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow, + clip, widget, "handlebox", + rect->x, rect->y, rect->width, rect->height, + GTK_ORIENTATION_VERTICAL); +} + +void +gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box, + GtkShadowType type) +{ + g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box)); - if (gdk_rectangle_intersect (rect, clip, &dest)) + if ((GtkShadowType) handle_box->shadow_type != type) { - gdk_draw_rectangle (window, - widget->style->bg_gc[GTK_STATE_NORMAL], - TRUE, - dest.x, dest.y, - dest.width, dest.height); - - light_gc = widget->style->light_gc[GTK_STATE_NORMAL]; - dark_gc = widget->style->dark_gc[GTK_STATE_NORMAL]; - - xthick = widget->style->klass->xthickness; - ythick = widget->style->klass->ythickness; - - gdk_gc_set_clip_rectangle (light_gc, &dest); - gdk_gc_set_clip_rectangle (dark_gc, &dest); - - for (y = rect->y + ythick; y < (rect->y + rect->height - ythick); y += 3) - for (x = rect->x + xthick; x < (rect->x + rect->width - xthick); x += 6) - { - gdk_draw_point (window, light_gc, x, y); - gdk_draw_point (window, dark_gc, x + 1, y + 1); - - gdk_draw_point (window, light_gc, x + 3, y + 1); - gdk_draw_point (window, dark_gc, x + 4, y + 2); - } - - gdk_gc_set_clip_rectangle (light_gc, NULL); - gdk_gc_set_clip_rectangle (dark_gc, NULL); - - gtk_draw_shadow (widget->style, window, - GTK_STATE_NORMAL, shadow, - rect->x, rect->y, - rect->width, rect->height); + handle_box->shadow_type = type; + g_object_notify (G_OBJECT (handle_box), "shadow-type"); + gtk_widget_queue_resize (GTK_WIDGET (handle_box)); } } -static void -gtk_handle_box_paint (GtkWidget *widget, - GdkEventExpose *event, - GdkRectangle *area) +/** + * gtk_handle_box_get_shadow_type: + * @handle_box: a #GtkHandleBox + * + * Gets the type of shadow drawn around the handle box. See + * gtk_handle_box_set_shadow_type(). + * + * Return value: the type of shadow currently drawn around the handle box. + **/ +GtkShadowType +gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box) { - GtkBin *bin; - GtkHandleBox *hb; - guint width; - guint height; - guint border_width; - GdkRectangle rect; + g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT); - bin = GTK_BIN (widget); - hb = GTK_HANDLE_BOX (widget); + return handle_box->shadow_type; +} - border_width = GTK_CONTAINER (hb)->border_width; +void +gtk_handle_box_set_handle_position (GtkHandleBox *handle_box, + GtkPositionType position) +{ + g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box)); - if (hb->child_detached) + if ((GtkPositionType) handle_box->handle_position != position) { - width = bin->child->allocation.width + 2 * border_width; - height = bin->child->allocation.height + 2 * border_width; - } - else if (hb->handle_position == GTK_POS_LEFT || - hb->handle_position == GTK_POS_RIGHT) - { - width = widget->allocation.width - DRAG_HANDLE_SIZE; - height = widget->allocation.height; + handle_box->handle_position = position; + g_object_notify (G_OBJECT (handle_box), "handle-position"); + gtk_widget_queue_resize (GTK_WIDGET (handle_box)); } - else - { - width = widget->allocation.width; - height = widget->allocation.height - DRAG_HANDLE_SIZE; - } - - if (!event) - gdk_window_clear_area (hb->bin_window, - area->x, - area->y, - area->width, - area->height); +} - gtk_draw_shadow (widget->style, - hb->bin_window, - GTK_WIDGET_STATE (widget), - GTK_SHADOW_OUT, - hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0, - hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0, - width, - height); +/** + * gtk_handle_box_get_handle_position: + * @handle_box: a #GtkHandleBox + * + * Gets the handle position of the handle box. See + * gtk_handle_box_set_handle_position(). + * + * Return value: the current handle position. + **/ +GtkPositionType +gtk_handle_box_get_handle_position (GtkHandleBox *handle_box) +{ + g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT); - rect.x = 0; - rect.y = 0; - rect.width = (hb->handle_position == GTK_POS_LEFT) ? DRAG_HANDLE_SIZE : width; - rect.height = (hb->handle_position == GTK_POS_TOP) ? DRAG_HANDLE_SIZE : height; + return handle_box->handle_position; +} - draw_textured_frame (widget, hb->bin_window, &rect, GTK_SHADOW_OUT, event ? &event->area : area); +void +gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box, + GtkPositionType edge) +{ + g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box)); - if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) + if (handle_box->snap_edge != edge) { - GdkRectangle child_area; - GdkEventExpose child_event; - - if (!event) /* we were called from draw() */ - { - if (gtk_widget_intersect (bin->child, area, &child_area)) - gtk_widget_draw (bin->child, &child_area); - } - else /* we were called from expose() */ - { - child_event = *event; - - if (GTK_WIDGET_NO_WINDOW (bin->child) && - gtk_widget_intersect (bin->child, &event->area, &child_event.area)) - gtk_widget_event (bin->child, (GdkEvent *) &child_event); - } + handle_box->snap_edge = edge; + + g_object_freeze_notify (G_OBJECT (handle_box)); + g_object_notify (G_OBJECT (handle_box), "snap-edge"); + g_object_notify (G_OBJECT (handle_box), "snap-edge-set"); + g_object_thaw_notify (G_OBJECT (handle_box)); } } +/** + * gtk_handle_box_get_snap_edge: + * @handle_box: a #GtkHandleBox + * + * Gets the edge used for determining reattachment of the handle box. See + * gtk_handle_box_set_snap_edge(). + * + * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this + * is determined (as per default) from the handle position. + **/ +GtkPositionType +gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box) +{ + g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1); + + return handle_box->snap_edge; +} + static void -gtk_handle_box_draw (GtkWidget *widget, - GdkRectangle *area) +gtk_handle_box_paint (GtkWidget *widget, + + GdkEventExpose *event, + GdkRectangle *area) { + GtkBin *bin; GtkHandleBox *hb; + guint width, height; + GdkRectangle rect; + GdkRectangle dest; + gint handle_position; - g_return_if_fail (widget != NULL); - g_return_if_fail (GTK_IS_HANDLE_BOX (widget)); - g_return_if_fail (area != NULL); - + bin = GTK_BIN (widget); hb = GTK_HANDLE_BOX (widget); - if (GTK_WIDGET_DRAWABLE (widget)) - { - if (hb->child_detached) - { - /* The area parameter does not make sense in this case, so we - * repaint everything. - */ + handle_position = effective_handle_position (hb); - gtk_handle_box_draw_ghost (hb); + gdk_drawable_get_size (hb->bin_window, &width, &height); + + if (!event) + gtk_paint_box (widget->style, + hb->bin_window, + GTK_WIDGET_STATE (widget), + hb->shadow_type, + area, widget, "handlebox_bin", + 0, 0, -1, -1); + else + gtk_paint_box (widget->style, + hb->bin_window, + GTK_WIDGET_STATE (widget), + hb->shadow_type, + &event->area, widget, "handlebox_bin", + 0, 0, -1, -1); + +/* We currently draw the handle _above_ the relief of the handlebox. + * it could also be drawn on the same level... + + hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0, + hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0, + width, + height);*/ + + switch (handle_position) + { + case GTK_POS_LEFT: + rect.x = 0; + rect.y = 0; + rect.width = DRAG_HANDLE_SIZE; + rect.height = height; + break; + case GTK_POS_RIGHT: + rect.x = width - DRAG_HANDLE_SIZE; + rect.y = 0; + rect.width = DRAG_HANDLE_SIZE; + rect.height = height; + break; + case GTK_POS_TOP: + rect.x = 0; + rect.y = 0; + rect.width = width; + rect.height = DRAG_HANDLE_SIZE; + break; + case GTK_POS_BOTTOM: + rect.x = 0; + rect.y = height - DRAG_HANDLE_SIZE; + rect.width = width; + rect.height = DRAG_HANDLE_SIZE; + break; + } - area->x = 0; - area->y = 0; - area->width = 2 * GTK_CONTAINER (hb)->border_width + DRAG_HANDLE_SIZE; - area->height = area->width + GTK_BIN (hb)->child->allocation.height; - area->width += GTK_BIN (hb)->child->allocation.width; + if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest)) + draw_textured_frame (widget, hb->bin_window, &rect, + GTK_SHADOW_OUT, + event ? &event->area : area); - gtk_handle_box_paint (widget, NULL, area); - } - else - gtk_handle_box_paint (widget, NULL, area); - } + if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) + (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event); } static gint @@ -776,10 +980,6 @@ gtk_handle_box_expose (GtkWidget *widget, { GtkHandleBox *hb; - g_return_val_if_fail (widget != NULL, FALSE); - g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE); - g_return_val_if_fail (event != NULL, FALSE); - if (GTK_WIDGET_DRAWABLE (widget)) { hb = GTK_HANDLE_BOX (widget); @@ -796,6 +996,46 @@ gtk_handle_box_expose (GtkWidget *widget, return FALSE; } +static GtkWidget * +gtk_handle_box_get_invisible (void) +{ + static GtkWidget *handle_box_invisible = NULL; + + if (!handle_box_invisible) + { + handle_box_invisible = gtk_invisible_new (); + gtk_widget_show (handle_box_invisible); + } + + return handle_box_invisible; +} + +static gboolean +gtk_handle_box_grab_event (GtkWidget *widget, + GdkEvent *event, + GtkHandleBox *hb) +{ + switch (event->type) + { + case GDK_BUTTON_RELEASE: + if (hb->in_drag) /* sanity check */ + { + gtk_handle_box_end_drag (hb, event->button.time); + return TRUE; + } + break; + + case GDK_MOTION_NOTIFY: + return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event); + break; + + default: + break; + } + + return FALSE; +} + static gint gtk_handle_box_button_changed (GtkWidget *widget, GdkEventButton *event) @@ -803,42 +1043,46 @@ gtk_handle_box_button_changed (GtkWidget *widget, GtkHandleBox *hb; gboolean event_handled; GdkCursor *fleur; - - g_return_val_if_fail (widget != NULL, FALSE); - g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE); - g_return_val_if_fail (event != NULL, FALSE); + gint handle_position; hb = GTK_HANDLE_BOX (widget); + handle_position = effective_handle_position (hb); + event_handled = FALSE; - if (event->button == 1 && - event->type == GDK_BUTTON_PRESS) + if ((event->button == 1) && + (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS)) { GtkWidget *child; gboolean in_handle; + if (event->window != hb->bin_window) + return FALSE; + child = GTK_BIN (hb)->child; - - switch (hb->handle_position) + + if (child) { - case GTK_POS_LEFT: - in_handle = event->x < DRAG_HANDLE_SIZE; - break; - case GTK_POS_TOP: - in_handle = event->y < DRAG_HANDLE_SIZE; - break; - case GTK_POS_RIGHT: - in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width; - break; - case GTK_POS_BOTTOM: - in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height; - break; - default: - in_handle = FALSE; - break; + switch (handle_position) + { + case GTK_POS_LEFT: + in_handle = event->x < DRAG_HANDLE_SIZE; + break; + case GTK_POS_TOP: + in_handle = event->y < DRAG_HANDLE_SIZE; + break; + case GTK_POS_RIGHT: + in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width; + break; + case GTK_POS_BOTTOM: + in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height; + break; + default: + in_handle = FALSE; + break; + } } - - if (!child) + else { in_handle = FALSE; event_handled = TRUE; @@ -846,32 +1090,76 @@ gtk_handle_box_button_changed (GtkWidget *widget, if (in_handle) { - hb->dragoff_x = event->x; - hb->dragoff_y = event->y; - - gtk_grab_add (widget); - hb->in_drag = TRUE; - fleur = gdk_cursor_new (GDK_FLEUR); - while (gdk_pointer_grab (hb->bin_window, - FALSE, - (GDK_BUTTON1_MOTION_MASK | - GDK_POINTER_MOTION_HINT_MASK | - GDK_BUTTON_RELEASE_MASK), - NULL, - fleur, - GDK_CURRENT_TIME) != 0); /* wait for success */ - gdk_cursor_destroy (fleur); - event_handled = TRUE; + if (event->type == GDK_BUTTON_PRESS) /* Start a drag */ + { + GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb); + GtkWidget *invisible = gtk_handle_box_get_invisible (); + gint desk_x, desk_y; + gint root_x, root_y; + gint width, height; + + gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y); + gdk_window_get_origin (hb->bin_window, &root_x, &root_y); + gdk_drawable_get_size (hb->bin_window, &width, &height); + + private->orig_x = event->x_root; + private->orig_y = event->y_root; + + hb->float_allocation.x = root_x - event->x_root; + hb->float_allocation.y = root_y - event->y_root; + hb->float_allocation.width = width; + hb->float_allocation.height = height; + + hb->deskoff_x = desk_x - root_x; + hb->deskoff_y = desk_y - root_y; + + if (gdk_window_is_viewable (widget->window)) + { + gdk_window_get_origin (widget->window, &root_x, &root_y); + gdk_drawable_get_size (widget->window, &width, &height); + + hb->attach_allocation.x = root_x; + hb->attach_allocation.y = root_y; + hb->attach_allocation.width = width; + hb->attach_allocation.height = height; + } + else + { + hb->attach_allocation.x = -1; + hb->attach_allocation.y = -1; + hb->attach_allocation.width = 0; + hb->attach_allocation.height = 0; + } + hb->in_drag = TRUE; + fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget), + GDK_FLEUR); + if (gdk_pointer_grab (invisible->window, + FALSE, + (GDK_BUTTON1_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK | + GDK_BUTTON_RELEASE_MASK), + NULL, + fleur, + event->time) != 0) + { + hb->in_drag = FALSE; + } + else + { + gtk_grab_add (invisible); + g_signal_connect (invisible, "event", + G_CALLBACK (gtk_handle_box_grab_event), hb); + } + + gdk_cursor_unref (fleur); + event_handled = TRUE; + } + else if (hb->child_detached) /* Double click */ + { + gtk_handle_box_reattach (hb); + } } } - else if (event->type == GDK_BUTTON_RELEASE && - hb->in_drag) - { - gdk_pointer_ungrab (GDK_CURRENT_TIME); - gtk_grab_remove (widget); - hb->in_drag = FALSE; - event_handled = TRUE; - } return event_handled; } @@ -880,88 +1168,153 @@ static gint gtk_handle_box_motion (GtkWidget *widget, GdkEventMotion *event) { - GtkHandleBox *hb; + GtkHandleBox *hb = GTK_HANDLE_BOX (widget); gint new_x, new_y; - gint ox, oy; - gint snap_x, snap_y; - gboolean in_handle; - GdkCursor *fleur; + gint snap_edge; + gboolean is_snapped = FALSE; + gint handle_position; + GdkGeometry geometry; + GdkScreen *screen, *pointer_screen; - g_return_val_if_fail (widget != NULL, FALSE); - g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE); - g_return_val_if_fail (event != NULL, FALSE); - - hb = GTK_HANDLE_BOX (widget); if (!hb->in_drag) return FALSE; + handle_position = effective_handle_position (hb); - ox = 0; - oy = 0; - gdk_window_get_origin (hb->float_window, &ox, &oy); + /* Calculate the attachment point on the float, if the float + * were detached + */ new_x = 0; new_y = 0; - gdk_window_get_pointer (hb->float_window, &new_x, &new_y, NULL); - new_x += ox; - new_y += oy; - new_x -= hb->dragoff_x; - new_y -= hb->dragoff_y; - snap_x = 0; - snap_y = 0; - gdk_window_get_pointer (widget->window, &snap_x, &snap_y, NULL); + screen = gtk_widget_get_screen (widget); + gdk_display_get_pointer (gdk_screen_get_display (screen), + &pointer_screen, + &new_x, &new_y, NULL); + if (pointer_screen != screen) + { + GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb); + + new_x = private->orig_x; + new_y = private->orig_y; + } - switch (hb->handle_position) + new_x += hb->float_allocation.x; + new_y += hb->float_allocation.y; + + snap_edge = hb->snap_edge; + if (snap_edge == -1) + snap_edge = (handle_position == GTK_POS_LEFT || + handle_position == GTK_POS_RIGHT) ? + GTK_POS_TOP : GTK_POS_LEFT; + + if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) + switch (snap_edge) + { + case GTK_POS_LEFT: + snap_edge = GTK_POS_RIGHT; + break; + case GTK_POS_RIGHT: + snap_edge = GTK_POS_LEFT; + break; + default: + break; + } + + /* First, check if the snapped edge is aligned + */ + switch (snap_edge) { - case GTK_POS_LEFT: - in_handle = (snap_x >= 0 && snap_x <= DRAG_HANDLE_SIZE && - snap_y >= 0 && snap_y <= widget->allocation.height); - break; case GTK_POS_TOP: - in_handle = (snap_x >= 0 && snap_x <= widget->allocation.width && - snap_y >= 0 && snap_y <= DRAG_HANDLE_SIZE); - break; - case GTK_POS_RIGHT: - in_handle = (snap_x >= widget->allocation.width - DRAG_HANDLE_SIZE && snap_x <= widget->allocation.width && - snap_y >= 0 && snap_y <= widget->allocation.height); + is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE; break; case GTK_POS_BOTTOM: - in_handle = (snap_x >= 0 && snap_x <= widget->allocation.width && - snap_y >= widget->allocation.height - DRAG_HANDLE_SIZE && snap_y <= widget->allocation.height); + is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height - + new_y - (gint)hb->float_allocation.height) < TOLERANCE; break; - default: - in_handle = FALSE; + case GTK_POS_LEFT: + is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE; + break; + case GTK_POS_RIGHT: + is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width - + new_x - (gint)hb->float_allocation.width) < TOLERANCE; break; } - if (in_handle) + /* Next, check if coordinates in the other direction are sufficiently + * aligned + */ + if (is_snapped) + { + gint float_pos1 = 0; /* Initialize to suppress warnings */ + gint float_pos2 = 0; + gint attach_pos1 = 0; + gint attach_pos2 = 0; + + switch (snap_edge) + { + case GTK_POS_TOP: + case GTK_POS_BOTTOM: + attach_pos1 = hb->attach_allocation.x; + attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width; + float_pos1 = new_x; + float_pos2 = new_x + hb->float_allocation.width; + break; + case GTK_POS_LEFT: + case GTK_POS_RIGHT: + attach_pos1 = hb->attach_allocation.y; + attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height; + float_pos1 = new_y; + float_pos2 = new_y + hb->float_allocation.height; + break; + } + + is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) && + (attach_pos2 + TOLERANCE > float_pos2)) || + ((float_pos1 - TOLERANCE < attach_pos1) && + (float_pos2 + TOLERANCE > attach_pos2)); + } + + if (is_snapped) { if (hb->child_detached) { - gdk_pointer_ungrab (GDK_CURRENT_TIME); - hb->child_detached = FALSE; gdk_window_hide (hb->float_window); gdk_window_reparent (hb->bin_window, widget->window, 0, 0); hb->float_window_mapped = FALSE; - gtk_signal_emit (GTK_OBJECT (hb), - handle_box_signals[SIGNAL_CHILD_ATTACHED], - GTK_BIN (hb)->child); - - fleur = gdk_cursor_new (GDK_FLEUR); - while (gdk_pointer_grab (hb->bin_window, - FALSE, - (GDK_BUTTON1_MOTION_MASK | - GDK_POINTER_MOTION_HINT_MASK | - GDK_BUTTON_RELEASE_MASK), - NULL, - fleur, - GDK_CURRENT_TIME) != 0); /* wait for success */ - gdk_cursor_destroy (fleur); + g_signal_emit (hb, + handle_box_signals[SIGNAL_CHILD_ATTACHED], + 0, + GTK_BIN (hb)->child); gtk_widget_queue_resize (widget); } } else { + gint width, height; + + gdk_drawable_get_size (hb->float_window, &width, &height); + new_x += hb->deskoff_x; + new_y += hb->deskoff_y; + + switch (handle_position) + { + case GTK_POS_LEFT: + new_y += ((gint)hb->float_allocation.height - height) / 2; + break; + case GTK_POS_RIGHT: + new_x += (gint)hb->float_allocation.width - width; + new_y += ((gint)hb->float_allocation.height - height) / 2; + break; + case GTK_POS_TOP: + new_x += ((gint)hb->float_allocation.width - width) / 2; + break; + case GTK_POS_BOTTOM: + new_x += ((gint)hb->float_allocation.width - width) / 2; + new_y += (gint)hb->float_allocation.height - height; + break; + } + if (hb->child_detached) { gdk_window_move (hb->float_window, new_x, new_y); @@ -971,42 +1324,45 @@ gtk_handle_box_motion (GtkWidget *widget, { gint width; gint height; - - gdk_pointer_ungrab (GDK_CURRENT_TIME); + GtkRequisition child_requisition; hb->child_detached = TRUE; - width = 0; - height = 0; - gdk_window_get_size (hb->bin_window, &width, &height); + + if (GTK_BIN (hb)->child) + gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition); + else + { + child_requisition.width = 0; + child_requisition.height = 0; + } + + width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width; + height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width; + + if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT) + width += DRAG_HANDLE_SIZE; + else + height += DRAG_HANDLE_SIZE; + gdk_window_move_resize (hb->float_window, new_x, new_y, width, height); gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0); - gdk_window_set_hints (hb->float_window, new_x, new_y, 0, 0, 0, 0, GDK_HINT_POS); + gdk_window_set_geometry_hints (hb->float_window, &geometry, GDK_HINT_POS); gdk_window_show (hb->float_window); hb->float_window_mapped = TRUE; #if 0 - /* this extra move is neccessary if we use decorations, or our + /* this extra move is necessary if we use decorations, or our * window manager insists on decorations. */ - gdk_flush (); + gdk_display_sync (gtk_widget_get_display (widget)); gdk_window_move (hb->float_window, new_x, new_y); - gdk_flush (); + gdk_display_sync (gtk_widget_get_display (widget)); #endif /* 0 */ - gtk_signal_emit (GTK_OBJECT (hb), - handle_box_signals[SIGNAL_CHILD_DETACHED], - GTK_BIN (hb)->child); + g_signal_emit (hb, + handle_box_signals[SIGNAL_CHILD_DETACHED], + 0, + GTK_BIN (hb)->child); gtk_handle_box_draw_ghost (hb); - fleur = gdk_cursor_new (GDK_FLEUR); - while (gdk_pointer_grab (hb->bin_window, - FALSE, - (GDK_BUTTON1_MOTION_MASK | - GDK_POINTER_MOTION_HINT_MASK | - GDK_BUTTON_RELEASE_MASK), - NULL, - fleur, - GDK_CURRENT_TIME) != 0); /* wait for success */ - gdk_cursor_destroy (fleur); - gtk_widget_queue_resize (widget); } } @@ -1018,10 +1374,6 @@ static void gtk_handle_box_add (GtkContainer *container, GtkWidget *widget) { - g_return_if_fail (GTK_IS_HANDLE_BOX (container)); - g_return_if_fail (GTK_BIN (container)->child == NULL); - g_return_if_fail (widget->parent == NULL); - gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window); GTK_CONTAINER_CLASS (parent_class)->add (container, widget); } @@ -1030,62 +1382,69 @@ static void gtk_handle_box_remove (GtkContainer *container, GtkWidget *widget) { - GtkHandleBox *hb; + GTK_CONTAINER_CLASS (parent_class)->remove (container, widget); - g_return_if_fail (GTK_IS_HANDLE_BOX (container)); - g_return_if_fail (GTK_BIN (container)->child == widget); + gtk_handle_box_reattach (GTK_HANDLE_BOX (container)); +} + +static gint +gtk_handle_box_delete_event (GtkWidget *widget, + GdkEventAny *event) +{ + GtkHandleBox *hb = GTK_HANDLE_BOX (widget); - hb = GTK_HANDLE_BOX (container); + if (event->window == hb->float_window) + { + gtk_handle_box_reattach (hb); + + return TRUE; + } + return FALSE; +} + +static void +gtk_handle_box_reattach (GtkHandleBox *hb) +{ + GtkWidget *widget = GTK_WIDGET (hb); + if (hb->child_detached) { hb->child_detached = FALSE; if (GTK_WIDGET_REALIZED (hb)) { gdk_window_hide (hb->float_window); - gdk_window_reparent (hb->bin_window, GTK_WIDGET (hb)->window, 0, 0); + gdk_window_reparent (hb->bin_window, widget->window, 0, 0); + + if (GTK_BIN (hb)->child) + g_signal_emit (hb, + handle_box_signals[SIGNAL_CHILD_ATTACHED], + 0, + GTK_BIN (hb)->child); + } hb->float_window_mapped = FALSE; } if (hb->in_drag) - { - gdk_pointer_ungrab (GDK_CURRENT_TIME); - gtk_grab_remove (GTK_WIDGET (container)); - hb->in_drag = FALSE; - } - - GTK_CONTAINER_CLASS (parent_class)->remove (container, widget); -} - -static gint -gtk_handle_box_delete_event (GtkWidget *widget, - GdkEventAny *event) -{ - GtkHandleBox *hb; + gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME); - g_return_val_if_fail (widget != NULL, FALSE); - g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE); - g_return_val_if_fail (event != NULL, FALSE); - - hb = GTK_HANDLE_BOX (widget); - g_return_val_if_fail (event->window == hb->float_window, FALSE); - - hb->child_detached = FALSE; - gdk_window_hide (hb->float_window); - gdk_window_reparent (hb->bin_window, widget->window, 0, 0); - hb->float_window_mapped = FALSE; - gtk_signal_emit (GTK_OBJECT (hb), - handle_box_signals[SIGNAL_CHILD_ATTACHED], - GTK_BIN (hb)->child); - if (hb->in_drag) - { - gdk_pointer_ungrab (GDK_CURRENT_TIME); - gtk_grab_remove (widget); - hb->in_drag = FALSE; - } - gtk_widget_queue_resize (GTK_WIDGET (hb)); +} - return TRUE; +static void +gtk_handle_box_end_drag (GtkHandleBox *hb, + guint32 time) +{ + GtkWidget *invisible = gtk_handle_box_get_invisible (); + + hb->in_drag = FALSE; + + gtk_grab_remove (invisible); + gdk_pointer_ungrab (time); + g_signal_handlers_disconnect_by_func (invisible, + G_CALLBACK (gtk_handle_box_grab_event), + hb); } +#define __GTK_HANDLE_BOX_C__ +#include "gtkaliasdef.c"