1 | /* |
2 | * Copyright © 2020 Benjamin Otte |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2.1 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | * |
17 | * Authors: Benjamin Otte <otte@gnome.org> |
18 | */ |
19 | |
20 | #include "config.h" |
21 | |
22 | #include "gtkdropprivate.h" |
23 | |
24 | typedef struct _GtkDrop GtkDrop; |
25 | |
26 | struct _GtkDrop |
27 | { |
28 | /* TRUE if we are waiting for a gdk_drop_status() call */ |
29 | gboolean waiting; |
30 | /* TRUE if begin_event() has been called but end_event() hasn't yet - purely for debugging */ |
31 | gboolean active; |
32 | }; |
33 | |
34 | static void |
35 | gtk_drop_free (gpointer data) |
36 | { |
37 | GtkDrop *self = data; |
38 | |
39 | g_slice_free (GtkDrop, self); |
40 | } |
41 | |
42 | static GtkDrop * |
43 | gtk_drop_lookup (GdkDrop *drop) |
44 | { |
45 | static GQuark drop_quark = 0; |
46 | GtkDrop *result; |
47 | |
48 | if (G_UNLIKELY (drop_quark == 0)) |
49 | drop_quark = g_quark_from_string (string: "-gtk-drop-data" ); |
50 | |
51 | result = g_object_get_qdata (G_OBJECT (drop), quark: drop_quark); |
52 | if (result == NULL) |
53 | { |
54 | result = g_slice_new0 (GtkDrop); |
55 | g_object_set_qdata_full (G_OBJECT (drop), quark: drop_quark, data: result, destroy: gtk_drop_free); |
56 | } |
57 | |
58 | return result; |
59 | } |
60 | |
61 | void |
62 | gtk_drop_begin_event (GdkDrop *drop, |
63 | GdkEventType event_type) |
64 | { |
65 | GtkDrop *self; |
66 | |
67 | self = gtk_drop_lookup (drop); |
68 | |
69 | g_assert (self->waiting == FALSE); |
70 | g_assert (self->active == FALSE); |
71 | |
72 | self->active = TRUE; |
73 | if (event_type == GDK_DRAG_ENTER || |
74 | event_type == GDK_DRAG_MOTION) |
75 | self->waiting = TRUE; |
76 | } |
77 | |
78 | void |
79 | gtk_drop_end_event (GdkDrop *drop) |
80 | { |
81 | GtkDrop *self; |
82 | |
83 | self = gtk_drop_lookup (drop); |
84 | |
85 | g_assert (self->active == TRUE); |
86 | |
87 | if (self->waiting) |
88 | { |
89 | gdk_drop_status (self: drop, actions: 0, preferred: 0); |
90 | self->waiting = FALSE; |
91 | } |
92 | self->active = FALSE; |
93 | } |
94 | |
95 | gboolean |
96 | gtk_drop_status (GdkDrop *drop, |
97 | GdkDragAction actions, |
98 | GdkDragAction preferred_action) |
99 | { |
100 | GtkDrop *self; |
101 | |
102 | self = gtk_drop_lookup (drop); |
103 | |
104 | g_assert (self->active == TRUE); |
105 | |
106 | if (!self->waiting) |
107 | return FALSE; |
108 | |
109 | gdk_drop_status (self: drop, actions, preferred: preferred_action); |
110 | self->waiting = FALSE; |
111 | return TRUE; |
112 | } |
113 | |
114 | |