PROGRAM(GdkPixmap);
INHERIT(GdkDrawable);
// This class creates a GDK.Pixmap from either an GDK.Image or
// Image.image object.  <p> The GDK.Pixmap object can be used in a lot
// of different GTK widgets.  The most notable is the 'GTK.Pixmap'
// widget.
GdkImage *pgtk_pixmap_setup( struct object *o, int *free)
{
  if(get_gdkobject( o, Image ))
  {
    return get_gdkobject( o, Image );
  }
  else
  {
    *free=1;
    return gdkimage_from_pikeimage( o, GDK_IMAGE_FASTEST, 0 );
  }
}

void pgtk__pixmap_draw( GdkImage *i )
{
  GdkGC *gc;
  if(THIS->extra_data)
    gc = THIS->extra_data;
  else
    THIS->extra_data = gc = gdk_gc_new( (GdkWindow *)THIS->obj );
  gdk_draw_image( (GdkWindow *)THIS->obj, gc, i, 0, 0, 0, 0,
		  i->width, i->height);
}

FUNCTION(create, "function(object:void)")
ARGS(object(GDK.Image)|object(Image.image));
NAME_ARGS(image);
// Create a new GDK.Pixmap object. 
// Argument is a GDK.Image object or a Image.image object
{
  GdkImage *i;
  int f = 0;
  struct object *o;
  get_all_args("create", args, "%o", &o );
  i = pgtk_pixmap_setup( o, &f );

  THIS->obj = (void *)gdk_pixmap_new( 0, i->width, i->height, i->depth );
  if(!THIS->obj) 
  {
    if(f) gdk_image_destroy( i );
    error("Failed to create pixmap.\n");
  }
  pgtk__pixmap_draw( i );
  if(f) gdk_image_destroy( i );
  /*one extra ref for the internal gdk object..*/
  fp->current_object->refs++;
}

FUNCTION(set, "function(object:object)");
RETURNS(GDK.Image);
ARGS(object(GDK.Image)|object(Image.image));
NAME_ARGS(image);
// Argument is a GDK.Image object or an Image.image object.
// It is much faster to use an gdkImage object, especially one 
// allocated in shared memory. This is only an issue if you are
// going to change the contents of the pixmap often, toggling between
// a small number of images.
{
  struct object *o;
  int f = 0;
  GdkImage *i;
  get_all_args("set", args, "%o", &o );
  pgtk__pixmap_draw( i=pgtk_pixmap_setup( o, &f ) );
  if(f) gdk_image_destroy( i );

  RETURN_THIS();
}

FUNCTION(destroy, "function(void:void)");
// Destructor. Destroys the pixmap.
{
  if(THIS->obj)
    gdk_pixmap_unref( (void *)THIS->obj );
  THIS->obj = 0;

  if(THIS->extra_data)
    gdk_gc_destroy( (void *)THIS->extra_data );
  THIS->extra_data = 0;
}
