]> Pileus Git - ~andy/gtk/blobdiff - gdk/win32/gdkpixmap-win32.c
Internal GDK error reporting changes: (gdk_win32_gdi_failed) New function
[~andy/gtk] / gdk / win32 / gdkpixmap-win32.c
index a943ba65c8f57c5bd349254a64c36b609f12cf6c..144ae7f40e371a215df9bcb0f6e412699631c43e 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "gdkpixmap.h"
 #include "gdkprivate.h"
+#include "gdkwin32.h"
 
 typedef struct
 {
@@ -48,6 +49,49 @@ typedef struct
   gulong pixels[1];
 } _GdkPixmapInfo;
 
+static void
+gdk_win32_pixmap_destroy (GdkPixmap *pixmap)
+{
+  GdkDrawablePrivate *private = (GdkDrawablePrivate *) pixmap;
+
+  GDK_NOTE (MISC, g_print ("gdk_win32_pixmap_destroy: %#x\n",
+                          GDK_DRAWABLE_XID (pixmap)));
+
+  if (!DeleteObject (GDK_DRAWABLE_XID (pixmap)))
+    WIN32_GDI_FAILED ("DeleteObject");
+
+  gdk_xid_table_remove (GDK_DRAWABLE_XID (pixmap));
+
+  g_free (GDK_DRAWABLE_WIN32DATA (pixmap));
+}
+
+static GdkDrawable *
+gdk_win32_pixmap_alloc (void)
+{
+  GdkDrawable *drawable;
+  GdkDrawablePrivate *private;
+  
+  static GdkDrawableClass klass;
+  static gboolean initialized = FALSE;
+
+  if (!initialized)
+    {
+      initialized = TRUE;
+      
+      klass = _gdk_win32_drawable_class;
+      klass.destroy = gdk_win32_pixmap_destroy;
+    }
+
+  drawable = gdk_drawable_alloc ();
+  private = (GdkDrawablePrivate *) drawable;
+
+  private->klass = &klass;
+  private->klass_data = g_new (GdkDrawableWin32Data, 1);
+  private->window_type = GDK_DRAWABLE_PIXMAP;
+
+  return drawable;
+}
+
 GdkPixmap*
 gdk_pixmap_new (GdkWindow *window,
                gint       width,
@@ -55,8 +99,7 @@ gdk_pixmap_new (GdkWindow *window,
                gint       depth)
 {
   GdkPixmap *pixmap;
-  GdkWindowPrivate *private;
-  GdkWindowPrivate *window_private;
+  GdkDrawablePrivate *private;
   struct {
     BITMAPINFOHEADER bmiHeader;
     union {
@@ -71,75 +114,30 @@ gdk_pixmap_new (GdkWindow *window,
   guchar *bits;
   gint i;
 
+  g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
   g_return_val_if_fail ((window != NULL) || (depth != -1), NULL);
   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
 
   if (!window)
-    window = (GdkWindow*) &gdk_root_parent;
+    window = gdk_parent_root;
 
-  window_private = (GdkWindowPrivate*) window;
-  if (window_private->destroyed)
+  if (GDK_DRAWABLE_DESTROYED (window))
     return NULL;
 
   if (depth == -1)
-    depth = gdk_window_get_visual (window)->depth;
-
-  GDK_NOTE (MISC, g_print ("gdk_pixmap_new: %dx%dx%d\n", width, height, depth));
+    depth = gdk_drawable_get_visual (window)->depth;
 
-  private = g_new0 (GdkWindowPrivate, 1);
-  pixmap = (GdkPixmap*) private;
+  GDK_NOTE (MISC, g_print ("gdk_pixmap_new: %dx%dx%d\n",
+                          width, height, depth));
 
-  private->window_type = GDK_WINDOW_PIXMAP;
+  pixmap = gdk_win32_pixmap_alloc ();
+  private = (GdkDrawablePrivate *) pixmap;
 
-  visual = gdk_window_get_visual (window);
+  visual = gdk_drawable_get_visual (window);
 
-#if 0
-  if (depth == 1)
+  if ((hdc = GetDC (GDK_DRAWABLE_XID (window))) == NULL)
     {
-      if ((private->xwindow =
-          CreateBitmap (width, height, 1, 1, NULL)) == NULL)
-       {
-         g_warning ("gdk_pixmap_new: CreateBitmap failed");
-         g_free (private);
-         return NULL;
-       }
-
-      private->colormap = NULL;
-    }
-  else
-    {
-      if (depth != visual->depth)
-       g_warning ("gdk_pixmap_new: depth %d doesn't match display depth %d",
-                  depth, visual->depth);
-
-      if ((hdc = GetDC (window_private->xwindow)) == NULL)
-       {
-         g_warning ("gdk_pixmap_new: GetDC failed");
-         g_free (private);
-         return NULL;
-       }
-
-      if ((private->xwindow =
-          CreateCompatibleBitmap (hdc, width, height)) == NULL)
-       {
-         g_warning ("gdk_pixmap_new: %dx%d CreateCompatibleBitmap failed",
-                    width, height);
-         ReleaseDC (window_private->xwindow, hdc);
-         g_free (private);
-         return NULL;
-       }
-
-      ReleaseDC (window_private->xwindow, hdc);
-
-      private->colormap = window_private->colormap;
-      if (private->colormap == NULL)
-       private->colormap = gdk_colormap_get_system ();
-    }
-#else
-
-  if ((hdc = GetDC (window_private->xwindow)) == NULL)
-    {
-      g_warning ("gdk_pixmap_new: GetDC failed");
+      WIN32_GDI_FAILED ("GetDC");
       g_free (private);
       return NULL;
     }
@@ -174,13 +172,13 @@ gdk_pixmap_new (GdkWindow *window,
 
       bmi.u.bmiColors[1].rgbBlue =
        bmi.u.bmiColors[1].rgbGreen =
-      bmi.u.bmiColors[1].rgbRed = 0xFF;
+       bmi.u.bmiColors[1].rgbRed = 0xFF;
       bmi.u.bmiColors[1].rgbReserved = 0x00;
       private->colormap = NULL;
     }
   else
     {
-      private->colormap = window_private->colormap;
+      private->colormap = ((GdkWindowPrivate *) window)->drawable.colormap;
       if (private->colormap == NULL)
        private->colormap = gdk_colormap_get_system ();
 
@@ -205,31 +203,23 @@ gdk_pixmap_new (GdkWindow *window,
 #endif
        }
     }
-  if ((private->xwindow =
+  if ((GDK_DRAWABLE_WIN32DATA (pixmap)->xid =
        CreateDIBSection (hdc, (BITMAPINFO *) &bmi,
                         iUsage, (PVOID *) &bits, NULL, 0)) == NULL)
     {
-      g_warning ("gdk_pixmap_new: CreateDIBSection failed: %d", GetLastError ());
-      ReleaseDC (window_private->xwindow, hdc);
-      g_free (private);
+      WIN32_GDI_FAILED ("CreateDIBSection");
+      ReleaseDC (GDK_DRAWABLE_XID (window), hdc);
+      g_free (pixmap);
       return NULL;
     }
-  ReleaseDC (window_private->xwindow, hdc);
+  ReleaseDC (GDK_DRAWABLE_XID (window), hdc);
 
-#endif
-
-  GDK_NOTE (MISC, g_print ("... = %#x\n", private->xwindow));
+  GDK_NOTE (MISC, g_print ("... = %#x\n", GDK_DRAWABLE_XID (pixmap)));
 
-  private->parent = NULL;
-  private->x = 0;
-  private->y = 0;
   private->width = width;
   private->height = height;
-  private->resize_count = 0;
-  private->ref_count = 1;
-  private->destroyed = 0;
 
-  gdk_xid_table_insert (&private->xwindow, pixmap);
+  gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
 
   return pixmap;
 }
@@ -243,13 +233,10 @@ gdk_pixmap_create_on_shared_image (GdkImage **image_return,
                                   gint       depth)
 {
   GdkPixmap *pixmap;
-  GdkImagePrivate *image_private;
-  GdkWindowPrivate *private;
-  GdkWindowPrivate *window_private;
+  GdkDrawablePrivate *private;
 
   g_return_val_if_fail (window != NULL, NULL);
 
-  window_private = (GdkWindowPrivate *) window;
 
   if (depth == 1)
     *image_return = gdk_image_bitmap_new (GDK_IMAGE_SHARED_PIXMAP, visual, width, height);
@@ -261,28 +248,20 @@ gdk_pixmap_create_on_shared_image (GdkImage **image_return,
 
   g_return_val_if_fail (*image_return != NULL, NULL);
 
-  image_private = (GdkImagePrivate *) *image_return;
+  pixmap = gdk_win32_pixmap_alloc ();
+  private = (GdkDrawablePrivate *) pixmap;
 
-  private = g_new0 (GdkWindowPrivate, 1);
-  pixmap = (GdkPixmap*) private;
-
-  private->xwindow = image_private->ximage;
-  private->window_type = GDK_WINDOW_PIXMAP;
-  private->colormap = window_private->colormap;
-  private->parent = NULL;
-  private->x = 0;
-  private->y = 0;
+  GDK_DRAWABLE_WIN32DATA (pixmap)->xid =
+    ((GdkImagePrivateWin32 *) *image_return)->ximage;
+  private->colormap = ((GdkWindowPrivate *) window)->drawable.colormap;
   private->width = width;
   private->height = height;
-  private->resize_count = 0;
-  private->ref_count = 1;
-  private->destroyed = 0;
 
-  gdk_xid_table_insert (&private->xwindow, pixmap);
+  gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
 
   GDK_NOTE (MISC,
            g_print ("gdk_pixmap_create_on_shared_image: %dx%dx%d = %#x\n",
-                    width, height, depth, private->xwindow));
+                    width, height, depth, GDK_DRAWABLE_XID (pixmap)));
 
   return pixmap;
 }
@@ -329,33 +308,25 @@ gdk_bitmap_create_from_data (GdkWindow   *window,
                             gint         height)
 {
   GdkPixmap *pixmap;
-  GdkWindowPrivate *private;
-  GdkWindowPrivate *window_private;
+  GdkDrawablePrivate *private;
   gint i, j, bpl, aligned_bpl;
   guchar *bits;
 
   g_return_val_if_fail (data != NULL, NULL);
   g_return_val_if_fail ((width != 0) && (height != 0), NULL);
+  g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
 
   if (!window)
-    window = (GdkWindow*) &gdk_root_parent;
+    window = gdk_parent_root;
 
-  window_private = (GdkWindowPrivate*) window;
-  if (window_private->destroyed)
+  if (GDK_DRAWABLE_DESTROYED (window))
     return NULL;
 
-  private = g_new0 (GdkWindowPrivate, 1);
-  pixmap = (GdkPixmap*) private;
+  pixmap = gdk_win32_pixmap_alloc ();
+  private = (GdkDrawablePrivate *) pixmap;
 
-  private->parent = NULL;
-  private->window_type = GDK_WINDOW_PIXMAP;
-  private->x = 0;
-  private->y = 0;
   private->width = width;
   private->height = height;
-  private->resize_count = 0;
-  private->ref_count = 1;
-  private->destroyed = FALSE;
 
   bpl = ((width - 1) / 8 + 1);
   aligned_bpl = ((bpl - 1) / 2 + 1) * 2;
@@ -363,15 +334,17 @@ gdk_bitmap_create_from_data (GdkWindow   *window,
   for (i = 0; i < height; i++)
     for (j = 0; j < bpl; j++)
       bits[i*aligned_bpl + j] = mirror[(guchar) data[i*bpl + j]];
-  private->xwindow = CreateBitmap (width, height, 1, 1, bits);
+
+  GDK_DRAWABLE_WIN32DATA (pixmap)->xid =
+    CreateBitmap (width, height, 1, 1, bits);
 
   GDK_NOTE (MISC, g_print ("gdk_bitmap_create_from_data: %dx%d = %#x\n",
-                          width, height, private->xwindow));
+                          width, height, GDK_DRAWABLE_XID (pixmap)));
 
   g_free (bits);
 
   private->colormap = NULL;
-  gdk_xid_table_insert (&private->xwindow, pixmap);
+  gdk_xid_table_insert (&GDK_DRAWABLE_XID (pixmap), pixmap);
 
   return pixmap;
 }
@@ -397,13 +370,13 @@ gdk_pixmap_create_from_data (GdkWindow   *window,
   GdkGC *gc = gdk_gc_new (result);
   gdk_gc_set_foreground (gc, fg);
   gdk_gc_set_background (gc, bg);
-  gdk_draw_pixmap (result, gc, source, 0, 0, 0, 0, width, height);
-  gdk_pixmap_unref (source);
+  gdk_draw_drawable (result, gc, source, 0, 0, 0, 0, width, height);
+  gdk_drawable_unref (source);
   gdk_gc_unref (gc);
 
   GDK_NOTE (MISC, g_print ("gdk_pixmap_create_from_data: %dx%dx%d = %#x\n",
                           width, height, depth,
-                          ((GdkPixmapPrivate *) result)->xwindow));
+                          GDK_DRAWABLE_XID (result)));
   return result;
 }
 
@@ -646,13 +619,13 @@ gdk_xpm_destroy_notify (gpointer data)
 }
 
 static GdkPixmap *
-_gdk_pixmap_create_from_xpm (GdkWindow  *window,
+_gdk_pixmap_create_from_xpm (GdkWindow   *window,
                             GdkColormap *colormap,
-                            GdkBitmap **mask,
-                            GdkColor   *transparent_color,
-                            gchar *   (*get_buf) (enum buffer_op op,
-                                                  gpointer       handle),
-                            gpointer    handle)
+                            GdkBitmap  **mask,
+                            GdkColor    *transparent_color,
+                            gchar *    (*get_buf) (enum buffer_op op,
+                                                   gpointer       handle),
+                            gpointer     handle)
 {
   GdkPixmap *pixmap = NULL;
   GdkImage *image = NULL;
@@ -672,12 +645,12 @@ _gdk_pixmap_create_from_xpm (GdkWindow  *window,
     g_warning ("Creating pixmap from xpm with NULL window and colormap");
 
   if (window == NULL)
-    window = (GdkWindow *)&gdk_root_parent;
+    window = gdk_parent_root;
 
   if (colormap == NULL)
     {
-      colormap = gdk_window_get_colormap (window);
-      visual = gdk_window_get_visual (window);
+      colormap = gdk_drawable_get_colormap (window);
+      visual = gdk_drawable_get_visual (window);
     }
   else
     visual = ((GdkColormapPrivate *)colormap)->visual;
@@ -794,14 +767,14 @@ _gdk_pixmap_create_from_xpm (GdkWindow  *window,
          strncpy (pixel_str, &buffer[n], cpp);
          pixel_str[cpp] = 0;
          ns = 0;
-       
+         
          color = g_hash_table_lookup (color_hash, pixel_str);
-       
+         
          if (!color) /* screwed up XPM file */
            color = fallbackcolor;
-       
+         
          gdk_image_put_pixel (image, xcnt, ycnt, color->color.pixel);
-       
+         
          if (mask && color->transparent)
            {
              if (cnt < xcnt)
@@ -817,7 +790,7 @@ _gdk_pixmap_create_from_xpm (GdkWindow  *window,
  error:
 
   if (mask)
-    gdk_gc_destroy (gc);
+    gdk_gc_unref (gc);
 
   if (image != NULL)
     {
@@ -830,8 +803,8 @@ _gdk_pixmap_create_from_xpm (GdkWindow  *window,
       gc = gdk_gc_new (pixmap);
       gdk_gc_set_foreground (gc, transparent_color);
       gdk_draw_image (pixmap, gc, image, 0, 0, 0, 0, image->width, image->height);
-      gdk_gc_destroy (gc);
-      gdk_image_destroy (image);
+      gdk_gc_unref (gc);
+      gdk_image_unref (image);
     }
   else if (color_info)
     gdk_xpm_destroy_notify (color_info);
@@ -915,7 +888,7 @@ gdk_pixmap_create_from_xpm (GdkWindow  *window,
                            const gchar *filename)
 {
   return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
-                                      transparent_color, filename);
+                                             transparent_color, filename);
 }
 
 struct mem_handle
@@ -972,8 +945,7 @@ GdkPixmap*
 gdk_pixmap_foreign_new (guint32 anid)
 {
   GdkPixmap *pixmap;
-  GdkWindowPrivate *window_private;
-  GdkWindowPrivate *private;
+  GdkDrawablePrivate *private;
   HBITMAP xpixmap;
   SIZE size;
   unsigned int x_ret, y_ret, w_ret, h_ret, bw_ret, depth_ret;
@@ -984,8 +956,6 @@ gdk_pixmap_foreign_new (guint32 anid)
 
   /* set the pixmap to the passed in value */
   xpixmap = (HBITMAP) anid;
-  /* get the root window */
-  window_private = &gdk_root_parent;
 
   /* get information about the BITMAP to fill in the structure for
      the gdk window */
@@ -994,66 +964,15 @@ gdk_pixmap_foreign_new (guint32 anid)
   h_ret = size.cy;
 
   /* allocate a new gdk pixmap */
-  private = g_new(GdkWindowPrivate, 1);
-  pixmap = (GdkPixmap *)private;
+  pixmap = gdk_win32_pixmap_alloc ();
+  private = (GdkDrawablePrivate *) pixmap;
 
-  private->window_type = GDK_WINDOW_PIXMAP;
-  private->xwindow = xpixmap;
+  GDK_DRAWABLE_WIN32DATA (pixmap)->xid = xpixmap;
   private->colormap = NULL;
-  private->parent = NULL;
-  private->x = 0;
-  private->y = 0;
   private->width = w_ret;
   private->height = h_ret;
-  private->resize_count = 0;
-  private->ref_count = 1;
-  private->destroyed = 0;
-
-  gdk_xid_table_insert(&private->xwindow, pixmap);
-
-  return pixmap;
-}
 
-GdkPixmap*
-gdk_pixmap_ref (GdkPixmap *pixmap)
-{
-  GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap;
-  g_return_val_if_fail (pixmap != NULL, NULL);
+  gdk_xid_table_insert(&GDK_DRAWABLE_XID (pixmap), pixmap);
 
-  private->ref_count += 1;
   return pixmap;
 }
-
-void
-gdk_pixmap_unref (GdkPixmap *pixmap)
-{
-  GdkWindowPrivate *private = (GdkWindowPrivate *)pixmap;
-  g_return_if_fail(pixmap != NULL);
-
-  private->ref_count -= 1;
-
-  GDK_NOTE (MISC, g_print ("gdk_pixmap_unref: %#x %d%s\n",
-                          private->xwindow, private->ref_count,
-                          (private->ref_count == 0 ? " freeing" : "")));
-
-  if (private->ref_count == 0)
-    {
-      if (!DeleteObject (private->xwindow))
-       g_warning ("gdk_pixmap_unref: DeleteObject failed");
-      gdk_xid_table_remove (private->xwindow);
-      g_dataset_destroy (private);
-      g_free (private);
-    }
-}
-
-GdkBitmap *
-gdk_bitmap_ref (GdkBitmap *bitmap)
-{
-  return (GdkBitmap *)gdk_pixmap_ref ((GdkPixmap *)bitmap);
-}
-
-void
-gdk_bitmap_unref (GdkBitmap *bitmap)
-{
-  gdk_pixmap_unref ((GdkPixmap *)bitmap);
-}