]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkdnd.c
Deprecate flag macros for toplevel, state, no window and composite child
[~andy/gtk] / gtk / gtkdnd.c
index 18e6e017a57fd10985c172933d618c3f6f2bcaee..bdd3ab9e72ac120dd01abc46e61c98226cb650cd 100644 (file)
 
 #include "gdk/gdkkeysyms.h"
 
+#ifdef GDK_WINDOWING_X11
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+#include "gdk/x11/gdkx.h"
+#endif
+
 #include "gtkdnd.h"
 #include "gtkiconfactory.h"
 #include "gtkicontheme.h"
@@ -393,6 +399,139 @@ gtk_drag_get_ipc_widget (GtkWidget *widget)
 }
 
 
+#ifdef GDK_WINDOWING_X11
+
+/*
+ * We want to handle a handful of keys during DND, e.g. Escape to abort.
+ * Grabbing the keyboard has the unfortunate side-effect of preventing
+ * useful things such as using Alt-Tab to cycle between windows or
+ * switching workspaces. Therefore, we just grab the few keys we are
+ * interested in. Note that we need to put the grabs on the root window
+ * in order for them to still work when the focus is moved to another
+ * app/workspace.
+ *
+ * GDK needs a little help to successfully deliver root key events...
+ */
+
+static GdkFilterReturn
+root_key_filter (GdkXEvent *xevent,
+                 GdkEvent  *event,
+                 gpointer   data)
+{
+  XEvent *ev = (XEvent *)xevent;
+
+  if ((ev->type == KeyPress || ev->type == KeyRelease) &&
+      ev->xkey.root == ev->xkey.window)
+    ev->xkey.window = (Window)data;
+
+  return GDK_FILTER_CONTINUE;
+}
+
+typedef struct {
+  gint keysym;
+  gint modifiers;
+} GrabKey;
+
+static GrabKey grab_keys[] = {
+  { XK_Escape, 0 },
+  { XK_space, 0 },
+  { XK_KP_Space, 0 },
+  { XK_Return, 0 },
+  { XK_KP_Enter, 0 },
+  { XK_Up, 0 },
+  { XK_Up, Mod1Mask },
+  { XK_Down, 0 },
+  { XK_Down, Mod1Mask },
+  { XK_Left, 0 },
+  { XK_Left, Mod1Mask },
+  { XK_Right, 0 },
+  { XK_Right, Mod1Mask },
+  { XK_KP_Up, 0 },
+  { XK_KP_Up, Mod1Mask },
+  { XK_KP_Down, 0 },
+  { XK_KP_Down, Mod1Mask },
+  { XK_KP_Left, 0 },
+  { XK_KP_Left, Mod1Mask },
+  { XK_KP_Right, 0 },
+  { XK_KP_Right, Mod1Mask }
+};
+
+static void
+grab_dnd_keys (GtkWidget *widget,
+               guint32    time)
+{
+  guint i;
+  GdkWindow *window, *root;
+  gint keycode;
+
+  window = widget->window;
+  root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
+
+  gdk_error_trap_push ();
+
+  for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
+    {
+      keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
+      XGrabKey (GDK_WINDOW_XDISPLAY (window),
+               keycode, grab_keys[i].modifiers,
+               GDK_WINDOW_XID (root),
+               FALSE,
+               GrabModeAsync,
+               GrabModeAsync);
+    }
+
+  gdk_flush ();
+  gdk_error_trap_pop ();
+
+  gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
+}
+
+static void
+ungrab_dnd_keys (GtkWidget *widget,
+                 guint32    time)
+{
+  guint i;
+  GdkWindow *window, *root;
+  gint keycode;
+
+  window = widget->window;
+  root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
+
+  gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
+
+  gdk_error_trap_push ();
+
+  for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
+    {
+      keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
+      XUngrabKey (GDK_WINDOW_XDISPLAY (window),
+                 keycode, grab_keys[i].modifiers,
+                  GDK_WINDOW_XID (root));
+    }
+
+  gdk_flush ();
+  gdk_error_trap_pop ();
+}
+
+#else
+
+static void
+grab_dnd_keys (GtkWidget *widget,
+               guint32    time)
+{
+  gdk_keyboard_grab (widget->window, FALSE, time);
+}
+
+static void
+ungrab_dnd_keys (GtkWidget *widget,
+                 guint32    time)
+{
+  gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), time);
+}
+
+#endif
+
+
 /***************************************************************
  * gtk_drag_release_ipc_widget:
  *     Releases widget retrieved with gtk_drag_get_ipc_widget ()
@@ -408,6 +547,7 @@ gtk_drag_release_ipc_widget (GtkWidget *widget)
   GdkScreen *screen = gtk_widget_get_screen (widget);
   GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
                                            "gtk-dnd-ipc-widgets");
+  ungrab_dnd_keys (widget, GDK_CURRENT_TIME);
   if (window->group)
     gtk_window_group_remove_window (window->group, window);
   drag_widgets = g_slist_prepend (drag_widgets, widget);
@@ -966,7 +1106,7 @@ gtk_drag_highlight_expose (GtkWidget      *widget,
     {
       cairo_t *cr;
       
-      if (GTK_WIDGET_NO_WINDOW (widget))
+      if (!gtk_widget_get_has_window (widget))
        {
          x = widget->allocation.x;
          y = widget->allocation.y;
@@ -1099,6 +1239,28 @@ gtk_drag_dest_set_internal (GtkWidget       *widget,
  * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
  * Especially the later is dramatic, when your own #GtkWidget:drag-motion
  * handler calls gtk_drag_get_data() to inspect the dragged data.
+ *
+ * There's no way to set a default action here, you can use the
+ * #GtkWidget:drag-motion callback for that. Here's an example which selects
+ * the action to use depending on whether the control key is pressed or not:
+ * |[
+ * static void
+ * drag_motion (GtkWidget *widget,
+ *              GdkDragContext *context,
+ *              gint x,
+ *              gint y,
+ *              guint time)
+ * {
+ *   GdkModifierType mask;
+ *
+ *   gdk_window_get_pointer (gtk_widget_get_window (widget),
+ *                           NULL, NULL, &mask);
+ *   if (mask & GDK_CONTROL_MASK)
+ *     gdk_drag_status (context, GDK_ACTION_COPY, time);
+ *   else
+ *     gdk_drag_status (context, GDK_ACTION_MOVE, time);
+ * }
+ * ]|
  */
 void
 gtk_drag_dest_set (GtkWidget            *widget,
@@ -1698,7 +1860,7 @@ gtk_drag_find_widget (GtkWidget       *widget,
       allocation_to_window_x = widget->allocation.x;
       allocation_to_window_y = widget->allocation.y;
 
-      if (!GTK_WIDGET_NO_WINDOW (widget))
+      if (gtk_widget_get_has_window (widget))
        {
          /* The allocation is relative to the parent window for
           * window widgets, not to widget->window.
@@ -1903,7 +2065,7 @@ gtk_drag_dest_realized (GtkWidget *widget)
 {
   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
 
-  if (GTK_WIDGET_TOPLEVEL (toplevel))
+  if (gtk_widget_is_toplevel (toplevel))
     gdk_window_register_dnd (toplevel->window);
 }
 
@@ -1913,7 +2075,7 @@ gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
 {
   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
 
-  if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_WIDGET_REALIZED (toplevel))
+  if (gtk_widget_is_toplevel (toplevel) && GTK_WIDGET_REALIZED (toplevel))
     gdk_window_register_dnd (toplevel->window);
 }
 
@@ -2224,8 +2386,8 @@ gtk_drag_begin_internal (GtkWidget         *widget,
       return NULL;
     }
 
-  gdk_keyboard_grab (ipc_widget->window, FALSE, time);
-    
+  grab_dnd_keys (ipc_widget, time);
+
   /* We use a GTK grab here to override any grabs that the widget
    * we are dragging from might have held
    */
@@ -2669,10 +2831,10 @@ gtk_drag_source_unset_icon (GtkDragSourceSite *site)
  * @widget: a #GtkWidget
  * @colormap: the colormap of the icon
  * @pixmap: the image data for the icon
- * @mask: the transparency mask for an image.
- * 
+ * @mask: (allow-none): the transparency mask for an image.
+ *
  * Sets the icon that will be used for drags from a particular widget
- * from a pixmap/mask. GTK+ retains references for the arguments, and 
+ * from a pixmap/mask. GTK+ retains references for the arguments, and
  * will release them when they are no longer needed.
  * Use gtk_drag_source_set_icon_pixbuf() instead.
  **/
@@ -3239,8 +3401,9 @@ gtk_drag_set_icon_default (GdkDragContext *context)
  * 
  * Changes the default drag icon. GTK+ retains references for the
  * arguments, and will release them when they are no longer needed.
- * This function is obsolete. The default icon should now be changed
- * via the stock system by changing the stock pixbuf for #GTK_STOCK_DND.
+ *
+ * Deprecated: Change the default drag icon via the stock system by 
+ *     changing the stock pixbuf for #GTK_STOCK_DND instead.
  **/
 void 
 gtk_drag_set_default_icon (GdkColormap   *colormap,
@@ -3964,7 +4127,7 @@ gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
                                        info);
 
   gdk_display_pointer_ungrab (display, time);
-  gdk_display_keyboard_ungrab (display, time);
+  ungrab_dnd_keys (info->ipc_widget, time);
   gtk_grab_remove (info->ipc_widget);
 
   /* Send on a release pair to the original