#include ONCE "window9.bi"

' In the list: 1 - number event , 2 - hwnd , 3 - Group , 4 closure
function AddKeyboardShortcut(Hwnd as HWND , Syskey as integer , Shortcut as integer , IDevent as integer) as any ptr export
	
	if pGlobalTypeWindow9->ListShortCut = 0 then
		
		pGlobalTypeWindow9->ListShortCut = new LinList
		
		if pGlobalTypeWindow9->ListShortCut = 0 then
			
			print "Allocated memory error in AddKeyboardShortcut!"
			
			return 0
			
		EndIf
		
	EndIf
	
	dim as Long iFlagGroup 
	
	dim as GtkAccelGroup ptr accel_group
	
	dim pTemp as ListT ptr = pGlobalTypeWindow9->ListShortCut->firstNode
	
	do while pTemp
		
		if pTemp->anyTwoData = Hwnd Then
			
			iFlagGroup = 1
			
			accel_group = cast(any ptr,pTemp->anyThreeData)
			
			if IDevent = pTemp->anyOneData then
				
				gtk_accel_group_disconnect(cast(any ptr,pTemp->anyThreeData) , cast(any ptr,pTemp->anyFourData))
				
				pGlobalTypeWindow9->ListShortCut->removeNodeSub(pTemp->anyOneData)
				
				exit do
				
			EndIf
			
		end if
		
		pTemp = pTemp->nextNode		
		
	Loop
	
	if iFlagGroup = 0 then
		
		accel_group = gtk_accel_group_new()
		
		gtk_window_add_accel_group(GTK_WINDOW(hwnd), accel_group)
		
	EndIf
	
	if Shortcut then
		
		dim as GClosure ptr closure = g_cclosure_new(cast(any ptr,@AcceleratorPressedFunc), cast(any ptr,IDevent) , 0)
		
		gtk_accel_group_connect(accel_group, Shortcut , Syskey, 0, closure)	
		
		pGlobalTypeWindow9->ListShortCut->addNodeSub(IDevent , cint(hwnd) , cint(accel_group) , cint(closure))		
		
	EndIf
	
	return accel_group
	
End Function

sub DeleteAllKeyboardShortcut(hwnd as Hwnd) export
	
	if pGlobalTypeWindow9->ListShortCut then
		
		dim pTemp as ListT ptr = pGlobalTypeWindow9->ListShortCut->firstNode 
		
		dim pTemp2 as ListT ptr
		
		do while pTemp
			
			pTemp2 = pTemp->nextNode
			
			if pTemp->anyTwoData = Hwnd Then
				
				gtk_accel_group_disconnect(cast(any ptr,pTemp->anyThreeData) , cast(any ptr,pTemp->anyFourData))
				
				pGlobalTypeWindow9->ListShortCut->removeNodeSub(pTemp->anyOneData)
				
			end if
			
			pTemp = pTemp2		
			
		Loop		
		
	EndIf
	
End Sub