]> Pileus Git - ~andy/gtk/blob - gdk/gdkevents.c
Merge branch 'gtk-3-0' into broadway
[~andy/gtk] / gdk / gdkevents.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include "gdkinternals.h"
30 #include "gdkdisplayprivate.h"
31
32 #include <string.h>
33 #include <math.h>
34
35
36 /**
37  * SECTION:events
38  * @Short_description: Functions for handling events from the window system
39  * @Title: Events
40  * @See_also: <link linkend="gdk-Event-Structures">Event Structures</link>
41  *
42  * This section describes functions dealing with events from the window
43  * system.
44  *
45  * In GTK+ applications the events are handled automatically in
46  * gtk_main_do_event() and passed on to the appropriate widgets, so these
47  * functions are rarely needed. Though some of the fields in the
48  * <link linkend="gdk-Event-Structures">Event Structures</link> are useful.
49  */
50
51
52 typedef struct _GdkIOClosure GdkIOClosure;
53
54 struct _GdkIOClosure
55 {
56   GDestroyNotify notify;
57   gpointer data;
58 };
59
60 /* Private variable declarations
61  */
62
63 static GdkEventFunc   _gdk_event_func = NULL;    /* Callback for events */
64 static gpointer       _gdk_event_data = NULL;
65 static GDestroyNotify _gdk_event_notify = NULL;
66
67 void
68 _gdk_event_emit (GdkEvent *event)
69 {
70   if (_gdk_event_func)
71     (*_gdk_event_func) (event, _gdk_event_data);
72 }
73
74 /*********************************************
75  * Functions for maintaining the event queue *
76  *********************************************/
77
78 /**
79  * _gdk_event_queue_find_first:
80  * @display: a #GdkDisplay
81  * 
82  * Find the first event on the queue that is not still
83  * being filled in.
84  * 
85  * Return value: Pointer to the list node for that event, or NULL.
86  **/
87 GList*
88 _gdk_event_queue_find_first (GdkDisplay *display)
89 {
90   GList *tmp_list = display->queued_events;
91
92   while (tmp_list)
93     {
94       GdkEventPrivate *event = tmp_list->data;
95       if (!(event->flags & GDK_EVENT_PENDING))
96         return tmp_list;
97
98       tmp_list = g_list_next (tmp_list);
99     }
100
101   return NULL;
102 }
103
104 /**
105  * _gdk_event_queue_prepend:
106  * @display: a #GdkDisplay
107  * @event: Event to prepend.
108  *
109  * Prepends an event before the head of the event queue.
110  *
111  * Returns: the newly prepended list node.
112  **/
113 GList*
114 _gdk_event_queue_prepend (GdkDisplay *display,
115                           GdkEvent   *event)
116 {
117   display->queued_events = g_list_prepend (display->queued_events, event);
118   if (!display->queued_tail)
119     display->queued_tail = display->queued_events;
120   return display->queued_events;
121 }
122
123 /**
124  * _gdk_event_queue_append:
125  * @display: a #GdkDisplay
126  * @event: Event to append.
127  * 
128  * Appends an event onto the tail of the event queue.
129  *
130  * Returns: the newly appended list node.
131  **/
132 GList *
133 _gdk_event_queue_append (GdkDisplay *display,
134                          GdkEvent   *event)
135 {
136   display->queued_tail = g_list_append (display->queued_tail, event);
137   
138   if (!display->queued_events)
139     display->queued_events = display->queued_tail;
140   else
141     display->queued_tail = display->queued_tail->next;
142
143   return display->queued_tail;
144 }
145
146 /**
147  * _gdk_event_queue_insert_after:
148  * @display: a #GdkDisplay
149  * @sibling: Append after this event.
150  * @event: Event to append.
151  *
152  * Appends an event after the specified event, or if it isn't in
153  * the queue, onto the tail of the event queue.
154  *
155  * Returns: the newly appended list node.
156  *
157  * Since: 2.16
158  */
159 GList*
160 _gdk_event_queue_insert_after (GdkDisplay *display,
161                                GdkEvent   *sibling,
162                                GdkEvent   *event)
163 {
164   GList *prev = g_list_find (display->queued_events, sibling);
165   if (prev && prev->next)
166     {
167       display->queued_events = g_list_insert_before (display->queued_events, prev->next, event);
168       return prev->next;
169     }
170   else
171     return _gdk_event_queue_append (display, event);
172 }
173
174 /**
175  * _gdk_event_queue_insert_after:
176  * @display: a #GdkDisplay
177  * @sibling: Append after this event.
178  * @event: Event to append.
179  *
180  * Appends an event before the specified event, or if it isn't in
181  * the queue, onto the tail of the event queue.
182  *
183  * Returns: the newly appended list node.
184  *
185  * Since: 2.16
186  */
187 GList*
188 _gdk_event_queue_insert_before (GdkDisplay *display,
189                                 GdkEvent   *sibling,
190                                 GdkEvent   *event)
191 {
192   GList *next = g_list_find (display->queued_events, sibling);
193   if (next)
194     {
195       display->queued_events = g_list_insert_before (display->queued_events, next, event);
196       return next->prev;
197     }
198   else
199     return _gdk_event_queue_append (display, event);
200 }
201
202
203 /**
204  * _gdk_event_queue_remove_link:
205  * @display: a #GdkDisplay
206  * @node: node to remove
207  * 
208  * Removes a specified list node from the event queue.
209  **/
210 void
211 _gdk_event_queue_remove_link (GdkDisplay *display,
212                               GList      *node)
213 {
214   if (node->prev)
215     node->prev->next = node->next;
216   else
217     display->queued_events = node->next;
218   
219   if (node->next)
220     node->next->prev = node->prev;
221   else
222     display->queued_tail = node->prev;
223 }
224
225 /**
226  * _gdk_event_unqueue:
227  * @display: a #GdkDisplay
228  * 
229  * Removes and returns the first event from the event
230  * queue that is not still being filled in.
231  * 
232  * Return value: the event, or %NULL. Ownership is transferred
233  * to the caller.
234  **/
235 GdkEvent*
236 _gdk_event_unqueue (GdkDisplay *display)
237 {
238   GdkEvent *event = NULL;
239   GList *tmp_list;
240
241   tmp_list = _gdk_event_queue_find_first (display);
242
243   if (tmp_list)
244     {
245       event = tmp_list->data;
246       _gdk_event_queue_remove_link (display, tmp_list);
247       g_list_free_1 (tmp_list);
248     }
249
250   return event;
251 }
252
253 /**
254  * gdk_event_handler_set:
255  * @func: the function to call to handle events from GDK.
256  * @data: user data to pass to the function. 
257  * @notify: the function to call when the handler function is removed, i.e. when
258  *          gdk_event_handler_set() is called with another event handler.
259  * 
260  * Sets the function to call to handle all events from GDK.
261  *
262  * Note that GTK+ uses this to install its own event handler, so it is
263  * usually not useful for GTK+ applications. (Although an application
264  * can call this function then call gtk_main_do_event() to pass
265  * events to GTK+.)
266  **/
267 void 
268 gdk_event_handler_set (GdkEventFunc   func,
269                        gpointer       data,
270                        GDestroyNotify notify)
271 {
272   if (_gdk_event_notify)
273     (*_gdk_event_notify) (_gdk_event_data);
274
275   _gdk_event_func = func;
276   _gdk_event_data = data;
277   _gdk_event_notify = notify;
278 }
279
280 /**
281  * gdk_events_pending:
282  *
283  * Checks if any events are ready to be processed for any display.
284  *
285  * Return value: %TRUE if any events are pending.
286  */
287 gboolean
288 gdk_events_pending (void)
289 {
290   GSList *list, *l;
291   gboolean pending;
292
293   pending = FALSE;
294   list = gdk_display_manager_list_displays (gdk_display_manager_get ());
295   for (l = list; l; l = l->next)
296     {
297       if (_gdk_event_queue_find_first (l->data))
298         {
299           pending = TRUE;
300           goto out;
301         }
302     }
303
304   for (l = list; l; l = l->next)
305     {
306       if (gdk_display_has_pending (l->data))
307         {
308           pending = TRUE;
309           goto out;
310         }
311     }
312
313  out:
314   g_slist_free (list);
315
316   return pending;
317 }
318
319 /**
320  * gdk_event_get:
321  * 
322  * Checks all open displays for a #GdkEvent to process,to be processed
323  * on, fetching events from the windowing system if necessary.
324  * See gdk_display_get_event().
325  * 
326  * Return value: the next #GdkEvent to be processed, or %NULL if no events
327  * are pending. The returned #GdkEvent should be freed with gdk_event_free().
328  **/
329 GdkEvent*
330 gdk_event_get (void)
331 {
332   GSList *list, *l;
333   GdkEvent *event;
334
335   event = NULL;
336   list = gdk_display_manager_list_displays (gdk_display_manager_get ());
337   for (l = list; l; l = l->next)
338     {
339       event = gdk_display_get_event (l->data);
340       if (event)
341         break;
342     }
343
344   g_slist_free (list);
345
346   return event;
347 }
348
349 /**
350  * gdk_event_peek:
351  *
352  * If there is an event waiting in the event queue of some open
353  * display, returns a copy of it. See gdk_display_peek_event().
354  * 
355  * Return value: a copy of the first #GdkEvent on some event queue, or %NULL if no
356  * events are in any queues. The returned #GdkEvent should be freed with
357  * gdk_event_free().
358  **/
359 GdkEvent*
360 gdk_event_peek (void)
361 {
362   GSList *list, *l;
363   GdkEvent *event;
364
365   event = NULL;
366   list = gdk_display_manager_list_displays (gdk_display_manager_get ());
367   for (l = list; l; l = l->next)
368     {
369       event = gdk_display_peek_event (l->data);
370       if (event)
371         break;
372     }
373
374   g_slist_free (list);
375
376   return event;
377 }
378
379 /**
380  * gdk_event_put:
381  * @event: a #GdkEvent.
382  *
383  * Appends a copy of the given event onto the front of the event
384  * queue for event->any.window's display, or the default event
385  * queue if event->any.window is %NULL. See gdk_display_put_event().
386  **/
387 void
388 gdk_event_put (const GdkEvent *event)
389 {
390   GdkDisplay *display;
391   
392   g_return_if_fail (event != NULL);
393
394   if (event->any.window)
395     display = gdk_window_get_display (event->any.window);
396   else
397     {
398       GDK_NOTE (MULTIHEAD,
399                 g_message ("Falling back to default display for gdk_event_put()"));
400       display = gdk_display_get_default ();
401     }
402
403   gdk_display_put_event (display, event);
404 }
405
406 static GHashTable *event_hash = NULL;
407
408 /**
409  * gdk_event_new:
410  * @type: a #GdkEventType 
411  * 
412  * Creates a new event of the given type. All fields are set to 0.
413  * 
414  * Return value: a newly-allocated #GdkEvent. The returned #GdkEvent 
415  * should be freed with gdk_event_free().
416  *
417  * Since: 2.2
418  **/
419 GdkEvent*
420 gdk_event_new (GdkEventType type)
421 {
422   GdkEventPrivate *new_private;
423   GdkEvent *new_event;
424   
425   if (!event_hash)
426     event_hash = g_hash_table_new (g_direct_hash, NULL);
427
428   new_private = g_slice_new0 (GdkEventPrivate);
429   
430   new_private->flags = 0;
431   new_private->screen = NULL;
432
433   g_hash_table_insert (event_hash, new_private, GUINT_TO_POINTER (1));
434
435   new_event = (GdkEvent *) new_private;
436
437   new_event->any.type = type;
438
439   /*
440    * Bytewise 0 initialization is reasonable for most of the 
441    * current event types. Explicitely initialize double fields
442    * since I trust bytewise 0 == 0. less than for integers
443    * or pointers.
444    */
445   switch (type)
446     {
447     case GDK_MOTION_NOTIFY:
448       new_event->motion.x = 0.;
449       new_event->motion.y = 0.;
450       new_event->motion.x_root = 0.;
451       new_event->motion.y_root = 0.;
452       break;
453     case GDK_BUTTON_PRESS:
454     case GDK_2BUTTON_PRESS:
455     case GDK_3BUTTON_PRESS:
456     case GDK_BUTTON_RELEASE:
457       new_event->button.x = 0.;
458       new_event->button.y = 0.;
459       new_event->button.x_root = 0.;
460       new_event->button.y_root = 0.;
461       break;
462     case GDK_SCROLL:
463       new_event->scroll.x = 0.;
464       new_event->scroll.y = 0.;
465       new_event->scroll.x_root = 0.;
466       new_event->scroll.y_root = 0.;
467       break;
468     case GDK_ENTER_NOTIFY:
469     case GDK_LEAVE_NOTIFY:
470       new_event->crossing.x = 0.;
471       new_event->crossing.y = 0.;
472       new_event->crossing.x_root = 0.;
473       new_event->crossing.y_root = 0.;
474       break;
475     default:
476       break;
477     }
478   
479   return new_event;
480 }
481
482 static gboolean
483 gdk_event_is_allocated (const GdkEvent *event)
484 {
485   if (event_hash)
486     return g_hash_table_lookup (event_hash, event) != NULL;
487
488   return FALSE;
489 }
490  
491 /**
492  * gdk_event_copy:
493  * @event: a #GdkEvent
494  * 
495  * Copies a #GdkEvent, copying or incrementing the reference count of the
496  * resources associated with it (e.g. #GdkWindow's and strings).
497  * 
498  * Return value: a copy of @event. The returned #GdkEvent should be freed with
499  * gdk_event_free().
500  **/
501 GdkEvent*
502 gdk_event_copy (const GdkEvent *event)
503 {
504   GdkEventPrivate *new_private;
505   GdkEvent *new_event;
506
507   g_return_val_if_fail (event != NULL, NULL);
508
509   new_event = gdk_event_new (GDK_NOTHING);
510   new_private = (GdkEventPrivate *)new_event;
511
512   *new_event = *event;
513   if (new_event->any.window)
514     g_object_ref (new_event->any.window);
515
516   if (gdk_event_is_allocated (event))
517     {
518       GdkEventPrivate *private = (GdkEventPrivate *)event;
519
520       new_private->screen = private->screen;
521       new_private->device = private->device;
522       new_private->source_device = private->source_device;
523     }
524
525   switch (event->any.type)
526     {
527     case GDK_KEY_PRESS:
528     case GDK_KEY_RELEASE:
529       new_event->key.string = g_strdup (event->key.string);
530       break;
531
532     case GDK_ENTER_NOTIFY:
533     case GDK_LEAVE_NOTIFY:
534       if (event->crossing.subwindow != NULL)
535         g_object_ref (event->crossing.subwindow);
536       break;
537
538     case GDK_DRAG_ENTER:
539     case GDK_DRAG_LEAVE:
540     case GDK_DRAG_MOTION:
541     case GDK_DRAG_STATUS:
542     case GDK_DROP_START:
543     case GDK_DROP_FINISHED:
544       g_object_ref (event->dnd.context);
545       break;
546
547     case GDK_EXPOSE:
548     case GDK_DAMAGE:
549       if (event->expose.region)
550         new_event->expose.region = cairo_region_copy (event->expose.region);
551       break;
552
553     case GDK_SETTING:
554       new_event->setting.name = g_strdup (new_event->setting.name);
555       break;
556
557     case GDK_BUTTON_PRESS:
558     case GDK_2BUTTON_PRESS:
559     case GDK_3BUTTON_PRESS:
560     case GDK_BUTTON_RELEASE:
561       if (event->button.axes)
562         new_event->button.axes = g_memdup (event->button.axes,
563                                            sizeof (gdouble) * gdk_device_get_n_axes (event->button.device));
564       break;
565
566     case GDK_MOTION_NOTIFY:
567       if (event->motion.axes)
568         new_event->motion.axes = g_memdup (event->motion.axes,
569                                            sizeof (gdouble) * gdk_device_get_n_axes (event->motion.device));
570       break;
571
572     case GDK_OWNER_CHANGE:
573       new_event->owner_change.owner = event->owner_change.owner;
574       if (new_event->owner_change.owner)
575         g_object_ref (new_event->owner_change.owner);
576       break;
577
578     case GDK_SELECTION_CLEAR:
579     case GDK_SELECTION_NOTIFY:
580     case GDK_SELECTION_REQUEST:
581       new_event->selection.requestor = event->selection.requestor;
582       if (new_event->selection.requestor)
583         g_object_unref (new_event->selection.requestor);
584       break;
585
586     default:
587       break;
588     }
589
590   if (gdk_event_is_allocated (event))
591     _gdk_display_event_data_copy (gdk_display_get_default (), event, new_event);
592
593   return new_event;
594 }
595
596 /**
597  * gdk_event_free:
598  * @event:  a #GdkEvent.
599  * 
600  * Frees a #GdkEvent, freeing or decrementing any resources associated with it.
601  * Note that this function should only be called with events returned from
602  * functions such as gdk_event_peek(), gdk_event_get(), gdk_event_copy()
603  * and gdk_event_new().
604  **/
605 void
606 gdk_event_free (GdkEvent *event)
607 {
608   g_return_if_fail (event != NULL);
609
610   if (event->any.window)
611     g_object_unref (event->any.window);
612   
613   switch (event->any.type)
614     {
615     case GDK_KEY_PRESS:
616     case GDK_KEY_RELEASE:
617       g_free (event->key.string);
618       break;
619       
620     case GDK_ENTER_NOTIFY:
621     case GDK_LEAVE_NOTIFY:
622       if (event->crossing.subwindow != NULL)
623         g_object_unref (event->crossing.subwindow);
624       break;
625       
626     case GDK_DRAG_ENTER:
627     case GDK_DRAG_LEAVE:
628     case GDK_DRAG_MOTION:
629     case GDK_DRAG_STATUS:
630     case GDK_DROP_START:
631     case GDK_DROP_FINISHED:
632       if (event->dnd.context != NULL)
633         g_object_unref (event->dnd.context);
634       break;
635
636     case GDK_BUTTON_PRESS:
637     case GDK_2BUTTON_PRESS:
638     case GDK_3BUTTON_PRESS:
639     case GDK_BUTTON_RELEASE:
640       g_free (event->button.axes);
641       break;
642       
643     case GDK_EXPOSE:
644     case GDK_DAMAGE:
645       if (event->expose.region)
646         cairo_region_destroy (event->expose.region);
647       break;
648       
649     case GDK_MOTION_NOTIFY:
650       g_free (event->motion.axes);
651       break;
652       
653     case GDK_SETTING:
654       g_free (event->setting.name);
655       break;
656       
657     case GDK_OWNER_CHANGE:
658       if (event->owner_change.owner)
659         g_object_unref (event->owner_change.owner);
660       break;
661
662     case GDK_SELECTION_CLEAR:
663     case GDK_SELECTION_NOTIFY:
664     case GDK_SELECTION_REQUEST:
665       if (event->selection.requestor)
666         g_object_unref (event->selection.requestor);
667       break;
668
669     default:
670       break;
671     }
672
673   _gdk_display_event_data_free (gdk_display_get_default (), event);
674
675   g_hash_table_remove (event_hash, event);
676   g_slice_free (GdkEventPrivate, (GdkEventPrivate*) event);
677 }
678
679 /**
680  * gdk_event_get_time:
681  * @event: a #GdkEvent
682  * 
683  * Returns the time stamp from @event, if there is one; otherwise
684  * returns #GDK_CURRENT_TIME. If @event is %NULL, returns #GDK_CURRENT_TIME.
685  * 
686  * Return value: time stamp field from @event
687  **/
688 guint32
689 gdk_event_get_time (const GdkEvent *event)
690 {
691   if (event)
692     switch (event->type)
693       {
694       case GDK_MOTION_NOTIFY:
695         return event->motion.time;
696       case GDK_BUTTON_PRESS:
697       case GDK_2BUTTON_PRESS:
698       case GDK_3BUTTON_PRESS:
699       case GDK_BUTTON_RELEASE:
700         return event->button.time;
701       case GDK_SCROLL:
702         return event->scroll.time;
703       case GDK_KEY_PRESS:
704       case GDK_KEY_RELEASE:
705         return event->key.time;
706       case GDK_ENTER_NOTIFY:
707       case GDK_LEAVE_NOTIFY:
708         return event->crossing.time;
709       case GDK_PROPERTY_NOTIFY:
710         return event->property.time;
711       case GDK_SELECTION_CLEAR:
712       case GDK_SELECTION_REQUEST:
713       case GDK_SELECTION_NOTIFY:
714         return event->selection.time;
715       case GDK_PROXIMITY_IN:
716       case GDK_PROXIMITY_OUT:
717         return event->proximity.time;
718       case GDK_DRAG_ENTER:
719       case GDK_DRAG_LEAVE:
720       case GDK_DRAG_MOTION:
721       case GDK_DRAG_STATUS:
722       case GDK_DROP_START:
723       case GDK_DROP_FINISHED:
724         return event->dnd.time;
725       case GDK_CLIENT_EVENT:
726       case GDK_VISIBILITY_NOTIFY:
727       case GDK_CONFIGURE:
728       case GDK_FOCUS_CHANGE:
729       case GDK_NOTHING:
730       case GDK_DAMAGE:
731       case GDK_DELETE:
732       case GDK_DESTROY:
733       case GDK_EXPOSE:
734       case GDK_MAP:
735       case GDK_UNMAP:
736       case GDK_WINDOW_STATE:
737       case GDK_SETTING:
738       case GDK_OWNER_CHANGE:
739       case GDK_GRAB_BROKEN:
740       case GDK_EVENT_LAST:
741         /* return current time */
742         break;
743       }
744   
745   return GDK_CURRENT_TIME;
746 }
747
748 /**
749  * gdk_event_get_state:
750  * @event: a #GdkEvent or NULL
751  * @state: (out): return location for state
752  * 
753  * If the event contains a "state" field, puts that field in @state. Otherwise
754  * stores an empty state (0). Returns %TRUE if there was a state field
755  * in the event. @event may be %NULL, in which case it's treated
756  * as if the event had no state field.
757  * 
758  * Return value: %TRUE if there was a state field in the event 
759  **/
760 gboolean
761 gdk_event_get_state (const GdkEvent        *event,
762                      GdkModifierType       *state)
763 {
764   g_return_val_if_fail (state != NULL, FALSE);
765   
766   if (event)
767     switch (event->type)
768       {
769       case GDK_MOTION_NOTIFY:
770         *state = event->motion.state;
771         return TRUE;
772       case GDK_BUTTON_PRESS:
773       case GDK_2BUTTON_PRESS:
774       case GDK_3BUTTON_PRESS:
775       case GDK_BUTTON_RELEASE:
776         *state =  event->button.state;
777         return TRUE;
778       case GDK_SCROLL:
779         *state =  event->scroll.state;
780         return TRUE;
781       case GDK_KEY_PRESS:
782       case GDK_KEY_RELEASE:
783         *state =  event->key.state;
784         return TRUE;
785       case GDK_ENTER_NOTIFY:
786       case GDK_LEAVE_NOTIFY:
787         *state =  event->crossing.state;
788         return TRUE;
789       case GDK_PROPERTY_NOTIFY:
790       case GDK_VISIBILITY_NOTIFY:
791       case GDK_CLIENT_EVENT:
792       case GDK_CONFIGURE:
793       case GDK_FOCUS_CHANGE:
794       case GDK_SELECTION_CLEAR:
795       case GDK_SELECTION_REQUEST:
796       case GDK_SELECTION_NOTIFY:
797       case GDK_PROXIMITY_IN:
798       case GDK_PROXIMITY_OUT:
799       case GDK_DAMAGE:
800       case GDK_DRAG_ENTER:
801       case GDK_DRAG_LEAVE:
802       case GDK_DRAG_MOTION:
803       case GDK_DRAG_STATUS:
804       case GDK_DROP_START:
805       case GDK_DROP_FINISHED:
806       case GDK_NOTHING:
807       case GDK_DELETE:
808       case GDK_DESTROY:
809       case GDK_EXPOSE:
810       case GDK_MAP:
811       case GDK_UNMAP:
812       case GDK_WINDOW_STATE:
813       case GDK_SETTING:
814       case GDK_OWNER_CHANGE:
815       case GDK_GRAB_BROKEN:
816       case GDK_EVENT_LAST:
817         /* no state field */
818         break;
819       }
820
821   *state = 0;
822   return FALSE;
823 }
824
825 /**
826  * gdk_event_get_coords:
827  * @event: a #GdkEvent
828  * @x_win: (out): location to put event window x coordinate
829  * @y_win: (out): location to put event window y coordinate
830  * 
831  * Extract the event window relative x/y coordinates from an event.
832  * 
833  * Return value: %TRUE if the event delivered event window coordinates
834  **/
835 gboolean
836 gdk_event_get_coords (const GdkEvent *event,
837                       gdouble        *x_win,
838                       gdouble        *y_win)
839 {
840   gdouble x = 0, y = 0;
841   gboolean fetched = TRUE;
842   
843   g_return_val_if_fail (event != NULL, FALSE);
844
845   switch (event->type)
846     {
847     case GDK_CONFIGURE:
848       x = event->configure.x;
849       y = event->configure.y;
850       break;
851     case GDK_ENTER_NOTIFY:
852     case GDK_LEAVE_NOTIFY:
853       x = event->crossing.x;
854       y = event->crossing.y;
855       break;
856     case GDK_SCROLL:
857       x = event->scroll.x;
858       y = event->scroll.y;
859       break;
860     case GDK_BUTTON_PRESS:
861     case GDK_2BUTTON_PRESS:
862     case GDK_3BUTTON_PRESS:
863     case GDK_BUTTON_RELEASE:
864       x = event->button.x;
865       y = event->button.y;
866       break;
867     case GDK_MOTION_NOTIFY:
868       x = event->motion.x;
869       y = event->motion.y;
870       break;
871     default:
872       fetched = FALSE;
873       break;
874     }
875
876   if (x_win)
877     *x_win = x;
878   if (y_win)
879     *y_win = y;
880
881   return fetched;
882 }
883
884 /**
885  * gdk_event_get_root_coords:
886  * @event: a #GdkEvent
887  * @x_root: (out): location to put root window x coordinate
888  * @y_root: (out): location to put root window y coordinate
889  * 
890  * Extract the root window relative x/y coordinates from an event.
891  * 
892  * Return value: %TRUE if the event delivered root window coordinates
893  **/
894 gboolean
895 gdk_event_get_root_coords (const GdkEvent *event,
896                            gdouble        *x_root,
897                            gdouble        *y_root)
898 {
899   gdouble x = 0, y = 0;
900   gboolean fetched = TRUE;
901   
902   g_return_val_if_fail (event != NULL, FALSE);
903
904   switch (event->type)
905     {
906     case GDK_MOTION_NOTIFY:
907       x = event->motion.x_root;
908       y = event->motion.y_root;
909       break;
910     case GDK_SCROLL:
911       x = event->scroll.x_root;
912       y = event->scroll.y_root;
913       break;
914     case GDK_BUTTON_PRESS:
915     case GDK_2BUTTON_PRESS:
916     case GDK_3BUTTON_PRESS:
917     case GDK_BUTTON_RELEASE:
918       x = event->button.x_root;
919       y = event->button.y_root;
920       break;
921     case GDK_ENTER_NOTIFY:
922     case GDK_LEAVE_NOTIFY:
923       x = event->crossing.x_root;
924       y = event->crossing.y_root;
925       break;
926     case GDK_DRAG_ENTER:
927     case GDK_DRAG_LEAVE:
928     case GDK_DRAG_MOTION:
929     case GDK_DRAG_STATUS:
930     case GDK_DROP_START:
931     case GDK_DROP_FINISHED:
932       x = event->dnd.x_root;
933       y = event->dnd.y_root;
934       break;
935     default:
936       fetched = FALSE;
937       break;
938     }
939
940   if (x_root)
941     *x_root = x;
942   if (y_root)
943     *y_root = y;
944
945   return fetched;
946 }
947
948 /**
949  * gdk_event_get_axis:
950  * @event: a #GdkEvent
951  * @axis_use: the axis use to look for
952  * @value: (out): location to store the value found
953  * 
954  * Extract the axis value for a particular axis use from
955  * an event structure.
956  * 
957  * Return value: %TRUE if the specified axis was found, otherwise %FALSE
958  **/
959 gboolean
960 gdk_event_get_axis (const GdkEvent *event,
961                     GdkAxisUse      axis_use,
962                     gdouble        *value)
963 {
964   gdouble *axes;
965   GdkDevice *device;
966   
967   g_return_val_if_fail (event != NULL, FALSE);
968   
969   if (axis_use == GDK_AXIS_X || axis_use == GDK_AXIS_Y)
970     {
971       gdouble x, y;
972       
973       switch (event->type)
974         {
975         case GDK_MOTION_NOTIFY:
976           x = event->motion.x;
977           y = event->motion.y;
978           break;
979         case GDK_SCROLL:
980           x = event->scroll.x;
981           y = event->scroll.y;
982           break;
983         case GDK_BUTTON_PRESS:
984         case GDK_BUTTON_RELEASE:
985           x = event->button.x;
986           y = event->button.y;
987           break;
988         case GDK_ENTER_NOTIFY:
989         case GDK_LEAVE_NOTIFY:
990           x = event->crossing.x;
991           y = event->crossing.y;
992           break;
993           
994         default:
995           return FALSE;
996         }
997
998       if (axis_use == GDK_AXIS_X && value)
999         *value = x;
1000       if (axis_use == GDK_AXIS_Y && value)
1001         *value = y;
1002
1003       return TRUE;
1004     }
1005   else if (event->type == GDK_BUTTON_PRESS ||
1006            event->type == GDK_BUTTON_RELEASE)
1007     {
1008       device = event->button.device;
1009       axes = event->button.axes;
1010     }
1011   else if (event->type == GDK_MOTION_NOTIFY)
1012     {
1013       device = event->motion.device;
1014       axes = event->motion.axes;
1015     }
1016   else
1017     return FALSE;
1018
1019   return gdk_device_get_axis (device, axes, axis_use, value);
1020 }
1021
1022 /**
1023  * gdk_event_set_device:
1024  * @event: a #GdkEvent
1025  * @device: a #GdkDevice
1026  *
1027  * Sets the device for @event to @device. The event must
1028  * have been allocated by GTK+, for instance, by
1029  * gdk_event_copy().
1030  *
1031  * Since: 3.0
1032  **/
1033 void
1034 gdk_event_set_device (GdkEvent  *event,
1035                       GdkDevice *device)
1036 {
1037   GdkEventPrivate *private;
1038
1039   g_return_if_fail (gdk_event_is_allocated (event));
1040
1041   private = (GdkEventPrivate *) event;
1042
1043   private->device = device;
1044
1045   switch (event->type)
1046     {
1047     case GDK_MOTION_NOTIFY:
1048       event->motion.device = device;
1049       break;
1050     case GDK_BUTTON_PRESS:
1051     case GDK_2BUTTON_PRESS:
1052     case GDK_3BUTTON_PRESS:
1053     case GDK_BUTTON_RELEASE:
1054       event->button.device = device;
1055       break;
1056     case GDK_SCROLL:
1057       event->scroll.device = device;
1058       break;
1059     case GDK_PROXIMITY_IN:
1060     case GDK_PROXIMITY_OUT:
1061       event->proximity.device = device;
1062       break;
1063     default:
1064       break;
1065     }
1066 }
1067
1068 /**
1069  * gdk_event_get_device:
1070  * @event: a #GdkEvent.
1071  *
1072  * If the event contains a "device" field, this function will return
1073  * it, else it will return %NULL.
1074  *
1075  * Returns: (transfer none): a #GdkDevice, or %NULL.
1076  *
1077  * Since: 3.0
1078  **/
1079 GdkDevice *
1080 gdk_event_get_device (const GdkEvent *event)
1081 {
1082   g_return_val_if_fail (event != NULL, NULL);
1083
1084   if (gdk_event_is_allocated (event))
1085     {
1086       GdkEventPrivate *private = (GdkEventPrivate *) event;
1087
1088       if (private->device)
1089         return private->device;
1090     }
1091
1092   switch (event->type)
1093     {
1094     case GDK_MOTION_NOTIFY:
1095       return event->motion.device;
1096     case GDK_BUTTON_PRESS:
1097     case GDK_2BUTTON_PRESS:
1098     case GDK_3BUTTON_PRESS:
1099     case GDK_BUTTON_RELEASE:
1100       return event->button.device;
1101     case GDK_SCROLL:
1102       return event->scroll.device;
1103     case GDK_PROXIMITY_IN:
1104     case GDK_PROXIMITY_OUT:
1105       return event->proximity.device;
1106     default:
1107       break;
1108     }
1109
1110   /* Fallback if event has no device set */
1111   switch (event->type)
1112     {
1113     case GDK_MOTION_NOTIFY:
1114     case GDK_BUTTON_PRESS:
1115     case GDK_2BUTTON_PRESS:
1116     case GDK_3BUTTON_PRESS:
1117     case GDK_BUTTON_RELEASE:
1118     case GDK_ENTER_NOTIFY:
1119     case GDK_LEAVE_NOTIFY:
1120     case GDK_FOCUS_CHANGE:
1121     case GDK_PROXIMITY_IN:
1122     case GDK_PROXIMITY_OUT:
1123     case GDK_DRAG_ENTER:
1124     case GDK_DRAG_LEAVE:
1125     case GDK_DRAG_MOTION:
1126     case GDK_DRAG_STATUS:
1127     case GDK_DROP_START:
1128     case GDK_DROP_FINISHED:
1129     case GDK_SCROLL:
1130     case GDK_GRAB_BROKEN:
1131     case GDK_KEY_PRESS:
1132     case GDK_KEY_RELEASE:
1133       {
1134         GdkDisplay *display;
1135         GdkDeviceManager *device_manager;
1136         GdkDevice *client_pointer;
1137
1138         g_warning ("Event with type %d not holding a GdkDevice. "
1139                    "It is most likely synthesized outside Gdk/GTK+\n",
1140                    event->type);
1141
1142         display = gdk_window_get_display (event->any.window);
1143         device_manager = gdk_display_get_device_manager (display);
1144         client_pointer = gdk_device_manager_get_client_pointer (device_manager);
1145
1146         if (event->type == GDK_KEY_PRESS ||
1147             event->type == GDK_KEY_RELEASE)
1148           return gdk_device_get_associated_device (client_pointer);
1149         else
1150           return client_pointer;
1151       }
1152       break;
1153     default:
1154       return NULL;
1155     }
1156 }
1157
1158 /**
1159  * gdk_event_set_source_device:
1160  * @event: a #GdkEvent
1161  * @device: a #GdkDevice
1162  *
1163  * Sets the slave device for @event to @device.
1164  *
1165  * The event must have been allocated by GTK+,
1166  * for instance by gdk_event_copy().
1167  *
1168  * Since: 3.0
1169  **/
1170 void
1171 gdk_event_set_source_device (GdkEvent  *event,
1172                              GdkDevice *device)
1173 {
1174   GdkEventPrivate *private;
1175
1176   g_return_if_fail (gdk_event_is_allocated (event));
1177   g_return_if_fail (GDK_IS_DEVICE (device));
1178
1179   private = (GdkEventPrivate *) event;
1180
1181   private->source_device = device;
1182 }
1183
1184 /**
1185  * gdk_event_get_source_device:
1186  * @event: a #GdkEvent
1187  *
1188  * This function returns the hardware (slave) #GdkDevice that has
1189  * triggered the event, falling back to the virtual (master) device
1190  * (as in gdk_event_get_device()) if the event wasn't caused by
1191  * interaction with a hardware device. This may happen for example
1192  * in synthesized crossing events after a #GdkWindow updates its
1193  * geometry or a grab is acquired/released.
1194  *
1195  * If the event does not contain a device field, this function will
1196  * return %NULL.
1197  *
1198  * Returns: (transfer none): a #GdkDevice, or %NULL.
1199  *
1200  * Since: 3.0
1201  **/
1202 GdkDevice *
1203 gdk_event_get_source_device (const GdkEvent *event)
1204 {
1205   GdkEventPrivate *private;
1206
1207   g_return_val_if_fail (event != NULL, NULL);
1208
1209   if (!gdk_event_is_allocated (event))
1210     return NULL;
1211
1212   private = (GdkEventPrivate *) event;
1213
1214   if (private->source_device)
1215     return private->source_device;
1216
1217   /* Fallback to event device */
1218   return gdk_event_get_device (event);
1219 }
1220
1221 /**
1222  * gdk_event_request_motions:
1223  * @event: a valid #GdkEvent
1224  *
1225  * Request more motion notifies if @event is a motion notify hint event.
1226  *
1227  * This function should be used instead of gdk_window_get_pointer() to
1228  * request further motion notifies, because it also works for extension
1229  * events where motion notifies are provided for devices other than the
1230  * core pointer. Coordinate extraction, processing and requesting more
1231  * motion events from a %GDK_MOTION_NOTIFY event usually works like this:
1232  *
1233  * |[
1234  * {
1235  *   /&ast; motion_event handler &ast;/
1236  *   x = motion_event->x;
1237  *   y = motion_event->y;
1238  *   /&ast; handle (x,y) motion &ast;/
1239  *   gdk_event_request_motions (motion_event); /&ast; handles is_hint events &ast;/
1240  * }
1241  * ]|
1242  *
1243  * Since: 2.12
1244  **/
1245 void
1246 gdk_event_request_motions (const GdkEventMotion *event)
1247 {
1248   GdkDisplay *display;
1249   
1250   g_return_if_fail (event != NULL);
1251   
1252   if (event->type == GDK_MOTION_NOTIFY && event->is_hint)
1253     {
1254       gdk_device_get_state (event->device, event->window, NULL, NULL);
1255       
1256       display = gdk_window_get_display (event->window);
1257       _gdk_display_enable_motion_hints (display, event->device);
1258     }
1259 }
1260
1261 static gboolean
1262 gdk_events_get_axis_distances (GdkEvent *event1,
1263                                GdkEvent *event2,
1264                                gdouble  *x_distance,
1265                                gdouble  *y_distance,
1266                                gdouble  *distance)
1267 {
1268   gdouble x1, x2, y1, y2;
1269   gdouble xd, yd;
1270
1271   if (!gdk_event_get_coords (event1, &x1, &y1) ||
1272       !gdk_event_get_coords (event2, &x2, &y2))
1273     return FALSE;
1274
1275   xd = x2 - x1;
1276   yd = y2 - y1;
1277
1278   if (x_distance)
1279     *x_distance = xd;
1280
1281   if (y_distance)
1282     *y_distance = yd;
1283
1284   if (distance)
1285     *distance = sqrt ((xd * xd) + (yd * yd));
1286
1287   return TRUE;
1288 }
1289
1290 /**
1291  * gdk_events_get_distance:
1292  * @event1: first #GdkEvent
1293  * @event2: second #GdkEvent
1294  * @distance: (out): return location for the distance
1295  *
1296  * If both events have X/Y information, the distance between both coordinates
1297  * (as in a straight line going from @event1 to @event2) will be returned.
1298  *
1299  * Returns: %TRUE if the distance could be calculated.
1300  *
1301  * Since: 3.0
1302  **/
1303 gboolean
1304 gdk_events_get_distance (GdkEvent *event1,
1305                          GdkEvent *event2,
1306                          gdouble  *distance)
1307 {
1308   return gdk_events_get_axis_distances (event1, event2,
1309                                         NULL, NULL,
1310                                         distance);
1311 }
1312
1313 /**
1314  * gdk_events_get_angle:
1315  * @event1: first #GdkEvent
1316  * @event2: second #GdkEvent
1317  * @angle: (out): return location for the relative angle between both events
1318  *
1319  * If both events contain X/Y information, this function will return %TRUE
1320  * and return in @angle the relative angle from @event1 to @event2. The rotation
1321  * direction for positive angles is from the positive X axis towards the positive
1322  * Y axis.
1323  *
1324  * Returns: %TRUE if the angle could be calculated.
1325  *
1326  * Since: 3.0
1327  **/
1328 gboolean
1329 gdk_events_get_angle (GdkEvent *event1,
1330                       GdkEvent *event2,
1331                       gdouble  *angle)
1332 {
1333   gdouble x_distance, y_distance, distance;
1334
1335   if (!gdk_events_get_axis_distances (event1, event2,
1336                                       &x_distance, &y_distance,
1337                                       &distance))
1338     return FALSE;
1339
1340   if (angle)
1341     {
1342       *angle = atan2 (x_distance, y_distance);
1343
1344       /* Invert angle */
1345       *angle = (2 * G_PI) - *angle;
1346
1347       /* Shift it 90° */
1348       *angle += G_PI / 2;
1349
1350       /* And constraint it to 0°-360° */
1351       *angle = fmod (*angle, 2 * G_PI);
1352     }
1353
1354   return TRUE;
1355 }
1356
1357 /**
1358  * gdk_events_get_center:
1359  * @event1: first #GdkEvent
1360  * @event2: second #GdkEvent
1361  * @x: (out): return location for the X coordinate of the center
1362  * @y: (out): return location for the Y coordinate of the center
1363  *
1364  * If both events contain X/Y information, the center of both coordinates
1365  * will be returned in @x and @y.
1366  *
1367  * Returns: %TRUE if the center could be calculated.
1368  *
1369  * Since: 3.0
1370  **/
1371 gboolean
1372 gdk_events_get_center (GdkEvent *event1,
1373                        GdkEvent *event2,
1374                        gdouble  *x,
1375                        gdouble  *y)
1376 {
1377   gdouble x1, x2, y1, y2;
1378
1379   if (!gdk_event_get_coords (event1, &x1, &y1) ||
1380       !gdk_event_get_coords (event2, &x2, &y2))
1381     return FALSE;
1382
1383   if (x)
1384     *x = (x2 + x1) / 2;
1385
1386   if (y)
1387     *y = (y2 + y1) / 2;
1388
1389   return TRUE;
1390 }
1391
1392 /**
1393  * gdk_event_set_screen:
1394  * @event: a #GdkEvent
1395  * @screen: a #GdkScreen
1396  * 
1397  * Sets the screen for @event to @screen. The event must
1398  * have been allocated by GTK+, for instance, by
1399  * gdk_event_copy().
1400  *
1401  * Since: 2.2
1402  **/
1403 void
1404 gdk_event_set_screen (GdkEvent  *event,
1405                       GdkScreen *screen)
1406 {
1407   GdkEventPrivate *private;
1408   
1409   g_return_if_fail (gdk_event_is_allocated (event));
1410
1411   private = (GdkEventPrivate *)event;
1412   
1413   private->screen = screen;
1414 }
1415
1416 /**
1417  * gdk_event_get_screen:
1418  * @event: a #GdkEvent
1419  * 
1420  * Returns the screen for the event. The screen is
1421  * typically the screen for <literal>event->any.window</literal>, but
1422  * for events such as mouse events, it is the screen
1423  * where the pointer was when the event occurs -
1424  * that is, the screen which has the root window 
1425  * to which <literal>event->motion.x_root</literal> and
1426  * <literal>event->motion.y_root</literal> are relative.
1427  * 
1428  * Return value: (transfer none): the screen for the event
1429  *
1430  * Since: 2.2
1431  **/
1432 GdkScreen *
1433 gdk_event_get_screen (const GdkEvent *event)
1434 {
1435   if (gdk_event_is_allocated (event))
1436     {
1437       GdkEventPrivate *private = (GdkEventPrivate *)event;
1438
1439       if (private->screen)
1440         return private->screen;
1441     }
1442
1443   if (event->any.window)
1444     return gdk_window_get_screen (event->any.window);
1445
1446   return NULL;
1447 }
1448
1449 /**
1450  * gdk_set_show_events:
1451  * @show_events:  %TRUE to output event debugging information.
1452  * 
1453  * Sets whether a trace of received events is output.
1454  * Note that GTK+ must be compiled with debugging (that is,
1455  * configured using the <option>--enable-debug</option> option)
1456  * to use this option.
1457  **/
1458 void
1459 gdk_set_show_events (gboolean show_events)
1460 {
1461   if (show_events)
1462     _gdk_debug_flags |= GDK_DEBUG_EVENTS;
1463   else
1464     _gdk_debug_flags &= ~GDK_DEBUG_EVENTS;
1465 }
1466
1467 /**
1468  * gdk_get_show_events:
1469  * 
1470  * Gets whether event debugging output is enabled.
1471  * 
1472  * Return value: %TRUE if event debugging output is enabled.
1473  **/
1474 gboolean
1475 gdk_get_show_events (void)
1476 {
1477   return (_gdk_debug_flags & GDK_DEBUG_EVENTS) != 0;
1478 }
1479
1480 /* What do we do with G_IO_NVAL?
1481  */
1482 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
1483 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
1484 #define EXCEPTION_CONDITION (G_IO_PRI)
1485
1486 static void
1487 gdk_synthesize_click (GdkDisplay *display,
1488                       GdkEvent   *event,
1489                       gint        nclicks)
1490 {
1491   GdkEvent *event_copy;
1492
1493   event_copy = gdk_event_copy (event);
1494   event_copy->type = (nclicks == 2) ? GDK_2BUTTON_PRESS : GDK_3BUTTON_PRESS;
1495
1496   _gdk_event_queue_append (display, event_copy);
1497 }
1498
1499 void
1500 _gdk_event_button_generate (GdkDisplay *display,
1501                             GdkEvent   *event)
1502 {
1503   GdkMultipleClickInfo *info;
1504
1505   g_return_if_fail (event->type == GDK_BUTTON_PRESS);
1506
1507   info = g_hash_table_lookup (display->multiple_click_info, event->button.device);
1508
1509   if (G_UNLIKELY (!info))
1510     {
1511       info = g_new0 (GdkMultipleClickInfo, 1);
1512       info->button_number[0] = info->button_number[1] = -1;
1513
1514       g_hash_table_insert (display->multiple_click_info,
1515                            event->button.device, info);
1516     }
1517
1518   if ((event->button.time < (info->button_click_time[1] + 2 * display->double_click_time)) &&
1519       (event->button.window == info->button_window[1]) &&
1520       (event->button.button == info->button_number[1]) &&
1521       (ABS (event->button.x - info->button_x[1]) <= display->double_click_distance) &&
1522       (ABS (event->button.y - info->button_y[1]) <= display->double_click_distance))
1523     {
1524       gdk_synthesize_click (display, event, 3);
1525
1526       info->button_click_time[1] = 0;
1527       info->button_click_time[0] = 0;
1528       info->button_window[1] = NULL;
1529       info->button_window[0] = NULL;
1530       info->button_number[1] = -1;
1531       info->button_number[0] = -1;
1532       info->button_x[0] = info->button_x[1] = 0;
1533       info->button_y[0] = info->button_y[1] = 0;
1534     }
1535   else if ((event->button.time < (info->button_click_time[0] + display->double_click_time)) &&
1536            (event->button.window == info->button_window[0]) &&
1537            (event->button.button == info->button_number[0]) &&
1538            (ABS (event->button.x - info->button_x[0]) <= display->double_click_distance) &&
1539            (ABS (event->button.y - info->button_y[0]) <= display->double_click_distance))
1540     {
1541       gdk_synthesize_click (display, event, 2);
1542       
1543       info->button_click_time[1] = info->button_click_time[0];
1544       info->button_click_time[0] = event->button.time;
1545       info->button_window[1] = info->button_window[0];
1546       info->button_window[0] = event->button.window;
1547       info->button_number[1] = info->button_number[0];
1548       info->button_number[0] = event->button.button;
1549       info->button_x[1] = info->button_x[0];
1550       info->button_x[0] = event->button.x;
1551       info->button_y[1] = info->button_y[0];
1552       info->button_y[0] = event->button.y;
1553     }
1554   else
1555     {
1556       info->button_click_time[1] = 0;
1557       info->button_click_time[0] = event->button.time;
1558       info->button_window[1] = NULL;
1559       info->button_window[0] = event->button.window;
1560       info->button_number[1] = -1;
1561       info->button_number[0] = event->button.button;
1562       info->button_x[1] = 0;
1563       info->button_x[0] = event->button.x;
1564       info->button_y[1] = 0;
1565       info->button_y[0] = event->button.y;
1566     }
1567 }
1568
1569 void
1570 gdk_synthesize_window_state (GdkWindow     *window,
1571                              GdkWindowState unset_flags,
1572                              GdkWindowState set_flags)
1573 {
1574   GdkEvent temp_event;
1575   GdkWindowState old;
1576   
1577   g_return_if_fail (window != NULL);
1578   
1579   temp_event.window_state.window = window;
1580   temp_event.window_state.type = GDK_WINDOW_STATE;
1581   temp_event.window_state.send_event = FALSE;
1582   
1583   old = temp_event.window_state.window->state;
1584   
1585   temp_event.window_state.new_window_state = old;
1586   temp_event.window_state.new_window_state |= set_flags;
1587   temp_event.window_state.new_window_state &= ~unset_flags;
1588   temp_event.window_state.changed_mask = temp_event.window_state.new_window_state ^ old;
1589
1590   if (temp_event.window_state.new_window_state == old)
1591     return; /* No actual work to do, nothing changed. */
1592
1593   /* Actually update the field in GdkWindow, this is sort of an odd
1594    * place to do it, but seems like the safest since it ensures we expose no
1595    * inconsistent state to the user.
1596    */
1597   
1598   window->state = temp_event.window_state.new_window_state;
1599
1600   if (temp_event.window_state.changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
1601     _gdk_window_update_viewable (window);
1602
1603   /* We only really send the event to toplevels, since
1604    * all the window states don't apply to non-toplevels.
1605    * Non-toplevels do use the GDK_WINDOW_STATE_WITHDRAWN flag
1606    * internally so we needed to update window->state.
1607    */
1608   switch (window->window_type)
1609     {
1610     case GDK_WINDOW_TOPLEVEL:
1611     case GDK_WINDOW_TEMP: /* ? */
1612       gdk_display_put_event (gdk_window_get_display (window), &temp_event);
1613       break;
1614       
1615     case GDK_WINDOW_FOREIGN:
1616     case GDK_WINDOW_ROOT:
1617     case GDK_WINDOW_CHILD:
1618       break;
1619     }
1620 }
1621
1622 /**
1623  * gdk_display_set_double_click_time:
1624  * @display: a #GdkDisplay
1625  * @msec: double click time in milliseconds (thousandths of a second) 
1626  * 
1627  * Sets the double click time (two clicks within this time interval
1628  * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1629  * Applications should <emphasis>not</emphasis> set this, it is a global 
1630  * user-configured setting.
1631  *
1632  * Since: 2.2
1633  **/
1634 void
1635 gdk_display_set_double_click_time (GdkDisplay *display,
1636                                    guint       msec)
1637 {
1638   display->double_click_time = msec;
1639 }
1640
1641 /**
1642  * gdk_set_double_click_time:
1643  * @msec: double click time in milliseconds (thousandths of a second)
1644  *
1645  * Set the double click time for the default display. See
1646  * gdk_display_set_double_click_time(). 
1647  * See also gdk_display_set_double_click_distance().
1648  * Applications should <emphasis>not</emphasis> set this, it is a 
1649  * global user-configured setting.
1650  **/
1651 void
1652 gdk_set_double_click_time (guint msec)
1653 {
1654   gdk_display_set_double_click_time (gdk_display_get_default (), msec);
1655 }
1656
1657 /**
1658  * gdk_display_set_double_click_distance:
1659  * @display: a #GdkDisplay
1660  * @distance: distance in pixels
1661  * 
1662  * Sets the double click distance (two clicks within this distance
1663  * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1664  * See also gdk_display_set_double_click_time().
1665  * Applications should <emphasis>not</emphasis> set this, it is a global 
1666  * user-configured setting.
1667  *
1668  * Since: 2.4
1669  **/
1670 void
1671 gdk_display_set_double_click_distance (GdkDisplay *display,
1672                                        guint       distance)
1673 {
1674   display->double_click_distance = distance;
1675 }
1676
1677 G_DEFINE_BOXED_TYPE (GdkEvent, gdk_event,
1678                      gdk_event_copy,
1679                      gdk_event_free)
1680
1681 /**
1682  * gdk_setting_get:
1683  * @name: the name of the setting.
1684  * @value: location to store the value of the setting.
1685  *
1686  * Obtains a desktop-wide setting, such as the double-click time,
1687  * for the default screen. See gdk_screen_get_setting().
1688  *
1689  * Returns: %TRUE if the setting existed and a value was stored
1690  *   in @value, %FALSE otherwise.
1691  **/
1692 gboolean
1693 gdk_setting_get (const gchar *name,
1694                  GValue      *value)
1695 {
1696   return gdk_screen_get_setting (gdk_screen_get_default (), name, value);
1697 }