#include once "window9.bi"

Dim Shared g_tbBmp(0 To 14) As Byte = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}
Dim Shared g_tbAddBmp As TBADDBITMAP
Dim Shared g_tbb As TBBUTTON
Dim Shared g_ToolBarHWND As HWND

Function CreateToolBar(ByVal nID As Integer, ByVal Style As Integer, ByVal ExStyle As Integer) As Integer Export
	Dim hwndToolBar As HWND=CreateWindowEx( ExStyle, TOOLBARCLASSNAME, 0,WS_CHILD Or WS_VISIBLE Or Style, 0,0,0,0, Cast(HWND,win9GetCurrent()),0,0,0)
	g_ToolBarHWND = hwndToolBar
	SendMessage( hwndToolBar, TB_BUTTONSTRUCTSIZE, Len( g_tbb ), 0 )
	g_tbAddBmp.hInst = HINST_COMMCTRL
	g_tbAddBmp.nID   = nID
	SendMessage( hwndToolBar, TB_ADDBITMAP, 0, CInt( @g_tbAddBmp ) )
	Return Cast(Integer,hwndToolBar)
End Function

sub ToolBarToolTip(ByVal Hwnd As hwnd, ByVal buttonID As Integer, ByRef sToolTipS As USTRING)  Export 
	if len(sToolTipS) = 0 then exit sub
	#ifdef UNICODE
		SetProp(Hwnd, Str(buttonID), cast(any ptr , *sToolTipS))
	#else	
		SetProp(Hwnd, Str(buttonID), StrPtr(sToolTipS))
	#EndIf  
End Sub

Function ToolBarStandardButton(ByVal hwndToolBar As Integer, _
								ByVal buttonID    As Integer, _
								ByVal IndexImage  As Integer, _
								ByRef sText     As USTRING, _
								ByVal IndexButton As Integer, _
								ByVal State       As Integer, _
								ByVal Style       As Integer) As Integer Export
	With g_tbb
		.iBitmap   = g_tbBmp(Indeximage)
		.fsState   = State
		.fsStyle   = Style
		.idCommand =  buttonID
		.dwData    = 0
		if len(sText) then
			#ifdef UNICODE
				.iString = Cast(Integer, *sText)
			#else	
				.iString = Cast(Integer,StrPtr(sText))
			#EndIf
		endif	
		Function=SendMessage( Cast(hwnd,hwndToolBar), TB_INSERTBUTTON, indexButton, CInt( @g_tbb ) )
	End With
	SendMessage(Cast(hwnd,hwndToolBar),TB_AUTOSIZE,0,0)
End Function

Function ToolBarSeparator(ByVal hwndToolBar As Integer,ByVal IndexButton As Integer,ByVal  iFlagDrawSeparator as Long = 1) As Integer Export
	With g_tbb
		.iBitmap   = g_tbBmp(IndexButton)
		.fsState   = TBSTATE_ENABLED
		.fsStyle   = TBSTYLE_SEP
		.idCommand =  0
		.dwData    = 0
		.iString = 0
		Function=SendMessage(Cast(hwnd, hwndToolBar), TB_INSERTBUTTON,IndexButton , CInt( @g_tbb ) )
	End With
	SendMessage(Cast(hwnd,hwndToolBar),TB_AUTOSIZE,0,0)
End Function

Function ToolBarImageButton(ByVal hwndToolBar As Integer, _
							ByVal buttonID    As Integer, _
							ByVal ImageID_    As Integer, _
							ByRef sText     As USTRING, _
							ByVal IndexButton As Integer, _
							ByVal State       As Integer, _
							ByVal Style       As Integer, _
							ByVal SizeIcon As Integer) As Integer Export
	Dim As hImageList hImageList = GetProp(Cast(HWND,hwndToolBar),"HIMAGELISTTOOLBAR")
	If hImageList = 0 Then
		hImageList = ImageList_Create(SizeIcon,SizeIcon,ILC_COLOR32,1,1)
		SetProp(Cast(HWND,hwndToolBar),"HIMAGELISTTOOLBAR",hImageList)
		SendMessage(Cast(hwnd,hwndToolBar), TB_SETIMAGELIST, 0, Cast(LPARAM,hImageList))
	EndIf
	Dim As Integer ppoos=ImageList_Add(hImageList,Cast(hbitmap,ImageID_),0)
	With g_tbb
		.iBitmap   = ppoos
		.fsState   = State
		.fsStyle   = Style
		.idCommand = buttonID
		.dwData    = 0
		if len(sText) then
			#ifdef UNICODE
				.iString = Cast(Integer, *sText)
			#else	
				.iString = Cast(Integer,StrPtr(sText))
			#EndIf
		endif
	End With
	Return SendMessage( Cast(hwnd,hwndToolBar), TB_INSERTBUTTON,IndexButton , CInt( @g_tbb ) )
	SendMessage(Cast(hwnd,hwndToolBar),TB_AUTOSIZE,0,0)
End Function
