#include Once "window9.bi"

Function GetFocus() As Hwnd
	
	Dim As HWND hwRet
	
	Dim As Glist Ptr pList = gtk_window_list_toplevels() , pL
	
	Dim As GDKWindow Ptr gdkwin = gdk_screen_get_active_window(gdk_screen_get_default())
	
	pL = pList
	
	While pL
		
		If gtk_widget_get_window(Cast(hwnd,pL->Data)) = gdkwin Then
			
			hwRet = Cast(HWND,pL->Data)
			
			Exit While
			
		Endif
		
		pL = pL->Next
		
	Wend
	
	g_list_free(pList)
	
	If hwRet Then
		
		Dim As Hwnd hWidget = gtk_window_get_focus (Cast(Any Ptr,hwRet))
		
		If hWidget Then
			
			Return hWidget
			
		Else
			
			Return hwRet
			
		Endif
		
	Endif
	
End Function

Function SetFocus(hw As Hwnd) As Hwnd
	
	Function = GetFocus()
	
	Dim As HWND mHwnd = gtk_widget_get_toplevel(hw)
	
	gtk_window_present(Cast(Any Ptr , mHwnd))
	
	If mHwnd = hw Orelse mHwnd = 0 Then
		
		gtk_window_set_focus(Cast(Any Ptr , mHwnd),0)
		
	Else
		
		gtk_widget_grab_focus(hw)
		
	Endif
	
End Function