#include once "window9.bi"

Function PanelGadget(ByVal gadget As Long, _
					ByVal x As Long, ByVal y As Long, _
					ByVal w As Long, ByVal h As Long, _
					ByVal SizeIcon As Long, _
					ByVal Style As Long) As HWND Export
	Dim As RECT rcClient
	Dim As HWND hwndTab
	Dim As hImageList hImageList
	
	GetClientRect(Cast(HWND,win9GetCurrent), @rcClient)
	If w=0 Or h=0 Then
		w = rcClient.right
		h = rcClient.bottom
	EndIf
	hwndTab = CreateWindow(WC_TABCONTROL, "", WS_CHILD or WS_CLIPSIBLINGS Or WS_VISIBLE Or Style, x,y,w,h, Cast(HWND,win9GetCurrent), Cast(HMENU,CInt(gadget)), 0, NULL)
	win9AddNewGadget (gadget,hwndTab)
    dim as hwnd hwParent = Cast(HWND,win9GetCurrent())
    for i as Long = 0 to 1000
        dim as USTRING sz = getclassname_(hwParent)
        if instr(sz , "CLASSCLASS_") andalso getparent(hwParent) = 0 then
            if getprop(hwParent , "tabstopenable") then
                win9AddTabStop(hwParent , gadget)
            EndIf
            exit for
        else
            hwParent = getparent(hwParent)
            if hwParent = 0 then exit for
        EndIf
    Next
	If win9GetGadgetFont()<>0 Then
		SendMessage(Cast(hwnd,hwndTab), WM_SETFONT,win9GetGadgetFont(), 0)
	EndIf
	hImageList = ImageList_Create(SizeIcon, SizeIcon, ILC_COLOR32, 1, 1)
	SetProp(hwndTab, "addparam60000", hImageList)
	SendMessage(Cast(hwnd,hwndTab), TCM_SETIMAGELIST, 0, Cast(LPARAM, hImageList))
	Return hwndTab
End Function

Function AddPanelGadgetItem(ByVal gadget As Long, ByVal Item As Long, ByRef sText As USTRING, ByVal ImageID As HBITMAP, ByVal flag As Long) As HWND Export
	Dim As RECT re
	Dim As Integer iPos=-1
	If ImageID<>0 Then
		iPos = ImageList_Add(Cast(HIMAGELIST,GetHimageCombo_(gadget)),Cast(HBITMAP,ImageID),0)
	EndIf
	Var t1 = OpenWindow("",0,0,0,0,0)
	SetProp(t1,"panelevent",Cast(Handle,1))
	Dim As  TCITEM tie
	tie.mask = TCIF_TEXT or TCIF_IMAGE Or TCIF_PARAM
	#ifdef UNICODE
		tie.pszText = cast(any ptr , *sText)
	#else	
		tie.pszText = StrPtr(sText)
	#EndIf	
	tie.iImage  = iPos
	tie.lParam  = Cast(LPARAM,t1)
	TabCtrl_InsertItem(GadgetID(gadget), item, @tie)
	SetParent(t1, GadgetID(gadget))
	SetWindowLong(t1, GWL_STYLE,  WS_CHILD)
	SendMessage(GadgetID(gadget),TCM_GETITEMRECT,item,Cast(LPARAM,@re))
	ResizeWindow(t1,1,re.bottom+1,GadgetWidth(gadget)-3,GadgetHeight(gadget)-26)
	If flag=0 Then
		UseGadgetList(GetParent(GadgetID(gadget)))
	EndIf
	DeleteObject(Cast(HGDIOBJ,iPos))
	Return t1
End Function

Sub DeleteItemPanelGadget(ByVal gadget As Long, ByVal item As Long)  Export
	Dim As TC_ITEM TC_ITEM_
	TC_ITEM_.mask=TCIF_PARAM
	SendMessage(GadgetID(gadget),TCM_GETITEM,item,Cast(LPARAM,@TC_ITEM_))
	DestroyWindow(Cast(HWND,TC_ITEM_.LPARAm))
	SendMessage(GadgetID(gadget),TCM_DELETEITEM,item,0)
End Sub
