#include Once "window9.bi"

Type TStatusField
	
	label As Hwnd
	
	sep As hwnd
	
	Width As Long
	
	iAllWidth As Long
	
	x_Label As Long
	
	iFEnd As Byte
	
End Type

Function Statusbargadget(iGadget As Long , sTextInput As String , iStyle1 As Long  = 0, iStyle2 As Long = 0) As HWND Export
	
	Dim As HWND statusbar , vBox , mainBox , eventbox  , hBox , text
	
	Dim As Long iWidthWin , iHeightWin
	
	Dim As ListT Ptr pListTemp
	
	Dim As String sText
	
	If Len(sTextInput) = 0 Then
		
		sText = " "
		
	Else
		
		sText = sTextInput
		
	Endif
	
	Freegadget(iGadget)
	
	If g_object_get_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "IDstattusBar" )  Then Return 0 ' only one statusbar may be
	
	Updateinfoxserver()
	
	gtk_window_get_size ( GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd) , @iWidthWin , @iHeightWin )	
	
	pGlobalTypeWindow9->OptGroup = 0 ' reset Group OptionGadget
	
	pListTemp = Cast(ListT Ptr,pGlobalTypeWindow9->ListWinAndContainers->findNodeFunc(Cint(pGlobalTypeWindow9->CurentHwnd)))
	
	mainBox = Cast(hwnd , pListTemp->anyTwoData)	
	
	vbox = gtk_fixed_new()
	
	gtk_container_add (GTK_CONTAINER(mainBox), vbox)	
	
	statusbar = gtk_frame_new (0)		
	
	pGlobalTypeWindow9->ListGadgets->addNodeSub(iGadget , Cint(statusbar) ,  27)
	
	gtk_widget_set_size_request(statusbar, iWidthWin , 25)
	
	gtk_fixed_put(GTK_FIXED(vbox), statusbar, 0 , iHeightWin - 25)
	
	gtk_frame_set_shadow_type(Cast(Any Ptr,statusbar) , GTK_SHADOW_IN)
	
	hBox = gtk_fixed_new()
	
	text = gtk_label_new (sText)
	
	gtk_misc_set_alignment(Cast(Any Ptr,text) , 0 , 0.5)
	
	gtk_container_add( Cast(Any Ptr,statusbar), hbox )
	
	gtk_container_add( Cast(Any Ptr,hbox) ,  text)
	
	tree_add_node(iGadget,pGlobalTypeWindow9->treeNumberGadgets)
	
	Dim As Node Ptr fNode = tree_find_node(iGadget,pGlobalTypeWindow9->treeNumberGadgets)	
	
	'g_signal_connect(G_OBJECT(eventbox), "event", G_CALLBACK (@EventsframeGadgetAllFunc), @(fNode->x))
	
	Var ihID = g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "size-allocate", G_CALLBACK (@StatusBarSizeSub), statusbar)
	
	g_object_set_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "IDstattusBar"  , Cast(Any Ptr,ihID))
	
	g_object_set_data (Cast(Any Ptr, statusbar), "W_SIZE" , Cast(Any Ptr,Cint(iWidthWin)))
	
	g_object_set_data (Cast(Any Ptr, statusbar), "H_SIZE" , Cast(Any Ptr,Cint(25)))	
	
	gtk_widget_show(vbox)
	
	gtk_widget_show(statusbar)
	
	gtk_widget_show(hbox)
	
	gtk_widget_show(text)
	
	g_object_set_data (Cast(Any Ptr,statusbar), "statusbarchild" , text)
	
	g_object_set_data (Cast(Any Ptr,statusbar), "statusbarParent" , pGlobalTypeWindow9->CurentHwnd)
	
	gtk_widget_modify_font(text , pGlobalTypeWindow9->fontCurent)
	
	If iStyle1 And SBT_TOOLTIPS Then
		
		g_object_set_data (Cast(Any Ptr,statusbar), "tooltip" , Cast(Any Ptr,1))
		
	Endif
	
	Return statusbar
	
End Function

Function Setstatusbarfield (iGadget As Long , iNfield As Long , iWidth As Long ,  sTextInput As String) As hwnd Export
	
	Dim As Any Ptr statusbar = Gadgetid(iGadget)
	
	Dim As Any Ptr win = g_object_get_data (Cast(Any Ptr,statusbar), "statusbarParent")
	
	Dim As GSList Ptr pStoreField = g_object_get_data (Cast(Any Ptr,statusbar), "statusbarlist")
	
	Dim As Any Ptr childtext = g_object_get_data (Cast(Any Ptr,statusbar), "statusbarchild")
	
	Dim As Any Ptr hBox	 = gtk_widget_get_parent(childtext)
	
	Dim As Long iHeightStatusbar = Cint(g_object_get_data (Cast(Any Ptr, statusbar), "H_SIZE"))
	
	Dim As String sText
	
	If Len(sTextInput) = 0 Then
		
		sText = " "
		
	Else
		
		sText = sTextInput
		
	Endif
	
	If pStoreField = 0 Then
		
		gtk_label_set_text(Cast(Any Ptr,childtext) ,0)
		
		gtk_widget_set_size_request(childtext, 1 ,	1)
		
		Dim tsf As TStatusField Ptr = New TStatusField
		
		if tsf then
			
			pStoreField = g_slist_append(0, tsf)
			
			g_object_set_data (Cast(Any Ptr,statusbar), "statusbarlist" , pStoreField)
			
			If iWidth = -1 Then
				
				tsf->Width = Windowclientwidth(win)
				
				tsf->iFEnd = 1
				
				tsf->iAllWidth = 4000
				
			Else
				
				tsf->Width = iWidth
				
				tsf->iAllWidth = tsf->Width
				
			Endif 
			
			tsf->label = gtk_label_new (sText)
			
			gtk_misc_set_alignment(Cast(Any Ptr,tsf->label) , 0 , 0.5)
			
			Setstatusbarfield = tsf->label
			
			tsf->x_Label = 0
			
			gtk_widget_set_size_request(tsf->label, tsf->Width-2 , iHeightStatusbar)
			
			gtk_fixed_put(GTK_FIXED(hbox),tsf->label , 0 , 0)
			
			tsf->sep = gtk_vseparator_new()
			
			gtk_widget_set_size_request(tsf->sep , 2 , iHeightStatusbar)	
			
			gtk_widget_modify_font(tsf->label , pGlobalTypeWindow9->fontCurent)	
			
			If tsf->iFEnd = 0 Then
				
				gtk_fixed_put(GTK_FIXED(hbox),tsf->sep , tsf->Width-2 , 0)	
				
			Else
				
				gtk_fixed_put(GTK_FIXED(hbox),tsf->sep , 4000 , 0)			
				
			Endif
			
			gtk_widget_show(tsf->label)
			
			gtk_widget_show(tsf->sep)
			
		else
			
			print "Allocated memory error in Setstatusbarfield!"
			
		endif
		
	Else
		
		Dim As Long iCountElement = g_slist_length(pStoreField)
		
		If iCountElement <= iNfield Then
			
			Dim tsf As TStatusField Ptr = New TStatusField
			
			if tsf then
				
				g_slist_append(pStoreField, tsf)
				
				tsf->x_Label = Cast(TStatusField Ptr,(g_slist_nth(pStoreField , iCountElement - 1))->Data)->iAllWidth
				
				If iWidth = -1 Then
					
					tsf->Width = Windowclientwidth(win) - Cast(TStatusField Ptr,(g_slist_nth(pStoreField , iCountElement - 1))->Data)->iAllWidth
					
					tsf->iFEnd = 1
					
					tsf->iAllWidth = 4000
					
				Else
					
					tsf->Width = Iif(tsf->x_Label < iWidth , iWidth - tsf->x_Label , tsf->x_Label+3)
					
					tsf->iAllWidth = tsf->x_Label+tsf->Width
					
				Endif 
				
				tsf->label = gtk_label_new (sText)
				
				gtk_misc_set_alignment(Cast(Any Ptr,tsf->label) , 0 , 0.5)
				
				Setstatusbarfield = tsf->label
				
				gtk_widget_set_size_request(tsf->label, tsf->Width-2 , iHeightStatusbar)
				
				gtk_fixed_put(GTK_FIXED(hbox),tsf->label , tsf->x_Label , 0)
				
				tsf->sep = gtk_vseparator_new()				
				
				gtk_widget_set_size_request(tsf->sep , 2 , iHeightStatusbar)			
				
				If 	tsf->iFEnd = 0 Then
					
					gtk_fixed_put(GTK_FIXED(hbox),tsf->sep , tsf->x_Label+tsf->Width-2 , 0)	
					
				Else
					
					gtk_fixed_put(GTK_FIXED(hbox),tsf->sep , 4000, 0)	
					
				Endif	
				
				gtk_widget_modify_font(tsf->label , pGlobalTypeWindow9->fontCurent)	
				
				gtk_widget_show(tsf->label)
				
				gtk_widget_show(tsf->sep)
				
			else
				
				print "Allocated memory error in Setstatusbarfield!"
				
			endif
			
		Elseif iCountElement > iNfield Andalso iNfield >= 0 Then ' edit
			
			Dim As Long iOldW , iChange
			
			Dim As GSList Ptr pTemp = g_slist_nth(pStoreField , iNfield)
			
			Dim As TStatusField Ptr pData = pTemp->Data
			
			iOldW = pData->Width
			
			If iWidth = -1 Then
				' if only 1 field
				If iCountElement = 1 Then 
					
					pData->Width = Windowclientwidth(win) 
					
					pData->iFEnd = 1
					
					pData->iAllWidth = 4000		
					
				Else
					
					pData->Width = Windowclientwidth(win) - Cast(TStatusField Ptr,(g_slist_nth(pStoreField , iNfield - 1))->Data)->iAllWidth
					
					pData->iFEnd = 1
					
					pData->iAllWidth = 4000					
					
				Endif
				
				gtk_widget_set_size_request(pData->label, pData->Width-2 , iHeightStatusbar)
				
				gtk_fixed_move(GTK_FIXED(hbox),pData->sep , 4000 , 0)
				
				gtk_label_set_text(Cast(Any Ptr,pData->label) , Strptr(sText))	
				
			Else
				
				Dim As GSList Ptr pTempPrev
				
				Dim As TStatusField Ptr pDataPrev
				
				If iNfield > 0 Then
					
					pTempPrev = g_slist_nth(pStoreField , iNfield-1)
					
					pDataPrev = pTempPrev->Data
					
					pData->Width = Iif(pDataPrev->iAllWidth < iWidth , iWidth - pDataPrev->iAllWidth , pDataPrev->iAllWidth+3)
					
				Else
					
					pData->Width = iWidth
					
				Endif
				
				iChange = pData->Width - iOldW
				
				pData->iAllWidth = pData->x_Label+pData->Width
				
				gtk_widget_set_size_request(pData->label, pData->Width-2 , iHeightStatusbar)
				
				gtk_fixed_move(GTK_FIXED(hbox), pData->sep , pData->x_Label+pData->Width-2 , 0)	
				
				gtk_label_set_text(Cast(Any Ptr,pData->label) , Strptr(sText))		
				
				If iNfield < iCountElement-1 Then 
					
					For i As Long = iNfield+1 To iCountElement-1
						
						Dim As GSList Ptr pTemp = g_slist_nth(pStoreField , i)
						
						Dim As TStatusField Ptr pData = pTemp->Data
						
						pData->x_Label  += iChange
						
						pData->iAllWidth += iChange
						
						gtk_fixed_move(GTK_FIXED(hbox), pData->Label , pData->x_Label , 0)
						
						If 	pData->iFEnd = 0 Then
							
							gtk_fixed_move(GTK_FIXED(hbox),pData->sep , pData->x_Label+pData->Width-2 , 0)						
							
						Else
							
							gtk_fixed_move(GTK_FIXED(hbox),pData->sep , 4000 , 0)
							
						Endif	
						
					Next
					
				Endif	
				
			Endif					
			
		Endif
		
	Endif
	
End Function

Sub Tooltipstatusbar(iGadget As Long , iField As Long , sTextInput As String) Export
	
	Dim As String sText
	
	If Len(sTextInput) = 0 Then
		
		sText = " "
		
	Else
		
		sText = sTextInput
		
	Endif
	
	Dim As Any Ptr statusbar = Gadgetid(iGadget)
	
	If g_object_get_data (Cast(Any Ptr,statusbar), "tooltip") Then
		
		Dim As GSList Ptr pTemp = g_object_get_data (Cast(Any Ptr,statusbar), "statusbarlist") 
		
		pTemp = g_slist_nth(pTemp , iField)
		
		Dim tsf As TStatusField Ptr = pTemp->Data
		
		If tsf Then
			
			gtk_widget_set_tooltip_text(Cast(Any Ptr , tsf->label) , sText)
			
		Endif		
		
	Endif
	
End Sub

Sub disconnect( statusbar As Any Ptr) 
	
	Dim As hwnd win = g_object_get_data (Cast(Any Ptr,statusbar), "statusbarParent" )
	
	If win Then
		
		Dim As Integer ihID = Cint(g_object_get_data (Cast(Any Ptr, win), "IDstattusBar" ))
		
		If ihID Then
			
			g_signal_handler_disconnect(win , ihID)
			
			g_object_set_data (Cast(Any Ptr, win), "IDstattusBar" , 0 )
			
			Dim As GSList Ptr pTemp = g_object_get_data (Cast(Any Ptr,statusbar), "statusbarlist") , pTemp2 = pTemp
			
			While pTemp
				
				if pTemp->Data then Delete Cast(TStatusField Ptr,pTemp->Data)
				
				pTemp = g_slist_next(pTemp)
				
			Wend
			
			g_slist_free(pTemp2)
			
		Endif
		
	Endif
	
End Sub