]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkevents.c
gdk/: fully remove gdkalias hacks
[~andy/gtk] / gdk / gdkevents.c
index 90e16d7bd85e320fde7e7da66ac9ecca2c3b7e78..fb9373aee1a2c678e1a34d56009f14fb3d557908 100644 (file)
 
 #include "config.h"
 #include <string.h>            /* For memset() */
+#include <math.h>
 
 #include "gdk.h"
 #include "gdkinternals.h"
-#include "gdkalias.h"
+
 
 typedef struct _GdkIOClosure GdkIOClosure;
 
 struct _GdkIOClosure
 {
-  GdkInputFunction function;
-  GdkInputCondition condition;
   GDestroyNotify notify;
   gpointer data;
 };
@@ -446,6 +445,7 @@ gdk_event_copy (const GdkEvent *event)
       GdkEventPrivate *private = (GdkEventPrivate *)event;
 
       new_private->screen = private->screen;
+      new_private->device = private->device;
     }
   
   switch (event->any.type)
@@ -471,8 +471,9 @@ gdk_event_copy (const GdkEvent *event)
       break;
       
     case GDK_EXPOSE:
+    case GDK_DAMAGE:
       if (event->expose.region)
-       new_event->expose.region = gdk_region_copy (event->expose.region);
+       new_event->expose.region = cairo_region_copy (event->expose.region);
       break;
       
     case GDK_SETTING:
@@ -509,8 +510,8 @@ gdk_event_copy (const GdkEvent *event)
  * 
  * Frees a #GdkEvent, freeing or decrementing any resources associated with it.
  * Note that this function should only be called with events returned from
- * functions such as gdk_event_peek(), gdk_event_get(),
- * gdk_event_get_graphics_expose() and gdk_event_copy().
+ * functions such as gdk_event_peek(), gdk_event_get(), gdk_event_copy()
+ * and gdk_event_new().
  **/
 void
 gdk_event_free (GdkEvent *event)
@@ -548,8 +549,9 @@ gdk_event_free (GdkEvent *event)
       break;
       
     case GDK_EXPOSE:
+    case GDK_DAMAGE:
       if (event->expose.region)
-       gdk_region_destroy (event->expose.region);
+       cairo_region_destroy (event->expose.region);
       break;
       
     case GDK_MOTION_NOTIFY:
@@ -643,7 +645,7 @@ gdk_event_get_time (const GdkEvent *event)
 /**
  * gdk_event_get_state:
  * @event: a #GdkEvent or NULL
- * @state: return location for state
+ * @state: (out): return location for state
  * 
  * If the event contains a "state" field, puts that field in @state. Otherwise
  * stores an empty state (0). Returns %TRUE if there was a state field
@@ -723,8 +725,8 @@ gdk_event_get_state (const GdkEvent        *event,
 /**
  * gdk_event_get_coords:
  * @event: a #GdkEvent
- * @x_win: location to put event window x coordinate
- * @y_win: location to put event window y coordinate
+ * @x_win: (out): location to put event window x coordinate
+ * @y_win: (out): location to put event window y coordinate
  * 
  * Extract the event window relative x/y coordinates from an event.
  * 
@@ -782,8 +784,8 @@ gdk_event_get_coords (const GdkEvent *event,
 /**
  * gdk_event_get_root_coords:
  * @event: a #GdkEvent
- * @x_root: location to put root window x coordinate
- * @y_root: location to put root window y coordinate
+ * @x_root: (out): location to put root window x coordinate
+ * @y_root: (out): location to put root window y coordinate
  * 
  * Extract the root window relative x/y coordinates from an event.
  * 
@@ -846,8 +848,8 @@ gdk_event_get_root_coords (const GdkEvent *event,
 /**
  * gdk_event_get_axis:
  * @event: a #GdkEvent
- * @axis_use: the axis use to look for
- * @value: location to store the value found
+ * @axis_use: (out): the axis use to look for
+ * @value: (out): location to store the value found
  * 
  * Extract the axis value for a particular axis use from
  * an event structure.
@@ -917,6 +919,140 @@ gdk_event_get_axis (const GdkEvent *event,
   return gdk_device_get_axis (device, axes, axis_use, value);
 }
 
+/**
+ * gdk_event_set_device:
+ * @event: a #GdkEvent
+ * @device: a #GdkDevice
+ *
+ * Sets the device for @event to @device. The event must
+ * have been allocated by GTK+, for instance, by
+ * gdk_event_copy().
+ *
+ * Since: 3.0
+ **/
+void
+gdk_event_set_device (GdkEvent  *event,
+                      GdkDevice *device)
+{
+  GdkEventPrivate *private;
+
+  g_return_if_fail (gdk_event_is_allocated (event));
+
+  private = (GdkEventPrivate *) event;
+
+  private->device = device;
+
+  switch (event->type)
+    {
+    case GDK_MOTION_NOTIFY:
+      event->motion.device = device;
+      break;
+    case GDK_BUTTON_PRESS:
+    case GDK_2BUTTON_PRESS:
+    case GDK_3BUTTON_PRESS:
+    case GDK_BUTTON_RELEASE:
+      event->button.device = device;
+      break;
+    case GDK_SCROLL:
+      event->scroll.device = device;
+      break;
+    case GDK_PROXIMITY_IN:
+    case GDK_PROXIMITY_OUT:
+      event->proximity.device = device;
+      break;
+    default:
+      break;
+    }
+}
+
+/**
+ * gdk_event_get_device:
+ * @event: a #GdkEvent.
+ *
+ * If the event contains a "device" field, this function will return
+ * it, else it will return %NULL.
+ *
+ * Returns: a #GdkDevice, or %NULL.
+ *
+ * Since: 3.0
+ **/
+GdkDevice *
+gdk_event_get_device (const GdkEvent *event)
+{
+  g_return_val_if_fail (event != NULL, NULL);
+
+  if (gdk_event_is_allocated (event))
+    {
+      GdkEventPrivate *private = (GdkEventPrivate *) event;
+
+      if (private->device)
+        return private->device;
+    }
+
+  switch (event->type)
+    {
+    case GDK_MOTION_NOTIFY:
+      return event->motion.device;
+    case GDK_BUTTON_PRESS:
+    case GDK_2BUTTON_PRESS:
+    case GDK_3BUTTON_PRESS:
+    case GDK_BUTTON_RELEASE:
+      return event->button.device;
+    case GDK_SCROLL:
+      return event->scroll.device;
+    case GDK_PROXIMITY_IN:
+    case GDK_PROXIMITY_OUT:
+      return event->proximity.device;
+    default:
+      break;
+    }
+
+  /* Fallback if event has no device set */
+  switch (event->type)
+    {
+    case GDK_MOTION_NOTIFY:
+    case GDK_BUTTON_PRESS:
+    case GDK_2BUTTON_PRESS:
+    case GDK_3BUTTON_PRESS:
+    case GDK_BUTTON_RELEASE:
+    case GDK_ENTER_NOTIFY:
+    case GDK_LEAVE_NOTIFY:
+    case GDK_FOCUS_CHANGE:
+    case GDK_PROXIMITY_IN:
+    case GDK_PROXIMITY_OUT:
+    case GDK_DRAG_ENTER:
+    case GDK_DRAG_LEAVE:
+    case GDK_DRAG_MOTION:
+    case GDK_DRAG_STATUS:
+    case GDK_DROP_START:
+    case GDK_DROP_FINISHED:
+    case GDK_SCROLL:
+    case GDK_GRAB_BROKEN:
+    case GDK_KEY_PRESS:
+    case GDK_KEY_RELEASE:
+      {
+        GdkDisplay *display;
+        GdkDevice *core_pointer;
+
+        g_warning ("Event with type %d not holding a GdkDevice. "
+                   "It is most likely synthesized outside Gdk/GTK+\n",
+                   event->type);
+
+        display = gdk_drawable_get_display (event->any.window);
+        core_pointer = gdk_display_get_core_pointer (display);
+
+        if (event->type == GDK_KEY_PRESS ||
+            event->type == GDK_KEY_RELEASE)
+          return gdk_device_get_associated_device (core_pointer);
+        else
+          return core_pointer;
+      }
+      break;
+    default:
+      return NULL;
+    }
+}
+
 /**
  * gdk_event_request_motions:
  * @event: a valid #GdkEvent
@@ -952,10 +1088,141 @@ gdk_event_request_motions (const GdkEventMotion *event)
       gdk_device_get_state (event->device, event->window, NULL, NULL);
       
       display = gdk_drawable_get_display (event->window);
-      _gdk_display_enable_motion_hints (display);
+      _gdk_display_enable_motion_hints (display, event->device);
     }
 }
 
+static gboolean
+gdk_events_get_axis_distances (GdkEvent *event1,
+                               GdkEvent *event2,
+                               gdouble  *x_distance,
+                               gdouble  *y_distance,
+                               gdouble  *distance)
+{
+  gdouble x1, x2, y1, y2;
+  gdouble xd, yd;
+
+  if (!gdk_event_get_coords (event1, &x1, &y1) ||
+      !gdk_event_get_coords (event2, &x2, &y2))
+    return FALSE;
+
+  xd = x2 - x1;
+  yd = y2 - y1;
+
+  if (x_distance)
+    *x_distance = xd;
+
+  if (y_distance)
+    *y_distance = yd;
+
+  if (distance)
+    *distance = sqrt ((xd * xd) + (yd * yd));
+
+  return TRUE;
+}
+
+/**
+ * gdk_events_get_distance:
+ * @event1: first #GdkEvent
+ * @event2: second #GdkEvent
+ * @distance: return location for the distance
+ *
+ * If both events have X/Y information, the distance between both coordinates
+ * (as in a straight line going from @event1 to @event2) will be returned.
+ *
+ * Returns: %TRUE if the distance could be calculated.
+ *
+ * Since: 3.0
+ **/
+gboolean
+gdk_events_get_distance (GdkEvent *event1,
+                         GdkEvent *event2,
+                         gdouble  *distance)
+{
+  return gdk_events_get_axis_distances (event1, event2,
+                                        NULL, NULL,
+                                        distance);
+}
+
+/**
+ * gdk_events_get_angle:
+ * @event1: first #GdkEvent
+ * @event2: second #GdkEvent
+ * @angle: return location for the relative angle between both events
+ *
+ * If both events contain X/Y information, this function will return %TRUE
+ * and return in @angle the relative angle from @event1 to @event2. The rotation
+ * direction for positive angles is from the positive X axis towards the positive
+ * Y axis.
+ *
+ * Returns: %TRUE if the angle could be calculated.
+ *
+ * Since: 3.0
+ **/
+gboolean
+gdk_events_get_angle (GdkEvent *event1,
+                      GdkEvent *event2,
+                      gdouble  *angle)
+{
+  gdouble x_distance, y_distance, distance;
+
+  if (!gdk_events_get_axis_distances (event1, event2,
+                                      &x_distance, &y_distance,
+                                      &distance))
+    return FALSE;
+
+  if (angle)
+    {
+      *angle = atan2 (x_distance, y_distance);
+
+      /* Invert angle */
+      *angle = (2 * G_PI) - *angle;
+
+      /* Shift it 90° */
+      *angle += G_PI / 2;
+
+      /* And constraint it to 0°-360° */
+      *angle = fmod (*angle, 2 * G_PI);
+    }
+
+  return TRUE;
+}
+
+/**
+ * gdk_events_get_center:
+ * @event1: first #GdkEvent
+ * @event2: second #GdkEvent
+ * @x: (out): return location for the X coordinate of the center
+ * @y: (out): return location for the Y coordinate of the center
+ *
+ * If both events contain X/Y information, the center of both coordinates
+ * will be returned in @x and @y.
+ *
+ * Returns: %TRUE if the center could be calculated.
+ *
+ * Since: 3.0
+ **/
+gboolean
+gdk_events_get_center (GdkEvent *event1,
+                       GdkEvent *event2,
+                       gdouble  *x,
+                       gdouble  *y)
+{
+  gdouble x1, x2, y1, y2;
+
+  if (!gdk_event_get_coords (event1, &x1, &y1) ||
+      !gdk_event_get_coords (event2, &x2, &y2))
+    return FALSE;
+
+  if (x)
+    *x = (x2 + x1) / 2;
+
+  if (y)
+    *y = (y2 + y1) / 2;
+
+  return TRUE;
+}
+
 /**
  * gdk_event_set_screen:
  * @event: a #GdkEvent
@@ -1044,124 +1311,12 @@ gdk_get_show_events (void)
   return (_gdk_debug_flags & GDK_DEBUG_EVENTS) != 0;
 }
 
-static void
-gdk_io_destroy (gpointer data)
-{
-  GdkIOClosure *closure = data;
-
-  if (closure->notify)
-    closure->notify (closure->data);
-
-  g_free (closure);
-}
-
 /* What do we do with G_IO_NVAL?
  */
 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
 #define EXCEPTION_CONDITION (G_IO_PRI)
 
-static gboolean  
-gdk_io_invoke (GIOChannel   *source,
-              GIOCondition  condition,
-              gpointer      data)
-{
-  GdkIOClosure *closure = data;
-  GdkInputCondition gdk_cond = 0;
-
-  if (condition & READ_CONDITION)
-    gdk_cond |= GDK_INPUT_READ;
-  if (condition & WRITE_CONDITION)
-    gdk_cond |= GDK_INPUT_WRITE;
-  if (condition & EXCEPTION_CONDITION)
-    gdk_cond |= GDK_INPUT_EXCEPTION;
-
-  if (closure->condition & gdk_cond)
-    closure->function (closure->data, g_io_channel_unix_get_fd (source), gdk_cond);
-
-  return TRUE;
-}
-
-/**
- * gdk_input_add_full:
- * @source: a file descriptor.
- * @condition: the condition.
- * @function: the callback function.
- * @data: callback data passed to @function.
- * @destroy: callback function to call with @data when the input
- * handler is removed.
- *
- * Establish a callback when a condition becomes true on
- * a file descriptor.
- *
- * Returns: a tag that can later be used as an argument to
- * gdk_input_remove().
- *
- * Deprecated: Use g_io_add_watch_full() on a #GIOChannel
- */
-gint
-gdk_input_add_full (gint             source,
-                   GdkInputCondition condition,
-                   GdkInputFunction  function,
-                   gpointer          data,
-                   GDestroyNotify    destroy)
-{
-  guint result;
-  GdkIOClosure *closure = g_new (GdkIOClosure, 1);
-  GIOChannel *channel;
-  GIOCondition cond = 0;
-
-  closure->function = function;
-  closure->condition = condition;
-  closure->notify = destroy;
-  closure->data = data;
-
-  if (condition & GDK_INPUT_READ)
-    cond |= READ_CONDITION;
-  if (condition & GDK_INPUT_WRITE)
-    cond |= WRITE_CONDITION;
-  if (condition & GDK_INPUT_EXCEPTION)
-    cond |= EXCEPTION_CONDITION;
-
-  channel = g_io_channel_unix_new (source);
-  result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, cond, 
-                               gdk_io_invoke,
-                               closure, gdk_io_destroy);
-  g_io_channel_unref (channel);
-
-  return result;
-}
-
-/**
- * gdk_input_add:
- * @source: a file descriptor.
- * @condition: the condition.
- * @function: the callback function.
- * @data: callback data passed to @function.
- *
- * Establish a callback when a condition becomes true on
- * a file descriptor.
- *
- * Returns: a tag that can later be used as an argument to
- * gdk_input_remove().
- *
- * Deprecated: Use g_io_add_watch() on a #GIOChannel
- */
-gint
-gdk_input_add (gint             source,
-              GdkInputCondition condition,
-              GdkInputFunction  function,
-              gpointer          data)
-{
-  return gdk_input_add_full (source, condition, function, data, NULL);
-}
-
-void
-gdk_input_remove (gint tag)
-{
-  g_source_remove (tag);
-}
-
 static void
 gdk_synthesize_click (GdkDisplay *display,
                      GdkEvent   *event,
@@ -1184,54 +1339,67 @@ void
 _gdk_event_button_generate (GdkDisplay *display,
                            GdkEvent   *event)
 {
-  if ((event->button.time < (display->button_click_time[1] + 2*display->double_click_time)) &&
-      (event->button.window == display->button_window[1]) &&
-      (event->button.button == display->button_number[1]) &&
-      (ABS (event->button.x - display->button_x[1]) <= display->double_click_distance) &&
-      (ABS (event->button.y - display->button_y[1]) <= display->double_click_distance))
-{
+  GdkMultipleClickInfo *info;
+
+  info = g_hash_table_lookup (display->multiple_click_info, event->button.device);
+
+  if (G_UNLIKELY (!info))
+    {
+      info = g_new0 (GdkMultipleClickInfo, 1);
+      info->button_number[0] = info->button_number[1] = -1;
+
+      g_hash_table_insert (display->multiple_click_info,
+                           event->button.device, info);
+    }
+
+  if ((event->button.time < (info->button_click_time[1] + 2 * display->double_click_time)) &&
+      (event->button.window == info->button_window[1]) &&
+      (event->button.button == info->button_number[1]) &&
+      (ABS (event->button.x - info->button_x[1]) <= display->double_click_distance) &&
+      (ABS (event->button.y - info->button_y[1]) <= display->double_click_distance))
+    {
       gdk_synthesize_click (display, event, 3);
-            
-      display->button_click_time[1] = 0;
-      display->button_click_time[0] = 0;
-      display->button_window[1] = NULL;
-      display->button_window[0] = NULL;
-      display->button_number[1] = -1;
-      display->button_number[0] = -1;
-      display->button_x[0] = display->button_x[1] = 0;
-      display->button_y[0] = display->button_y[1] = 0;
+
+      info->button_click_time[1] = 0;
+      info->button_click_time[0] = 0;
+      info->button_window[1] = NULL;
+      info->button_window[0] = NULL;
+      info->button_number[1] = -1;
+      info->button_number[0] = -1;
+      info->button_x[0] = info->button_x[1] = 0;
+      info->button_y[0] = info->button_y[1] = 0;
     }
-  else if ((event->button.time < (display->button_click_time[0] + display->double_click_time)) &&
-          (event->button.window == display->button_window[0]) &&
-          (event->button.button == display->button_number[0]) &&
-          (ABS (event->button.x - display->button_x[0]) <= display->double_click_distance) &&
-          (ABS (event->button.y - display->button_y[0]) <= display->double_click_distance))
+  else if ((event->button.time < (info->button_click_time[0] + display->double_click_time)) &&
+          (event->button.window == info->button_window[0]) &&
+          (event->button.button == info->button_number[0]) &&
+          (ABS (event->button.x - info->button_x[0]) <= display->double_click_distance) &&
+          (ABS (event->button.y - info->button_y[0]) <= display->double_click_distance))
     {
       gdk_synthesize_click (display, event, 2);
       
-      display->button_click_time[1] = display->button_click_time[0];
-      display->button_click_time[0] = event->button.time;
-      display->button_window[1] = display->button_window[0];
-      display->button_window[0] = event->button.window;
-      display->button_number[1] = display->button_number[0];
-      display->button_number[0] = event->button.button;
-      display->button_x[1] = display->button_x[0];
-      display->button_x[0] = event->button.x;
-      display->button_y[1] = display->button_y[0];
-      display->button_y[0] = event->button.y;
+      info->button_click_time[1] = info->button_click_time[0];
+      info->button_click_time[0] = event->button.time;
+      info->button_window[1] = info->button_window[0];
+      info->button_window[0] = event->button.window;
+      info->button_number[1] = info->button_number[0];
+      info->button_number[0] = event->button.button;
+      info->button_x[1] = info->button_x[0];
+      info->button_x[0] = event->button.x;
+      info->button_y[1] = info->button_y[0];
+      info->button_y[0] = event->button.y;
     }
   else
     {
-      display->button_click_time[1] = 0;
-      display->button_click_time[0] = event->button.time;
-      display->button_window[1] = NULL;
-      display->button_window[0] = event->button.window;
-      display->button_number[1] = -1;
-      display->button_number[0] = event->button.button;
-      display->button_x[1] = 0;
-      display->button_x[0] = event->button.x;
-      display->button_y[1] = 0;
-      display->button_y[0] = event->button.y;
+      info->button_click_time[1] = 0;
+      info->button_click_time[0] = event->button.time;
+      info->button_window[1] = NULL;
+      info->button_window[0] = event->button.window;
+      info->button_number[1] = -1;
+      info->button_number[0] = event->button.button;
+      info->button_x[1] = 0;
+      info->button_x[0] = event->button.x;
+      info->button_y[1] = 0;
+      info->button_y[0] = event->button.y;
     }
 }
 
@@ -1373,6 +1541,3 @@ gdk_setting_get (const gchar *name,
 {
   return gdk_screen_get_setting (gdk_screen_get_default (), name, value);
 }
-
-#define __GDK_EVENTS_C__
-#include "gdkaliasdef.c"