#include Once "window9.bi"

Sub Setimagegadget(iGadget As Long , pImage As Hbitmap) Export
	
	Dim As ListT Ptr pTemp = pGlobalTypeWindow9->ListGadgets->findNodeFunc(iGadget)
	
	If pTemp <> 0 Andalso pImage <> 0 Then
		
		Dim As hwnd pWidget = Cast(hwnd,pTemp->anyTwoData)
		
		Dim As Long iClass = pTemp->anyThreeData
		
		Dim As Long width_ , height_
		
		If iClass = 3 Then			
			
			Dim As GdkPixbuf Ptr pix2
			
			Dim As gint wIm ,hIm , xTemp  , yTemp , wTemp , hTemp 
			
			Dim As GtkRequisition  req
			
			gtk_widget_size_request(pWidget, @req)
			
			width_ = req.Width	
			
			height_ = req.Height		
			
			wIm = gdk_pixbuf_get_width(pImage)
			
			hIm = gdk_pixbuf_get_height(pImage)
			
			If (wIm + 10) >  width_ Then
				
				wTemp = width_ - 10
				
				xTemp = wIm\2 - wTemp\2
				
			Else 
				
				wTemp = wIm	
				
				xTemp = 0
				
			Endif
			
			If (hIm + 10) >  height_ Then
				
				hTemp = height_ - 10
				
				yTemp = hIm\2 - hTemp\2
				
			Else 
				
				hTemp = hIm	
				
				yTemp = 0
				
			Endif
			
			pix2 = gdk_pixbuf_new_subpixbuf ( pImage , xTemp , yTemp , wTemp , hTemp)
			
			Dim As Any Ptr SavedImage = gtk_button_get_image(Cast(Any Ptr,pWidget))	
			
			If SavedImage = 0 Then
	
				gtk_button_set_image(Cast(Any Ptr,pWidget) , gtk_image_new_from_pixbuf(pix2))
				
			Else
			
				gtk_image_set_from_pixbuf(Cast(Any Ptr,SavedImage) , pix2)

			Endif
				
			g_object_unref (pix2)
			
			g_object_set_data (Cast(Any Ptr, pWidget), "BITMAP" , Cast(Any Ptr,pImage))	
			
		Elseif iClass = 4 Then	
			
			Dim As ListT Ptr pListTemp
			
			Dim As GdkPixbuf Ptr  pix2  , delpix
			
			Dim As gint wIm ,hIm
			
			pListTemp = Cast(ListT Ptr,pGlobalTypeWindow9->ListWinAndContainers->findNodeFunc(Cint(pWidget)))
			
			Dim As Any Ptr SavedImage = Cast(Any Ptr , pListTemp->anyTwoData)
			
			pix2 = gdk_pixbuf_copy (pImage)
			
			wIm = gdk_pixbuf_get_width(pImage)
			
			hIm = gdk_pixbuf_get_height(pImage)	
			
			Dim As GtkRequisition  req
			
			gtk_widget_size_request(pWidget, @req)
			
			If req.Width <> wIm	Orelse 	req.Height <> hIm Then
	
				gtk_widget_set_size_request( pWidget, wIm ,	hIm)

			Endif			
			
			If SavedImage = 0 Then
	
				Dim As Any Ptr imTemp  = gtk_image_new_from_pixbuf(pix2)
				
				gtk_container_add (GTK_CONTAINER (pWidget), imTemp)
				
				pListTemp->anyTwoData = Cint(imTemp)
				
				gtk_widget_show(imTemp)
	
			Else
			
				delpix = gtk_image_get_pixbuf(SavedImage)
		
				g_object_unref (delpix)	
				
				gtk_image_set_from_pixbuf(Cast(Any Ptr,SavedImage) , pix2)				

			Endif
			
			g_object_set_data (Cast(Any Ptr, pWidget), "BITMAP" , Cast(Any Ptr,pImage))		
							
		Endif
		
		gtk_widget_show(pWidget)
		
	Endif		
	
End Sub