#include once "window9.bi"

Dim Shared As TOOLINFO g_Tool

Function GadgetToolTip(ByVal gadget As long, ByRef text As USTRING, ByVal Mygadget As Long) As Integer Export
	Var  fbguiTemp=CreateWindowEx(WS_EX_TOPMOST,"Tooltips_Class32",0,1 Or 2 Or WS_POPUP,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,Cast(HWND,GadgetID(gadget)),0,0,0)
	If Mygadget<>0 Then
		win9AddNewGadget (Mygadget,fbguiTemp)
		win9AddColor(fbguiTemp,GetSysColor(COLOR_WINDOW),GetSysColor(COLOR_WINDOWTEXT))
	EndIf
	SendMessage(fbguiTemp,TTM_SETDELAYTIME,TTDT_INITIAL,500)
	SendMessage(fbguiTemp,TTM_SETMAXTIPWIDTH,0,160)
	#ifdef UNICODE
		g_Tool.cbSize=TTTOOLINFOW_V2_SIZE
	#else
		g_Tool.cbSize=SizeOf(TOOLINFO)
	#EndIf
	g_Tool.uFlags=TTF_SUBCLASS Or TTF_IDISHWND
	g_Tool.hwnd=GadgetID(gadget)
	g_Tool.uID=cast(UINT_PTR,GadgetID(gadget))
	g_Tool.hInst=0
	#ifdef UNICODE
		g_Tool.lpszText=cast(any ptr , *Text)
	#else
		g_Tool.lpszText=StrPtr(Text)
	#EndIf
	SendMessage(fbguiTemp,TTM_ADDTOOL,0,Cast(LPARAM,@g_Tool))
	If win9GetGadgetFont()<>0 Then
		SendMessage(fbguiTemp,WM_SETFONT,win9GetGadgetFont(),0)
	EndIf
	Return Cast(Integer,@g_Tool)
End Function

Sub DelToolTip(ByVal GadgetPARRENT As long,ByVal gadget As long) Export
	g_Tool.hwnd=GadgetID(GadgetPARRENT)
	g_Tool.uId=cast(UINT_PTR,GadgetID(GadgetPARRENT))
	SendMessage(GadgetID(gadget),TTM_DELTOOL,0,Cast(LPARAM,@g_Tool))
End Sub

Function GetToolTipText(ByVal GadgetPARRENT As long,ByVal gadget As Long) As USTRING Export	
	Dim As USTRING sRet
	Dim As wstring Ptr pzs = Callocate(500)
	if pzs = 0 then return sRet
	g_Tool.lpszText = cast(any ptr,pzs)
	g_Tool.hwnd=GadgetID(GadgetPARRENT)
	g_Tool.uId=cast(UINT_PTR,GadgetID(GadgetPARRENT))
	SendMessage(GadgetID(gadget),TTM_GETTEXT,0,Cast(LPARAM,@g_Tool))
	If g_Tool.lpszText Then
		sRet = *g_Tool.lpszText
	EndIf
	DeAllocate(pzs)
	Return sRet	
End Function

Sub DisableToolTip(ByVal gadget As Long,ByVal state As Long)  Export
	SendMessage(GadgetID(gadget),TTM_ACTIVATE,iif(state,0,1),0)
End sub

Sub SetToolTipText(ByVal GadgetPARRENT As Long,ByVal gadget As long,ByRef text As USTRING) Export
	g_Tool.hwnd     = GadgetID(GadgetPARRENT)
	g_Tool.uId      = cast(UINT_PTR,GadgetID(GadgetPARRENT))
	#ifdef UNICODE
		g_Tool.lpszText = cast(any ptr , *Text)
	#else
		g_Tool.lpszText = StrPtr(Text)
	#EndIf
	SendMessage(GadgetID(gadget),TTM_UPDATETIPTEXT,0,Cast(LPARAM,@g_Tool))
End Sub
