xref: /reactos/dll/win32/comctl32/tooltips.c (revision 3a49e26f)
1 /*
2  * Tool tip control
3  *
4  * Copyright 1998, 1999 Eric Kohl
5  * Copyright 2004 Robert Shearman
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTES
22  *
23  * This code was audited for completeness against the documented features
24  * of Comctl32.dll version 6.0 on Sep. 08, 2004, by Robert Shearman.
25  *
26  * Unless otherwise noted, we believe this code to be complete, as per
27  * the specification mentioned above.
28  * If you discover missing features or bugs please note them below.
29  *
30  * TODO:
31  *   - Custom draw support.
32  *   - Animation.
33  *   - Links.
34  *   - Messages:
35  *     o TTM_ADJUSTRECT
36  *     o TTM_GETTITLEA
37  *     o TTM_GETTTILEW
38  *     o TTM_POPUP
39  *   - Styles:
40  *     o TTS_NOANIMATE
41  *     o TTS_NOFADE
42  *     o TTS_CLOSE
43  *
44  * Testing:
45  *   - Run tests using Waite Group Windows95 API Bible Volume 2.
46  *     The second cdrom (chapter 3) contains executables activate.exe,
47  *     curtool.exe, deltool.exe, enumtools.exe, getinfo.exe, getiptxt.exe,
48  *     hittest.exe, needtext.exe, newrect.exe, updtext.exe and winfrpt.exe.
49  *
50  *   Timer logic.
51  *
52  * One important point to remember is that tools don't necessarily get
53  * a WM_MOUSEMOVE once the cursor leaves the tool, an example is when
54  * a tool sets TTF_IDISHWND (i.e. an entire window is a tool) because
55  * here WM_MOUSEMOVEs only get sent when the cursor is inside the
56  * client area.  Therefore the only reliable way to know that the
57  * cursor has left a tool is to keep a timer running and check the
58  * position every time it expires.  This is the role of timer
59  * ID_TIMERLEAVE.
60  *
61  *
62  * On entering a tool (detected in a relayed WM_MOUSEMOVE) we start
63  * ID_TIMERSHOW, if this times out and we're still in the tool we show
64  * the tip.  On showing a tip we start both ID_TIMERPOP and
65  * ID_TIMERLEAVE.  On hiding a tooltip we kill ID_TIMERPOP.
66  * ID_TIMERPOP is restarted on every relayed WM_MOUSEMOVE.  If
67  * ID_TIMERPOP expires the tool is hidden and ID_TIMERPOP is killed.
68  * ID_TIMERLEAVE remains running - this is important as we need to
69  * determine when the cursor leaves the tool.
70  *
71  * When ID_TIMERLEAVE expires or on a relayed WM_MOUSEMOVE if we're
72  * still in the tool do nothing (apart from restart ID_TIMERPOP if
73  * this is a WM_MOUSEMOVE) (ID_TIMERLEAVE remains running).  If we've
74  * left the tool and entered another one then hide the tip and start
75  * ID_TIMERSHOW with time ReshowTime and kill ID_TIMERLEAVE.  If we're
76  * outside all tools hide the tip and kill ID_TIMERLEAVE.  On Relayed
77  * mouse button messages hide the tip but leave ID_TIMERLEAVE running,
78  * this again will let us keep track of when the cursor leaves the
79  * tool.
80  *
81  *
82  * infoPtr->nTool is the tool the mouse was on on the last relayed MM
83  * or timer expiry or -1 if the mouse was not on a tool.
84  *
85  * infoPtr->nCurrentTool is the tool for which the tip is currently
86  * displaying text for or -1 if the tip is not shown.  Actually this
87  * will only ever be infoPtr-nTool or -1, so it could be changed to a
88  * BOOL.
89  *
90  */
91 
92 
93 
94 #include <stdarg.h>
95 #include <string.h>
96 
97 #include "windef.h"
98 #include "winbase.h"
99 #include "wine/unicode.h"
100 #include "wingdi.h"
101 #include "winuser.h"
102 #include "winnls.h"
103 #include "commctrl.h"
104 #include "comctl32.h"
105 #include "wine/debug.h"
106 
107 WINE_DEFAULT_DEBUG_CHANNEL(tooltips);
108 
109 static HICON hTooltipIcons[TTI_ERROR+1];
110 
111 typedef struct
112 {
113     UINT      uFlags;
114     UINT      uInternalFlags;
115     HWND      hwnd;
116     BOOL      bNotifyUnicode;
117     UINT_PTR  uId;
118     RECT      rect;
119     HINSTANCE hinst;
120     LPWSTR      lpszText;
121     LPARAM      lParam;
122 } TTTOOL_INFO;
123 
124 
125 typedef struct
126 {
127     HWND     hwndSelf;
128     WCHAR      szTipText[INFOTIPSIZE];
129     BOOL     bActive;
130     BOOL     bTrackActive;
131     UINT     uNumTools;
132     COLORREF   clrBk;
133     COLORREF   clrText;
134     HFONT    hFont;
135     HFONT    hTitleFont;
136     INT      xTrackPos;
137     INT      yTrackPos;
138     INT      nMaxTipWidth;
139     INT      nTool; /* tool that mouse was on on last relayed mouse move */
140     INT      nCurrentTool;
141     INT      nTrackTool;
142     INT      nReshowTime;
143     INT      nAutoPopTime;
144     INT      nInitialTime;
145     RECT     rcMargin;
146     BOOL     bToolBelow;
147     LPWSTR   pszTitle;
148     HICON    hTitleIcon;
149 
150     TTTOOL_INFO *tools;
151 } TOOLTIPS_INFO;
152 
153 #define ID_TIMERSHOW   1    /* show delay timer */
154 #define ID_TIMERPOP    2    /* auto pop timer */
155 #define ID_TIMERLEAVE  3    /* tool leave timer */
156 
157 
158 #define TOOLTIPS_GetInfoPtr(hWindow) ((TOOLTIPS_INFO *)GetWindowLongPtrW (hWindow, 0))
159 
160 /* offsets from window edge to start of text */
161 #define NORMAL_TEXT_MARGIN 2
162 #define BALLOON_TEXT_MARGIN (NORMAL_TEXT_MARGIN+8)
163 /* value used for CreateRoundRectRgn that specifies how much
164  * each corner is curved */
165 #ifdef __REACTOS__
166 #define BALLOON_ROUNDEDNESS 16
167 #define BALLOON_STEMHEIGHT 18
168 #define BALLOON_STEMWIDTH 18
169 #define BALLOON_STEMINDENT 16
170 #else
171 #define BALLOON_ROUNDEDNESS 20
172 #define BALLOON_STEMHEIGHT 13
173 #define BALLOON_STEMWIDTH 10
174 #define BALLOON_STEMINDENT 20
175 #endif // __REACTOS__
176 
177 #define BALLOON_ICON_TITLE_SPACING 8 /* horizontal spacing between icon and title */
178 #define BALLOON_TITLE_TEXT_SPACING 8 /* vertical spacing between icon/title and main text */
179 #define ICON_HEIGHT 16
180 #define ICON_WIDTH  16
181 
182 #define MAX_TEXT_SIZE_A 80 /* maximum retrieving text size by ANSI message */
183 
184 static LRESULT CALLBACK
185 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uId, DWORD_PTR dwRef);
186 
187 
188 static inline BOOL TOOLTIPS_IsCallbackString(LPCWSTR str, BOOL isW)
189 {
190     if (isW)
191       return str == LPSTR_TEXTCALLBACKW;
192     else
193       return (LPCSTR)str == LPSTR_TEXTCALLBACKA;
194 }
195 
196 static inline UINT_PTR
197 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
198 {
199     UINT i;
200     for (i = 0; i <= TTI_ERROR; i++)
201         if (hTooltipIcons[i] == hIcon)
202             return i;
203     return (UINT_PTR)hIcon;
204 }
205 
206 static void
207 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
208 {
209     NONCLIENTMETRICSW nclm;
210 
211     infoPtr->clrBk   = comctl32_color.clrInfoBk;
212     infoPtr->clrText = comctl32_color.clrInfoText;
213 
214     DeleteObject (infoPtr->hFont);
215     nclm.cbSize = sizeof(nclm);
216     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
217     infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
218 
219     DeleteObject (infoPtr->hTitleFont);
220     nclm.lfStatusFont.lfWeight = FW_BOLD;
221     infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
222 }
223 
224 /* Custom draw routines */
225 static void
226 TOOLTIPS_customdraw_fill(const TOOLTIPS_INFO *infoPtr, NMTTCUSTOMDRAW *lpnmttcd,
227                          HDC hdc, const RECT *rcBounds, UINT uFlags)
228 {
229     ZeroMemory(lpnmttcd, sizeof(NMTTCUSTOMDRAW));
230     lpnmttcd->uDrawFlags = uFlags;
231     lpnmttcd->nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
232     lpnmttcd->nmcd.hdr.code     = NM_CUSTOMDRAW;
233     if (infoPtr->nCurrentTool != -1) {
234         TTTOOL_INFO *toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
235         lpnmttcd->nmcd.hdr.idFrom = toolPtr->uId;
236     }
237     lpnmttcd->nmcd.hdc = hdc;
238     lpnmttcd->nmcd.rc = *rcBounds;
239     /* FIXME - dwItemSpec, uItemState, lItemlParam */
240 }
241 
242 static inline DWORD
243 TOOLTIPS_notify_customdraw (DWORD dwDrawStage, NMTTCUSTOMDRAW *lpnmttcd)
244 {
245     LRESULT result;
246     lpnmttcd->nmcd.dwDrawStage = dwDrawStage;
247 
248     TRACE("Notifying stage %d, flags %x, id %x\n", lpnmttcd->nmcd.dwDrawStage,
249           lpnmttcd->uDrawFlags, lpnmttcd->nmcd.hdr.code);
250 
251     result = SendMessageW(GetParent(lpnmttcd->nmcd.hdr.hwndFrom), WM_NOTIFY,
252                           0, (LPARAM)lpnmttcd);
253 
254     TRACE("Notify result %x\n", (unsigned int)result);
255 
256     return result;
257 }
258 
259 static void
260 TOOLTIPS_Refresh (const TOOLTIPS_INFO *infoPtr, HDC hdc)
261 {
262     RECT rc;
263     INT oldBkMode;
264     HFONT hOldFont;
265     HBRUSH hBrush;
266     UINT uFlags = DT_EXTERNALLEADING;
267     HRGN hRgn = NULL;
268     DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
269     NMTTCUSTOMDRAW nmttcd;
270     DWORD cdmode;
271 
272     if (infoPtr->nMaxTipWidth > -1)
273 	uFlags |= DT_WORDBREAK;
274     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)
275 	uFlags |= DT_NOPREFIX;
276     GetClientRect (infoPtr->hwndSelf, &rc);
277 
278     hBrush = CreateSolidBrush(infoPtr->clrBk);
279 
280     oldBkMode = SetBkMode (hdc, TRANSPARENT);
281     SetTextColor (hdc, infoPtr->clrText);
282     hOldFont = SelectObject (hdc, infoPtr->hFont);
283 
284     /* Custom draw - Call PrePaint once initial properties set up     */
285     /* Note: Contrary to MSDN, CDRF_SKIPDEFAULT still draws a tooltip */
286     TOOLTIPS_customdraw_fill(infoPtr, &nmttcd, hdc, &rc, uFlags);
287     cdmode = TOOLTIPS_notify_customdraw(CDDS_PREPAINT, &nmttcd);
288     uFlags = nmttcd.uDrawFlags;
289 
290     if (dwStyle & TTS_BALLOON)
291     {
292         /* create a region to store result into */
293         hRgn = CreateRectRgn(0, 0, 0, 0);
294 
295         GetWindowRgn(infoPtr->hwndSelf, hRgn);
296 
297         /* fill the background */
298         FillRgn(hdc, hRgn, hBrush);
299         DeleteObject(hBrush);
300         hBrush = NULL;
301     }
302     else
303     {
304         /* fill the background */
305         FillRect(hdc, &rc, hBrush);
306         DeleteObject(hBrush);
307         hBrush = NULL;
308     }
309 
310     if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
311     {
312         /* calculate text rectangle */
313         rc.left   += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
314         rc.top    += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
315         rc.right  -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
316         rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
317         if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
318 
319         if (infoPtr->pszTitle)
320         {
321             RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
322             int height;
323             BOOL icon_present;
324             HFONT prevFont;
325 
326             /* draw icon */
327             icon_present = infoPtr->hTitleIcon &&
328                 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
329                            ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
330             if (icon_present)
331                 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
332 
333             rcTitle.bottom = rc.top + ICON_HEIGHT;
334 
335             /* draw title text */
336             prevFont = SelectObject (hdc, infoPtr->hTitleFont);
337             height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
338             SelectObject (hdc, prevFont);
339             rc.top += height + BALLOON_TITLE_TEXT_SPACING;
340         }
341     }
342     else
343     {
344         /* calculate text rectangle */
345         rc.left   += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
346         rc.top    += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
347         rc.right  -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
348         rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
349     }
350 
351     /* draw text */
352 #ifdef __REACTOS__
353     uFlags |= DT_EXPANDTABS;
354 #endif
355     DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
356 
357     /* Custom draw - Call PostPaint after drawing */
358     if (cdmode & CDRF_NOTIFYPOSTPAINT) {
359         TOOLTIPS_notify_customdraw(CDDS_POSTPAINT, &nmttcd);
360     }
361 
362     /* be polite and reset the things we changed in the dc */
363     SelectObject (hdc, hOldFont);
364     SetBkMode (hdc, oldBkMode);
365 
366     if (dwStyle & TTS_BALLOON)
367     {
368         /* frame region because default window proc doesn't do it */
369         INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
370         INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
371 
372         hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
373         FrameRgn(hdc, hRgn, hBrush, width, height);
374     }
375 
376     if (hRgn)
377         DeleteObject(hRgn);
378 }
379 
380 static void TOOLTIPS_GetDispInfoA(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
381 {
382     NMTTDISPINFOA ttnmdi;
383 
384     /* fill NMHDR struct */
385     ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
386     ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
387     ttnmdi.hdr.idFrom = toolPtr->uId;
388     ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
389     ttnmdi.lpszText = ttnmdi.szText;
390     ttnmdi.uFlags = toolPtr->uFlags;
391     ttnmdi.lParam = toolPtr->lParam;
392 
393     TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
394     SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
395 
396     if (IS_INTRESOURCE(ttnmdi.lpszText)) {
397         LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
398                buffer, INFOTIPSIZE);
399         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
400             toolPtr->hinst = ttnmdi.hinst;
401             toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
402         }
403     }
404     else if (ttnmdi.lpszText == 0) {
405         buffer[0] = '\0';
406     }
407     else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
408         Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
409         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
410             toolPtr->hinst = 0;
411             toolPtr->lpszText = NULL;
412             Str_SetPtrW(&toolPtr->lpszText, buffer);
413         }
414     }
415     else {
416         ERR("recursive text callback\n");
417         buffer[0] = '\0';
418     }
419 
420 #ifndef __REACTOS_
421     /* no text available - try calling parent instead as per native */
422     /* FIXME: Unsure if SETITEM should save the value or not        */
423     if (buffer[0] == 0x00) {
424 
425         SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
426 
427         if (IS_INTRESOURCE(ttnmdi.lpszText)) {
428             LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
429                    buffer, INFOTIPSIZE);
430         } else if (ttnmdi.lpszText &&
431                    ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
432             Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
433         }
434     }
435 #endif
436 }
437 
438 static void TOOLTIPS_GetDispInfoW(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
439 {
440     NMTTDISPINFOW ttnmdi;
441 
442     /* fill NMHDR struct */
443     ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
444     ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
445     ttnmdi.hdr.idFrom = toolPtr->uId;
446     ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
447     ttnmdi.lpszText = ttnmdi.szText;
448     ttnmdi.uFlags = toolPtr->uFlags;
449     ttnmdi.lParam = toolPtr->lParam;
450 
451     TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
452     SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
453 
454     if (IS_INTRESOURCE(ttnmdi.lpszText)) {
455         LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
456                buffer, INFOTIPSIZE);
457         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
458             toolPtr->hinst = ttnmdi.hinst;
459             toolPtr->lpszText = ttnmdi.lpszText;
460         }
461     }
462     else if (ttnmdi.lpszText == 0) {
463         buffer[0] = '\0';
464     }
465     else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
466         Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
467         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
468             toolPtr->hinst = 0;
469             toolPtr->lpszText = NULL;
470             Str_SetPtrW(&toolPtr->lpszText, buffer);
471         }
472     }
473     else {
474         ERR("recursive text callback\n");
475         buffer[0] = '\0';
476     }
477 
478 #ifndef __REACTOS__
479     /* no text available - try calling parent instead as per native */
480     /* FIXME: Unsure if SETITEM should save the value or not        */
481     if (buffer[0] == 0x00) {
482 
483         SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
484 
485         if (IS_INTRESOURCE(ttnmdi.lpszText)) {
486             LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
487                    buffer, INFOTIPSIZE);
488         } else if (ttnmdi.lpszText &&
489                    ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
490             Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
491         }
492     }
493 #endif
494 
495 }
496 
497 static void
498 TOOLTIPS_GetTipText (const TOOLTIPS_INFO *infoPtr, INT nTool, WCHAR *buffer)
499 {
500     TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
501 
502     if (IS_INTRESOURCE(toolPtr->lpszText)) {
503 	/* load a resource */
504 	TRACE("load res string %p %x\n",
505 	       toolPtr->hinst, LOWORD(toolPtr->lpszText));
506 	if (!LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText), buffer, INFOTIPSIZE))
507 	    buffer[0] = '\0';
508     }
509     else if (toolPtr->lpszText) {
510 	if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
511 	    if (toolPtr->bNotifyUnicode)
512 		TOOLTIPS_GetDispInfoW(infoPtr, toolPtr, buffer);
513 	    else
514 		TOOLTIPS_GetDispInfoA(infoPtr, toolPtr, buffer);
515 	}
516 	else {
517 	    /* the item is a usual (unicode) text */
518 	    lstrcpynW (buffer, toolPtr->lpszText, INFOTIPSIZE);
519 	}
520     }
521     else {
522 	/* no text available */
523         buffer[0] = '\0';
524     }
525 
526     if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)) {
527         WCHAR *ptrW;
528         if ((ptrW = strchrW(buffer, '\t')))
529             *ptrW = 0;
530     }
531 
532     TRACE("%s\n", debugstr_w(buffer));
533 }
534 
535 
536 static void
537 TOOLTIPS_CalcTipSize (const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
538 {
539     HDC hdc;
540     HFONT hOldFont;
541     DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
542     UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
543     RECT rc = {0, 0, 0, 0};
544     SIZE title = {0, 0};
545 
546     if (infoPtr->nMaxTipWidth > -1) {
547 	rc.right = infoPtr->nMaxTipWidth;
548 	uFlags |= DT_WORDBREAK;
549     }
550     if (style & TTS_NOPREFIX)
551 	uFlags |= DT_NOPREFIX;
552     TRACE("%s\n", debugstr_w(infoPtr->szTipText));
553 
554     hdc = GetDC (infoPtr->hwndSelf);
555     if (infoPtr->pszTitle)
556     {
557         RECT rcTitle = {0, 0, 0, 0};
558         TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
559         if (infoPtr->hTitleIcon)
560         {
561             title.cx = ICON_WIDTH;
562             title.cy = ICON_HEIGHT;
563         }
564         if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
565         hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
566         DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
567         SelectObject (hdc, hOldFont);
568         title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
569         title.cx += (rcTitle.right - rcTitle.left);
570     }
571     hOldFont = SelectObject (hdc, infoPtr->hFont);
572 #ifdef __REACTOS__
573     uFlags |= DT_EXPANDTABS;
574 #endif
575     DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
576     SelectObject (hdc, hOldFont);
577     ReleaseDC (infoPtr->hwndSelf, hdc);
578 
579     if ((style & TTS_BALLOON) || infoPtr->pszTitle)
580     {
581         lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
582                        infoPtr->rcMargin.left + infoPtr->rcMargin.right;
583         lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
584                        infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
585                        BALLOON_STEMHEIGHT;
586     }
587     else
588     {
589         lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
590                        infoPtr->rcMargin.left + infoPtr->rcMargin.right;
591         lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
592                        infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
593     }
594 }
595 
596 
597 static void
598 TOOLTIPS_Show (TOOLTIPS_INFO *infoPtr, BOOL track_activate)
599 {
600     TTTOOL_INFO *toolPtr;
601     HMONITOR monitor;
602     MONITORINFO mon_info;
603     RECT rect;
604     SIZE size;
605     NMHDR  hdr;
606     int ptfx = 0;
607     DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
608     INT nTool, current;
609 
610     if (track_activate)
611     {
612         if (infoPtr->nTrackTool == -1)
613         {
614             TRACE("invalid tracking tool %d\n", infoPtr->nTrackTool);
615             return;
616         }
617         nTool = infoPtr->nTrackTool;
618     }
619     else
620     {
621         if (infoPtr->nTool == -1)
622         {
623             TRACE("invalid tool %d\n", infoPtr->nTool);
624             return;
625         }
626         nTool = infoPtr->nTool;
627     }
628 
629     TRACE("Show tooltip pre %d, %p\n", nTool, infoPtr->hwndSelf);
630 
631     current = infoPtr->nCurrentTool;
632     if (!track_activate)
633         infoPtr->nCurrentTool = infoPtr->nTool;
634 
635     TOOLTIPS_GetTipText (infoPtr, nTool, infoPtr->szTipText);
636 
637     if (infoPtr->szTipText[0] == '\0')
638     {
639         infoPtr->nCurrentTool = current;
640         return;
641     }
642 
643     toolPtr = &infoPtr->tools[nTool];
644     TOOLTIPS_CalcTipSize (infoPtr, &size);
645 
646     TRACE("Show tooltip %d, %s, size %d x %d\n", nTool, debugstr_w(infoPtr->szTipText),
647         size.cx, size.cy);
648 
649     if (track_activate && (toolPtr->uFlags & TTF_TRACK))
650     {
651         rect.left = infoPtr->xTrackPos;
652         rect.top  = infoPtr->yTrackPos;
653         ptfx = rect.left;
654 
655         if (toolPtr->uFlags & TTF_CENTERTIP)
656         {
657             rect.left -= (size.cx / 2);
658             if (!(style & TTS_BALLOON))
659                 rect.top  -= (size.cy / 2);
660         }
661         if (!(infoPtr->bToolBelow = (infoPtr->yTrackPos + size.cy <= GetSystemMetrics(SM_CYSCREEN))))
662             rect.top -= size.cy;
663 
664         if (!(toolPtr->uFlags & TTF_ABSOLUTE))
665         {
666             if (style & TTS_BALLOON)
667                 rect.left -= BALLOON_STEMINDENT;
668             else
669             {
670                 RECT rcTool;
671 
672                 if (toolPtr->uFlags & TTF_IDISHWND)
673                     GetWindowRect ((HWND)toolPtr->uId, &rcTool);
674                 else
675                 {
676                     rcTool = toolPtr->rect;
677                     MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
678                 }
679 
680                 /* smart placement */
681                 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
682                     (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
683                     rect.left = rcTool.right;
684             }
685         }
686     }
687     else
688     {
689         if (toolPtr->uFlags & TTF_CENTERTIP)
690         {
691 		RECT rc;
692 
693             if (toolPtr->uFlags & TTF_IDISHWND)
694                 GetWindowRect ((HWND)toolPtr->uId, &rc);
695             else {
696                 rc = toolPtr->rect;
697                 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
698             }
699             rect.left = (rc.left + rc.right - size.cx) / 2;
700             if (style & TTS_BALLOON)
701             {
702                 ptfx = rc.left + ((rc.right - rc.left) / 2);
703 
704                 /* CENTERTIP ballon tooltips default to below the field
705                  * if they fit on the screen */
706                 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
707                 {
708                     rect.top = rc.top - size.cy;
709                     infoPtr->bToolBelow = FALSE;
710                 }
711                 else
712                 {
713                     infoPtr->bToolBelow = TRUE;
714                     rect.top = rc.bottom;
715                 }
716                 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
717             }
718             else
719             {
720                 rect.top  = rc.bottom + 2;
721                 infoPtr->bToolBelow = TRUE;
722             }
723         }
724         else
725         {
726             GetCursorPos ((LPPOINT)&rect);
727             if (style & TTS_BALLOON)
728             {
729                 ptfx = rect.left;
730                 if(rect.top - size.cy >= 0)
731                 {
732                     rect.top -= size.cy;
733                     infoPtr->bToolBelow = FALSE;
734                 }
735                 else
736                 {
737                     infoPtr->bToolBelow = TRUE;
738                     rect.top += 20;
739                 }
740                 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
741             }
742             else
743             {
744 		    rect.top += 20;
745 		    infoPtr->bToolBelow = TRUE;
746             }
747         }
748     }
749 
750     TRACE("pos %d - %d\n", rect.left, rect.top);
751 
752     rect.right = rect.left + size.cx;
753     rect.bottom = rect.top + size.cy;
754 
755     /* check position */
756 
757     monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
758     mon_info.cbSize = sizeof(mon_info);
759     GetMonitorInfoW( monitor, &mon_info );
760 
761 #ifdef __REACTOS__
762     if (rect.right > mon_info.rcMonitor.right)
763     {
764         rect.left -= size.cx - (BALLOON_STEMINDENT + BALLOON_STEMWIDTH);
765         rect.right -= size.cx - (BALLOON_STEMINDENT + BALLOON_STEMWIDTH);
766         if (rect.right > mon_info.rcMonitor.right)
767         {
768             rect.left -= (rect.right - mon_info.rcMonitor.right);
769             rect.right = mon_info.rcMonitor.right;
770         }
771     }
772 
773     if (rect.left < mon_info.rcMonitor.left)
774     {
775         rect.right += abs(rect.left);
776         rect.left = 0;
777     }
778 
779     if (rect.bottom > mon_info.rcMonitor.bottom)
780     {
781         RECT rc;
782         if (toolPtr->uFlags & TTF_IDISHWND)
783         {
784             GetWindowRect((HWND)toolPtr->uId, &rc);
785         }
786         else
787         {
788             rc = toolPtr->rect;
789             MapWindowPoints(toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
790         }
791 	    rect.bottom = rc.top - 2;
792     	rect.top = rect.bottom - size.cy;
793     }
794 #else
795     if( rect.right > mon_info.rcWork.right ) {
796         rect.left -= rect.right - mon_info.rcWork.right + 2;
797         rect.right = mon_info.rcWork.right - 2;
798     }
799     if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
800 
801     if( rect.bottom > mon_info.rcWork.bottom ) {
802         RECT rc;
803 
804 	if (toolPtr->uFlags & TTF_IDISHWND)
805 	    GetWindowRect ((HWND)toolPtr->uId, &rc);
806 	else {
807 	    rc = toolPtr->rect;
808 	    MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
809 	}
810 	rect.bottom = rc.top - 2;
811     	rect.top = rect.bottom - size.cy;
812     }
813 #endif // __REACTOS__
814 
815     AdjustWindowRectEx (&rect, GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE),
816 			FALSE, GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE));
817 
818     if (style & TTS_BALLOON)
819     {
820         HRGN hRgn;
821         HRGN hrStem;
822         POINT pts[3];
823 
824         ptfx -= rect.left;
825 
826         if(infoPtr->bToolBelow)
827         {
828           pts[0].x = ptfx;
829           pts[0].y = 0;
830 #ifdef __REACTOS__
831           pts[1].x = max(BALLOON_STEMINDENT, ptfx - BALLOON_STEMWIDTH);
832 #else
833           pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
834 #endif
835           pts[1].y = BALLOON_STEMHEIGHT;
836           pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
837           pts[2].y = pts[1].y;
838           if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
839           {
840             pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
841             pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
842           }
843         }
844         else
845         {
846 #ifdef __REACTOS__
847           pts[0].x = max(BALLOON_STEMINDENT, ptfx - BALLOON_STEMWIDTH);
848 #else
849           pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
850 #endif
851           pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
852           pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
853           pts[1].y = pts[0].y;
854           pts[2].x = ptfx;
855           pts[2].y = (rect.bottom - rect.top);
856           if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
857           {
858             pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
859             pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
860           }
861         }
862 
863         hrStem = CreatePolygonRgn(pts, ARRAY_SIZE(pts), ALTERNATE);
864 
865         hRgn = CreateRoundRectRgn(0,
866                                   (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
867                                   rect.right - rect.left,
868 #ifdef __REACTOS__
869                                   (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT + 1),
870 #else
871                                   (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
872 #endif
873                                   BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
874 
875         CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
876         DeleteObject(hrStem);
877 
878         SetWindowRgn(infoPtr->hwndSelf, hRgn, FALSE);
879         /* we don't free the region handle as the system deletes it when
880          * it is no longer needed */
881     }
882 
883     SetWindowPos (infoPtr->hwndSelf, NULL, rect.left, rect.top,
884         rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
885 
886     hdr.hwndFrom = infoPtr->hwndSelf;
887     hdr.idFrom = toolPtr->uId;
888     hdr.code = TTN_SHOW;
889     SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
890 
891     SetWindowPos (infoPtr->hwndSelf, HWND_TOPMOST, 0, 0, 0, 0,
892         SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
893 
894     /* repaint the tooltip */
895     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
896     UpdateWindow(infoPtr->hwndSelf);
897 
898     if (!track_activate)
899     {
900         SetTimer (infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
901         TRACE("timer 2 started\n");
902         SetTimer (infoPtr->hwndSelf, ID_TIMERLEAVE, infoPtr->nReshowTime, 0);
903         TRACE("timer 3 started\n");
904     }
905 }
906 
907 
908 static void
909 TOOLTIPS_Hide (TOOLTIPS_INFO *infoPtr)
910 {
911     TTTOOL_INFO *toolPtr;
912     NMHDR hdr;
913 
914     TRACE("Hide tooltip %d, %p.\n", infoPtr->nCurrentTool, infoPtr->hwndSelf);
915 
916     if (infoPtr->nCurrentTool == -1)
917 	return;
918 
919     toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
920     KillTimer (infoPtr->hwndSelf, ID_TIMERPOP);
921 
922     hdr.hwndFrom = infoPtr->hwndSelf;
923     hdr.idFrom = toolPtr->uId;
924     hdr.code = TTN_POP;
925     SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
926 
927     infoPtr->nCurrentTool = -1;
928 
929     SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
930 		    SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
931 }
932 
933 
934 static void
935 TOOLTIPS_TrackShow (TOOLTIPS_INFO *infoPtr)
936 {
937     TOOLTIPS_Show(infoPtr, TRUE);
938 }
939 
940 
941 static void
942 TOOLTIPS_TrackHide (const TOOLTIPS_INFO *infoPtr)
943 {
944     TTTOOL_INFO *toolPtr;
945     NMHDR hdr;
946 
947     TRACE("hide tracking tooltip %d\n", infoPtr->nTrackTool);
948 
949     if (infoPtr->nTrackTool == -1)
950 	return;
951 
952     toolPtr = &infoPtr->tools[infoPtr->nTrackTool];
953 
954     hdr.hwndFrom = infoPtr->hwndSelf;
955     hdr.idFrom = toolPtr->uId;
956     hdr.code = TTN_POP;
957     SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
958 
959     SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
960 		    SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
961 }
962 
963 /* Structure layout is the same for TTTOOLINFOW and TTTOOLINFOA,
964    this helper is used in both cases. */
965 static INT
966 TOOLTIPS_GetToolFromInfoT (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
967 {
968     TTTOOL_INFO *toolPtr;
969     UINT nTool;
970 
971     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
972 	toolPtr = &infoPtr->tools[nTool];
973 
974 	if (!(toolPtr->uFlags & TTF_IDISHWND) &&
975 	    (lpToolInfo->hwnd == toolPtr->hwnd) &&
976 	    (lpToolInfo->uId == toolPtr->uId))
977 	    return nTool;
978     }
979 
980     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
981 	toolPtr = &infoPtr->tools[nTool];
982 
983 	if ((toolPtr->uFlags & TTF_IDISHWND) &&
984 	    (lpToolInfo->uId == toolPtr->uId))
985 	    return nTool;
986     }
987 
988     return -1;
989 }
990 
991 
992 static INT
993 TOOLTIPS_GetToolFromPoint (const TOOLTIPS_INFO *infoPtr, HWND hwnd, const POINT *lpPt)
994 {
995     TTTOOL_INFO *toolPtr;
996     UINT nTool;
997 
998     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
999 	toolPtr = &infoPtr->tools[nTool];
1000 
1001 	if (!(toolPtr->uFlags & TTF_IDISHWND)) {
1002 	    if (hwnd != toolPtr->hwnd)
1003 		continue;
1004 	    if (!PtInRect (&toolPtr->rect, *lpPt))
1005 		continue;
1006 	    return nTool;
1007 	}
1008     }
1009 
1010     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
1011 	toolPtr = &infoPtr->tools[nTool];
1012 
1013 	if (toolPtr->uFlags & TTF_IDISHWND) {
1014 	    if ((HWND)toolPtr->uId == hwnd)
1015 		return nTool;
1016 	}
1017     }
1018 
1019     return -1;
1020 }
1021 
1022 static inline void
1023 TOOLTIPS_CopyInfoT (const TOOLTIPS_INFO *infoPtr, INT index, TTTOOLINFOW *ti, BOOL isW)
1024 {
1025     const TTTOOL_INFO *toolPtr = &infoPtr->tools[index];
1026 
1027     ti->uFlags = toolPtr->uFlags;
1028     ti->hwnd   = toolPtr->hwnd;
1029     ti->uId    = toolPtr->uId;
1030     ti->rect   = toolPtr->rect;
1031     ti->hinst  = toolPtr->hinst;
1032 
1033     if (ti->lpszText) {
1034         if (toolPtr->lpszText == NULL ||
1035             IS_INTRESOURCE(toolPtr->lpszText) ||
1036             toolPtr->lpszText == LPSTR_TEXTCALLBACKW)
1037             ti->lpszText = toolPtr->lpszText;
1038         else if (isW)
1039             strcpyW (ti->lpszText, toolPtr->lpszText);
1040         else
1041             /* ANSI version, the buffer is maximum 80 bytes without null. */
1042             WideCharToMultiByte(CP_ACP, 0, toolPtr->lpszText, -1,
1043                                 (LPSTR)ti->lpszText, MAX_TEXT_SIZE_A, NULL, NULL);
1044     }
1045 
1046     if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1047         ti->lParam = toolPtr->lParam;
1048 
1049     /* lpReserved is intentionally not set. */
1050 }
1051 
1052 static BOOL
1053 TOOLTIPS_IsWindowActive (HWND hwnd)
1054 {
1055     HWND hwndActive = GetActiveWindow ();
1056     if (!hwndActive)
1057 	return FALSE;
1058     if (hwndActive == hwnd)
1059 	return TRUE;
1060     return IsChild (hwndActive, hwnd);
1061 }
1062 
1063 
1064 static INT
1065 TOOLTIPS_CheckTool (const TOOLTIPS_INFO *infoPtr, BOOL bShowTest)
1066 {
1067     POINT pt;
1068     HWND hwndTool;
1069     INT nTool;
1070 
1071     GetCursorPos (&pt);
1072     hwndTool = (HWND)SendMessageW (infoPtr->hwndSelf, TTM_WINDOWFROMPOINT, 0, (LPARAM)&pt);
1073     if (hwndTool == 0)
1074 	return -1;
1075 
1076     ScreenToClient (hwndTool, &pt);
1077     nTool = TOOLTIPS_GetToolFromPoint (infoPtr, hwndTool, &pt);
1078     if (nTool == -1)
1079 	return -1;
1080 
1081     if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_ALWAYSTIP) && bShowTest)
1082     {
1083         TTTOOL_INFO *ti = &infoPtr->tools[nTool];
1084         HWND hwnd = (ti->uFlags & TTF_IDISHWND) ? (HWND)ti->uId : ti->hwnd;
1085 
1086         if (!TOOLTIPS_IsWindowActive(hwnd))
1087         {
1088             TRACE("not active: hwnd %p, parent %p, active %p\n",
1089                   hwnd, GetParent(hwnd), GetActiveWindow());
1090             return -1;
1091         }
1092     }
1093 
1094     TRACE("tool %d\n", nTool);
1095 
1096     return nTool;
1097 }
1098 
1099 
1100 static LRESULT
1101 TOOLTIPS_Activate (TOOLTIPS_INFO *infoPtr, BOOL activate)
1102 {
1103     infoPtr->bActive = activate;
1104 
1105     TRACE("activate %d\n", activate);
1106 
1107     if (!(infoPtr->bActive) && (infoPtr->nCurrentTool != -1))
1108 	TOOLTIPS_Hide (infoPtr);
1109 
1110     return 0;
1111 }
1112 
1113 
1114 static LRESULT
1115 TOOLTIPS_AddToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1116 {
1117     TTTOOL_INFO *toolPtr;
1118     INT nResult;
1119 
1120     if (!ti) return FALSE;
1121 
1122     TRACE("add tool (%p) %p %ld%s\n", infoPtr->hwndSelf, ti->hwnd, ti->uId,
1123         (ti->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
1124 
1125     if (ti->cbSize > TTTOOLINFOW_V3_SIZE && isW)
1126         return FALSE;
1127 
1128     if (infoPtr->uNumTools == 0) {
1129 	infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
1130 	toolPtr = infoPtr->tools;
1131     }
1132     else {
1133 	TTTOOL_INFO *oldTools = infoPtr->tools;
1134 	infoPtr->tools =
1135 	    Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1136 	memcpy (infoPtr->tools, oldTools,
1137 		infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1138 	Free (oldTools);
1139 	toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1140     }
1141 
1142     infoPtr->uNumTools++;
1143 
1144     /* copy tool data */
1145     toolPtr->uFlags         = ti->uFlags;
1146     toolPtr->uInternalFlags = (ti->uFlags & (TTF_SUBCLASS | TTF_IDISHWND));
1147     toolPtr->hwnd           = ti->hwnd;
1148     toolPtr->uId            = ti->uId;
1149     toolPtr->rect           = ti->rect;
1150     toolPtr->hinst          = ti->hinst;
1151 
1152     if (ti->cbSize >= TTTOOLINFOW_V1_SIZE) {
1153         if (IS_INTRESOURCE(ti->lpszText)) {
1154             TRACE("add string id %x\n", LOWORD(ti->lpszText));
1155             toolPtr->lpszText = ti->lpszText;
1156         }
1157         else if (ti->lpszText) {
1158             if (TOOLTIPS_IsCallbackString(ti->lpszText, isW)) {
1159                 TRACE("add CALLBACK\n");
1160                 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1161             }
1162             else if (isW) {
1163                 INT len = lstrlenW (ti->lpszText);
1164                 TRACE("add text %s\n", debugstr_w(ti->lpszText));
1165                 toolPtr->lpszText =	Alloc ((len + 1)*sizeof(WCHAR));
1166                 strcpyW (toolPtr->lpszText, ti->lpszText);
1167             }
1168             else {
1169                 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, NULL, 0);
1170                 TRACE("add text \"%s\"\n", debugstr_a((char *)ti->lpszText));
1171                 toolPtr->lpszText =	Alloc (len * sizeof(WCHAR));
1172                 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, toolPtr->lpszText, len);
1173             }
1174         }
1175     }
1176 
1177     if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1178 	toolPtr->lParam = ti->lParam;
1179 
1180     /* install subclassing hook */
1181     if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1182 	if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1183 	    SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1184 			      (DWORD_PTR)infoPtr->hwndSelf);
1185 	}
1186 	else {
1187 	    SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1188 			      (DWORD_PTR)infoPtr->hwndSelf);
1189 	}
1190 	TRACE("subclassing installed\n");
1191     }
1192 
1193     nResult = SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1194                             (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1195     if (nResult == NFR_ANSI) {
1196         toolPtr->bNotifyUnicode = FALSE;
1197 	TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1198     } else if (nResult == NFR_UNICODE) {
1199         toolPtr->bNotifyUnicode = TRUE;
1200 	TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1201     } else {
1202         TRACE (" -- WM_NOTIFYFORMAT returns: %d\n", nResult);
1203     }
1204 
1205     return TRUE;
1206 }
1207 
1208 static void TOOLTIPS_ResetSubclass (const TTTOOL_INFO *toolPtr)
1209 {
1210     /* Reset subclassing data. */
1211     if (toolPtr->uInternalFlags & TTF_SUBCLASS)
1212         SetWindowSubclass(toolPtr->uInternalFlags & TTF_IDISHWND ? (HWND)toolPtr->uId : toolPtr->hwnd,
1213             TOOLTIPS_SubclassProc, 1, 0);
1214 }
1215 
1216 static LRESULT
1217 TOOLTIPS_DelToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1218 {
1219     TTTOOL_INFO *toolPtr;
1220     INT nTool;
1221 
1222     if (!ti) return 0;
1223     if (isW && ti->cbSize > TTTOOLINFOW_V2_SIZE &&
1224                ti->cbSize != TTTOOLINFOW_V3_SIZE)
1225 	return 0;
1226     if (infoPtr->uNumTools == 0)
1227 	return 0;
1228 
1229     nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1230 
1231     TRACE("tool %d\n", nTool);
1232 
1233     if (nTool == -1)
1234         return 0;
1235 
1236     /* make sure the tooltip has disappeared before deleting it */
1237     TOOLTIPS_Hide(infoPtr);
1238 
1239     /* delete text string */
1240     toolPtr = &infoPtr->tools[nTool];
1241     if (toolPtr->lpszText) {
1242 	if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1243 	     !IS_INTRESOURCE(toolPtr->lpszText) )
1244 	    Free (toolPtr->lpszText);
1245     }
1246 
1247     TOOLTIPS_ResetSubclass (toolPtr);
1248 
1249     /* delete tool from tool list */
1250     if (infoPtr->uNumTools == 1) {
1251 	Free (infoPtr->tools);
1252 	infoPtr->tools = NULL;
1253     }
1254     else {
1255 	TTTOOL_INFO *oldTools = infoPtr->tools;
1256 	infoPtr->tools =
1257 	    Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1258 
1259 	if (nTool > 0)
1260 	    memcpy (&infoPtr->tools[0], &oldTools[0],
1261 		    nTool * sizeof(TTTOOL_INFO));
1262 
1263 	if (nTool < infoPtr->uNumTools - 1)
1264 	    memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1265 		    (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1266 
1267 	Free (oldTools);
1268     }
1269 
1270     /* update any indices affected by delete */
1271 
1272     /* destroying tool that mouse was on on last relayed mouse move */
1273     if (infoPtr->nTool == nTool)
1274         /* -1 means no current tool (0 means first tool) */
1275         infoPtr->nTool = -1;
1276     else if (infoPtr->nTool > nTool)
1277         infoPtr->nTool--;
1278 
1279     if (infoPtr->nTrackTool == nTool)
1280         /* -1 means no current tool (0 means first tool) */
1281         infoPtr->nTrackTool = -1;
1282     else if (infoPtr->nTrackTool > nTool)
1283         infoPtr->nTrackTool--;
1284 
1285     if (infoPtr->nCurrentTool == nTool)
1286         /* -1 means no current tool (0 means first tool) */
1287         infoPtr->nCurrentTool = -1;
1288     else if (infoPtr->nCurrentTool > nTool)
1289         infoPtr->nCurrentTool--;
1290 
1291     infoPtr->uNumTools--;
1292 
1293     return 0;
1294 }
1295 
1296 static LRESULT
1297 TOOLTIPS_EnumToolsT (const TOOLTIPS_INFO *infoPtr, UINT uIndex, TTTOOLINFOW *ti,
1298                      BOOL isW)
1299 {
1300     if (!ti || ti->cbSize < TTTOOLINFOW_V1_SIZE)
1301 	return FALSE;
1302     if (uIndex >= infoPtr->uNumTools)
1303 	return FALSE;
1304 
1305     TRACE("index=%u\n", uIndex);
1306 
1307     TOOLTIPS_CopyInfoT (infoPtr, uIndex, ti, isW);
1308 
1309     return TRUE;
1310 }
1311 
1312 static LRESULT
1313 TOOLTIPS_GetBubbleSize (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
1314 {
1315     INT nTool;
1316     SIZE size;
1317 
1318     if (lpToolInfo == NULL)
1319 	return FALSE;
1320     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1321 	return FALSE;
1322 
1323     nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, lpToolInfo);
1324     if (nTool == -1) return 0;
1325 
1326     TRACE("tool %d\n", nTool);
1327 
1328     TOOLTIPS_CalcTipSize (infoPtr, &size);
1329     TRACE("size %d x %d\n", size.cx, size.cy);
1330 
1331     return MAKELRESULT(size.cx, size.cy);
1332 }
1333 
1334 static LRESULT
1335 TOOLTIPS_GetCurrentToolT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1336 {
1337     if (ti) {
1338         if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1339             return FALSE;
1340 
1341         if (infoPtr->nCurrentTool != -1)
1342             TOOLTIPS_CopyInfoT (infoPtr, infoPtr->nCurrentTool, ti, isW);
1343     }
1344 
1345     return infoPtr->nCurrentTool != -1;
1346 }
1347 
1348 
1349 static LRESULT
1350 TOOLTIPS_GetDelayTime (const TOOLTIPS_INFO *infoPtr, DWORD duration)
1351 {
1352     switch (duration) {
1353     case TTDT_RESHOW:
1354         return infoPtr->nReshowTime;
1355 
1356     case TTDT_AUTOPOP:
1357         return infoPtr->nAutoPopTime;
1358 
1359     case TTDT_INITIAL:
1360     case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1361         return infoPtr->nInitialTime;
1362 
1363     default:
1364         WARN("Invalid duration flag %x\n", duration);
1365 	break;
1366     }
1367 
1368     return -1;
1369 }
1370 
1371 
1372 static LRESULT
1373 TOOLTIPS_GetMargin (const TOOLTIPS_INFO *infoPtr, RECT *rect)
1374 {
1375     if (rect)
1376         *rect = infoPtr->rcMargin;
1377 
1378     return 0;
1379 }
1380 
1381 
1382 static inline LRESULT
1383 TOOLTIPS_GetMaxTipWidth (const TOOLTIPS_INFO *infoPtr)
1384 {
1385     return infoPtr->nMaxTipWidth;
1386 }
1387 
1388 
1389 static LRESULT
1390 TOOLTIPS_GetTextT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1391 {
1392     INT nTool;
1393 
1394     if (!ti) return 0;
1395     if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1396 	return 0;
1397 
1398     nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1399     if (nTool == -1) return 0;
1400 
1401     if (infoPtr->tools[nTool].lpszText == NULL)
1402 	return 0;
1403 
1404     if (isW) {
1405         ti->lpszText[0] = '\0';
1406         TOOLTIPS_GetTipText(infoPtr, nTool, ti->lpszText);
1407     }
1408     else {
1409         WCHAR buffer[INFOTIPSIZE];
1410 
1411         /* NB this API is broken, there is no way for the app to determine
1412            what size buffer it requires nor a way to specify how long the
1413            one it supplies is.  According to the test result, it's up to
1414            80 bytes by the ANSI version. */
1415 
1416         buffer[0] = '\0';
1417         TOOLTIPS_GetTipText(infoPtr, nTool, buffer);
1418         WideCharToMultiByte(CP_ACP, 0, buffer, -1, (LPSTR)ti->lpszText,
1419                                                    MAX_TEXT_SIZE_A, NULL, NULL);
1420     }
1421 
1422     return 0;
1423 }
1424 
1425 
1426 static inline LRESULT
1427 TOOLTIPS_GetTipBkColor (const TOOLTIPS_INFO *infoPtr)
1428 {
1429     return infoPtr->clrBk;
1430 }
1431 
1432 
1433 static inline LRESULT
1434 TOOLTIPS_GetTipTextColor (const TOOLTIPS_INFO *infoPtr)
1435 {
1436     return infoPtr->clrText;
1437 }
1438 
1439 
1440 static inline LRESULT
1441 TOOLTIPS_GetToolCount (const TOOLTIPS_INFO *infoPtr)
1442 {
1443     return infoPtr->uNumTools;
1444 }
1445 
1446 
1447 static LRESULT
1448 TOOLTIPS_GetToolInfoT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1449 {
1450     INT nTool;
1451     HWND hwnd;
1452 
1453     if (!ti) return FALSE;
1454     if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1455 	return FALSE;
1456     if (infoPtr->uNumTools == 0)
1457 	return FALSE;
1458 
1459     nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1460     if (nTool == -1)
1461 	return FALSE;
1462 
1463     TRACE("tool %d\n", nTool);
1464 
1465     hwnd = ti->hwnd;
1466     TOOLTIPS_CopyInfoT (infoPtr, nTool, ti, isW);
1467     ti->hwnd = hwnd;
1468 
1469     return TRUE;
1470 }
1471 
1472 
1473 static LRESULT
1474 TOOLTIPS_HitTestT (const TOOLTIPS_INFO *infoPtr, LPTTHITTESTINFOW lptthit,
1475                    BOOL isW)
1476 {
1477     INT nTool;
1478 
1479     if (lptthit == 0)
1480 	return FALSE;
1481 
1482     nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1483     if (nTool == -1)
1484 	return FALSE;
1485 
1486     TRACE("tool %d\n", nTool);
1487 
1488     /* copy tool data */
1489     if (lptthit->ti.cbSize >= TTTOOLINFOW_V1_SIZE)
1490         TOOLTIPS_CopyInfoT (infoPtr, nTool, &lptthit->ti, isW);
1491 
1492     return TRUE;
1493 }
1494 
1495 
1496 static LRESULT
1497 TOOLTIPS_NewToolRectT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti)
1498 {
1499     INT nTool;
1500 
1501     if (!ti) return 0;
1502     if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1503 	return FALSE;
1504 
1505     nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1506 
1507     TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&ti->rect));
1508 
1509     if (nTool == -1) return 0;
1510 
1511     infoPtr->tools[nTool].rect = ti->rect;
1512 
1513     return 0;
1514 }
1515 
1516 
1517 static inline LRESULT
1518 TOOLTIPS_Pop (TOOLTIPS_INFO *infoPtr)
1519 {
1520     TOOLTIPS_Hide (infoPtr);
1521 
1522     return 0;
1523 }
1524 
1525 
1526 static LRESULT
1527 TOOLTIPS_RelayEvent (TOOLTIPS_INFO *infoPtr, LPMSG lpMsg)
1528 {
1529     POINT pt;
1530     INT nOldTool;
1531 
1532     if (!lpMsg) {
1533 	ERR("lpMsg == NULL\n");
1534 	return 0;
1535     }
1536 
1537     switch (lpMsg->message) {
1538 	case WM_LBUTTONDOWN:
1539 	case WM_LBUTTONUP:
1540 	case WM_MBUTTONDOWN:
1541 	case WM_MBUTTONUP:
1542 	case WM_RBUTTONDOWN:
1543 	case WM_RBUTTONUP:
1544 	    TOOLTIPS_Hide (infoPtr);
1545 	    break;
1546 
1547 	case WM_MOUSEMOVE:
1548 	    pt.x = (short)LOWORD(lpMsg->lParam);
1549 	    pt.y = (short)HIWORD(lpMsg->lParam);
1550 	    nOldTool = infoPtr->nTool;
1551 	    infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1552 						       &pt);
1553 	    TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
1554 		  infoPtr->nTool, infoPtr->nCurrentTool);
1555             TRACE("WM_MOUSEMOVE (%p %s)\n", infoPtr->hwndSelf, wine_dbgstr_point(&pt));
1556 
1557 	    if (infoPtr->nTool != nOldTool) {
1558 	        if(infoPtr->nTool == -1) { /* Moved out of all tools */
1559 		    TOOLTIPS_Hide(infoPtr);
1560 		    KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1561 		} else if (nOldTool == -1) { /* Moved from outside */
1562 		    if(infoPtr->bActive) {
1563 		        SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1564                         TRACE("timer 1 started\n");
1565 		    }
1566 		} else { /* Moved from one to another */
1567 		    TOOLTIPS_Hide (infoPtr);
1568 		    KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1569 		    if(infoPtr->bActive) {
1570 		        SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1571                         TRACE("timer 1 started\n");
1572 		    }
1573 		}
1574 	    } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1575 	        KillTimer(infoPtr->hwndSelf, ID_TIMERPOP);
1576 		SetTimer(infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1577 		TRACE("timer 2 restarted\n");
1578 	    } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1579                 /* previous show attempt didn't result in tooltip so try again */
1580 		SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1581                 TRACE("timer 1 started\n");
1582 	    }
1583 	    break;
1584     }
1585 
1586     return 0;
1587 }
1588 
1589 
1590 static LRESULT
1591 TOOLTIPS_SetDelayTime (TOOLTIPS_INFO *infoPtr, DWORD duration, INT nTime)
1592 {
1593     switch (duration) {
1594     case TTDT_AUTOMATIC:
1595         if (nTime <= 0)
1596 	    nTime = GetDoubleClickTime();
1597 	infoPtr->nReshowTime    = nTime / 5;
1598 	infoPtr->nAutoPopTime   = nTime * 10;
1599 	infoPtr->nInitialTime   = nTime;
1600 	break;
1601 
1602     case TTDT_RESHOW:
1603         if(nTime < 0)
1604 	    nTime = GetDoubleClickTime() / 5;
1605 	infoPtr->nReshowTime = nTime;
1606 	break;
1607 
1608     case TTDT_AUTOPOP:
1609         if(nTime < 0)
1610 	    nTime = GetDoubleClickTime() * 10;
1611 	infoPtr->nAutoPopTime = nTime;
1612 	break;
1613 
1614     case TTDT_INITIAL:
1615         if(nTime < 0)
1616 	    nTime = GetDoubleClickTime();
1617 	infoPtr->nInitialTime = nTime;
1618 	    break;
1619 
1620     default:
1621         WARN("Invalid duration flag %x\n", duration);
1622 	break;
1623     }
1624 
1625     return 0;
1626 }
1627 
1628 
1629 static LRESULT
1630 TOOLTIPS_SetMargin (TOOLTIPS_INFO *infoPtr, const RECT *rect)
1631 {
1632     if (rect)
1633         infoPtr->rcMargin = *rect;
1634 
1635     return 0;
1636 }
1637 
1638 
1639 static inline LRESULT
1640 TOOLTIPS_SetMaxTipWidth (TOOLTIPS_INFO *infoPtr, INT MaxWidth)
1641 {
1642     INT nTemp = infoPtr->nMaxTipWidth;
1643 
1644     infoPtr->nMaxTipWidth = MaxWidth;
1645 
1646     return nTemp;
1647 }
1648 
1649 
1650 static inline LRESULT
1651 TOOLTIPS_SetTipBkColor (TOOLTIPS_INFO *infoPtr, COLORREF clrBk)
1652 {
1653     infoPtr->clrBk = clrBk;
1654 
1655     return 0;
1656 }
1657 
1658 
1659 static inline LRESULT
1660 TOOLTIPS_SetTipTextColor (TOOLTIPS_INFO *infoPtr, COLORREF clrText)
1661 {
1662     infoPtr->clrText = clrText;
1663 
1664     return 0;
1665 }
1666 
1667 
1668 static LRESULT
1669 TOOLTIPS_SetTitleT (TOOLTIPS_INFO *infoPtr, UINT_PTR uTitleIcon, LPCWSTR pszTitle,
1670                     BOOL isW)
1671 {
1672     UINT size;
1673 
1674     TRACE("hwnd = %p, title = %s, icon = %p\n", infoPtr->hwndSelf, debugstr_w(pszTitle),
1675         (void*)uTitleIcon);
1676 
1677     Free(infoPtr->pszTitle);
1678 
1679     if (pszTitle)
1680     {
1681         if (isW)
1682         {
1683             size = (strlenW(pszTitle)+1)*sizeof(WCHAR);
1684             infoPtr->pszTitle = Alloc(size);
1685             if (!infoPtr->pszTitle)
1686                 return FALSE;
1687             memcpy(infoPtr->pszTitle, pszTitle, size);
1688         }
1689         else
1690         {
1691             size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, NULL, 0);
1692             infoPtr->pszTitle = Alloc(size);
1693             if (!infoPtr->pszTitle)
1694                 return FALSE;
1695             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1696         }
1697     }
1698     else
1699         infoPtr->pszTitle = NULL;
1700 
1701     if (uTitleIcon <= TTI_ERROR)
1702         infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1703     else
1704         infoPtr->hTitleIcon = CopyIcon((HICON)uTitleIcon);
1705 
1706     TRACE("icon = %p\n", infoPtr->hTitleIcon);
1707 
1708     return TRUE;
1709 }
1710 
1711 
1712 static LRESULT
1713 TOOLTIPS_SetToolInfoT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1714 {
1715     TTTOOL_INFO *toolPtr;
1716     INT nTool;
1717 
1718     if (!ti) return 0;
1719     if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1720 	return 0;
1721 
1722     nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1723     if (nTool == -1) return 0;
1724 
1725     TRACE("tool %d\n", nTool);
1726 
1727     toolPtr = &infoPtr->tools[nTool];
1728 
1729     /* copy tool data */
1730     toolPtr->uFlags = ti->uFlags;
1731     toolPtr->hwnd   = ti->hwnd;
1732     toolPtr->uId    = ti->uId;
1733     toolPtr->rect   = ti->rect;
1734     toolPtr->hinst  = ti->hinst;
1735 
1736     if (IS_INTRESOURCE(ti->lpszText)) {
1737 	TRACE("set string id %x\n", LOWORD(ti->lpszText));
1738 	toolPtr->lpszText = ti->lpszText;
1739     }
1740     else {
1741 	if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1742 	    toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1743 	else {
1744 	    if ( (toolPtr->lpszText) &&
1745 		 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1746 		if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1747                     Free (toolPtr->lpszText);
1748 		toolPtr->lpszText = NULL;
1749 	    }
1750 	    if (ti->lpszText) {
1751 		if (isW) {
1752 		    INT len = lstrlenW (ti->lpszText);
1753 		    toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1754 		    strcpyW (toolPtr->lpszText, ti->lpszText);
1755 		}
1756 		else {
1757 		    INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1758 					      -1, NULL, 0);
1759 		    toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1760 		    MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1761 					toolPtr->lpszText, len);
1762 		}
1763 	    }
1764 	}
1765     }
1766 
1767     if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1768 	toolPtr->lParam = ti->lParam;
1769 
1770     if (infoPtr->nCurrentTool == nTool)
1771     {
1772         TOOLTIPS_GetTipText (infoPtr, infoPtr->nCurrentTool, infoPtr->szTipText);
1773 
1774         if (infoPtr->szTipText[0] == 0)
1775             TOOLTIPS_Hide(infoPtr);
1776         else
1777             TOOLTIPS_Show (infoPtr, FALSE);
1778     }
1779 
1780     return 0;
1781 }
1782 
1783 
1784 static LRESULT
1785 TOOLTIPS_TrackActivate (TOOLTIPS_INFO *infoPtr, BOOL track_activate, const TTTOOLINFOA *ti)
1786 {
1787     if (track_activate) {
1788 
1789 	if (!ti) return 0;
1790 	if (ti->cbSize < TTTOOLINFOA_V1_SIZE)
1791 	    return FALSE;
1792 
1793 	/* activate */
1794 	infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoT (infoPtr, (const TTTOOLINFOW*)ti);
1795 	if (infoPtr->nTrackTool != -1) {
1796 	    TRACE("activated\n");
1797 	    infoPtr->bTrackActive = TRUE;
1798 	    TOOLTIPS_TrackShow (infoPtr);
1799 	}
1800     }
1801     else {
1802 	/* deactivate */
1803 	TOOLTIPS_TrackHide (infoPtr);
1804 
1805 	infoPtr->bTrackActive = FALSE;
1806 	infoPtr->nTrackTool = -1;
1807 
1808         TRACE("deactivated\n");
1809     }
1810 
1811     return 0;
1812 }
1813 
1814 
1815 static LRESULT
1816 TOOLTIPS_TrackPosition (TOOLTIPS_INFO *infoPtr, LPARAM coord)
1817 {
1818     infoPtr->xTrackPos = (INT)LOWORD(coord);
1819     infoPtr->yTrackPos = (INT)HIWORD(coord);
1820 
1821     if (infoPtr->bTrackActive) {
1822 	TRACE("[%d %d]\n",
1823 	       infoPtr->xTrackPos, infoPtr->yTrackPos);
1824 
1825 	TOOLTIPS_TrackShow (infoPtr);
1826     }
1827 
1828     return 0;
1829 }
1830 
1831 
1832 static LRESULT
1833 TOOLTIPS_Update (TOOLTIPS_INFO *infoPtr)
1834 {
1835     if (infoPtr->nCurrentTool != -1)
1836 	UpdateWindow (infoPtr->hwndSelf);
1837 
1838     return 0;
1839 }
1840 
1841 
1842 static LRESULT
1843 TOOLTIPS_UpdateTipTextT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1844 {
1845     TTTOOL_INFO *toolPtr;
1846     INT nTool;
1847 
1848     if (!ti) return 0;
1849     if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1850 	return FALSE;
1851 
1852     nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1853     if (nTool == -1)
1854 	return 0;
1855 
1856     TRACE("tool %d\n", nTool);
1857 
1858     toolPtr = &infoPtr->tools[nTool];
1859 
1860     /* copy tool text */
1861     toolPtr->hinst  = ti->hinst;
1862 
1863     if (IS_INTRESOURCE(ti->lpszText)){
1864 	toolPtr->lpszText = ti->lpszText;
1865     }
1866     else if (ti->lpszText) {
1867 	if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1868 	    toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1869 	else {
1870 	    if ( (toolPtr->lpszText)  &&
1871 		 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1872 		if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1873                     Free (toolPtr->lpszText);
1874 		toolPtr->lpszText = NULL;
1875 	    }
1876 	    if (ti->lpszText) {
1877 		if (isW) {
1878 		    INT len = lstrlenW (ti->lpszText);
1879 		    toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1880 		    strcpyW (toolPtr->lpszText, ti->lpszText);
1881 		}
1882 		else {
1883 		    INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1884 						-1, NULL, 0);
1885 		    toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1886 		    MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1887 					toolPtr->lpszText, len);
1888 	        }
1889 	    }
1890 	}
1891     }
1892 
1893     if(infoPtr->nCurrentTool == -1) return 0;
1894     /* force repaint */
1895     if (infoPtr->bActive)
1896 	TOOLTIPS_Show (infoPtr, FALSE);
1897     else if (infoPtr->bTrackActive)
1898 	TOOLTIPS_Show (infoPtr, TRUE);
1899 
1900     return 0;
1901 }
1902 
1903 
1904 static LRESULT
1905 TOOLTIPS_Create (HWND hwnd)
1906 {
1907     TOOLTIPS_INFO *infoPtr;
1908 
1909     /* allocate memory for info structure */
1910     infoPtr = Alloc (sizeof(TOOLTIPS_INFO));
1911     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1912 
1913     /* initialize info structure */
1914     infoPtr->bActive = TRUE;
1915     infoPtr->bTrackActive = FALSE;
1916 
1917     infoPtr->nMaxTipWidth = -1;
1918     infoPtr->nTool = -1;
1919     infoPtr->nCurrentTool = -1;
1920     infoPtr->nTrackTool = -1;
1921     infoPtr->hwndSelf = hwnd;
1922 
1923     /* initialize colours and fonts */
1924     TOOLTIPS_InitSystemSettings(infoPtr);
1925 
1926     TOOLTIPS_SetDelayTime(infoPtr, TTDT_AUTOMATIC, 0);
1927 
1928     SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
1929 
1930     return 0;
1931 }
1932 
1933 
1934 static LRESULT
1935 TOOLTIPS_Destroy (TOOLTIPS_INFO *infoPtr)
1936 {
1937     TTTOOL_INFO *toolPtr;
1938     UINT i;
1939 
1940     /* free tools */
1941     if (infoPtr->tools) {
1942 	for (i = 0; i < infoPtr->uNumTools; i++) {
1943 	    toolPtr = &infoPtr->tools[i];
1944 	    if (toolPtr->lpszText) {
1945 		if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1946 		     !IS_INTRESOURCE(toolPtr->lpszText) )
1947 		{
1948 		    Free (toolPtr->lpszText);
1949 		    toolPtr->lpszText = NULL;
1950 		}
1951 	    }
1952 
1953             TOOLTIPS_ResetSubclass (toolPtr);
1954         }
1955 
1956 	Free (infoPtr->tools);
1957     }
1958 
1959     /* free title string */
1960     Free (infoPtr->pszTitle);
1961     /* free title icon if not a standard one */
1962     if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
1963         DeleteObject(infoPtr->hTitleIcon);
1964 
1965     /* delete fonts */
1966     DeleteObject (infoPtr->hFont);
1967     DeleteObject (infoPtr->hTitleFont);
1968 
1969     /* free tool tips info data */
1970     SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1971     Free (infoPtr);
1972 
1973     return 0;
1974 }
1975 
1976 
1977 static inline LRESULT
1978 TOOLTIPS_GetFont (const TOOLTIPS_INFO *infoPtr)
1979 {
1980     return (LRESULT)infoPtr->hFont;
1981 }
1982 
1983 
1984 static LRESULT
1985 TOOLTIPS_MouseMessage (TOOLTIPS_INFO *infoPtr)
1986 {
1987     TOOLTIPS_Hide (infoPtr);
1988 
1989     return 0;
1990 }
1991 
1992 
1993 static LRESULT
1994 TOOLTIPS_NCCreate (HWND hwnd)
1995 {
1996     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1997     DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
1998 
1999     dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
2000     dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
2001 
2002     /* WS_BORDER only draws a border round the window rect, not the
2003      * window region, therefore it is useless to us in balloon mode */
2004     if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
2005 
2006     SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
2007 
2008     dwExStyle |= WS_EX_TOOLWINDOW;
2009     SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
2010 
2011     return TRUE;
2012 }
2013 
2014 
2015 static LRESULT
2016 TOOLTIPS_NCHitTest (const TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2017 {
2018     INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
2019 
2020     TRACE(" nTool=%d\n", nTool);
2021 
2022     if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
2023 	if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
2024 	    TRACE("-- in transparent mode\n");
2025 	    return HTTRANSPARENT;
2026 	}
2027     }
2028 
2029     return DefWindowProcW (infoPtr->hwndSelf, WM_NCHITTEST, wParam, lParam);
2030 }
2031 
2032 
2033 static LRESULT
2034 TOOLTIPS_NotifyFormat (TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2035 {
2036 #ifdef __REACTOS__
2037     TTTOOL_INFO *toolPtr = infoPtr->tools;
2038     LRESULT nResult;
2039 
2040     TRACE("infoPtr=%p wParam=%lx lParam=%p\n", infoPtr, wParam, (PVOID)lParam);
2041 
2042     if (lParam == NF_QUERY) {
2043         if (toolPtr->bNotifyUnicode) {
2044             return NFR_UNICODE;
2045         } else {
2046             return NFR_ANSI;
2047         }
2048     }
2049     else if (lParam == NF_REQUERY) {
2050         nResult = SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
2051                     (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
2052         if (nResult == NFR_ANSI) {
2053             toolPtr->bNotifyUnicode = FALSE;
2054             TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
2055         } else if (nResult == NFR_UNICODE) {
2056             toolPtr->bNotifyUnicode = TRUE;
2057             TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
2058         } else {
2059             TRACE (" -- WM_NOTIFYFORMAT returns: error!\n");
2060         }
2061         return nResult;
2062     }
2063 #else
2064     FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", infoPtr->hwndSelf, wParam, lParam);
2065 #endif
2066 
2067     return 0;
2068 }
2069 
2070 
2071 static LRESULT
2072 TOOLTIPS_Paint (const TOOLTIPS_INFO *infoPtr, HDC hDC)
2073 {
2074     HDC hdc;
2075     PAINTSTRUCT ps;
2076 
2077     hdc = (hDC == NULL) ? BeginPaint (infoPtr->hwndSelf, &ps) : hDC;
2078     TOOLTIPS_Refresh (infoPtr, hdc);
2079     if (!hDC)
2080 	EndPaint (infoPtr->hwndSelf, &ps);
2081     return 0;
2082 }
2083 
2084 
2085 static LRESULT
2086 TOOLTIPS_SetFont (TOOLTIPS_INFO *infoPtr, HFONT hFont, BOOL redraw)
2087 {
2088     LOGFONTW lf;
2089 
2090     if(!GetObjectW(hFont, sizeof(lf), &lf))
2091         return 0;
2092 
2093     DeleteObject (infoPtr->hFont);
2094     infoPtr->hFont = CreateFontIndirectW(&lf);
2095 
2096     DeleteObject (infoPtr->hTitleFont);
2097     lf.lfWeight = FW_BOLD;
2098     infoPtr->hTitleFont = CreateFontIndirectW(&lf);
2099 
2100     if (redraw && infoPtr->nCurrentTool != -1)
2101         FIXME("full redraw needed\n");
2102 
2103     return 0;
2104 }
2105 
2106 /******************************************************************
2107  * TOOLTIPS_GetTextLength
2108  *
2109  * This function is called when the tooltip receive a
2110  * WM_GETTEXTLENGTH message.
2111  *
2112  * returns the length, in characters, of the tip text
2113  */
2114 static inline LRESULT
2115 TOOLTIPS_GetTextLength(const TOOLTIPS_INFO *infoPtr)
2116 {
2117     return strlenW(infoPtr->szTipText);
2118 }
2119 
2120 /******************************************************************
2121  * TOOLTIPS_OnWMGetText
2122  *
2123  * This function is called when the tooltip receive a
2124  * WM_GETTEXT message.
2125  * wParam : specifies the maximum number of characters to be copied
2126  * lParam : is the pointer to the buffer that will receive
2127  *          the tip text
2128  *
2129  * returns the number of characters copied
2130  */
2131 static LRESULT
2132 TOOLTIPS_OnWMGetText (const TOOLTIPS_INFO *infoPtr, WPARAM size, LPWSTR pszText)
2133 {
2134     LRESULT res;
2135 
2136     if(!size)
2137         return 0;
2138 
2139     res = min(strlenW(infoPtr->szTipText)+1, size);
2140     memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2141     pszText[res-1] = '\0';
2142     return res-1;
2143 }
2144 
2145 static LRESULT
2146 TOOLTIPS_Timer (TOOLTIPS_INFO *infoPtr, INT iTimer)
2147 {
2148     INT nOldTool;
2149 
2150     TRACE("timer %d (%p) expired\n", iTimer, infoPtr->hwndSelf);
2151 
2152     switch (iTimer) {
2153     case ID_TIMERSHOW:
2154         KillTimer (infoPtr->hwndSelf, ID_TIMERSHOW);
2155 	nOldTool = infoPtr->nTool;
2156 	if ((infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, TRUE)) == nOldTool)
2157 	    TOOLTIPS_Show (infoPtr, FALSE);
2158 	break;
2159 
2160     case ID_TIMERPOP:
2161         TOOLTIPS_Hide (infoPtr);
2162 	break;
2163 
2164     case ID_TIMERLEAVE:
2165         nOldTool = infoPtr->nTool;
2166 	infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, FALSE);
2167 	TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
2168 	      infoPtr->nTool, infoPtr->nCurrentTool);
2169 	if (infoPtr->nTool != nOldTool) {
2170 	    if(infoPtr->nTool == -1) { /* Moved out of all tools */
2171 	        TOOLTIPS_Hide(infoPtr);
2172 		KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2173 	    } else if (nOldTool == -1) { /* Moved from outside */
2174 	        ERR("How did this happen?\n");
2175 	    } else { /* Moved from one to another */
2176 	        TOOLTIPS_Hide (infoPtr);
2177 		KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2178 		if(infoPtr->bActive) {
2179 		    SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2180 		    TRACE("timer 1 started!\n");
2181 		}
2182 	    }
2183 	}
2184 	break;
2185 
2186     default:
2187         ERR("Unknown timer id %d\n", iTimer);
2188 	break;
2189     }
2190     return 0;
2191 }
2192 
2193 
2194 static LRESULT
2195 TOOLTIPS_WinIniChange (TOOLTIPS_INFO *infoPtr)
2196 {
2197     TOOLTIPS_InitSystemSettings (infoPtr);
2198 
2199     return 0;
2200 }
2201 
2202 
2203 static LRESULT CALLBACK
2204 TOOLTIPS_SubclassProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2205 {
2206     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr ((HWND)dwRef);
2207     MSG msg;
2208 
2209     switch (message)
2210     {
2211     case WM_MOUSEMOVE:
2212     case WM_LBUTTONDOWN:
2213     case WM_LBUTTONUP:
2214     case WM_MBUTTONDOWN:
2215     case WM_MBUTTONUP:
2216     case WM_RBUTTONDOWN:
2217     case WM_RBUTTONUP:
2218         if (infoPtr)
2219         {
2220             msg.hwnd = hwnd;
2221             msg.message = message;
2222             msg.wParam = wParam;
2223             msg.lParam = lParam;
2224             TOOLTIPS_RelayEvent(infoPtr, &msg);
2225         }
2226         break;
2227     case WM_NCDESTROY:
2228         RemoveWindowSubclass(hwnd, TOOLTIPS_SubclassProc, 1);
2229         break;
2230     default:
2231         break;
2232     }
2233 
2234     return DefSubclassProc(hwnd, message, wParam, lParam);
2235 }
2236 
2237 
2238 static LRESULT CALLBACK
2239 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2240 {
2241     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2242 
2243     TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2244     if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2245         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2246     switch (uMsg)
2247     {
2248 	case TTM_ACTIVATE:
2249 	    return TOOLTIPS_Activate (infoPtr, (BOOL)wParam);
2250 
2251 	case TTM_ADDTOOLA:
2252 	case TTM_ADDTOOLW:
2253 	    return TOOLTIPS_AddToolT (infoPtr, (LPTTTOOLINFOW)lParam, uMsg == TTM_ADDTOOLW);
2254 
2255 	case TTM_DELTOOLA:
2256 	case TTM_DELTOOLW:
2257 	    return TOOLTIPS_DelToolT (infoPtr, (LPTOOLINFOW)lParam,
2258                                       uMsg == TTM_DELTOOLW);
2259 	case TTM_ENUMTOOLSA:
2260 	case TTM_ENUMTOOLSW:
2261 	    return TOOLTIPS_EnumToolsT (infoPtr, (UINT)wParam, (LPTTTOOLINFOW)lParam,
2262                                         uMsg == TTM_ENUMTOOLSW);
2263 	case TTM_GETBUBBLESIZE:
2264 	    return TOOLTIPS_GetBubbleSize (infoPtr, (LPTTTOOLINFOW)lParam);
2265 
2266 	case TTM_GETCURRENTTOOLA:
2267 	case TTM_GETCURRENTTOOLW:
2268 	    return TOOLTIPS_GetCurrentToolT (infoPtr, (LPTTTOOLINFOW)lParam,
2269                                              uMsg == TTM_GETCURRENTTOOLW);
2270 
2271 	case TTM_GETDELAYTIME:
2272 	    return TOOLTIPS_GetDelayTime (infoPtr, (DWORD)wParam);
2273 
2274 	case TTM_GETMARGIN:
2275 	    return TOOLTIPS_GetMargin (infoPtr, (LPRECT)lParam);
2276 
2277 	case TTM_GETMAXTIPWIDTH:
2278 	    return TOOLTIPS_GetMaxTipWidth (infoPtr);
2279 
2280 	case TTM_GETTEXTA:
2281 	case TTM_GETTEXTW:
2282 	    return TOOLTIPS_GetTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2283                                       uMsg == TTM_GETTEXTW);
2284 
2285 	case TTM_GETTIPBKCOLOR:
2286 	    return TOOLTIPS_GetTipBkColor (infoPtr);
2287 
2288 	case TTM_GETTIPTEXTCOLOR:
2289 	    return TOOLTIPS_GetTipTextColor (infoPtr);
2290 
2291 	case TTM_GETTOOLCOUNT:
2292 	    return TOOLTIPS_GetToolCount (infoPtr);
2293 
2294 	case TTM_GETTOOLINFOA:
2295 	case TTM_GETTOOLINFOW:
2296 	    return TOOLTIPS_GetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2297                                           uMsg == TTM_GETTOOLINFOW);
2298 
2299 	case TTM_HITTESTA:
2300 	case TTM_HITTESTW:
2301 	    return TOOLTIPS_HitTestT (infoPtr, (LPTTHITTESTINFOW)lParam,
2302                                       uMsg == TTM_HITTESTW);
2303 	case TTM_NEWTOOLRECTA:
2304 	case TTM_NEWTOOLRECTW:
2305 	    return TOOLTIPS_NewToolRectT (infoPtr, (LPTTTOOLINFOW)lParam);
2306 
2307 	case TTM_POP:
2308 	    return TOOLTIPS_Pop (infoPtr);
2309 
2310 	case TTM_RELAYEVENT:
2311 	    return TOOLTIPS_RelayEvent (infoPtr, (LPMSG)lParam);
2312 
2313 	case TTM_SETDELAYTIME:
2314 	    return TOOLTIPS_SetDelayTime (infoPtr, (DWORD)wParam, (INT)LOWORD(lParam));
2315 
2316 	case TTM_SETMARGIN:
2317 	    return TOOLTIPS_SetMargin (infoPtr, (LPRECT)lParam);
2318 
2319 	case TTM_SETMAXTIPWIDTH:
2320 	    return TOOLTIPS_SetMaxTipWidth (infoPtr, (INT)lParam);
2321 
2322 	case TTM_SETTIPBKCOLOR:
2323 	    return TOOLTIPS_SetTipBkColor (infoPtr, (COLORREF)wParam);
2324 
2325 	case TTM_SETTIPTEXTCOLOR:
2326 	    return TOOLTIPS_SetTipTextColor (infoPtr, (COLORREF)wParam);
2327 
2328 	case TTM_SETTITLEA:
2329 	case TTM_SETTITLEW:
2330 	    return TOOLTIPS_SetTitleT (infoPtr, (UINT_PTR)wParam, (LPCWSTR)lParam,
2331                                        uMsg == TTM_SETTITLEW);
2332 
2333 	case TTM_SETTOOLINFOA:
2334 	case TTM_SETTOOLINFOW:
2335 	    return TOOLTIPS_SetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2336                                           uMsg == TTM_SETTOOLINFOW);
2337 
2338 	case TTM_TRACKACTIVATE:
2339 	    return TOOLTIPS_TrackActivate (infoPtr, (BOOL)wParam, (LPTTTOOLINFOA)lParam);
2340 
2341 	case TTM_TRACKPOSITION:
2342 	    return TOOLTIPS_TrackPosition (infoPtr, lParam);
2343 
2344 	case TTM_UPDATE:
2345 	    return TOOLTIPS_Update (infoPtr);
2346 
2347 	case TTM_UPDATETIPTEXTA:
2348 	case TTM_UPDATETIPTEXTW:
2349 	    return TOOLTIPS_UpdateTipTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2350                                             uMsg == TTM_UPDATETIPTEXTW);
2351 
2352 	case TTM_WINDOWFROMPOINT:
2353 	    return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2354 
2355 	case WM_CREATE:
2356 	    return TOOLTIPS_Create (hwnd);
2357 
2358 	case WM_DESTROY:
2359 	    return TOOLTIPS_Destroy (infoPtr);
2360 
2361 	case WM_ERASEBKGND:
2362 	    /* we draw the background in WM_PAINT */
2363 	    return 0;
2364 
2365 	case WM_GETFONT:
2366 	    return TOOLTIPS_GetFont (infoPtr);
2367 
2368 	case WM_GETTEXT:
2369 	    return TOOLTIPS_OnWMGetText (infoPtr, wParam, (LPWSTR)lParam);
2370 
2371 	case WM_GETTEXTLENGTH:
2372 	    return TOOLTIPS_GetTextLength (infoPtr);
2373 
2374 	case WM_LBUTTONDOWN:
2375 	case WM_LBUTTONUP:
2376 	case WM_MBUTTONDOWN:
2377 	case WM_MBUTTONUP:
2378 	case WM_RBUTTONDOWN:
2379 	case WM_RBUTTONUP:
2380 	case WM_MOUSEMOVE:
2381 	    return TOOLTIPS_MouseMessage (infoPtr);
2382 
2383 	case WM_NCCREATE:
2384 	    return TOOLTIPS_NCCreate (hwnd);
2385 
2386 	case WM_NCHITTEST:
2387 	    return TOOLTIPS_NCHitTest (infoPtr, wParam, lParam);
2388 
2389 	case WM_NOTIFYFORMAT:
2390 	    return TOOLTIPS_NotifyFormat (infoPtr, wParam, lParam);
2391 
2392 	case WM_PRINTCLIENT:
2393 	case WM_PAINT:
2394 	    return TOOLTIPS_Paint (infoPtr, (HDC)wParam);
2395 
2396 	case WM_SETFONT:
2397 	    return TOOLTIPS_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
2398 
2399 	case WM_SYSCOLORCHANGE:
2400 	    COMCTL32_RefreshSysColors();
2401 	    return 0;
2402 
2403 	case WM_TIMER:
2404 	    return TOOLTIPS_Timer (infoPtr, (INT)wParam);
2405 
2406 	case WM_WININICHANGE:
2407 	    return TOOLTIPS_WinIniChange (infoPtr);
2408 
2409 	default:
2410 	    if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2411 		ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2412 		     uMsg, wParam, lParam);
2413 	    return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2414     }
2415 }
2416 
2417 
2418 VOID
2419 TOOLTIPS_Register (void)
2420 {
2421     WNDCLASSW wndClass;
2422 
2423     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2424     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2425     wndClass.lpfnWndProc   = TOOLTIPS_WindowProc;
2426     wndClass.cbClsExtra    = 0;
2427     wndClass.cbWndExtra    = sizeof(TOOLTIPS_INFO *);
2428     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2429     wndClass.hbrBackground = 0;
2430     wndClass.lpszClassName = TOOLTIPS_CLASSW;
2431 
2432     RegisterClassW (&wndClass);
2433 
2434     hTooltipIcons[TTI_NONE] = NULL;
2435     hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2436         (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2437     hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2438         (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2439     hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2440         (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2441 }
2442 
2443 
2444 VOID
2445 TOOLTIPS_Unregister (void)
2446 {
2447     int i;
2448     for (i = TTI_INFO; i <= TTI_ERROR; i++)
2449         DestroyIcon(hTooltipIcons[i]);
2450     UnregisterClassW (TOOLTIPS_CLASSW, NULL);
2451 }
2452