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,10 +97,11 @@ _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;
@ -202,46 +201,31 @@ gtk_drop_find_widget (GtkWidget *event_widget,
return FALSE; return FALSE;
} }
static void
clear_current_dest (gpointer data, GObject *former_object)
{
g_object_set_data (G_OBJECT (data), "current-dest", NULL);
}
void void
gtk_drag_dest_set_target (GtkDragDestInfo *info, gtk_drop_set_current_dest (GdkDrop *drop,
GtkDropTarget *dest) GtkDropTarget *dest)
{ {
if (info->dest) GtkDropTarget *old_dest;
g_object_remove_weak_pointer (G_OBJECT (info->dest), (gpointer *) &info->dest);
info->dest = dest; old_dest = g_object_get_data (G_OBJECT (drop), "current-dest");
if (info->dest) if (old_dest)
g_object_add_weak_pointer (G_OBJECT (info->dest), (gpointer *) &info->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);
} }
static void GtkDropTarget *
gtk_drag_dest_info_destroy (gpointer data) gtk_drop_get_current_dest (GdkDrop *drop)
{ {
GtkDragDestInfo *info = (GtkDragDestInfo *)data; return g_object_get_data (G_OBJECT (drop), "current-dest");
gtk_drag_dest_set_target (info, NULL);
g_slice_free (GtkDragDestInfo, data);
}
GtkDragDestInfo *
gtk_drag_get_dest_info (GdkDrop *drop,
gboolean create)
{
GtkDragDestInfo *info;
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;
} }

View File

@ -28,18 +28,9 @@ 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,
struct _GtkDragDestInfo
{
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); GtkDropTarget *dest);
GtkDropTarget *gtk_drop_get_current_dest (GdkDrop *drop);
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;