#include once "window9.bi"

Extern As Long ERROR_COLOR

Dim Shared As HBRUSH hBrush
Dim Shared As Long COLBK,COLTE


#Ifndef FBIDSCROLLBAR
#Define FBIDSCROLLBAR 5
#EndIf

#Ifndef FBIDCOMBOLBOX
#Define FBIDCOMBOLBOX 13
#EndIf

' LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

private Function wndproc(byval hwnd As HWND, byval msg As UINT, byval wparam As WPARAM, byval lparam As LPARAM) As LRESULT
	Select Case Msg
		Case WM_CTLCOLORSTATIC,WM_CTLCOLOREDIT,WM_CTLCOLORLISTBOX,WM_CTLCOLORSCROLLBAR
		Var GETGadgetClassCOMBO_and_SCROLLBAR=win9GetGadgetClass(Cast(HWND,lparam))
		If GETGadgetClassCOMBO_and_SCROLLBAR=FBIDCOMBOLBOX Then
			SetBkColor(Cast(HDC,wparam),COLBK)
			SetTextColor(Cast(HDC,wParam),COLTE)
			Return Cast(Integer,hBrush) ' !!!
		Else
			Static As  COLORREF ccooll
			Static As hwnd hwwn
			Static As Integer umsggg
			Dim As Integer hh
			If GETGadgetClassCOMBO_and_SCROLLBAR=FBIDSCROLLBAR And GetGadgetColor(lparam,4)=0 Then
				hh = &hf0f0f0
			Else
				hh=GetGadgetColor(lparam,3)
			EndIf
			If ERROR_COLOR=0 Then
				If hh<>ccooll Or hwwn<>hWnd Or umsggg<>msg Then
					ccooll=hh
					hwwn=hWnd
					umsggg=msg
					DeleteObject(hBrush)
					hBrush= CreateSolidBrush(ccooll)
					SetBkColor(Cast(HDC,wparam),ccooll)
				Else
					SetBkColor(Cast(HDC,wparam),ccooll)
				EndIf
				SetTextColor(Cast(HDC,wParam),Cast(COLORREF,GetGadgetColor(lparam,4)))
			Else
				ERROR_COLOR=0
			EndIf
		EndIf

		Return Cast(integer,hBrush) ' !!!
		Case WM_ACTIVATE,WM_SETFOCUS,WM_KILLFOCUS,WM_CLOSE,WM_NOTIFY, _
			WM_KEYDOWN,WM_KEYUP,WM_SYSKEYDOWN,WM_SYSKEYUP, _
			WM_COMMAND,WM_SYSCOMMAND,WM_TIMER,WM_HSCROLL,WM_VSCROLL, _
			WM_MENUSELECT,WM_DROPFILES, _
			WM_MOUSEMOVE, _
			WM_LBUTTONUP,WM_LBUTTONDBLCLK, _
			WM_RBUTTONUP,WM_RBUTTONDBLCLK, _
			WM_MBUTTONUP,WM_MBUTTONDBLCLK, _ 
			WM_HOTKEY,WM_USER,WM_PAINT,WM_VKEYTOITEM
			SendMessage(GetParent(Hwnd),msg,wparam,lparam)
		Case WM_LBUTTONDOWN,WM_RBUTTONDOWN,WM_MBUTTONDOWN
			SetProp(GetParent(Hwnd),"eventmousebutton",cast(handle,1))
			SendMessage(GetParent(Hwnd),msg,wparam,lparam)
	End Select
	Return DefWindowProc(hwnd,msg,wparam,lparam)
End Function

Dim Shared NameClass As USTRING
Function RegClassContainer() As BOOL
	Dim As WNDCLASSEX wc
	Static n As Integer
	NameClass="ContainerGadget" & n
	n+=1
	With wc
		.cbSize=SizeOf(WNDCLASSEX)
		.style=CS_HREDRAW Or CS_VREDRAW
		.lpfnWndProc=@wndproc
		.hInstance=GetModuleHandle(0)
		.hbrBackground=Cast(HBRUSH,COLOR_WINDOW)
		#ifdef UNICODE
			.lpszClassName=*NameClass
		#else	
			.lpszClassName=StrPtr(NameClass)
		#EndIf
		.hCursor = LoadCursor(0,IDC_ARROW)
	End With
	If RegisterClassEx(@wc)=0 Then
		Print "Register error, press any key"
		Return 0
	EndIf
	Return 1
End Function

Function ContainerGadget(ByVal gadget As Long, _
                         ByVal x As Long, ByVal y As Long, _
                         ByVal w As Long, ByVal h As Long, _
                         ByVal par As Long) As HWND Export
	RegClassContainer()
	#ifdef UNICODE
		Dim gID As HWND=CreateWindowEx(par,*NameClass,"", WS_VISIBLE Or WS_CHILD, x,y,w,h, Cast(HWND,win9GetCurrent()), Cast(HMENU,CInt(gadget)),0,0)	
	#else	
		Dim gID As HWND=CreateWindowEx(par,strptr(NameClass),"", WS_VISIBLE Or WS_CHILD, x,y,w,h, Cast(HWND,win9GetCurrent()), Cast(HMENU,CInt(gadget)),0,0)
	#EndIf
	win9AddNewGadget(gadget,gID)
	win9AddColor(gID,&hf0f0f0,0)
	UseGadgetList(gID)
	Return gID
End Function
