1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
29 #include "gdkinternals.h"
37 * @Short_description: Functions for handling events from the window system
39 * @See_also: <link linkend="gdk-Event-Structures">Event Structures</link>
41 * This section describes functions dealing with events from the window
44 * In GTK+ applications the events are handled automatically in
45 * gtk_main_do_event() and passed on to the appropriate widgets, so these
46 * functions are rarely needed. Though some of the fields in the
47 * <link linkend="gdk-Event-Structures">Event Structures</link> are useful.
51 typedef struct _GdkIOClosure GdkIOClosure;
55 GDestroyNotify notify;
59 /* Private variable declarations
62 GdkEventFunc _gdk_event_func = NULL; /* Callback for events */
63 gpointer _gdk_event_data = NULL;
64 GDestroyNotify _gdk_event_notify = NULL;
66 /*********************************************
67 * Functions for maintaining the event queue *
68 *********************************************/
71 * _gdk_event_queue_find_first:
72 * @display: a #GdkDisplay
74 * Find the first event on the queue that is not still
77 * Return value: Pointer to the list node for that event, or NULL.
80 _gdk_event_queue_find_first (GdkDisplay *display)
82 GList *tmp_list = display->queued_events;
86 GdkEventPrivate *event = tmp_list->data;
87 if (!(event->flags & GDK_EVENT_PENDING))
90 tmp_list = g_list_next (tmp_list);
97 * _gdk_event_queue_prepend:
98 * @display: a #GdkDisplay
99 * @event: Event to prepend.
101 * Prepends an event before the head of the event queue.
103 * Returns: the newly prepended list node.
106 _gdk_event_queue_prepend (GdkDisplay *display,
109 display->queued_events = g_list_prepend (display->queued_events, event);
110 if (!display->queued_tail)
111 display->queued_tail = display->queued_events;
112 return display->queued_events;
116 * _gdk_event_queue_append:
117 * @display: a #GdkDisplay
118 * @event: Event to append.
120 * Appends an event onto the tail of the event queue.
122 * Returns: the newly appended list node.
125 _gdk_event_queue_append (GdkDisplay *display,
128 display->queued_tail = g_list_append (display->queued_tail, event);
130 if (!display->queued_events)
131 display->queued_events = display->queued_tail;
133 display->queued_tail = display->queued_tail->next;
135 return display->queued_tail;
139 * _gdk_event_queue_insert_after:
140 * @display: a #GdkDisplay
141 * @sibling: Append after this event.
142 * @event: Event to append.
144 * Appends an event after the specified event, or if it isn't in
145 * the queue, onto the tail of the event queue.
147 * Returns: the newly appended list node.
152 _gdk_event_queue_insert_after (GdkDisplay *display,
156 GList *prev = g_list_find (display->queued_events, sibling);
157 if (prev && prev->next)
159 display->queued_events = g_list_insert_before (display->queued_events, prev->next, event);
163 return _gdk_event_queue_append (display, event);
167 * _gdk_event_queue_insert_after:
168 * @display: a #GdkDisplay
169 * @sibling: Append after this event.
170 * @event: Event to append.
172 * Appends an event before the specified event, or if it isn't in
173 * the queue, onto the tail of the event queue.
175 * Returns: the newly appended list node.
180 _gdk_event_queue_insert_before (GdkDisplay *display,
184 GList *next = g_list_find (display->queued_events, sibling);
187 display->queued_events = g_list_insert_before (display->queued_events, next, event);
191 return _gdk_event_queue_append (display, event);
196 * _gdk_event_queue_remove_link:
197 * @display: a #GdkDisplay
198 * @node: node to remove
200 * Removes a specified list node from the event queue.
203 _gdk_event_queue_remove_link (GdkDisplay *display,
207 node->prev->next = node->next;
209 display->queued_events = node->next;
212 node->next->prev = node->prev;
214 display->queued_tail = node->prev;
218 * _gdk_event_unqueue:
219 * @display: a #GdkDisplay
221 * Removes and returns the first event from the event
222 * queue that is not still being filled in.
224 * Return value: the event, or %NULL. Ownership is transferred
228 _gdk_event_unqueue (GdkDisplay *display)
230 GdkEvent *event = NULL;
233 tmp_list = _gdk_event_queue_find_first (display);
237 event = tmp_list->data;
238 _gdk_event_queue_remove_link (display, tmp_list);
239 g_list_free_1 (tmp_list);
246 * gdk_event_handler_set:
247 * @func: the function to call to handle events from GDK.
248 * @data: user data to pass to the function.
249 * @notify: the function to call when the handler function is removed, i.e. when
250 * gdk_event_handler_set() is called with another event handler.
252 * Sets the function to call to handle all events from GDK.
254 * Note that GTK+ uses this to install its own event handler, so it is
255 * usually not useful for GTK+ applications. (Although an application
256 * can call this function then call gtk_main_do_event() to pass
260 gdk_event_handler_set (GdkEventFunc func,
262 GDestroyNotify notify)
264 if (_gdk_event_notify)
265 (*_gdk_event_notify) (_gdk_event_data);
267 _gdk_event_func = func;
268 _gdk_event_data = data;
269 _gdk_event_notify = notify;
275 * Checks all open displays for a #GdkEvent to process,to be processed
276 * on, fetching events from the windowing system if necessary.
277 * See gdk_display_get_event().
279 * Return value: the next #GdkEvent to be processed, or %NULL if no events
280 * are pending. The returned #GdkEvent should be freed with gdk_event_free().
287 for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
289 GdkEvent *event = gdk_display_get_event (tmp_list->data);
300 * If there is an event waiting in the event queue of some open
301 * display, returns a copy of it. See gdk_display_peek_event().
303 * Return value: a copy of the first #GdkEvent on some event queue, or %NULL if no
304 * events are in any queues. The returned #GdkEvent should be freed with
308 gdk_event_peek (void)
312 for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
314 GdkEvent *event = gdk_display_peek_event (tmp_list->data);
324 * @event: a #GdkEvent.
326 * Appends a copy of the given event onto the front of the event
327 * queue for event->any.window's display, or the default event
328 * queue if event->any.window is %NULL. See gdk_display_put_event().
331 gdk_event_put (const GdkEvent *event)
335 g_return_if_fail (event != NULL);
337 if (event->any.window)
338 display = gdk_window_get_display (event->any.window);
342 g_message ("Falling back to default display for gdk_event_put()"));
343 display = gdk_display_get_default ();
346 gdk_display_put_event (display, event);
349 static GHashTable *event_hash = NULL;
353 * @type: a #GdkEventType
355 * Creates a new event of the given type. All fields are set to 0.
357 * Return value: a newly-allocated #GdkEvent. The returned #GdkEvent
358 * should be freed with gdk_event_free().
363 gdk_event_new (GdkEventType type)
365 GdkEventPrivate *new_private;
369 event_hash = g_hash_table_new (g_direct_hash, NULL);
371 new_private = g_slice_new0 (GdkEventPrivate);
373 new_private->flags = 0;
374 new_private->screen = NULL;
376 g_hash_table_insert (event_hash, new_private, GUINT_TO_POINTER (1));
378 new_event = (GdkEvent *) new_private;
380 new_event->any.type = type;
383 * Bytewise 0 initialization is reasonable for most of the
384 * current event types. Explicitely initialize double fields
385 * since I trust bytewise 0 == 0. less than for integers
390 case GDK_MOTION_NOTIFY:
391 new_event->motion.x = 0.;
392 new_event->motion.y = 0.;
393 new_event->motion.x_root = 0.;
394 new_event->motion.y_root = 0.;
396 case GDK_BUTTON_PRESS:
397 case GDK_2BUTTON_PRESS:
398 case GDK_3BUTTON_PRESS:
399 case GDK_BUTTON_RELEASE:
400 new_event->button.x = 0.;
401 new_event->button.y = 0.;
402 new_event->button.x_root = 0.;
403 new_event->button.y_root = 0.;
406 new_event->scroll.x = 0.;
407 new_event->scroll.y = 0.;
408 new_event->scroll.x_root = 0.;
409 new_event->scroll.y_root = 0.;
411 case GDK_ENTER_NOTIFY:
412 case GDK_LEAVE_NOTIFY:
413 new_event->crossing.x = 0.;
414 new_event->crossing.y = 0.;
415 new_event->crossing.x_root = 0.;
416 new_event->crossing.y_root = 0.;
426 gdk_event_is_allocated (const GdkEvent *event)
429 return g_hash_table_lookup (event_hash, event) != NULL;
436 * @event: a #GdkEvent
438 * Copies a #GdkEvent, copying or incrementing the reference count of the
439 * resources associated with it (e.g. #GdkWindow's and strings).
441 * Return value: a copy of @event. The returned #GdkEvent should be freed with
445 gdk_event_copy (const GdkEvent *event)
447 GdkEventPrivate *new_private;
450 g_return_val_if_fail (event != NULL, NULL);
452 new_event = gdk_event_new (GDK_NOTHING);
453 new_private = (GdkEventPrivate *)new_event;
456 if (new_event->any.window)
457 g_object_ref (new_event->any.window);
459 if (gdk_event_is_allocated (event))
461 GdkEventPrivate *private = (GdkEventPrivate *)event;
463 new_private->screen = private->screen;
464 new_private->device = private->device;
467 switch (event->any.type)
470 case GDK_KEY_RELEASE:
471 new_event->key.string = g_strdup (event->key.string);
474 case GDK_ENTER_NOTIFY:
475 case GDK_LEAVE_NOTIFY:
476 if (event->crossing.subwindow != NULL)
477 g_object_ref (event->crossing.subwindow);
482 case GDK_DRAG_MOTION:
483 case GDK_DRAG_STATUS:
485 case GDK_DROP_FINISHED:
486 g_object_ref (event->dnd.context);
491 if (event->expose.region)
492 new_event->expose.region = cairo_region_copy (event->expose.region);
496 new_event->setting.name = g_strdup (new_event->setting.name);
499 case GDK_BUTTON_PRESS:
500 case GDK_BUTTON_RELEASE:
501 if (event->button.axes)
502 new_event->button.axes = g_memdup (event->button.axes,
503 sizeof (gdouble) * gdk_device_get_n_axes (event->button.device));
506 case GDK_MOTION_NOTIFY:
507 if (event->motion.axes)
508 new_event->motion.axes = g_memdup (event->motion.axes,
509 sizeof (gdouble) * gdk_device_get_n_axes (event->motion.device));
516 if (gdk_event_is_allocated (event))
517 _gdk_windowing_event_data_copy (event, new_event);
524 * @event: a #GdkEvent.
526 * Frees a #GdkEvent, freeing or decrementing any resources associated with it.
527 * Note that this function should only be called with events returned from
528 * functions such as gdk_event_peek(), gdk_event_get(), gdk_event_copy()
529 * and gdk_event_new().
532 gdk_event_free (GdkEvent *event)
534 g_return_if_fail (event != NULL);
536 if (event->any.window)
537 g_object_unref (event->any.window);
539 switch (event->any.type)
542 case GDK_KEY_RELEASE:
543 g_free (event->key.string);
546 case GDK_ENTER_NOTIFY:
547 case GDK_LEAVE_NOTIFY:
548 if (event->crossing.subwindow != NULL)
549 g_object_unref (event->crossing.subwindow);
554 case GDK_DRAG_MOTION:
555 case GDK_DRAG_STATUS:
557 case GDK_DROP_FINISHED:
558 if (event->dnd.context != NULL)
559 g_object_unref (event->dnd.context);
562 case GDK_BUTTON_PRESS:
563 case GDK_BUTTON_RELEASE:
564 g_free (event->button.axes);
569 if (event->expose.region)
570 cairo_region_destroy (event->expose.region);
573 case GDK_MOTION_NOTIFY:
574 g_free (event->motion.axes);
578 g_free (event->setting.name);
585 _gdk_windowing_event_data_free (event);
587 g_hash_table_remove (event_hash, event);
588 g_slice_free (GdkEventPrivate, (GdkEventPrivate*) event);
592 * gdk_event_get_time:
593 * @event: a #GdkEvent
595 * Returns the time stamp from @event, if there is one; otherwise
596 * returns #GDK_CURRENT_TIME. If @event is %NULL, returns #GDK_CURRENT_TIME.
598 * Return value: time stamp field from @event
601 gdk_event_get_time (const GdkEvent *event)
606 case GDK_MOTION_NOTIFY:
607 return event->motion.time;
608 case GDK_BUTTON_PRESS:
609 case GDK_2BUTTON_PRESS:
610 case GDK_3BUTTON_PRESS:
611 case GDK_BUTTON_RELEASE:
612 return event->button.time;
614 return event->scroll.time;
616 case GDK_KEY_RELEASE:
617 return event->key.time;
618 case GDK_ENTER_NOTIFY:
619 case GDK_LEAVE_NOTIFY:
620 return event->crossing.time;
621 case GDK_PROPERTY_NOTIFY:
622 return event->property.time;
623 case GDK_SELECTION_CLEAR:
624 case GDK_SELECTION_REQUEST:
625 case GDK_SELECTION_NOTIFY:
626 return event->selection.time;
627 case GDK_PROXIMITY_IN:
628 case GDK_PROXIMITY_OUT:
629 return event->proximity.time;
632 case GDK_DRAG_MOTION:
633 case GDK_DRAG_STATUS:
635 case GDK_DROP_FINISHED:
636 return event->dnd.time;
637 case GDK_CLIENT_EVENT:
638 case GDK_VISIBILITY_NOTIFY:
641 case GDK_FOCUS_CHANGE:
649 case GDK_WINDOW_STATE:
651 case GDK_OWNER_CHANGE:
652 case GDK_GRAB_BROKEN:
654 /* return current time */
658 return GDK_CURRENT_TIME;
662 * gdk_event_get_state:
663 * @event: a #GdkEvent or NULL
664 * @state: (out): return location for state
666 * If the event contains a "state" field, puts that field in @state. Otherwise
667 * stores an empty state (0). Returns %TRUE if there was a state field
668 * in the event. @event may be %NULL, in which case it's treated
669 * as if the event had no state field.
671 * Return value: %TRUE if there was a state field in the event
674 gdk_event_get_state (const GdkEvent *event,
675 GdkModifierType *state)
677 g_return_val_if_fail (state != NULL, FALSE);
682 case GDK_MOTION_NOTIFY:
683 *state = event->motion.state;
685 case GDK_BUTTON_PRESS:
686 case GDK_2BUTTON_PRESS:
687 case GDK_3BUTTON_PRESS:
688 case GDK_BUTTON_RELEASE:
689 *state = event->button.state;
692 *state = event->scroll.state;
695 case GDK_KEY_RELEASE:
696 *state = event->key.state;
698 case GDK_ENTER_NOTIFY:
699 case GDK_LEAVE_NOTIFY:
700 *state = event->crossing.state;
702 case GDK_PROPERTY_NOTIFY:
703 *state = event->property.state;
705 case GDK_VISIBILITY_NOTIFY:
706 case GDK_CLIENT_EVENT:
709 case GDK_FOCUS_CHANGE:
710 case GDK_SELECTION_CLEAR:
711 case GDK_SELECTION_REQUEST:
712 case GDK_SELECTION_NOTIFY:
713 case GDK_PROXIMITY_IN:
714 case GDK_PROXIMITY_OUT:
718 case GDK_DRAG_MOTION:
719 case GDK_DRAG_STATUS:
721 case GDK_DROP_FINISHED:
728 case GDK_WINDOW_STATE:
730 case GDK_OWNER_CHANGE:
731 case GDK_GRAB_BROKEN:
742 * gdk_event_get_coords:
743 * @event: a #GdkEvent
744 * @x_win: (out): location to put event window x coordinate
745 * @y_win: (out): location to put event window y coordinate
747 * Extract the event window relative x/y coordinates from an event.
749 * Return value: %TRUE if the event delivered event window coordinates
752 gdk_event_get_coords (const GdkEvent *event,
756 gdouble x = 0, y = 0;
757 gboolean fetched = TRUE;
759 g_return_val_if_fail (event != NULL, FALSE);
764 x = event->configure.x;
765 y = event->configure.y;
767 case GDK_ENTER_NOTIFY:
768 case GDK_LEAVE_NOTIFY:
769 x = event->crossing.x;
770 y = event->crossing.y;
776 case GDK_BUTTON_PRESS:
777 case GDK_2BUTTON_PRESS:
778 case GDK_3BUTTON_PRESS:
779 case GDK_BUTTON_RELEASE:
783 case GDK_MOTION_NOTIFY:
801 * gdk_event_get_root_coords:
802 * @event: a #GdkEvent
803 * @x_root: (out): location to put root window x coordinate
804 * @y_root: (out): location to put root window y coordinate
806 * Extract the root window relative x/y coordinates from an event.
808 * Return value: %TRUE if the event delivered root window coordinates
811 gdk_event_get_root_coords (const GdkEvent *event,
815 gdouble x = 0, y = 0;
816 gboolean fetched = TRUE;
818 g_return_val_if_fail (event != NULL, FALSE);
822 case GDK_MOTION_NOTIFY:
823 x = event->motion.x_root;
824 y = event->motion.y_root;
827 x = event->scroll.x_root;
828 y = event->scroll.y_root;
830 case GDK_BUTTON_PRESS:
831 case GDK_2BUTTON_PRESS:
832 case GDK_3BUTTON_PRESS:
833 case GDK_BUTTON_RELEASE:
834 x = event->button.x_root;
835 y = event->button.y_root;
837 case GDK_ENTER_NOTIFY:
838 case GDK_LEAVE_NOTIFY:
839 x = event->crossing.x_root;
840 y = event->crossing.y_root;
844 case GDK_DRAG_MOTION:
845 case GDK_DRAG_STATUS:
847 case GDK_DROP_FINISHED:
848 x = event->dnd.x_root;
849 y = event->dnd.y_root;
865 * gdk_event_get_axis:
866 * @event: a #GdkEvent
867 * @axis_use: (out): the axis use to look for
868 * @value: (out): location to store the value found
870 * Extract the axis value for a particular axis use from
871 * an event structure.
873 * Return value: %TRUE if the specified axis was found, otherwise %FALSE
876 gdk_event_get_axis (const GdkEvent *event,
883 g_return_val_if_fail (event != NULL, FALSE);
885 if (axis_use == GDK_AXIS_X || axis_use == GDK_AXIS_Y)
891 case GDK_MOTION_NOTIFY:
899 case GDK_BUTTON_PRESS:
900 case GDK_BUTTON_RELEASE:
904 case GDK_ENTER_NOTIFY:
905 case GDK_LEAVE_NOTIFY:
906 x = event->crossing.x;
907 y = event->crossing.y;
914 if (axis_use == GDK_AXIS_X && value)
916 if (axis_use == GDK_AXIS_Y && value)
921 else if (event->type == GDK_BUTTON_PRESS ||
922 event->type == GDK_BUTTON_RELEASE)
924 device = event->button.device;
925 axes = event->button.axes;
927 else if (event->type == GDK_MOTION_NOTIFY)
929 device = event->motion.device;
930 axes = event->motion.axes;
935 return gdk_device_get_axis (device, axes, axis_use, value);
939 * gdk_event_set_device:
940 * @event: a #GdkEvent
941 * @device: a #GdkDevice
943 * Sets the device for @event to @device. The event must
944 * have been allocated by GTK+, for instance, by
950 gdk_event_set_device (GdkEvent *event,
953 GdkEventPrivate *private;
955 g_return_if_fail (gdk_event_is_allocated (event));
957 private = (GdkEventPrivate *) event;
959 private->device = device;
963 case GDK_MOTION_NOTIFY:
964 event->motion.device = device;
966 case GDK_BUTTON_PRESS:
967 case GDK_2BUTTON_PRESS:
968 case GDK_3BUTTON_PRESS:
969 case GDK_BUTTON_RELEASE:
970 event->button.device = device;
973 event->scroll.device = device;
975 case GDK_PROXIMITY_IN:
976 case GDK_PROXIMITY_OUT:
977 event->proximity.device = device;
985 * gdk_event_get_device:
986 * @event: a #GdkEvent.
988 * If the event contains a "device" field, this function will return
989 * it, else it will return %NULL.
991 * Returns: (transfer none): a #GdkDevice, or %NULL.
996 gdk_event_get_device (const GdkEvent *event)
998 g_return_val_if_fail (event != NULL, NULL);
1000 if (gdk_event_is_allocated (event))
1002 GdkEventPrivate *private = (GdkEventPrivate *) event;
1004 if (private->device)
1005 return private->device;
1008 switch (event->type)
1010 case GDK_MOTION_NOTIFY:
1011 return event->motion.device;
1012 case GDK_BUTTON_PRESS:
1013 case GDK_2BUTTON_PRESS:
1014 case GDK_3BUTTON_PRESS:
1015 case GDK_BUTTON_RELEASE:
1016 return event->button.device;
1018 return event->scroll.device;
1019 case GDK_PROXIMITY_IN:
1020 case GDK_PROXIMITY_OUT:
1021 return event->proximity.device;
1026 /* Fallback if event has no device set */
1027 switch (event->type)
1029 case GDK_MOTION_NOTIFY:
1030 case GDK_BUTTON_PRESS:
1031 case GDK_2BUTTON_PRESS:
1032 case GDK_3BUTTON_PRESS:
1033 case GDK_BUTTON_RELEASE:
1034 case GDK_ENTER_NOTIFY:
1035 case GDK_LEAVE_NOTIFY:
1036 case GDK_FOCUS_CHANGE:
1037 case GDK_PROXIMITY_IN:
1038 case GDK_PROXIMITY_OUT:
1039 case GDK_DRAG_ENTER:
1040 case GDK_DRAG_LEAVE:
1041 case GDK_DRAG_MOTION:
1042 case GDK_DRAG_STATUS:
1043 case GDK_DROP_START:
1044 case GDK_DROP_FINISHED:
1046 case GDK_GRAB_BROKEN:
1048 case GDK_KEY_RELEASE:
1050 GdkDisplay *display;
1051 GdkDeviceManager *device_manager;
1052 GdkDevice *client_pointer;
1054 g_warning ("Event with type %d not holding a GdkDevice. "
1055 "It is most likely synthesized outside Gdk/GTK+\n",
1058 display = gdk_window_get_display (event->any.window);
1059 device_manager = gdk_display_get_device_manager (display);
1060 client_pointer = gdk_device_manager_get_client_pointer (device_manager);
1062 if (event->type == GDK_KEY_PRESS ||
1063 event->type == GDK_KEY_RELEASE)
1064 return gdk_device_get_associated_device (client_pointer);
1066 return client_pointer;
1075 * gdk_event_request_motions:
1076 * @event: a valid #GdkEvent
1078 * Request more motion notifies if @event is a motion notify hint event.
1079 * This function should be used instead of gdk_window_get_pointer() to
1080 * request further motion notifies, because it also works for extension
1081 * events where motion notifies are provided for devices other than the
1082 * core pointer. Coordinate extraction, processing and requesting more
1083 * motion events from a %GDK_MOTION_NOTIFY event usually works like this:
1087 * /* motion_event handler */
1088 * x = motion_event->x;
1089 * y = motion_event->y;
1090 * /* handle (x,y) motion */
1091 * gdk_event_request_motions (motion_event); /* handles is_hint events */
1098 gdk_event_request_motions (const GdkEventMotion *event)
1100 GdkDisplay *display;
1102 g_return_if_fail (event != NULL);
1104 if (event->type == GDK_MOTION_NOTIFY && event->is_hint)
1106 gdk_device_get_state (event->device, event->window, NULL, NULL);
1108 display = gdk_window_get_display (event->window);
1109 _gdk_display_enable_motion_hints (display, event->device);
1114 gdk_events_get_axis_distances (GdkEvent *event1,
1116 gdouble *x_distance,
1117 gdouble *y_distance,
1120 gdouble x1, x2, y1, y2;
1123 if (!gdk_event_get_coords (event1, &x1, &y1) ||
1124 !gdk_event_get_coords (event2, &x2, &y2))
1137 *distance = sqrt ((xd * xd) + (yd * yd));
1143 * gdk_events_get_distance:
1144 * @event1: first #GdkEvent
1145 * @event2: second #GdkEvent
1146 * @distance: return location for the distance
1148 * If both events have X/Y information, the distance between both coordinates
1149 * (as in a straight line going from @event1 to @event2) will be returned.
1151 * Returns: %TRUE if the distance could be calculated.
1156 gdk_events_get_distance (GdkEvent *event1,
1160 return gdk_events_get_axis_distances (event1, event2,
1166 * gdk_events_get_angle:
1167 * @event1: first #GdkEvent
1168 * @event2: second #GdkEvent
1169 * @angle: return location for the relative angle between both events
1171 * If both events contain X/Y information, this function will return %TRUE
1172 * and return in @angle the relative angle from @event1 to @event2. The rotation
1173 * direction for positive angles is from the positive X axis towards the positive
1176 * Returns: %TRUE if the angle could be calculated.
1181 gdk_events_get_angle (GdkEvent *event1,
1185 gdouble x_distance, y_distance, distance;
1187 if (!gdk_events_get_axis_distances (event1, event2,
1188 &x_distance, &y_distance,
1194 *angle = atan2 (x_distance, y_distance);
1197 *angle = (2 * G_PI) - *angle;
1202 /* And constraint it to 0°-360° */
1203 *angle = fmod (*angle, 2 * G_PI);
1210 * gdk_events_get_center:
1211 * @event1: first #GdkEvent
1212 * @event2: second #GdkEvent
1213 * @x: (out): return location for the X coordinate of the center
1214 * @y: (out): return location for the Y coordinate of the center
1216 * If both events contain X/Y information, the center of both coordinates
1217 * will be returned in @x and @y.
1219 * Returns: %TRUE if the center could be calculated.
1224 gdk_events_get_center (GdkEvent *event1,
1229 gdouble x1, x2, y1, y2;
1231 if (!gdk_event_get_coords (event1, &x1, &y1) ||
1232 !gdk_event_get_coords (event2, &x2, &y2))
1245 * gdk_event_set_screen:
1246 * @event: a #GdkEvent
1247 * @screen: a #GdkScreen
1249 * Sets the screen for @event to @screen. The event must
1250 * have been allocated by GTK+, for instance, by
1256 gdk_event_set_screen (GdkEvent *event,
1259 GdkEventPrivate *private;
1261 g_return_if_fail (gdk_event_is_allocated (event));
1263 private = (GdkEventPrivate *)event;
1265 private->screen = screen;
1269 * gdk_event_get_screen:
1270 * @event: a #GdkEvent
1272 * Returns the screen for the event. The screen is
1273 * typically the screen for <literal>event->any.window</literal>, but
1274 * for events such as mouse events, it is the screen
1275 * where the pointer was when the event occurs -
1276 * that is, the screen which has the root window
1277 * to which <literal>event->motion.x_root</literal> and
1278 * <literal>event->motion.y_root</literal> are relative.
1280 * Return value: the screen for the event
1285 gdk_event_get_screen (const GdkEvent *event)
1287 if (gdk_event_is_allocated (event))
1289 GdkEventPrivate *private = (GdkEventPrivate *)event;
1291 if (private->screen)
1292 return private->screen;
1295 if (event->any.window)
1296 return gdk_window_get_screen (event->any.window);
1302 * gdk_set_show_events:
1303 * @show_events: %TRUE to output event debugging information.
1305 * Sets whether a trace of received events is output.
1306 * Note that GTK+ must be compiled with debugging (that is,
1307 * configured using the <option>--enable-debug</option> option)
1308 * to use this option.
1311 gdk_set_show_events (gboolean show_events)
1314 _gdk_debug_flags |= GDK_DEBUG_EVENTS;
1316 _gdk_debug_flags &= ~GDK_DEBUG_EVENTS;
1320 * gdk_get_show_events:
1322 * Gets whether event debugging output is enabled.
1324 * Return value: %TRUE if event debugging output is enabled.
1327 gdk_get_show_events (void)
1329 return (_gdk_debug_flags & GDK_DEBUG_EVENTS) != 0;
1332 /* What do we do with G_IO_NVAL?
1334 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
1335 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
1336 #define EXCEPTION_CONDITION (G_IO_PRI)
1339 gdk_synthesize_click (GdkDisplay *display,
1343 GdkEvent temp_event;
1344 GdkEvent *event_copy;
1347 g_return_if_fail (event != NULL);
1349 temp_event = *event;
1350 temp_event.type = (nclicks == 2) ? GDK_2BUTTON_PRESS : GDK_3BUTTON_PRESS;
1352 event_copy = gdk_event_copy (&temp_event);
1353 link = _gdk_event_queue_append (display, event_copy);
1357 _gdk_event_button_generate (GdkDisplay *display,
1360 GdkMultipleClickInfo *info;
1362 info = g_hash_table_lookup (display->multiple_click_info, event->button.device);
1364 if (G_UNLIKELY (!info))
1366 info = g_new0 (GdkMultipleClickInfo, 1);
1367 info->button_number[0] = info->button_number[1] = -1;
1369 g_hash_table_insert (display->multiple_click_info,
1370 event->button.device, info);
1373 if ((event->button.time < (info->button_click_time[1] + 2 * display->double_click_time)) &&
1374 (event->button.window == info->button_window[1]) &&
1375 (event->button.button == info->button_number[1]) &&
1376 (ABS (event->button.x - info->button_x[1]) <= display->double_click_distance) &&
1377 (ABS (event->button.y - info->button_y[1]) <= display->double_click_distance))
1379 gdk_synthesize_click (display, event, 3);
1381 info->button_click_time[1] = 0;
1382 info->button_click_time[0] = 0;
1383 info->button_window[1] = NULL;
1384 info->button_window[0] = NULL;
1385 info->button_number[1] = -1;
1386 info->button_number[0] = -1;
1387 info->button_x[0] = info->button_x[1] = 0;
1388 info->button_y[0] = info->button_y[1] = 0;
1390 else if ((event->button.time < (info->button_click_time[0] + display->double_click_time)) &&
1391 (event->button.window == info->button_window[0]) &&
1392 (event->button.button == info->button_number[0]) &&
1393 (ABS (event->button.x - info->button_x[0]) <= display->double_click_distance) &&
1394 (ABS (event->button.y - info->button_y[0]) <= display->double_click_distance))
1396 gdk_synthesize_click (display, event, 2);
1398 info->button_click_time[1] = info->button_click_time[0];
1399 info->button_click_time[0] = event->button.time;
1400 info->button_window[1] = info->button_window[0];
1401 info->button_window[0] = event->button.window;
1402 info->button_number[1] = info->button_number[0];
1403 info->button_number[0] = event->button.button;
1404 info->button_x[1] = info->button_x[0];
1405 info->button_x[0] = event->button.x;
1406 info->button_y[1] = info->button_y[0];
1407 info->button_y[0] = event->button.y;
1411 info->button_click_time[1] = 0;
1412 info->button_click_time[0] = event->button.time;
1413 info->button_window[1] = NULL;
1414 info->button_window[0] = event->button.window;
1415 info->button_number[1] = -1;
1416 info->button_number[0] = event->button.button;
1417 info->button_x[1] = 0;
1418 info->button_x[0] = event->button.x;
1419 info->button_y[1] = 0;
1420 info->button_y[0] = event->button.y;
1425 gdk_synthesize_window_state (GdkWindow *window,
1426 GdkWindowState unset_flags,
1427 GdkWindowState set_flags)
1429 GdkEvent temp_event;
1432 g_return_if_fail (window != NULL);
1434 temp_event.window_state.window = window;
1435 temp_event.window_state.type = GDK_WINDOW_STATE;
1436 temp_event.window_state.send_event = FALSE;
1438 old = ((GdkWindowObject*) temp_event.window_state.window)->state;
1440 temp_event.window_state.new_window_state = old;
1441 temp_event.window_state.new_window_state |= set_flags;
1442 temp_event.window_state.new_window_state &= ~unset_flags;
1443 temp_event.window_state.changed_mask = temp_event.window_state.new_window_state ^ old;
1445 if (temp_event.window_state.new_window_state == old)
1446 return; /* No actual work to do, nothing changed. */
1448 /* Actually update the field in GdkWindow, this is sort of an odd
1449 * place to do it, but seems like the safest since it ensures we expose no
1450 * inconsistent state to the user.
1453 ((GdkWindowObject*) window)->state = temp_event.window_state.new_window_state;
1455 if (temp_event.window_state.changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
1456 _gdk_window_update_viewable (window);
1458 /* We only really send the event to toplevels, since
1459 * all the window states don't apply to non-toplevels.
1460 * Non-toplevels do use the GDK_WINDOW_STATE_WITHDRAWN flag
1461 * internally so we needed to update window->state.
1463 switch (((GdkWindowObject*) window)->window_type)
1465 case GDK_WINDOW_TOPLEVEL:
1466 case GDK_WINDOW_TEMP: /* ? */
1467 gdk_display_put_event (gdk_window_get_display (window), &temp_event);
1470 case GDK_WINDOW_FOREIGN:
1471 case GDK_WINDOW_ROOT:
1472 case GDK_WINDOW_CHILD:
1478 * gdk_display_set_double_click_time:
1479 * @display: a #GdkDisplay
1480 * @msec: double click time in milliseconds (thousandths of a second)
1482 * Sets the double click time (two clicks within this time interval
1483 * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1484 * Applications should <emphasis>not</emphasis> set this, it is a global
1485 * user-configured setting.
1490 gdk_display_set_double_click_time (GdkDisplay *display,
1493 display->double_click_time = msec;
1497 * gdk_set_double_click_time:
1498 * @msec: double click time in milliseconds (thousandths of a second)
1500 * Set the double click time for the default display. See
1501 * gdk_display_set_double_click_time().
1502 * See also gdk_display_set_double_click_distance().
1503 * Applications should <emphasis>not</emphasis> set this, it is a
1504 * global user-configured setting.
1507 gdk_set_double_click_time (guint msec)
1509 gdk_display_set_double_click_time (gdk_display_get_default (), msec);
1513 * gdk_display_set_double_click_distance:
1514 * @display: a #GdkDisplay
1515 * @distance: distance in pixels
1517 * Sets the double click distance (two clicks within this distance
1518 * count as a double click and result in a #GDK_2BUTTON_PRESS event).
1519 * See also gdk_display_set_double_click_time().
1520 * Applications should <emphasis>not</emphasis> set this, it is a global
1521 * user-configured setting.
1526 gdk_display_set_double_click_distance (GdkDisplay *display,
1529 display->double_click_distance = distance;
1532 G_DEFINE_BOXED_TYPE (GdkEvent, gdk_event,
1538 * @name: the name of the setting.
1539 * @value: location to store the value of the setting.
1541 * Obtains a desktop-wide setting, such as the double-click time,
1542 * for the default screen. See gdk_screen_get_setting().
1544 * Returns: %TRUE if the setting existed and a value was stored
1545 * in @value, %FALSE otherwise.
1548 gdk_setting_get (const gchar *name,
1551 return gdk_screen_get_setting (gdk_screen_get_default (), name, value);