dragicon: Change how to acquire drag icons

Before, gtk_drag_icon_new_for_drag() allowed creating new drag icons.
This could cause multiple drag icons to exist for a single drag.

Now, gtk_drag_icon_get_for_drag() makes sure that only one drag icon is
created.
This commit is contained in:
Benjamin Otte 2020-03-02 02:55:38 +01:00
parent 9efc4e6777
commit 03882e1f96
5 changed files with 35 additions and 79 deletions

View File

@ -6880,7 +6880,7 @@ gtk_drop_controller_motion_get_type
<SECTION>
<FILE>gtkdragicon</FILE>
GtkDragIcon
gtk_drag_icon_new_for_drag
gtk_drag_icon_get_for_drag
gtk_drag_icon_set_child
gtk_drag_icon_get_child
gtk_drag_icon_set_from_paintable

View File

@ -17,7 +17,7 @@
#include "config.h"
#include "gtkdragiconprivate.h"
#include "gtkdragicon.h"
#include "gtkprivate.h"
#include "gtkintl.h"
@ -35,15 +35,17 @@
* @Short_description: A toplevel to use as drag icon
* @Title: GtkDragIcon
*
* GtkDragIcon is a #GtkNative implementation with the sole purpose
* GtkDragIcon is a #GtkRoot implementation with the sole purpose
* to serve as a drag icon during DND operations. A drag icon moves
* with the pointer during a drag operation and is destroyed when
* the drag ends.
*
* To set up a drag icon and associate it with an ongoing drag operation,
* use gtk_drag_icon_set_from_paintable(). It is also possible to create
* a GtkDragIcon with gtk_drag_icon_new_for_drag(() and populate it
* with widgets yourself.
* use gtk_drag_icon_get_for_drag() to get the icon for a drag. You can
* then use it like any other widget and use gtk_drag_icon_set_child() to
* set whatever widget should be used for the drag icon.
*
* Keep in mind that drag icons do not allow user input.
*/
struct _GtkDragIcon
{
@ -385,32 +387,41 @@ gtk_drag_icon_init (GtkDragIcon *self)
{
}
GtkWidget *
gtk_drag_icon_new (void)
{
return g_object_new (GTK_TYPE_DRAG_ICON, NULL);
}
/**
* gtk_drag_icon_new_for_drag:
* @drag: a #GtkDrag
* gtk_drag_icon_get_for_drag:
* @drag: a #GdkDrag
*
* Creates a #GtkDragIcon and associates it with the drag operation.
* Gets the #GtkDragIcon in use with @drag.
*
* Returns: the new #GtkDragIcon
* If no drag icon exists yet, a new one will be created
* and shown.
*
* Returns: (transfer none) the #GtkDragIcon
*/
GtkWidget *
gtk_drag_icon_new_for_drag (GdkDrag *drag)
gtk_drag_icon_get_for_drag (GdkDrag *drag)
{
GtkWidget *icon;
static GQuark drag_icon_quark = 0;
GtkWidget *self;
g_return_val_if_fail (GDK_IS_DRAG (drag), NULL);
icon = g_object_new (GTK_TYPE_DRAG_ICON, NULL);
if (G_UNLIKELY (drag_icon_quark == 0))
drag_icon_quark = g_quark_from_static_string ("-gtk-drag-icon");
gtk_drag_icon_set_surface (GTK_DRAG_ICON (icon), gdk_drag_get_drag_surface (drag));
self = g_object_get_qdata (G_OBJECT (drag), drag_icon_quark);
if (self == NULL)
{
self = g_object_new (GTK_TYPE_DRAG_ICON, NULL);
return icon;
GTK_DRAG_ICON (self)->surface = g_object_ref (gdk_drag_get_drag_surface (drag));
g_object_set_qdata_full (G_OBJECT (drag), drag_icon_quark, g_object_ref_sink (self), g_object_unref);
gtk_widget_show (self);
}
return self;
}
/**
@ -435,24 +446,11 @@ gtk_drag_icon_set_from_paintable (GdkDrag *drag,
gdk_drag_set_hotspot (drag, hot_x, hot_y);
icon = gtk_drag_icon_new_for_drag (drag);
icon = gtk_drag_icon_get_for_drag (drag);
picture = gtk_picture_new_for_paintable (paintable);
gtk_picture_set_can_shrink (GTK_PICTURE (picture), FALSE);
gtk_drag_icon_set_child (GTK_DRAG_ICON (icon), picture);
g_object_set_data_full (G_OBJECT (drag),
"icon",
g_object_ref_sink (icon),
(GDestroyNotify)gtk_widget_destroy);
gtk_widget_show (icon);
}
void
gtk_drag_icon_set_surface (GtkDragIcon *icon,
GdkSurface *surface)
{
g_set_object (&icon->surface, surface);
}
/**

View File

@ -38,7 +38,7 @@ GDK_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (GtkDragIcon, gtk_drag_icon, GTK, DRAG_ICON, GtkContainer)
GDK_AVAILABLE_IN_ALL
GtkWidget * gtk_drag_icon_new_for_drag (GdkDrag *drag);
GtkWidget * gtk_drag_icon_get_for_drag (GdkDrag *drag);
GDK_AVAILABLE_IN_ALL
void gtk_drag_icon_set_child (GtkDragIcon *self,

View File

@ -1,42 +0,0 @@
/* GTK - The GIMP Toolkit
* Copyright 2019 Matthias Clasen
*
* This library is free software; you can redistribute it and/or
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* 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/.
*/
#ifndef __GTK_DRAG_ICON_PRIVATE_H__
#define __GTK_DRAG_ICON_PRIVATE_H__
#include <gio/gio.h>
#include <gtk/gtkdragicon.h>
G_BEGIN_DECLS
GtkWidget * gtk_drag_icon_new (void);
void gtk_drag_icon_set_surface (GtkDragIcon *icon,
GdkSurface *surface);
void gtk_drag_icon_set_widget (GtkDragIcon *icon,
GtkWidget *widget);
G_END_DECLS
#endif /* __GTK_DRAG_ICON_PRIVATE_H__ */

View File

@ -34,7 +34,7 @@
#include "gtkintl.h"
#include "gtkstylecontext.h"
#include "gtkimageprivate.h"
#include "gtkdragiconprivate.h"
#include "gtkdragicon.h"
#include "gtkprivate.h"
#include "gtkmarshalers.h"
#include "gtkicontheme.h"