typedef struct		{
	HWND hWnd, hWndOwner;
	WNDPROC defWndProc;
	char *texte;
} TT_CTRL_INFO;

#define MAX_TOOLTIPINFO 100
TT_CTRL_INFO tooltip_ttci[MAX_TOOLTIPINFO];
int tooltip_nbreToolTips=0;

HWND tooltip_g_hwndTT;
HHOOK tooltip_g_hhk;

BOOL tooltip_CreateTooltip()		{
	tooltip_g_hwndTT = CreateWindowEx(0, TOOLTIPS_CLASS, (LPSTR) NULL,
		TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, NULL, (HMENU) NULL, hInst, NULL);

	if (tooltip_g_hwndTT == NULL)
		return FALSE;
	else		{
		//Il y a pas mal de trucs intéressants (comme le marge, etc.)
		SendMessage(tooltip_g_hwndTT, TTM_SETMAXTIPWIDTH, 0, 400);
//		SendMessage(tooltip_g_hwndTT, TTM_SETDELAYTIME, TTDT_INITIAL, MAKELONG(0,0));
		return TRUE;
	}
}

void tooltip_DestroyTooltip()		{
	DestroyWindow(tooltip_g_hwndTT);
}

TT_CTRL_INFO *tooltip_FindControl(HWND hDlg)		{
	int i;
	for (i=0;i<tooltip_nbreToolTips;i++)			{
		if (tooltip_ttci[i].hWnd == hDlg)
			return &tooltip_ttci[i];
	}
	return NULL;
}

void tooltip_RemoveControl(HWND hDlg)			{
	int i;
	TOOLINFO ti;

	for (i=0;i<tooltip_nbreToolTips;i++)			{
		if (tooltip_ttci[i].hWnd == hDlg)
			break;
	}
	if (i>=tooltip_nbreToolTips)
		return;

	ti.cbSize = sizeof(TOOLINFO); 
	ti.hwnd = tooltip_ttci[i].hWndOwner;
	ti.uId = (UINT) hDlg; 
	SendMessage(tooltip_g_hwndTT, TTM_DELTOOL, 0, 
		(LPARAM) (LPTOOLINFO) &ti);
	
	if (i<tooltip_nbreToolTips-1)
		memmove(tooltip_ttci+i, tooltip_ttci+i+1, sizeof(tooltip_ttci[0])*(tooltip_nbreToolTips-i-1));
	tooltip_nbreToolTips--;
}

LRESULT CALLBACK ToolTipWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	TT_CTRL_INFO *ttci;
	WNDPROC wdp;
	ttci=tooltip_FindControl(hDlg);
	wdp=ttci->defWndProc;
	switch (message)
	{
		case WM_MOUSEMOVE:
		case WM_LBUTTONDOWN:
		case WM_LBUTTONUP:
			MSG msg;
			msg.lParam = lParam;
			msg.wParam = wParam;
			msg.message = message;
			msg.hwnd = hDlg;
			SendMessage (tooltip_g_hwndTT, TTM_RELAYEVENT, 0, (LPARAM)(LPMSG)&msg);
			break;
		case WM_NCDESTROY:
			tooltip_RemoveControl(hDlg);
			break;
	}
	return CallWindowProc (wdp, hDlg, message, wParam, lParam);
}

void tooltip_AddControl(HWND hWndOwner, UINT ctlId, char *texte)		{
	TOOLINFO ti;
	HWND hCtrl;

	//Genre tooltip_AddControl(hDlg, IDC_CHECK1, "Test");
	hCtrl = GetDlgItem(hWndOwner, ctlId);
	ti.cbSize = sizeof(TOOLINFO); 
	ti.uFlags = TTF_IDISHWND; 
	ti.hwnd = hWndOwner; 
	ti.uId = (UINT) hCtrl; 
	ti.hinst = 0; 
	ti.lpszText = LPSTR_TEXTCALLBACK; 
	SendMessage(tooltip_g_hwndTT, TTM_ADDTOOL, 0, 
		(LPARAM) (LPTOOLINFO) &ti);

	tooltip_ttci[tooltip_nbreToolTips].defWndProc = (WNDPROC) GetWindowLong (hCtrl, GWL_WNDPROC);
	SetWindowLong(hCtrl, GWL_WNDPROC, (LONG)ToolTipWndProc);
	tooltip_ttci[tooltip_nbreToolTips].hWnd = hCtrl;
	tooltip_ttci[tooltip_nbreToolTips].hWndOwner = hWndOwner;
	tooltip_ttci[tooltip_nbreToolTips].texte = texte;
	tooltip_nbreToolTips++;
}


void tooltip_OnWMNotify(LPARAM lParam)				{
    LPTOOLTIPTEXT lpttt;
	TT_CTRL_INFO *ttci;

    if ((((LPNMHDR) lParam)->code) == TTN_NEEDTEXT) {
		ttci = tooltip_FindControl((HWND) ((LPNMHDR) lParam)->idFrom);
        lpttt = (LPTOOLTIPTEXT) lParam;
		lpttt->lpszText = ttci->texte;
    }
	return;
}

