Get rid of GtkDragDestInfo

We can just attach the GtkDropTarget directly to
the GdkDrop.
This commit is contained in:
Matthias Clasen 2020-01-05 23:09:26 -05:00
parent dfb2cbdfdb
commit 6a7f39e6d2
3 changed files with 41 additions and 66 deletions

View File

@ -80,7 +80,7 @@ void
_gtk_drag_dest_handle_event (GtkWidget *toplevel, _gtk_drag_dest_handle_event (GtkWidget *toplevel,
GdkEvent *event) GdkEvent *event)
{ {
GtkDragDestInfo *info; GtkDropTarget *dest;
GdkDrop *drop; GdkDrop *drop;
GdkEventType event_type; GdkEventType event_type;
@ -90,8 +90,6 @@ _gtk_drag_dest_handle_event (GtkWidget *toplevel,
event_type = gdk_event_get_event_type (event); event_type = gdk_event_get_event_type (event);
drop = gdk_event_get_drop (event); drop = gdk_event_get_drop (event);
info = gtk_drag_get_dest_info (drop, TRUE);
/* Find the widget for the event */ /* Find the widget for the event */
switch ((guint) event_type) switch ((guint) event_type)
{ {
@ -99,13 +97,14 @@ _gtk_drag_dest_handle_event (GtkWidget *toplevel,
break; break;
case GDK_DRAG_LEAVE: case GDK_DRAG_LEAVE:
if (info->dest) dest = gtk_drop_get_current_dest (drop);
if (dest)
{ {
gtk_drop_target_emit_drag_leave (info->dest, drop); gtk_drop_target_emit_drag_leave (dest, drop);
gtk_drag_dest_set_target (info, NULL); gtk_drop_set_current_dest (drop, NULL);
} }
break; break;
case GDK_DRAG_MOTION: case GDK_DRAG_MOTION:
case GDK_DROP_START: case GDK_DROP_START:
{ {
@ -202,46 +201,31 @@ gtk_drop_find_widget (GtkWidget *event_widget,
return FALSE; return FALSE;
} }
void
gtk_drag_dest_set_target (GtkDragDestInfo *info,
GtkDropTarget *dest)
{
if (info->dest)
g_object_remove_weak_pointer (G_OBJECT (info->dest), (gpointer *) &info->dest);
info->dest = dest;
if (info->dest)
g_object_add_weak_pointer (G_OBJECT (info->dest), (gpointer *) &info->dest);
}
static void static void
gtk_drag_dest_info_destroy (gpointer data) clear_current_dest (gpointer data, GObject *former_object)
{ {
GtkDragDestInfo *info = (GtkDragDestInfo *)data; g_object_set_data (G_OBJECT (data), "current-dest", NULL);
gtk_drag_dest_set_target (info, NULL);
g_slice_free (GtkDragDestInfo, data);
} }
GtkDragDestInfo * void
gtk_drag_get_dest_info (GdkDrop *drop, gtk_drop_set_current_dest (GdkDrop *drop,
gboolean create) GtkDropTarget *dest)
{ {
GtkDragDestInfo *info; GtkDropTarget *old_dest;
static GQuark info_quark = 0;
if (!info_quark)
info_quark = g_quark_from_static_string ("gtk-dest-info");
info = g_object_get_qdata (G_OBJECT (drop), info_quark);
if (!info && create)
{
info = g_slice_new0 (GtkDragDestInfo);
info->drop = drop;
g_object_set_qdata_full (G_OBJECT (drop), info_quark,
info, gtk_drag_dest_info_destroy);
}
return info; old_dest = g_object_get_data (G_OBJECT (drop), "current-dest");
if (old_dest)
g_object_weak_unref (G_OBJECT (old_dest), clear_current_dest, drop);
g_object_set_data (G_OBJECT (drop), "current-dest", dest);
if (dest)
g_object_weak_ref (G_OBJECT (dest), clear_current_dest, drop);
}
GtkDropTarget *
gtk_drop_get_current_dest (GdkDrop *drop)
{
return g_object_get_data (G_OBJECT (drop), "current-dest");
} }

View File

@ -25,21 +25,12 @@
G_BEGIN_DECLS G_BEGIN_DECLS
void _gtk_drag_dest_handle_event (GtkWidget *toplevel, void _gtk_drag_dest_handle_event (GtkWidget *toplevel,
GdkEvent *event); GdkEvent *event);
typedef struct _GtkDragDestInfo GtkDragDestInfo; void gtk_drop_set_current_dest (GdkDrop *drop,
GtkDropTarget *dest);
struct _GtkDragDestInfo GtkDropTarget *gtk_drop_get_current_dest (GdkDrop *drop);
{
GtkDropTarget *dest;
GdkDrop *drop; /* drop */
};
GtkDragDestInfo * gtk_drag_get_dest_info (GdkDrop *drop,
gboolean create);
void gtk_drag_dest_set_target (GtkDragDestInfo *info,
GtkDropTarget *dest);
G_END_DECLS G_END_DECLS

View File

@ -691,14 +691,14 @@ gtk_drop_target_handle_event (GtkEventController *controller,
{ {
GtkDropTarget *dest = GTK_DROP_TARGET (controller); GtkDropTarget *dest = GTK_DROP_TARGET (controller);
GdkDrop *drop; GdkDrop *drop;
GtkDragDestInfo *info; GtkDropTarget *old_dest;
double x, y; double x, y;
gboolean found = FALSE; gboolean found = FALSE;
gdk_event_get_coords (event, &x, &y); gdk_event_get_coords (event, &x, &y);
drop = gdk_event_get_drop (event); drop = gdk_event_get_drop (event);
info = gtk_drag_get_dest_info (drop, TRUE); old_dest = gtk_drop_get_current_dest (drop);
switch ((int)gdk_event_get_event_type (event)) switch ((int)gdk_event_get_event_type (event))
{ {
@ -710,10 +710,10 @@ gtk_drop_target_handle_event (GtkEventController *controller,
/* We send a leave before the drop so that the widget unhighlights /* We send a leave before the drop so that the widget unhighlights
* properly. * properly.
*/ */
if (info->dest) if (old_dest)
{ {
gtk_drop_target_emit_drag_leave (info->dest, drop); gtk_drop_target_emit_drag_leave (old_dest, drop);
gtk_drag_dest_set_target (info, NULL); gtk_drop_set_current_dest (drop, NULL);
} }
found = gtk_drop_target_emit_drag_drop (dest, drop, x, y); found = gtk_drop_target_emit_drag_drop (dest, drop, x, y);
@ -725,13 +725,13 @@ gtk_drop_target_handle_event (GtkEventController *controller,
if (found) if (found)
{ {
if (info->dest && info->dest != dest) if (old_dest && old_dest != dest)
{ {
gtk_drop_target_emit_drag_leave (info->dest, drop); gtk_drop_target_emit_drag_leave (old_dest, drop);
gtk_drag_dest_set_target (info, NULL); gtk_drop_set_current_dest (drop, NULL);
} }
gtk_drag_dest_set_target (info, dest); gtk_drop_set_current_dest (drop, dest);
} }
return found; return found;