]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkdnd-quartz.c
Merge libgdk and libgtk
[~andy/gtk] / gtk / gtkdnd-quartz.c
index e00ef203cd7f625e39d6f2a28051b0795bfde09f..858f9c2d34392b4d5cd23c427c69ca9c0e5a999c 100644 (file)
@@ -29,9 +29,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "gdkconfig.h"
-
-#include "gdk/gdkkeysyms.h"
+#include "gdk/gdk.h"
 
 #include "gtkdnd.h"
 #include "gtkiconfactory.h"
@@ -44,7 +42,6 @@
 #include "gtkwindow.h"
 #include "gtkintl.h"
 #include "gtkquartz.h"
-#include "gtkalias.h"
 #include "gdk/quartz/gdkquartz.h"
 
 typedef struct _GtkDragSourceSite GtkDragSourceSite;
@@ -78,14 +75,10 @@ struct _GtkDragSourceSite
   GtkImageType icon_type;
   union
   {
-    GtkImagePixmapData pixmap;
     GtkImagePixbufData pixbuf;
     GtkImageStockData stock;
     GtkImageIconNameData name;
   } icon_data;
-  GdkBitmap *icon_mask;
-
-  GdkColormap       *colormap;          /* Colormap for drag icon */
 
   /* Stored button press information to detect drag beginning */
   gint               state;
@@ -337,7 +330,7 @@ gtk_drag_get_source_widget (GdkDragContext *context)
 }
 
 /*************************************************************
- * gtk_drag_highlight_expose:
+ * gtk_drag_highlight_draw:
  *     Callback for expose_event for highlighted widgets.
  *   arguments:
  *     widget:
@@ -347,45 +340,25 @@ gtk_drag_get_source_widget (GdkDragContext *context)
  *************************************************************/
 
 static gboolean
-gtk_drag_highlight_expose (GtkWidget      *widget,
-                          GdkEventExpose *event,
-                          gpointer        data)
-{
-  gint x, y, width, height;
-  
-  if (GTK_WIDGET_DRAWABLE (widget))
-    {
-      cairo_t *cr;
-      
-      if (GTK_WIDGET_NO_WINDOW (widget))
-       {
-         x = widget->allocation.x;
-         y = widget->allocation.y;
-         width = widget->allocation.width;
-         height = widget->allocation.height;
-       }
-      else
-       {
-         x = 0;
-         y = 0;
-         gdk_drawable_get_size (widget->window, &width, &height);
-       }
-      
-      gtk_paint_shadow (widget->style, widget->window,
-                       GTK_STATE_NORMAL, GTK_SHADOW_OUT,
-                       NULL, widget, "dnd",
-                       x, y, width, height);
-
-      cr = gdk_cairo_create (widget->window);
-      cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
-      cairo_set_line_width (cr, 1.0);
-      cairo_rectangle (cr,
-                      x + 0.5, y + 0.5,
-                      width - 1, height - 1);
-      cairo_stroke (cr);
-      cairo_destroy (cr);
-    }
-
+gtk_drag_highlight_draw (GtkWidget *widget,
+                         cairo_t   *cr,
+                        gpointer   data)
+{
+  int width = gtk_widget_get_allocated_width (widget);
+  int height = gtk_widget_get_allocated_height (widget);
+
+  gtk_paint_shadow (gtk_widget_get_style (widget), cr,
+                    GTK_STATE_NORMAL, GTK_SHADOW_OUT,
+                    widget, "dnd",
+                    0, 0, width, height);
+
+  cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
+  cairo_set_line_width (cr, 1.0);
+  cairo_rectangle (cr,
+                   0.5, 0.5,
+                   width - 1, height - 1);
+  cairo_stroke (cr);
   return FALSE;
 }
 
@@ -402,8 +375,8 @@ gtk_drag_highlight (GtkWidget  *widget)
 {
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
-  g_signal_connect_after (widget, "expose-event",
-                         G_CALLBACK (gtk_drag_highlight_expose),
+  g_signal_connect_after (widget, "draw",
+                         G_CALLBACK (gtk_drag_highlight_draw),
                          NULL);
 
   gtk_widget_queue_draw (widget);
@@ -423,7 +396,7 @@ gtk_drag_unhighlight (GtkWidget *widget)
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
   g_signal_handlers_disconnect_by_func (widget,
-                                       gtk_drag_highlight_expose,
+                                       gtk_drag_highlight_draw,
                                        NULL);
   
   gtk_widget_queue_draw (widget);
@@ -433,9 +406,10 @@ static NSWindow *
 get_toplevel_nswindow (GtkWidget *widget)
 {
   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
+  GdkWindow *window = gtk_widget_get_window (toplevel);
   
-  if (GTK_WIDGET_TOPLEVEL (toplevel) && toplevel->window)
-    return [gdk_quartz_window_get_nsview (toplevel->window) window];
+  if (gtk_widget_is_toplevel (toplevel) && window)
+    return [gdk_quartz_window_get_nsview (window) window];
   else
     return NULL;
 }
@@ -505,8 +479,6 @@ gtk_drag_dest_set (GtkWidget            *widget,
 
   old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
 
-  gtk_drag_dest_unset (widget);
-
   site = g_new (GtkDragDestSite, 1);
   site->flags = flags;
   site->have_drag = FALSE;
@@ -521,7 +493,9 @@ gtk_drag_dest_set (GtkWidget            *widget,
   else
     site->track_motion = FALSE;
 
-  if (GTK_WIDGET_REALIZED (widget))
+  gtk_drag_dest_unset (widget);
+
+  if (gtk_widget_get_realized (widget))
     gtk_drag_dest_realized (widget, site);
 
   g_signal_connect (widget, "realize",
@@ -667,7 +641,7 @@ gtk_drag_find_widget (GtkWidget       *widget,
   gint x_offset = 0;
   gint y_offset = 0;
 
-  if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
+  if (data->found || !gtk_widget_get_mapped (widget) || !gtk_widget_get_sensitive (widget))
     return;
 
   /* Note that in the following code, we only count the
@@ -684,20 +658,25 @@ gtk_drag_find_widget (GtkWidget       *widget,
    * our coordinates to be relative to widget->window and
    * recurse.
    */  
-  new_allocation = widget->allocation;
+  gtk_widget_get_allocation (widget, &new_allocation);
 
-  if (widget->parent)
+  if (gtk_widget_get_parent (widget))
     {
       gint tx, ty;
-      GdkWindow *window = widget->window;
+      GdkWindow *window = gtk_widget_get_window (widget);
+      GdkWindow *parent_window;
+      GtkAllocation allocation;
+
+      parent_window = gtk_widget_get_window (gtk_widget_get_parent (widget));
 
       /* Compute the offset from allocation-relative to
        * window-relative coordinates.
        */
-      allocation_to_window_x = widget->allocation.x;
-      allocation_to_window_y = widget->allocation.y;
+      gtk_widget_get_allocation (widget, &allocation);
+      allocation_to_window_x = allocation.x;
+      allocation_to_window_y = 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.
@@ -711,11 +690,12 @@ gtk_drag_find_widget (GtkWidget       *widget,
       new_allocation.x = 0 + allocation_to_window_x;
       new_allocation.y = 0 + allocation_to_window_y;
       
-      while (window && window != widget->parent->window)
+      while (window && window != parent_window)
        {
          GdkRectangle window_rect = { 0, 0, 0, 0 };
          
-         gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
+          window_rect.width = gdk_window_get_width (window);
+          window_rect.height = gdk_window_get_height (window);
 
          gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
 
@@ -757,7 +737,7 @@ gtk_drag_find_widget (GtkWidget       *widget,
          gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
          for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
            {
-             if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
+             if (!new_data.found && gtk_widget_is_drawable (tmp_list->data))
                gtk_drag_find_widget (tmp_list->data, &new_data);
              g_object_unref (tmp_list->data);
            }
@@ -971,7 +951,7 @@ _gtk_drag_dest_handle_event (GtkWidget *toplevel,
              }
          }
 
-       gdk_window_get_position (toplevel->window, &tx, &ty);
+       gdk_window_get_position (gtk_widget_get_window (toplevel), &tx, &ty);
        
        data.x = event->dnd.x_root - tx;
        data.y = event->dnd.y_root - ty;
@@ -1098,6 +1078,9 @@ gtk_drag_begin_idle (gpointer arg)
 
   drag_image = _gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf);
 
+  point.x -= info->hot_x;
+  point.y -= info->hot_y;
+
   [nswindow dragImage:drag_image
                    at:point
                offset:NSMakeSize(0, 0)
@@ -1151,30 +1134,6 @@ gtk_drag_begin_internal (GtkWidget         *widget,
       else
        switch (site->icon_type)
          {
-         case GTK_IMAGE_PIXMAP:
-           /* This is not supported, so just set a small transparent pixbuf
-            * since we need to have something.
-            */
-           if (0)
-             gtk_drag_set_icon_pixmap (context,
-                                       site->colormap,
-                                       site->icon_data.pixmap.pixmap,
-                                       site->icon_mask,
-                                       -2, -2);
-           else
-             {
-               GdkPixbuf *pixbuf;
-
-               pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
-               gdk_pixbuf_fill (pixbuf, 0xffffff);
-
-               gtk_drag_set_icon_pixbuf (context,
-                                         pixbuf,
-                                         0, 0);
-
-               g_object_unref (pixbuf);
-             }
-           break;
          case GTK_IMAGE_PIXBUF:
            gtk_drag_set_icon_pixbuf (context,
                                      site->icon_data.pixbuf.pixbuf,
@@ -1218,7 +1177,7 @@ gtk_drag_begin (GtkWidget         *widget,
                GdkEvent          *event)
 {
   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
-  g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
+  g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
   g_return_val_if_fail (targets != NULL, NULL);
 
   return gtk_drag_begin_internal (widget, NULL, targets,
@@ -1464,12 +1423,6 @@ gtk_drag_source_unset_icon (GtkDragSourceSite *site)
     {
     case GTK_IMAGE_EMPTY:
       break;
-    case GTK_IMAGE_PIXMAP:
-      if (site->icon_data.pixmap.pixmap)
-       g_object_unref (site->icon_data.pixmap.pixmap);
-      if (site->icon_mask)
-       g_object_unref (site->icon_mask);
-      break;
     case GTK_IMAGE_PIXBUF:
       g_object_unref (site->icon_data.pixbuf.pixbuf);
       break;
@@ -1484,10 +1437,6 @@ gtk_drag_source_unset_icon (GtkDragSourceSite *site)
       break;
     }
   site->icon_type = GTK_IMAGE_EMPTY;
-  
-  if (site->colormap)
-    g_object_unref (site->colormap);
-  site->colormap = NULL;
 }
 
 static void 
@@ -1502,36 +1451,6 @@ gtk_drag_source_site_destroy (gpointer data)
   g_free (site);
 }
 
-void 
-gtk_drag_source_set_icon (GtkWidget     *widget,
-                         GdkColormap   *colormap,
-                         GdkPixmap     *pixmap,
-                         GdkBitmap     *mask)
-{
-  GtkDragSourceSite *site;
-
-  g_return_if_fail (GTK_IS_WIDGET (widget));
-  g_return_if_fail (GDK_IS_COLORMAP (colormap));
-  g_return_if_fail (GDK_IS_PIXMAP (pixmap));
-  g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
-
-  site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
-  g_return_if_fail (site != NULL);
-  
-  g_object_ref (colormap);
-  g_object_ref (pixmap);
-  if (mask)
-    g_object_ref (mask);
-
-  gtk_drag_source_unset_icon (site);
-
-  site->icon_type = GTK_IMAGE_PIXMAP;
-  
-  site->icon_data.pixmap.pixmap = pixmap;
-  site->icon_mask = mask;
-  site->colormap = colormap;
-}
-
 void 
 gtk_drag_source_set_icon_pixbuf (GtkWidget   *widget,
                                 GdkPixbuf   *pixbuf)
@@ -1645,8 +1564,8 @@ set_icon_stock_pixbuf (GdkDragContext    *context,
 
   if (stock_id)
     {
-      pixbuf = gtk_widget_render_icon (info->widget, stock_id,
-                                      GTK_ICON_SIZE_DND, NULL);
+      pixbuf = gtk_widget_render_icon_pixbuf (info->widget, stock_id,
+                                             GTK_ICON_SIZE_DND, NULL);
 
       if (!pixbuf)
        {
@@ -1711,42 +1630,78 @@ gtk_drag_set_icon_stock  (GdkDragContext *context,
   set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
 }
 
+
+/* XXX: This function is in gdk, too. Should it be in Cairo? */
+static gboolean
+_gtk_cairo_surface_extents (cairo_surface_t *surface,
+                            GdkRectangle *extents)
+{
+  double x1, x2, y1, y2;
+  cairo_t *cr;
+
+  g_return_val_if_fail (surface != NULL, FALSE);
+  g_return_val_if_fail (extents != NULL, FALSE);
+
+  cr = cairo_create (surface);
+  cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
+
+  x1 = floor (x1);
+  y1 = floor (y1);
+  x2 = ceil (x2);
+  y2 = ceil (y2);
+  x2 -= x1;
+  y2 -= y1;
+  
+  if (x1 < G_MININT || x1 > G_MAXINT ||
+      y1 < G_MININT || y1 > G_MAXINT ||
+      x2 > G_MAXINT || y2 > G_MAXINT)
+    {
+      extents->x = extents->y = extents->width = extents->height = 0;
+      return FALSE;
+    }
+
+  extents->x = x1;
+  extents->y = y1;
+  extents->width = x2;
+  extents->height = y2;
+
+  return TRUE;
+}
+
 /**
- * gtk_drag_set_icon_pixmap:
- * @context: the context for a drag. (This must be called 
- *            with a  context for the source side of a drag)
- * @colormap: the colormap of the icon 
- * @pixmap: the image data for the icon 
- * @mask: the transparency mask for the icon
- * @hot_x: the X offset within @pixmap of the hotspot.
- * @hot_y: the Y offset within @pixmap of the hotspot.
- * 
- * Sets @pixmap as the icon for a given drag. GTK+ retains
+ * gtk_drag_set_icon_surface:
+ * @context: the context for a drag. (This must be called
+ *            with a context for the source side of a drag)
+ * @surface: the surface to use as icon
+ *
+ * Sets @surface as the icon for a given drag. GTK+ retains
  * references for the arguments, and will release them when
- * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
- * will be more convenient to use.
+ * they are no longer needed.
+ *
+ * To position the surface relative to the mouse, use
+ * cairo_surface_set_device_offset() on @surface. The mouse
+ * cursor will be positioned at the (0,0) coordinate of the
+ * surface.
  **/
-void 
-gtk_drag_set_icon_pixmap (GdkDragContext    *context,
-                         GdkColormap       *colormap,
-                         GdkPixmap         *pixmap,
-                         GdkBitmap         *mask,
-                         gint               hot_x,
-                         gint               hot_y)
+void
+gtk_drag_set_icon_surface (GdkDragContext  *context,
+                           cairo_surface_t *surface)
 {
   GdkPixbuf *pixbuf;
+  GdkRectangle extents;
+  double x_offset, y_offset;
 
   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
   g_return_if_fail (context->is_source);
-  g_return_if_fail (GDK_IS_COLORMAP (colormap));
-  g_return_if_fail (GDK_IS_PIXMAP (pixmap));
+  g_return_if_fail (surface != NULL);
 
-  pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, colormap,
-                                         0, 0, /* src */
-                                         0, 0, /* dst */
-                                         -1, -1);
+  _gtk_cairo_surface_extents (surface, &extents);
+  cairo_surface_get_device_offset (surface, &x_offset, &y_offset);
 
-  gtk_drag_set_icon_pixbuf (context, pixbuf, hot_x, hot_y);
+  pixbuf = gdk_pixbuf_get_from_surface (surface,
+                                        extents.x, extents.y,
+                                        extents.width, extents.height);
+  gtk_drag_set_icon_pixbuf (context, pixbuf, -x_offset, -y_offset);
   g_object_unref (pixbuf);
 }
 
@@ -1782,7 +1737,7 @@ gtk_drag_set_icon_name (GdkDragContext *context,
   g_return_if_fail (context->is_source);
   g_return_if_fail (icon_name != NULL);
 
-  screen = gdk_drawable_get_screen (context->source_window);
+  screen = gdk_window_get_screen (context->source_window);
   g_return_if_fail (screen != NULL);
 
   settings = gtk_settings_get_for_screen (screen);
@@ -1820,16 +1775,6 @@ gtk_drag_set_icon_default (GdkDragContext    *context)
   gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
 }
 
-void 
-gtk_drag_set_default_icon (GdkColormap   *colormap,
-                          GdkPixmap     *pixmap,
-                          GdkBitmap     *mask,
-                          gint           hot_x,
-                          gint           hot_y)
-{
-  g_warning ("gtk_drag_set_default_icon is not supported on Mac OS X.");
-}
-
 static void
 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
 {
@@ -1929,6 +1874,3 @@ gtk_drag_check_threshold (GtkWidget *widget,
   return (ABS (current_x - start_x) > drag_threshold ||
          ABS (current_y - start_y) > drag_threshold);
 }
-
-#define __GTK_DND_C__
-#include "gtkaliasdef.c"