]> Pileus Git - ~andy/gtk/blobdiff - gdk/x11/gdkcursor-x11.c
Change FSF Address
[~andy/gtk] / gdk / x11 / gdkcursor-x11.c
index b257b619e97899886e9d76759e93ee04eaea23f6..8d4da14a3ee94eefdc15362840b6247d2514f9c4 100644 (file)
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
 #include "gdkcursor.h"
-
+#include "gdkcursorprivate.h"
 #include "gdkprivate-x11.h"
 #include "gdkdisplay-x11.h"
-#include "gdkx.h"
 
 #include <X11/Xlib.h>
 #include <X11/cursorfont.h>
 #include <string.h>
 #include <errno.h>
 
+struct _GdkX11Cursor
+{
+  GdkCursor cursor;
+
+  Cursor xcursor;
+  gchar *name;
+  guint serial;
+};
+
+struct _GdkX11CursorClass
+{
+  GdkCursorClass cursor_class;
+};
 
 static guint theme_serial = 0;
 
@@ -70,12 +80,12 @@ struct cursor_cache_key
  * a non-NULL name.
  */
 static void
-add_to_cache (GdkCursorPrivate* cursor)
+add_to_cache (GdkX11Cursor* cursor)
 {
   cursor_cache = g_slist_prepend (cursor_cache, cursor);
 
   /* Take a ref so that if the caller frees it we still have it */
-  gdk_cursor_ref ((GdkCursor*) cursor);
+  g_object_ref (cursor);
 }
 
 /* Returns 0 on a match
@@ -84,11 +94,11 @@ static gint
 cache_compare_func (gconstpointer listelem, 
                     gconstpointer target)
 {
-  GdkCursorPrivate* cursor = (GdkCursorPrivate*)listelem;
+  GdkX11Cursor* cursor = (GdkX11Cursor*)listelem;
   struct cursor_cache_key* key = (struct cursor_cache_key*)target;
 
   if ((cursor->cursor.type != key->type) ||
-      (cursor->display != key->display))
+      (gdk_cursor_get_display (GDK_CURSOR (cursor)) != key->display))
     return 1; /* No match */
   
   /* Elements marked as pixmap must be named cursors 
@@ -104,7 +114,7 @@ cache_compare_func (gconstpointer listelem,
  * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
  * For unnamed, typed cursors, name shall be NULL
  */
-static GdkCursorPrivate*
+static GdkX11Cursor*
 find_in_cache (GdkDisplay    *display, 
                GdkCursorType  type,
                const char    *name)
@@ -119,15 +129,15 @@ find_in_cache (GdkDisplay    *display,
   res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
 
   if (res)
-    return (GdkCursorPrivate *) res->data;
+    return (GdkX11Cursor *) res->data;
 
   return NULL;
 }
 
-/* Called by gdk_display_x11_finalize to flush any cached cursors
+/* Called by gdk_x11_display_finalize to flush any cached cursors
  * for a dead display.
  */
-void 
+void
 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
 {
   GSList* item;
@@ -136,11 +146,11 @@ _gdk_x11_cursor_display_finalize (GdkDisplay *display)
   itemp = &cursor_cache;
   while (item)
     {
-      GdkCursorPrivate* cursor = (GdkCursorPrivate*)(item->data);
-      if (cursor->display == display)
+      GdkX11Cursor* cursor = (GdkX11Cursor*)(item->data);
+      if (gdk_cursor_get_display (GDK_CURSOR (cursor)) == display)
         {
           GSList* olditem;
-          gdk_cursor_unref ((GdkCursor*) cursor);
+          g_object_unref ((GdkCursor*) cursor);
           /* Remove this item from the list */
           *(itemp) = item->next;
           olditem = item;
@@ -155,6 +165,43 @@ _gdk_x11_cursor_display_finalize (GdkDisplay *display)
     }
 }
 
+/*** GdkX11Cursor ***/
+
+G_DEFINE_TYPE (GdkX11Cursor, gdk_x11_cursor, GDK_TYPE_CURSOR)
+
+static GdkPixbuf* gdk_x11_cursor_get_image (GdkCursor *cursor);
+
+static void
+gdk_x11_cursor_finalize (GObject *object)
+{
+  GdkX11Cursor *private = GDK_X11_CURSOR (object);
+  GdkDisplay *display;
+
+  display = gdk_cursor_get_display (GDK_CURSOR (object));
+  if (private->xcursor && !gdk_display_is_closed (display))
+    XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
+
+  g_free (private->name);
+
+  G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
+}
+
+static void
+gdk_x11_cursor_class_init (GdkX11CursorClass *xcursor_class)
+{
+  GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (xcursor_class);
+  GObjectClass *object_class = G_OBJECT_CLASS (xcursor_class);
+
+  object_class->finalize = gdk_x11_cursor_finalize;
+
+  cursor_class->get_image = gdk_x11_cursor_get_image;
+}
+
+static void
+gdk_x11_cursor_init (GdkX11Cursor *cursor)
+{
+}
+
 static Cursor
 get_blank_cursor (GdkDisplay *display)
 {
@@ -193,8 +240,7 @@ GdkCursor*
 _gdk_x11_display_get_cursor_for_type (GdkDisplay    *display,
                                       GdkCursorType  cursor_type)
 {
-  GdkCursorPrivate *private;
-  GdkCursor *cursor;
+  GdkX11Cursor *private;
   Cursor xcursor;
 
   if (gdk_display_is_closed (display))
@@ -208,7 +254,7 @@ _gdk_x11_display_get_cursor_for_type (GdkDisplay    *display,
       if (private)
         {
           /* Cache had it, add a ref for this user */
-          gdk_cursor_ref ((GdkCursor*) private);
+          g_object_ref (private);
 
           return (GdkCursor*) private;
         }
@@ -222,57 +268,39 @@ _gdk_x11_display_get_cursor_for_type (GdkDisplay    *display,
        }
     }
 
-  private = g_new (GdkCursorPrivate, 1);
-  private->display = display;
+  private = g_object_new (GDK_TYPE_X11_CURSOR,
+                          "cursor-type", cursor_type,
+                          "display", display,
+                          NULL);
   private->xcursor = xcursor;
   private->name = NULL;
   private->serial = theme_serial;
 
-  cursor = (GdkCursor *) private;
-  cursor->type = cursor_type;
-  cursor->ref_count = 1;
-  
   if (xcursor != None)
     add_to_cache (private);
 
-  return cursor;
-}
-
-void
-_gdk_cursor_destroy (GdkCursor *cursor)
-{
-  GdkCursorPrivate *private;
-
-  g_return_if_fail (cursor != NULL);
-  g_return_if_fail (cursor->ref_count == 0);
-
-  private = (GdkCursorPrivate *) cursor;
-  if (private->xcursor && !gdk_display_is_closed (private->display))
-    XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
-
-  g_free (private->name);
-  g_free (private);
+  return GDK_CURSOR (private);
 }
 
 /**
  * gdk_x11_cursor_get_xdisplay:
- * @cursor: a #GdkCursor.
+ * @cursor: (type GdkX11Cursor): a #GdkCursor.
  * 
  * Returns the display of a #GdkCursor.
  * 
- * Return value: an Xlib <type>Display*</type>.
+ * Return value: (transfer none): an Xlib <type>Display*</type>.
  **/
 Display *
 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
 {
   g_return_val_if_fail (cursor != NULL, NULL);
 
-  return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
+  return GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
 }
 
 /**
  * gdk_x11_cursor_get_xcursor:
- * @cursor: a #GdkCursor.
+ * @cursor: (type GdkX11Cursor): a #GdkCursor.
  * 
  * Returns the X cursor belonging to a #GdkCursor.
  * 
@@ -283,49 +311,16 @@ gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
 {
   g_return_val_if_fail (cursor != NULL, None);
 
-  return ((GdkCursorPrivate *)cursor)->xcursor;
-}
-
-/** 
- * gdk_cursor_get_display:
- * @cursor: a #GdkCursor.
- *
- * Returns the display on which the #GdkCursor is defined.
- *
- * Returns: (transfer none): the #GdkDisplay associated to @cursor
- *
- * Since: 2.2
- */
-
-GdkDisplay *
-gdk_cursor_get_display (GdkCursor *cursor)
-{
-  g_return_val_if_fail (cursor != NULL, NULL);
-
-  return ((GdkCursorPrivate *)cursor)->display;
+  return ((GdkX11Cursor *)cursor)->xcursor;
 }
 
 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
 
-/**
- * gdk_cursor_get_image:
- * @cursor: a #GdkCursor
- *
- * Returns a #GdkPixbuf with the image used to display the cursor.
- *
- * Note that depending on the capabilities of the windowing system and 
- * on the cursor, GDK may not be able to obtain the image data. In this 
- * case, %NULL is returned.
- *
- * Returns: (transfer full): a #GdkPixbuf representing @cursor, or %NULL
- *
- * Since: 2.8
- */
-GdkPixbuf*  
-gdk_cursor_get_image (GdkCursor *cursor)
+static GdkPixbuf*  
+gdk_x11_cursor_get_image (GdkCursor *cursor)
 {
   Display *xdisplay;
-  GdkCursorPrivate *private;
+  GdkX11Cursor *private;
   XcursorImages *images = NULL;
   XcursorImage *image;
   gint size;
@@ -334,11 +329,9 @@ gdk_cursor_get_image (GdkCursor *cursor)
   GdkPixbuf *pixbuf;
   gchar *theme;
   
-  g_return_val_if_fail (cursor != NULL, NULL);
-
-  private = (GdkCursorPrivate *) cursor;
+  private = GDK_X11_CURSOR (cursor);
     
-  xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
+  xdisplay = GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
 
   size = XcursorGetDefaultSize (xdisplay);
   theme = XcursorGetTheme (xdisplay);
@@ -387,13 +380,13 @@ void
 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
 {
   Display *xdisplay;
-  GdkCursorPrivate *private;
+  GdkX11Cursor *private;
   Cursor new_cursor = None;
-  GdkDisplayX11 *display_x11;
+  GdkX11Display *display_x11;
 
-  private = (GdkCursorPrivate *) cursor;
-  xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
-  display_x11 = GDK_DISPLAY_X11 (private->display);
+  private = (GdkX11Cursor *) cursor;
+  display_x11 = GDK_X11_DISPLAY (gdk_cursor_get_display (cursor));
+  xdisplay = GDK_DISPLAY_XDISPLAY (display_x11);
 
   if (!display_x11->have_xfixes)
     return;
@@ -434,26 +427,26 @@ update_cursor (gpointer data,
 
   if (!cursor)
     return;
-  
+
   _gdk_x11_cursor_update_theme (cursor);
 }
 
 /**
  * gdk_x11_display_set_cursor_theme:
- * @display: a #GdkDisplay
+ * @display: (type GdkX11Display): a #GdkDisplay
  * @theme: the name of the cursor theme to use, or %NULL to unset
- *         a previously set value 
+ *         a previously set value
  * @size: the cursor size to use, or 0 to keep the previous size
  *
  * Sets the cursor theme from which the images for cursor
- * should be taken. 
- * 
- * If the windowing system supports it, existing cursors created 
- * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
- * gdk_cursor_new_for_name() are updated to reflect the theme 
+ * should be taken.
+ *
+ * If the windowing system supports it, existing cursors created
+ * with gdk_cursor_new(), gdk_cursor_new_for_display() and
+ * gdk_cursor_new_for_name() are updated to reflect the theme
  * change. Custom cursors constructed with
  * gdk_cursor_new_from_pixbuf() will have to be handled
- * by the application (GTK+ applications can learn about 
+ * by the application (GTK+ applications can learn about
  * cursor theme changes by listening for change notification
  * for the corresponding #GtkSetting).
  *
@@ -464,14 +457,12 @@ gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
                                   const gchar *theme,
                                   const gint   size)
 {
-  GdkDisplayX11 *display_x11;
   Display *xdisplay;
   gchar *old_theme;
   gint old_size;
 
   g_return_if_fail (GDK_IS_DISPLAY (display));
 
-  display_x11 = GDK_DISPLAY_X11 (display);
   xdisplay = GDK_DISPLAY_XDISPLAY (display);
 
   old_theme = XcursorGetTheme (xdisplay);
@@ -487,17 +478,15 @@ gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
   XcursorSetTheme (xdisplay, theme);
   if (size > 0)
     XcursorSetDefaultSize (xdisplay, size);
-    
+
   g_slist_foreach (cursor_cache, update_cursor, NULL);
 }
 
 #else
 
-GdkPixbuf*  
-gdk_cursor_get_image (GdkCursor *cursor)
+static GdkPixbuf*
+gdk_x11_cursor_get_image (GdkCursor *cursor)
 {
-  g_return_val_if_fail (cursor != NULL, NULL);
-  
   return NULL;
 }
 
@@ -562,8 +551,7 @@ _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
 {
   XcursorImage *xcimage;
   Cursor xcursor;
-  GdkCursorPrivate *private;
-  GdkCursor *cursor;
+  GdkX11Cursor *private;
   const char *option;
   char *end;
   gint64 value;
@@ -603,17 +591,15 @@ _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
       XcursorImageDestroy (xcimage);
     }
 
-  private = g_new (GdkCursorPrivate, 1);
-  private->display = display;
+  private = g_object_new (GDK_TYPE_X11_CURSOR, 
+                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
+                          "display", display,
+                          NULL);
   private->xcursor = xcursor;
   private->name = NULL;
   private->serial = theme_serial;
 
-  cursor = (GdkCursor *) private;
-  cursor->type = GDK_CURSOR_IS_PIXMAP;
-  cursor->ref_count = 1;
-
-  return cursor;
+  return GDK_CURSOR (private);
 }
 
 GdkCursor*
@@ -622,8 +608,7 @@ _gdk_x11_display_get_cursor_for_name (GdkDisplay  *display,
 {
   Cursor xcursor;
   Display *xdisplay;
-  GdkCursorPrivate *private;
-  GdkCursor *cursor;
+  GdkX11Cursor *private;
 
   if (gdk_display_is_closed (display))
     {
@@ -636,7 +621,7 @@ _gdk_x11_display_get_cursor_for_name (GdkDisplay  *display,
       if (private)
         {
           /* Cache had it, add a ref for this user */
-          gdk_cursor_ref ((GdkCursor*) private);
+          g_object_ref (private);
 
           return (GdkCursor*) private;
         }
@@ -647,18 +632,17 @@ _gdk_x11_display_get_cursor_for_name (GdkDisplay  *display,
         return NULL;
     }
 
-  private = g_new (GdkCursorPrivate, 1);
-  private->display = display;
+  private = g_object_new (GDK_TYPE_X11_CURSOR,
+                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
+                          "display", display,
+                          NULL);
   private->xcursor = xcursor;
   private->name = g_strdup (name);
   private->serial = theme_serial;
 
-  cursor = (GdkCursor *) private;
-  cursor->type = GDK_CURSOR_IS_PIXMAP;
-  cursor->ref_count = 1;
   add_to_cache (private);
 
-  return cursor;
+  return GDK_CURSOR (private);
 }
 
 gboolean
@@ -687,44 +671,40 @@ static GdkCursor*
 gdk_cursor_new_from_pixmap (GdkDisplay     *display,
                             Pixmap          source_pixmap,
                             Pixmap          mask_pixmap,
-                            const GdkColor *fg,
-                            const GdkColor *bg,
+                            const GdkRGBA  *fg,
+                            const GdkRGBA  *bg,
                             gint            x,
                             gint            y)
 {
-  GdkCursorPrivate *private;
-  GdkCursor *cursor;
+  GdkX11Cursor *private;
   Cursor xcursor;
   XColor xfg, xbg;
 
   g_return_val_if_fail (fg != NULL, NULL);
   g_return_val_if_fail (bg != NULL, NULL);
 
-  xfg.pixel = fg->pixel;
-  xfg.red = fg->red;
-  xfg.blue = fg->blue;
-  xfg.green = fg->green;
-  xbg.pixel = bg->pixel;
-  xbg.red = bg->red;
-  xbg.blue = bg->blue;
-  xbg.green = bg->green;
-  
-  if (gdk_display_is_closed (display->closed))
+  xfg.red = fg->red * 65535;
+  xfg.blue = fg->blue * 65535;
+  xfg.green = fg->green * 65535;
+
+  xbg.red = bg->red * 65535;
+  xbg.blue = bg->blue * 65535;
+  xbg.green = bg->green * 65535;
+
+  if (gdk_display_is_closed (display))
     xcursor = None;
   else
     xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
                                    source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
-  private = g_new (GdkCursorPrivate, 1);
-  private->display = display;
+  private = g_object_new (GDK_TYPE_X11_CURSOR,
+                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
+                          "display", display,
+                          NULL);
   private->xcursor = xcursor;
   private->name = NULL;
   private->serial = theme_serial;
 
-  cursor = (GdkCursor *) private;
-  cursor->type = GDK_CURSOR_IS_PIXMAP;
-  cursor->ref_count = 1;
-
-  return cursor;
+  return GDK_CURSOR (private);
 }
 
 GdkCursor *
@@ -737,8 +717,8 @@ _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
   cairo_surface_t *pixmap, *mask;
   guint width, height, n_channels, rowstride, data_stride, i, j;
   guint8 *data, *mask_data, *pixels;
-  GdkColor fg = { 0, 0, 0, 0 };
-  GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
+  GdkRGBA fg = { 0, 0, 0, 1 };
+  GdkRGBA bg = { 1, 1, 1, 1 };
   GdkScreen *screen;
   cairo_surface_t *image;
   cairo_t *cr;
@@ -839,25 +819,17 @@ _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
 }
 
 void
-_gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
+_gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
+                                          guint      *width,
+                                          guint      *height)
 {
   /* no idea, really */
-  return 20;
+  *width = *height = 20;
+  return;
 }
 
 #endif
 
-
-/**
- * gdk_display_get_maximal_cursor_size:
- * @display: a #GdkDisplay
- * @width: (out): the return location for the maximal cursor width
- * @height: (out): the return location for the maximal cursor height
- *
- * Gets the maximal size to use for cursors on @display.
- *
- * Since: 2.4
- */
 void
 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
                                           guint       *width,