1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
* Nautilus
*
* Copyright (C) 2001 Eazel, Inc.
*
* Nautilus is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* Nautilus 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Author: John Harper <jsh@eazel.com>
*
*/
/* Registering that a window may be used as a drag 'n drop source */
#include <config.h>
#include "nautilus-drag-window.h"
#include "nautilus-gdk-extensions.h"
#include <gtk/gtk.h>
#include <X11/Xatom.h>
#include <gdk/gdkx.h>
#include <gdk/gdkprivate.h>
/* State for handling focus/raise */
typedef struct NautilusDragWindowDetails NautilusDragWindowDetails;
struct NautilusDragWindowDetails {
gboolean in_button_press;
gboolean pending_focus;
gboolean pending_raise;
Time focus_timestamp;
guint focus_timeout_tag;
};
/* Delay in milliseconds between receiving a TAKE_FOCUS or RAISE_WINDOW
* client message, and assuming that there's no following button-press
* event. This seems to be large enough to work, but small enough to be
* unnoticeable to the user.
*/
#define WINDOW_FOCUS_TIMEOUT 50
/* Key used to store a NautilusDragWindowDetails structure in each
* registered window's object data hash
*/
#define NAUTILUS_DRAG_WINDOW_DETAILS_KEY "nautilus-drag-window-details"
static Atom sawfish_wm_raise_window = 0;
/* Return the nearest ancestor of WIDGET that has type WIDGET_TYPE. But only
* if there's no widget between the two with type BLOCKING_TYPE.
*/
static GtkWidget *
get_ancestor_blocked_by (GtkWidget *widget,
GtkType widget_type,
GtkType blocking_type)
{
g_return_val_if_fail (widget != NULL, NULL);
g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
while (widget != NULL)
{
if (gtk_type_is_a (GTK_WIDGET_TYPE (widget), widget_type))
return widget;
else if (gtk_type_is_a (GTK_WIDGET_TYPE (widget), blocking_type))
return NULL;
widget = widget->parent;
}
return NULL;
}
/* Returns the details structure associated with WINDOW, or a null pointer
* if no such structure exists
*/
static NautilusDragWindowDetails *
get_details (GtkWindow *window)
{
NautilusDragWindowDetails *details;
details = gtk_object_get_data (GTK_OBJECT (window),
NAUTILUS_DRAG_WINDOW_DETAILS_KEY);
return details;
}
/* Commit any pending focus/raise requests for WINDOW. */
static void
execute_pending_requests (GtkWindow *window,
NautilusDragWindowDetails *details)
{
if (GTK_WIDGET_REALIZED (window)) {
if (details->pending_focus) {
nautilus_gdk_window_focus (GTK_WIDGET (window)->window,
details->focus_timestamp);
details->pending_focus = FALSE;
}
if (details->pending_raise) {
gdk_window_raise (GTK_WIDGET (window)->window);
details->pending_raise = FALSE;
}
}
}
/* Called when no button-press event arrived occurred shortly after
* receiving a TAKE_FOCUS or RAISE_WINDOW request. So just commit
* the pending requests.
*/
static gint
focus_timeout_callback (gpointer data)
{
GtkWindow *window;
NautilusDragWindowDetails *details;
window = GTK_WINDOW (data);
details = get_details (window);
if (details != NULL) {
execute_pending_requests (window, details);
details->focus_timeout_tag = 0;
}
/* Only ever a one-shot timeout */
return FALSE;
}
static void
remove_focus_timeout (GtkWindow *window)
{
NautilusDragWindowDetails *details;
details = get_details (window);
if (details != NULL && details->focus_timeout_tag != 0) {
gtk_timeout_remove (details->focus_timeout_tag);
details->focus_timeout_tag = 0;
}
}
static void
set_focus_timeout (GtkWindow *window)
{
NautilusDragWindowDetails *details;
details = get_details (window);
if (details != NULL) {
remove_focus_timeout (window);
details->focus_timeout_tag
= gtk_timeout_add (WINDOW_FOCUS_TIMEOUT,
focus_timeout_callback, window);
}
}
/* Called for all button-press events; sets the `in_button_press' flag */
static gboolean
button_press_emission_callback (GtkObject *object, guint signal_id,
guint n_params, GtkArg *params,
gpointer data)
{
GtkWidget *window;
NautilusDragWindowDetails *details;
/* This blocking is kind of a hack. But it seems necessary,
* otherwise we can get duped into counting unbalanced
* press/release events, which isn't healthy
*/
window = get_ancestor_blocked_by (GTK_WIDGET (object),
GTK_TYPE_WINDOW,
GTK_TYPE_MENU_SHELL);
if (window != NULL) {
details = get_details (GTK_WINDOW (window));
if (details != NULL) {
remove_focus_timeout (GTK_WINDOW (window));
if (!details->in_button_press) {
details->in_button_press = TRUE;
} else {
/* We never got the last button
* release. Adapt.
*/
execute_pending_requests (GTK_WINDOW (window),
details);
details->in_button_press = FALSE;
}
}
}
return TRUE;
}
/* Called for button-release events; commits any pending focus/raise */
static gboolean
button_release_emission_callback (GtkObject *object, guint signal_id,
guint n_params, GtkArg *params,
gpointer data)
{
GtkWidget *window;
NautilusDragWindowDetails *details;
window = get_ancestor_blocked_by (GTK_WIDGET (object),
GTK_TYPE_WINDOW,
GTK_TYPE_MENU_SHELL);
if (window != NULL) {
details = get_details (GTK_WINDOW (window));
if (details != NULL) {
execute_pending_requests (GTK_WINDOW (window),
details);
details->in_button_press = FALSE;
}
}
return TRUE;
}
/* Called when a drag is started. If a drag-window is found above the
* widget emitting the signal, cancel any pending focus/raise requests
*/
static gboolean
drag_begin_emission_callback (GtkObject *object, guint signal_id,
guint n_params, GtkArg *params,
gpointer data)
{
GtkWidget *window;
NautilusDragWindowDetails *details;
window = gtk_widget_get_ancestor (GTK_WIDGET (object),
GTK_TYPE_WINDOW);
if (window != NULL) {
details = get_details (GTK_WINDOW (window));
if (details != NULL) {
details->pending_focus = FALSE;
details->pending_raise = FALSE;
}
}
return TRUE;
}
/* The process-wide filter for WM_PROTOCOLS client messages */
static GdkFilterReturn
wm_protocols_filter (GdkXEvent *xev, GdkEvent *event, gpointer data)
{
XEvent *xevent;
GtkWindow *window;
NautilusDragWindowDetails *details;
xevent = (XEvent *)xev;
gdk_window_get_user_data (event->any.window, (gpointer *) &window);
if (window != NULL) {
details = get_details (window);
} else {
details = NULL;
}
if ((Atom) xevent->xclient.data.l[0] == gdk_wm_delete_window) {
/* (copied from gdkevents.c) */
/* The delete window request specifies a window
* to delete. We don't actually destroy the
* window because "it is only a request". (The
* window might contain vital data that the
* program does not want destroyed). Instead
* the event is passed along to the program,
* which should then destroy the window.
*/
event->any.type = GDK_DELETE;
return GDK_FILTER_TRANSLATE;
} else if ((Atom) xevent->xclient.data.l[0] == gdk_wm_take_focus) {
if (details != NULL) {
details->pending_focus = TRUE;
details->focus_timestamp = xevent->xclient.data.l[1];
/* Wait to see if a button-press event
* is received in the near future.
*/
set_focus_timeout (window);
}
return GDK_FILTER_REMOVE;
} else if ((Atom) xevent->xclient.data.l[0] == sawfish_wm_raise_window) {
if (details != NULL) {
details->pending_raise = TRUE;
/* Wait to see if a button-press event
* is received in the near future.
*/
set_focus_timeout (window);
}
return GDK_FILTER_REMOVE;
}
else {
return GDK_FILTER_CONTINUE;
}
}
static void
nautilus_drag_window_destroy (GtkObject *object, gpointer data)
{
NautilusDragWindowDetails *details;
details = get_details (GTK_WINDOW (object));
remove_focus_timeout (GTK_WINDOW (object));
/* The `details' will be freed automatically */
}
static void
nautilus_drag_window_realize (GtkWidget *widget, gpointer data)
{
GdkAtom protocols[3];
/* Tell the window manager _not_ to focus this window by itself */
nautilus_gdk_window_set_wm_hints_input (widget->window, FALSE);
/* Set WM_PROTOCOLS to the usual two atoms plus something that tells
* sawfish to send messages telling us when we might want to raise
* the window. (This won't work with other wm's, but it won't
* break anything either.)
*/
protocols[0] = gdk_wm_delete_window;
protocols[1] = gdk_wm_take_focus;
protocols[2] = sawfish_wm_raise_window;
nautilus_gdk_window_set_wm_protocols (widget->window, protocols, 3);
}
/* Public entry point */
/* initialize the instance's fields */
void
nautilus_drag_window_register (GtkWindow *window)
{
static gboolean initialized = FALSE;
NautilusDragWindowDetails *details;
guint signal_id;
if (!initialized) {
/* Add emission hooks for the signals we need to monitor
*/
signal_id = gtk_signal_lookup ("button_press_event",
GTK_TYPE_WIDGET);
gtk_signal_add_emission_hook (signal_id,
button_press_emission_callback,
NULL);
signal_id = gtk_signal_lookup ("button_release_event",
GTK_TYPE_WIDGET);
gtk_signal_add_emission_hook (signal_id,
button_release_emission_callback,
NULL);
signal_id = gtk_signal_lookup ("drag_begin",
GTK_TYPE_WIDGET);
gtk_signal_add_emission_hook (signal_id,
drag_begin_emission_callback,
NULL);
/* Intern the necessary X atoms
*/
sawfish_wm_raise_window = XInternAtom (GDK_DISPLAY (),
"_SAWFISH_WM_RAISE_WINDOW",
False);
/* Override the standard GTK filter for handling WM_PROTOCOLS
* client messages
*/
gdk_add_client_message_filter (gdk_wm_protocols,
wm_protocols_filter, NULL);
initialized = TRUE;
}
details = g_new0 (NautilusDragWindowDetails, 1);
gtk_object_set_data_full (GTK_OBJECT (window),
NAUTILUS_DRAG_WINDOW_DETAILS_KEY,
details, g_free);
gtk_signal_connect (GTK_OBJECT (window), "realize",
nautilus_drag_window_realize, NULL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
nautilus_drag_window_destroy, NULL);
}
|