comctl32: Disable propsheet apply button at start up.
[wine] / dlls / comctl32 / tooltips.c
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     HWND      hwnd;
115     BOOL      bNotifyUnicode;
116     UINT_PTR  uId;
117     RECT      rect;
118     HINSTANCE hinst;
119     LPWSTR      lpszText;
120     LPARAM      lParam;
121 } TTTOOL_INFO;
122
123
124 typedef struct
125 {
126     WCHAR      szTipText[INFOTIPSIZE];
127     BOOL     bActive;
128     BOOL     bTrackActive;
129     UINT     uNumTools;
130     COLORREF   clrBk;
131     COLORREF   clrText;
132     HFONT    hFont;
133     HFONT    hTitleFont;
134     INT      xTrackPos;
135     INT      yTrackPos;
136     INT      nMaxTipWidth;
137     INT      nTool; /* tool that mouse was on on last relayed mouse move */
138     INT      nCurrentTool;
139     INT      nTrackTool;
140     INT      nReshowTime;
141     INT      nAutoPopTime;
142     INT      nInitialTime;
143     RECT     rcMargin;
144     BOOL     bToolBelow;
145     LPWSTR   pszTitle;
146     HICON    hTitleIcon;
147
148     TTTOOL_INFO *tools;
149 } TOOLTIPS_INFO;
150
151 #define ID_TIMERSHOW   1    /* show delay timer */
152 #define ID_TIMERPOP    2    /* auto pop timer */
153 #define ID_TIMERLEAVE  3    /* tool leave timer */
154
155
156 #define TOOLTIPS_GetInfoPtr(hWindow) ((TOOLTIPS_INFO *)GetWindowLongPtrW (hWindow, 0))
157
158 /* offsets from window edge to start of text */
159 #define NORMAL_TEXT_MARGIN 2
160 #define BALLOON_TEXT_MARGIN (NORMAL_TEXT_MARGIN+8)
161 /* value used for CreateRoundRectRgn that specifies how much
162  * each corner is curved */
163 #define BALLOON_ROUNDEDNESS 20
164 #define BALLOON_STEMHEIGHT 13
165 #define BALLOON_STEMWIDTH 10
166 #define BALLOON_STEMINDENT 20
167
168 #define BALLOON_ICON_TITLE_SPACING 8 /* horizontal spacing between icon and title */
169 #define BALLOON_TITLE_TEXT_SPACING 8 /* vertical spacing between icon/title and main text */
170 #define ICON_HEIGHT 16
171 #define ICON_WIDTH  16
172
173 static LRESULT CALLBACK
174 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uId, DWORD_PTR dwRef);
175
176
177 static inline UINT_PTR
178 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
179 {
180     UINT i;
181     for (i = 0; i <= TTI_ERROR; i++)
182         if (hTooltipIcons[i] == hIcon)
183             return i;
184     return (UINT_PTR)hIcon;
185 }
186
187 static void
188 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
189 {
190     NONCLIENTMETRICSW nclm;
191
192     infoPtr->clrBk   = GetSysColor (COLOR_INFOBK);
193     infoPtr->clrText = GetSysColor (COLOR_INFOTEXT);
194
195     DeleteObject (infoPtr->hFont);
196     nclm.cbSize = sizeof(nclm);
197     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
198     infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
199
200     DeleteObject (infoPtr->hTitleFont);
201     nclm.lfStatusFont.lfWeight = FW_BOLD;
202     infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
203 }
204
205 static void
206 TOOLTIPS_Refresh (HWND hwnd, HDC hdc)
207 {
208     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr(hwnd);
209     RECT rc;
210     INT oldBkMode;
211     HFONT hOldFont;
212     HBRUSH hBrush;
213     UINT uFlags = DT_EXTERNALLEADING;
214     HRGN hRgn = NULL;
215     DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
216
217     if (infoPtr->nMaxTipWidth > -1)
218         uFlags |= DT_WORDBREAK;
219     if (GetWindowLongW (hwnd, GWL_STYLE) & TTS_NOPREFIX)
220         uFlags |= DT_NOPREFIX;
221     GetClientRect (hwnd, &rc);
222
223     hBrush = CreateSolidBrush(infoPtr->clrBk);
224
225     oldBkMode = SetBkMode (hdc, TRANSPARENT);
226     SetTextColor (hdc, infoPtr->clrText);
227
228     if (dwStyle & TTS_BALLOON)
229     {
230         /* create a region to store result into */
231         hRgn = CreateRectRgn(0, 0, 0, 0);
232
233         GetWindowRgn(hwnd, hRgn);
234
235         /* fill the background */
236         FillRgn(hdc, hRgn, hBrush);
237         DeleteObject(hBrush);
238         hBrush = NULL;
239     }
240     else
241     {
242         /* fill the background */
243         FillRect(hdc, &rc, hBrush);
244         DeleteObject(hBrush);
245         hBrush = NULL;
246     }
247
248     if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
249     {
250         /* calculate text rectangle */
251         rc.left   += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
252         rc.top    += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
253         rc.right  -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
254         rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
255         if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
256
257         if (infoPtr->pszTitle)
258         {
259             RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
260             int height;
261             BOOL icon_present;
262
263             /* draw icon */
264             icon_present = infoPtr->hTitleIcon && 
265                 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
266                            ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
267             if (icon_present)
268                 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
269
270             rcTitle.bottom = rc.top + ICON_HEIGHT;
271
272             /* draw title text */
273             hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
274             height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
275             SelectObject (hdc, hOldFont);
276             rc.top += height + BALLOON_TITLE_TEXT_SPACING;
277         }
278     }
279     else
280     {
281         /* calculate text rectangle */
282         rc.left   += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
283         rc.top    += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
284         rc.right  -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
285         rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
286     }
287
288     /* draw text */
289     hOldFont = SelectObject (hdc, infoPtr->hFont);
290     DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
291     /* be polite and reset the things we changed in the dc */
292     SelectObject (hdc, hOldFont);
293     SetBkMode (hdc, oldBkMode);
294
295     if (dwStyle & TTS_BALLOON)
296     {
297         /* frame region because default window proc doesn't do it */
298         INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
299         INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
300
301         hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
302         FrameRgn(hdc, hRgn, hBrush, width, height);
303     }
304
305     if (hRgn)
306         DeleteObject(hRgn);
307 }
308
309 static void TOOLTIPS_GetDispInfoA(HWND hwnd, TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr)
310 {
311     NMTTDISPINFOA ttnmdi;
312
313     /* fill NMHDR struct */
314     ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
315     ttnmdi.hdr.hwndFrom = hwnd;
316     ttnmdi.hdr.idFrom = toolPtr->uId;
317     ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
318     ttnmdi.lpszText = (LPSTR)&ttnmdi.szText;
319     ttnmdi.uFlags = toolPtr->uFlags;
320     ttnmdi.lParam = toolPtr->lParam;
321
322     TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
323     SendMessageW(toolPtr->hwnd, WM_NOTIFY,
324                  (WPARAM)toolPtr->uId, (LPARAM)&ttnmdi);
325
326     if (IS_INTRESOURCE(ttnmdi.lpszText)) {
327         LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
328                infoPtr->szTipText, INFOTIPSIZE);
329         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
330             toolPtr->hinst = ttnmdi.hinst;
331             toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
332         }
333     }
334     else if (ttnmdi.lpszText == 0) {
335         infoPtr->szTipText[0] = '\0';
336     }
337     else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
338         Str_GetPtrAtoW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
339         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
340             toolPtr->hinst = 0;
341             toolPtr->lpszText = NULL;
342             Str_SetPtrW(&toolPtr->lpszText, infoPtr->szTipText);
343         }
344     }
345     else {
346         ERR("recursive text callback!\n");
347         infoPtr->szTipText[0] = '\0';
348     }
349
350     /* no text available - try calling parent instead as per native */
351     /* FIXME: Unsure if SETITEM should save the value or not        */
352     if (infoPtr->szTipText[0] == 0x00) {
353
354         SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY,
355                     (WPARAM)toolPtr->uId, (LPARAM)&ttnmdi);
356
357         if (IS_INTRESOURCE(ttnmdi.lpszText)) {
358             LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
359                    infoPtr->szTipText, INFOTIPSIZE);
360         } else if (ttnmdi.lpszText &&
361                    ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
362             Str_GetPtrAtoW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
363         }
364     }
365 }
366
367 static void TOOLTIPS_GetDispInfoW(HWND hwnd, TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr)
368 {
369     NMTTDISPINFOW ttnmdi;
370
371     /* fill NMHDR struct */
372     ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
373     ttnmdi.hdr.hwndFrom = hwnd;
374     ttnmdi.hdr.idFrom = toolPtr->uId;
375     ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
376     ttnmdi.lpszText = (LPWSTR)&ttnmdi.szText;
377     ttnmdi.uFlags = toolPtr->uFlags;
378     ttnmdi.lParam = toolPtr->lParam;
379
380     TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
381     SendMessageW(toolPtr->hwnd, WM_NOTIFY,
382                  (WPARAM)toolPtr->uId, (LPARAM)&ttnmdi);
383
384     if (IS_INTRESOURCE(ttnmdi.lpszText)) {
385         LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
386                infoPtr->szTipText, INFOTIPSIZE);
387         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
388             toolPtr->hinst = ttnmdi.hinst;
389             toolPtr->lpszText = ttnmdi.lpszText;
390         }
391     }
392     else if (ttnmdi.lpszText == 0) {
393         infoPtr->szTipText[0] = '\0';
394     }
395     else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
396         Str_GetPtrW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
397         if (ttnmdi.uFlags & TTF_DI_SETITEM) {
398             toolPtr->hinst = 0;
399             toolPtr->lpszText = NULL;
400             Str_SetPtrW(&toolPtr->lpszText, infoPtr->szTipText);
401         }
402     }
403     else {
404         ERR("recursive text callback!\n");
405         infoPtr->szTipText[0] = '\0';
406     }
407
408     /* no text available - try calling parent instead as per native */
409     /* FIXME: Unsure if SETITEM should save the value or not        */
410     if (infoPtr->szTipText[0] == 0x00) {
411
412         SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY,
413                     (WPARAM)toolPtr->uId, (LPARAM)&ttnmdi);
414
415         if (IS_INTRESOURCE(ttnmdi.lpszText)) {
416             LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
417                    infoPtr->szTipText, INFOTIPSIZE);
418         } else if (ttnmdi.lpszText &&
419                    ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
420             Str_GetPtrW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
421         }
422     }
423
424 }
425
426 static void
427 TOOLTIPS_GetTipText (HWND hwnd, TOOLTIPS_INFO *infoPtr, INT nTool)
428 {
429     TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
430
431     if (IS_INTRESOURCE(toolPtr->lpszText) && toolPtr->hinst) {
432         /* load a resource */
433         TRACE("load res string %p %x\n",
434                toolPtr->hinst, LOWORD(toolPtr->lpszText));
435         LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText),
436                        infoPtr->szTipText, INFOTIPSIZE);
437     }
438     else if (toolPtr->lpszText) {
439         if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
440             if (toolPtr->bNotifyUnicode)
441                 TOOLTIPS_GetDispInfoW(hwnd, infoPtr, toolPtr);
442             else
443                 TOOLTIPS_GetDispInfoA(hwnd, infoPtr, toolPtr);
444         }
445         else {
446             /* the item is a usual (unicode) text */
447             lstrcpynW (infoPtr->szTipText, toolPtr->lpszText, INFOTIPSIZE);
448         }
449     }
450     else {
451         /* no text available */
452         infoPtr->szTipText[0] = '\0';
453     }
454
455     TRACE("%s\n", debugstr_w(infoPtr->szTipText));
456 }
457
458
459 static void
460 TOOLTIPS_CalcTipSize (HWND hwnd, const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
461 {
462     HDC hdc;
463     HFONT hOldFont;
464     DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
465     UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
466     RECT rc = {0, 0, 0, 0};
467     SIZE title = {0, 0};
468
469     if (infoPtr->nMaxTipWidth > -1) {
470         rc.right = infoPtr->nMaxTipWidth;
471         uFlags |= DT_WORDBREAK;
472     }
473     if (style & TTS_NOPREFIX)
474         uFlags |= DT_NOPREFIX;
475     TRACE("%s\n", debugstr_w(infoPtr->szTipText));
476
477     hdc = GetDC (hwnd);
478     if (infoPtr->pszTitle)
479     {
480         RECT rcTitle = {0, 0, 0, 0};
481         TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
482         if (infoPtr->hTitleIcon)
483         {
484             title.cx = ICON_WIDTH;
485             title.cy = ICON_HEIGHT;
486         }
487         if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
488         hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
489         DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
490         SelectObject (hdc, hOldFont);
491         title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
492         title.cx += (rcTitle.right - rcTitle.left);
493     }
494     hOldFont = SelectObject (hdc, infoPtr->hFont);
495     DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
496     SelectObject (hdc, hOldFont);
497     ReleaseDC (hwnd, hdc);
498
499     if ((style & TTS_BALLOON) || infoPtr->pszTitle)
500     {
501         lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
502                        infoPtr->rcMargin.left + infoPtr->rcMargin.right;
503         lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
504                        infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
505                        BALLOON_STEMHEIGHT;
506     }
507     else
508     {
509         lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
510                        infoPtr->rcMargin.left + infoPtr->rcMargin.right;
511         lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
512                        infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
513     }
514 }
515
516
517 static void
518 TOOLTIPS_Show (HWND hwnd, TOOLTIPS_INFO *infoPtr, BOOL track_activate)
519 {
520     TTTOOL_INFO *toolPtr;
521     HMONITOR monitor;
522     MONITORINFO mon_info;
523     RECT rect;
524     SIZE size;
525     NMHDR  hdr;
526     int ptfx = 0;
527     DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
528     INT nTool;
529
530     if (track_activate)
531     {
532         if (infoPtr->nTrackTool == -1)
533         {
534             TRACE("invalid tracking tool (-1)!\n");
535             return;
536         }
537         nTool = infoPtr->nTrackTool;
538     }
539     else
540     {
541         if (infoPtr->nTool == -1)
542         {
543             TRACE("invalid tool (-1)!\n");
544                 return;
545         }
546         nTool = infoPtr->nTool;
547     }
548
549     TRACE("Show tooltip pre %d! (%p)\n", nTool, hwnd);
550
551     TOOLTIPS_GetTipText (hwnd, infoPtr, nTool);
552
553     if (infoPtr->szTipText[0] == '\0')
554         return;
555
556     toolPtr = &infoPtr->tools[nTool];
557
558     if (!track_activate)
559         infoPtr->nCurrentTool = infoPtr->nTool;
560
561     TRACE("Show tooltip %d!\n", nTool);
562
563     hdr.hwndFrom = hwnd;
564     hdr.idFrom = toolPtr->uId;
565     hdr.code = TTN_SHOW;
566     SendMessageW (toolPtr->hwnd, WM_NOTIFY,
567                     (WPARAM)toolPtr->uId, (LPARAM)&hdr);
568
569     TRACE("%s\n", debugstr_w(infoPtr->szTipText));
570
571     TOOLTIPS_CalcTipSize (hwnd, infoPtr, &size);
572     TRACE("size %d x %d\n", size.cx, size.cy);
573
574     if (track_activate)
575     {
576         rect.left = infoPtr->xTrackPos;
577         rect.top  = infoPtr->yTrackPos;
578         ptfx = rect.left;
579
580         if (toolPtr->uFlags & TTF_CENTERTIP)
581         {
582             rect.left -= (size.cx / 2);
583             if (!(style & TTS_BALLOON))
584                 rect.top  -= (size.cy / 2);
585         }
586         infoPtr->bToolBelow = TRUE;
587
588         if (!(toolPtr->uFlags & TTF_ABSOLUTE))
589         {
590             if (style & TTS_BALLOON)
591                 rect.left -= BALLOON_STEMINDENT;
592             else
593             {
594                 RECT rcTool;
595
596                 if (toolPtr->uFlags & TTF_IDISHWND)
597                     GetWindowRect ((HWND)toolPtr->uId, &rcTool);
598                 else
599                 {
600                     rcTool = toolPtr->rect;
601                     MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
602                 }
603
604                 /* smart placement */
605                 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
606                     (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
607                     rect.left = rcTool.right;
608             }
609         }
610     }
611     else
612     {
613         if (toolPtr->uFlags & TTF_CENTERTIP)
614         {
615                 RECT rc;
616
617             if (toolPtr->uFlags & TTF_IDISHWND)
618                 GetWindowRect ((HWND)toolPtr->uId, &rc);
619             else {
620                 rc = toolPtr->rect;
621                 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
622             }
623             rect.left = (rc.left + rc.right - size.cx) / 2;
624             if (style & TTS_BALLOON)
625             {
626                 ptfx = rc.left + ((rc.right - rc.left) / 2);
627
628                 /* CENTERTIP ballon tooltips default to below the field
629                  * if they fit on the screen */
630                 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
631                 {
632                     rect.top = rc.top - size.cy;
633                     infoPtr->bToolBelow = FALSE;
634                 }
635                 else
636                 {
637                     infoPtr->bToolBelow = TRUE;
638                     rect.top = rc.bottom;
639                 }
640                 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
641             }
642             else
643             {
644                 rect.top  = rc.bottom + 2;
645                 infoPtr->bToolBelow = TRUE;
646             }
647         }
648         else
649         {
650             GetCursorPos ((LPPOINT)&rect);
651             if (style & TTS_BALLOON)
652             {
653                 ptfx = rect.left;
654                 if(rect.top - size.cy >= 0)
655                 {
656                     rect.top -= size.cy;
657                     infoPtr->bToolBelow = FALSE;
658                 }
659                 else
660                 {
661                     infoPtr->bToolBelow = TRUE;
662                     rect.top += 20;
663                 }
664                 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
665             }
666             else
667             {
668                     rect.top += 20;
669                     infoPtr->bToolBelow = TRUE;
670             }
671         }
672     }
673
674     TRACE("pos %d - %d\n", rect.left, rect.top);
675
676     rect.right = rect.left + size.cx;
677     rect.bottom = rect.top + size.cy;
678
679     /* check position */
680
681     monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
682     mon_info.cbSize = sizeof(mon_info);
683     GetMonitorInfoW( monitor, &mon_info );
684
685     if( rect.right > mon_info.rcWork.right ) {
686         rect.left -= rect.right - mon_info.rcWork.right + 2;
687         rect.right = mon_info.rcWork.right - 2;
688     }
689     if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
690
691     if( rect.bottom > mon_info.rcWork.bottom ) {
692         RECT rc;
693
694         if (toolPtr->uFlags & TTF_IDISHWND)
695             GetWindowRect ((HWND)toolPtr->uId, &rc);
696         else {
697             rc = toolPtr->rect;
698             MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
699         }
700         rect.bottom = rc.top - 2;
701         rect.top = rect.bottom - size.cy;
702     }
703
704     AdjustWindowRectEx (&rect, GetWindowLongW (hwnd, GWL_STYLE),
705                         FALSE, GetWindowLongW (hwnd, GWL_EXSTYLE));
706
707     if (style & TTS_BALLOON)
708     {
709         HRGN hRgn;
710         HRGN hrStem;
711         POINT pts[3];
712
713         ptfx -= rect.left;
714
715         if(infoPtr->bToolBelow)
716         {
717           pts[0].x = ptfx;
718           pts[0].y = 0;
719           pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
720           pts[1].y = BALLOON_STEMHEIGHT;
721           pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
722           pts[2].y = pts[1].y;
723           if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
724           {
725             pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
726             pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
727           }
728         }
729         else
730         {
731           pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
732           pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
733           pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
734           pts[1].y = pts[0].y;
735           pts[2].x = ptfx;
736           pts[2].y = (rect.bottom - rect.top);
737           if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
738           {
739             pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
740             pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
741           }
742         }
743
744         hrStem = CreatePolygonRgn(pts, sizeof(pts) / sizeof(pts[0]), ALTERNATE);
745         
746         hRgn = CreateRoundRectRgn(0,
747                                   (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
748                                   rect.right - rect.left,
749                                   (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
750                                   BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
751
752         CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
753         DeleteObject(hrStem);
754
755         SetWindowRgn(hwnd, hRgn, FALSE);
756         /* we don't free the region handle as the system deletes it when 
757          * it is no longer needed */
758     }
759
760     SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
761                     rect.right - rect.left, rect.bottom - rect.top,
762                     SWP_SHOWWINDOW | SWP_NOACTIVATE);
763
764     /* repaint the tooltip */
765     InvalidateRect(hwnd, NULL, TRUE);
766     UpdateWindow(hwnd);
767
768     if (!track_activate)
769     {
770         SetTimer (hwnd, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
771         TRACE("timer 2 started!\n");
772         SetTimer (hwnd, ID_TIMERLEAVE, infoPtr->nReshowTime, 0);
773         TRACE("timer 3 started!\n");
774     }
775 }
776
777
778 static void
779 TOOLTIPS_Hide (HWND hwnd, TOOLTIPS_INFO *infoPtr)
780 {
781     TTTOOL_INFO *toolPtr;
782     NMHDR hdr;
783
784     TRACE("Hide tooltip %d! (%p)\n", infoPtr->nCurrentTool, hwnd);
785
786     if (infoPtr->nCurrentTool == -1)
787         return;
788
789     toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
790     KillTimer (hwnd, ID_TIMERPOP);
791
792     hdr.hwndFrom = hwnd;
793     hdr.idFrom = toolPtr->uId;
794     hdr.code = TTN_POP;
795     SendMessageW (toolPtr->hwnd, WM_NOTIFY,
796                     (WPARAM)toolPtr->uId, (LPARAM)&hdr);
797
798     infoPtr->nCurrentTool = -1;
799
800     SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
801                     SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
802 }
803
804
805 static void
806 TOOLTIPS_TrackShow (HWND hwnd, TOOLTIPS_INFO *infoPtr)
807 {
808     TOOLTIPS_Show(hwnd, infoPtr, TRUE);
809 }
810
811
812 static void
813 TOOLTIPS_TrackHide (HWND hwnd, const TOOLTIPS_INFO *infoPtr)
814 {
815     TTTOOL_INFO *toolPtr;
816     NMHDR hdr;
817
818     TRACE("hide tracking tooltip %d\n", infoPtr->nTrackTool);
819
820     if (infoPtr->nTrackTool == -1)
821         return;
822
823     toolPtr = &infoPtr->tools[infoPtr->nTrackTool];
824
825     hdr.hwndFrom = hwnd;
826     hdr.idFrom = toolPtr->uId;
827     hdr.code = TTN_POP;
828     SendMessageW (toolPtr->hwnd, WM_NOTIFY,
829                     (WPARAM)toolPtr->uId, (LPARAM)&hdr);
830
831     SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
832                     SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
833 }
834
835
836 static INT
837 TOOLTIPS_GetToolFromInfoA (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOA *lpToolInfo)
838 {
839     TTTOOL_INFO *toolPtr;
840     INT nTool;
841
842     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
843         toolPtr = &infoPtr->tools[nTool];
844
845         if (!(toolPtr->uFlags & TTF_IDISHWND) &&
846             (lpToolInfo->hwnd == toolPtr->hwnd) &&
847             (lpToolInfo->uId == toolPtr->uId))
848             return nTool;
849     }
850
851     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
852         toolPtr = &infoPtr->tools[nTool];
853
854         if ((toolPtr->uFlags & TTF_IDISHWND) &&
855             (lpToolInfo->uId == toolPtr->uId))
856             return nTool;
857     }
858
859     return -1;
860 }
861
862
863 static INT
864 TOOLTIPS_GetToolFromInfoW (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
865 {
866     TTTOOL_INFO *toolPtr;
867     INT nTool;
868
869     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
870         toolPtr = &infoPtr->tools[nTool];
871
872         if (!(toolPtr->uFlags & TTF_IDISHWND) &&
873             (lpToolInfo->hwnd == toolPtr->hwnd) &&
874             (lpToolInfo->uId == toolPtr->uId))
875             return nTool;
876     }
877
878     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
879         toolPtr = &infoPtr->tools[nTool];
880
881         if ((toolPtr->uFlags & TTF_IDISHWND) &&
882             (lpToolInfo->uId == toolPtr->uId))
883             return nTool;
884     }
885
886     return -1;
887 }
888
889
890 static INT
891 TOOLTIPS_GetToolFromPoint (const TOOLTIPS_INFO *infoPtr, HWND hwnd, const POINT *lpPt)
892 {
893     TTTOOL_INFO *toolPtr;
894     INT  nTool;
895
896     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
897         toolPtr = &infoPtr->tools[nTool];
898
899         if (!(toolPtr->uFlags & TTF_IDISHWND)) {
900             if (hwnd != toolPtr->hwnd)
901                 continue;
902             if (!PtInRect (&toolPtr->rect, *lpPt))
903                 continue;
904             return nTool;
905         }
906     }
907
908     for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
909         toolPtr = &infoPtr->tools[nTool];
910
911         if (toolPtr->uFlags & TTF_IDISHWND) {
912             if ((HWND)toolPtr->uId == hwnd)
913                 return nTool;
914         }
915     }
916
917     return -1;
918 }
919
920
921 static BOOL
922 TOOLTIPS_IsWindowActive (HWND hwnd)
923 {
924     HWND hwndActive = GetActiveWindow ();
925     if (!hwndActive)
926         return FALSE;
927     if (hwndActive == hwnd)
928         return TRUE;
929     return IsChild (hwndActive, hwnd);
930 }
931
932
933 static INT
934 TOOLTIPS_CheckTool (HWND hwnd, BOOL bShowTest)
935 {
936     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
937     POINT pt;
938     HWND hwndTool;
939     INT nTool;
940
941     GetCursorPos (&pt);
942     hwndTool = (HWND)SendMessageW (hwnd, TTM_WINDOWFROMPOINT, 0, (LPARAM)&pt);
943     if (hwndTool == 0)
944         return -1;
945
946     ScreenToClient (hwndTool, &pt);
947     nTool = TOOLTIPS_GetToolFromPoint (infoPtr, hwndTool, &pt);
948     if (nTool == -1)
949         return -1;
950
951     if (!(GetWindowLongW (hwnd, GWL_STYLE) & TTS_ALWAYSTIP) && bShowTest) {
952         if (!TOOLTIPS_IsWindowActive (GetWindow (hwnd, GW_OWNER)))
953             return -1;
954     }
955
956     TRACE("tool %d\n", nTool);
957
958     return nTool;
959 }
960
961
962 static LRESULT
963 TOOLTIPS_Activate (HWND hwnd, WPARAM wParam, LPARAM lParam)
964 {
965     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
966
967     infoPtr->bActive = (BOOL)wParam;
968
969     if (infoPtr->bActive)
970         TRACE("activate!\n");
971
972     if (!(infoPtr->bActive) && (infoPtr->nCurrentTool != -1))
973         TOOLTIPS_Hide (hwnd, infoPtr);
974
975     return 0;
976 }
977
978
979 static LRESULT
980 TOOLTIPS_AddToolA (HWND hwnd, WPARAM wParam, LPARAM lParam)
981 {
982     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
983     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
984     TTTOOL_INFO *toolPtr;
985     INT nResult;
986
987     if (lpToolInfo == NULL)
988         return FALSE;
989     if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
990         return FALSE;
991
992     TRACE("add tool (%p) %p %ld%s!\n",
993            hwnd, lpToolInfo->hwnd, lpToolInfo->uId,
994            (lpToolInfo->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
995
996     if (infoPtr->uNumTools == 0) {
997         infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
998         toolPtr = infoPtr->tools;
999     }
1000     else {
1001         TTTOOL_INFO *oldTools = infoPtr->tools;
1002         infoPtr->tools =
1003             Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1004         memcpy (infoPtr->tools, oldTools,
1005                 infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1006         Free (oldTools);
1007         toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1008     }
1009
1010     infoPtr->uNumTools++;
1011
1012     /* copy tool data */
1013     toolPtr->uFlags = lpToolInfo->uFlags;
1014     toolPtr->hwnd   = lpToolInfo->hwnd;
1015     toolPtr->uId    = lpToolInfo->uId;
1016     toolPtr->rect   = lpToolInfo->rect;
1017     toolPtr->hinst  = lpToolInfo->hinst;
1018
1019     if (IS_INTRESOURCE(lpToolInfo->lpszText)) {
1020         TRACE("add string id %x!\n", LOWORD(lpToolInfo->lpszText));
1021         toolPtr->lpszText = (LPWSTR)lpToolInfo->lpszText;
1022     }
1023     else if (lpToolInfo->lpszText) {
1024         if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKA) {
1025             TRACE("add CALLBACK!\n");
1026             toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1027         }
1028         else {
1029             INT len = MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
1030                                           NULL, 0);
1031             TRACE("add text \"%s\"!\n", lpToolInfo->lpszText);
1032             toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1033             MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
1034                                 toolPtr->lpszText, len);
1035         }
1036     }
1037
1038     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOA))
1039         toolPtr->lParam = lpToolInfo->lParam;
1040
1041     /* install subclassing hook */
1042     if (toolPtr->uFlags & TTF_SUBCLASS) {
1043         if (toolPtr->uFlags & TTF_IDISHWND) {
1044             SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1045                                (DWORD_PTR)hwnd);
1046         }
1047         else {
1048             SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1049                               (DWORD_PTR)hwnd);
1050         }
1051         TRACE("subclassing installed!\n");
1052     }
1053
1054     nResult = (INT) SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1055                                   (WPARAM)hwnd, (LPARAM)NF_QUERY);
1056     if (nResult == NFR_ANSI) {
1057         toolPtr->bNotifyUnicode = FALSE;
1058         TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1059     } else if (nResult == NFR_UNICODE) {
1060         toolPtr->bNotifyUnicode = TRUE;
1061         TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1062     } else {
1063         TRACE (" -- WM_NOTIFYFORMAT returns: error!\n");
1064     }
1065
1066     return TRUE;
1067 }
1068
1069
1070 static LRESULT
1071 TOOLTIPS_AddToolW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1072 {
1073     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1074     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
1075     TTTOOL_INFO *toolPtr;
1076     INT nResult;
1077
1078     if (lpToolInfo == NULL)
1079         return FALSE;
1080     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1081         return FALSE;
1082
1083     TRACE("add tool (%p) %p %ld%s!\n",
1084            hwnd, lpToolInfo->hwnd, lpToolInfo->uId,
1085            (lpToolInfo->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
1086
1087     if (infoPtr->uNumTools == 0) {
1088         infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
1089         toolPtr = infoPtr->tools;
1090     }
1091     else {
1092         TTTOOL_INFO *oldTools = infoPtr->tools;
1093         infoPtr->tools =
1094             Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1095         memcpy (infoPtr->tools, oldTools,
1096                 infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1097         Free (oldTools);
1098         toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1099     }
1100
1101     infoPtr->uNumTools++;
1102
1103     /* copy tool data */
1104     toolPtr->uFlags = lpToolInfo->uFlags;
1105     toolPtr->hwnd   = lpToolInfo->hwnd;
1106     toolPtr->uId    = lpToolInfo->uId;
1107     toolPtr->rect   = lpToolInfo->rect;
1108     toolPtr->hinst  = lpToolInfo->hinst;
1109
1110     if (IS_INTRESOURCE(lpToolInfo->lpszText)) {
1111         TRACE("add string id %x\n", LOWORD(lpToolInfo->lpszText));
1112         toolPtr->lpszText = lpToolInfo->lpszText;
1113     }
1114     else if (lpToolInfo->lpszText) {
1115         if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKW) {
1116             TRACE("add CALLBACK!\n");
1117             toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1118         }
1119         else {
1120             INT len = lstrlenW (lpToolInfo->lpszText);
1121             TRACE("add text %s!\n",
1122                    debugstr_w(lpToolInfo->lpszText));
1123             toolPtr->lpszText = Alloc ((len + 1)*sizeof(WCHAR));
1124             strcpyW (toolPtr->lpszText, lpToolInfo->lpszText);
1125         }
1126     }
1127
1128     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOW))
1129         toolPtr->lParam = lpToolInfo->lParam;
1130
1131     /* install subclassing hook */
1132     if (toolPtr->uFlags & TTF_SUBCLASS) {
1133         if (toolPtr->uFlags & TTF_IDISHWND) {
1134             SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1135                               (DWORD_PTR)hwnd);
1136         }
1137         else {
1138             SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1139                               (DWORD_PTR)hwnd);
1140         }
1141         TRACE("subclassing installed!\n");
1142     }
1143
1144     nResult = (INT) SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1145                                   (WPARAM)hwnd, (LPARAM)NF_QUERY);
1146     if (nResult == NFR_ANSI) {
1147         toolPtr->bNotifyUnicode = FALSE;
1148         TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1149     } else if (nResult == NFR_UNICODE) {
1150         toolPtr->bNotifyUnicode = TRUE;
1151         TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1152     } else {
1153         TRACE (" -- WM_NOTIFYFORMAT returns: error!\n");
1154     }
1155
1156     return TRUE;
1157 }
1158
1159
1160 static void
1161 TOOLTIPS_DelToolCommon (HWND hwnd, TOOLTIPS_INFO *infoPtr, INT nTool)
1162 {
1163     TTTOOL_INFO *toolPtr;
1164
1165     TRACE("tool %d\n", nTool);
1166
1167     if (nTool == -1)
1168         return;
1169
1170     /* make sure the tooltip has disappeared before deleting it */
1171     TOOLTIPS_Hide(hwnd, infoPtr);
1172
1173     /* delete text string */
1174     toolPtr = &infoPtr->tools[nTool];
1175     if (toolPtr->lpszText) {
1176         if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1177              !IS_INTRESOURCE(toolPtr->lpszText) )
1178             Free (toolPtr->lpszText);
1179     }
1180
1181     /* remove subclassing */
1182     if (toolPtr->uFlags & TTF_SUBCLASS) {
1183         if (toolPtr->uFlags & TTF_IDISHWND) {
1184             RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
1185         }
1186         else {
1187             RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
1188         }
1189     }
1190
1191     /* delete tool from tool list */
1192     if (infoPtr->uNumTools == 1) {
1193         Free (infoPtr->tools);
1194         infoPtr->tools = NULL;
1195     }
1196     else {
1197         TTTOOL_INFO *oldTools = infoPtr->tools;
1198         infoPtr->tools =
1199             Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1200
1201         if (nTool > 0)
1202             memcpy (&infoPtr->tools[0], &oldTools[0],
1203                     nTool * sizeof(TTTOOL_INFO));
1204
1205         if (nTool < infoPtr->uNumTools - 1)
1206             memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1207                     (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1208
1209         Free (oldTools);
1210     }
1211
1212     /* update any indices affected by delete */
1213
1214     /* destroying tool that mouse was on on last relayed mouse move */
1215     if (infoPtr->nTool == nTool)
1216         /* -1 means no current tool (0 means first tool) */
1217         infoPtr->nTool = -1;
1218     else if (infoPtr->nTool > nTool)
1219         infoPtr->nTool--;
1220
1221     if (infoPtr->nTrackTool == nTool)
1222         /* -1 means no current tool (0 means first tool) */
1223         infoPtr->nTrackTool = -1;
1224     else if (infoPtr->nTrackTool > nTool)
1225         infoPtr->nTrackTool--;
1226
1227     if (infoPtr->nCurrentTool == nTool)
1228         /* -1 means no current tool (0 means first tool) */
1229         infoPtr->nCurrentTool = -1;
1230     else if (infoPtr->nCurrentTool > nTool)
1231         infoPtr->nCurrentTool--;
1232
1233     infoPtr->uNumTools--;
1234 }
1235
1236 static LRESULT
1237 TOOLTIPS_DelToolA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1238 {
1239     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1240     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
1241     INT nTool;
1242
1243     if (lpToolInfo == NULL)
1244         return 0;
1245     if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
1246         return 0;
1247     if (infoPtr->uNumTools == 0)
1248         return 0;
1249
1250     nTool = TOOLTIPS_GetToolFromInfoA (infoPtr, lpToolInfo);
1251
1252     TOOLTIPS_DelToolCommon (hwnd, infoPtr, nTool);
1253
1254     return 0;
1255 }
1256
1257
1258 static LRESULT
1259 TOOLTIPS_DelToolW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1260 {
1261     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1262     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
1263     INT nTool;
1264
1265     if (lpToolInfo == NULL)
1266         return 0;
1267     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1268         return 0;
1269     if (infoPtr->uNumTools == 0)
1270         return 0;
1271
1272     nTool = TOOLTIPS_GetToolFromInfoW (infoPtr, lpToolInfo);
1273
1274     TOOLTIPS_DelToolCommon (hwnd, infoPtr, nTool);
1275
1276     return 0;
1277 }
1278
1279
1280 static LRESULT
1281 TOOLTIPS_EnumToolsA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1282 {
1283     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1284     UINT uIndex = (UINT)wParam;
1285     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
1286     TTTOOL_INFO *toolPtr;
1287
1288     if (lpToolInfo == NULL)
1289         return FALSE;
1290     if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
1291         return FALSE;
1292     if (uIndex >= infoPtr->uNumTools)
1293         return FALSE;
1294
1295     TRACE("index=%u\n", uIndex);
1296
1297     toolPtr = &infoPtr->tools[uIndex];
1298
1299     /* copy tool data */
1300     lpToolInfo->uFlags   = toolPtr->uFlags;
1301     lpToolInfo->hwnd     = toolPtr->hwnd;
1302     lpToolInfo->uId      = toolPtr->uId;
1303     lpToolInfo->rect     = toolPtr->rect;
1304     lpToolInfo->hinst    = toolPtr->hinst;
1305 /*    lpToolInfo->lpszText = toolPtr->lpszText; */
1306     lpToolInfo->lpszText = NULL;  /* FIXME */
1307
1308     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOA))
1309         lpToolInfo->lParam = toolPtr->lParam;
1310
1311     return TRUE;
1312 }
1313
1314
1315 static LRESULT
1316 TOOLTIPS_EnumToolsW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1317 {
1318     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1319     UINT uIndex = (UINT)wParam;
1320     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
1321     TTTOOL_INFO *toolPtr;
1322
1323     if (lpToolInfo == NULL)
1324         return FALSE;
1325     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1326         return FALSE;
1327     if (uIndex >= infoPtr->uNumTools)
1328         return FALSE;
1329
1330     TRACE("index=%u\n", uIndex);
1331
1332     toolPtr = &infoPtr->tools[uIndex];
1333
1334     /* copy tool data */
1335     lpToolInfo->uFlags   = toolPtr->uFlags;
1336     lpToolInfo->hwnd     = toolPtr->hwnd;
1337     lpToolInfo->uId      = toolPtr->uId;
1338     lpToolInfo->rect     = toolPtr->rect;
1339     lpToolInfo->hinst    = toolPtr->hinst;
1340 /*    lpToolInfo->lpszText = toolPtr->lpszText; */
1341     lpToolInfo->lpszText = NULL;  /* FIXME */
1342
1343     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOW))
1344         lpToolInfo->lParam = toolPtr->lParam;
1345
1346     return TRUE;
1347 }
1348
1349 static LRESULT
1350 TOOLTIPS_GetBubbleSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
1351 {
1352     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1353     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
1354     INT nTool;
1355     SIZE size;
1356
1357     if (lpToolInfo == NULL)
1358         return FALSE;
1359     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1360         return FALSE;
1361
1362     nTool = TOOLTIPS_GetToolFromInfoW (infoPtr, lpToolInfo);
1363     if (nTool == -1) return 0;
1364
1365     TRACE("tool %d\n", nTool);
1366
1367     TOOLTIPS_CalcTipSize (hwnd, infoPtr, &size);
1368     TRACE("size %d x %d\n", size.cx, size.cy);
1369
1370     return MAKELRESULT(size.cx, size.cy);
1371 }
1372
1373 static LRESULT
1374 TOOLTIPS_GetCurrentToolA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1375 {
1376     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1377     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
1378     TTTOOL_INFO *toolPtr;
1379
1380     if (lpToolInfo) {
1381         if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
1382             return FALSE;
1383
1384         if (infoPtr->nCurrentTool > -1) {
1385             toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
1386
1387             /* copy tool data */
1388             lpToolInfo->uFlags   = toolPtr->uFlags;
1389             lpToolInfo->rect     = toolPtr->rect;
1390             lpToolInfo->hinst    = toolPtr->hinst;
1391 /*          lpToolInfo->lpszText = toolPtr->lpszText; */
1392             lpToolInfo->lpszText = NULL;  /* FIXME */
1393
1394             if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOA))
1395                 lpToolInfo->lParam = toolPtr->lParam;
1396
1397             return TRUE;
1398         }
1399         else
1400             return FALSE;
1401     }
1402     else
1403         return (infoPtr->nCurrentTool != -1);
1404 }
1405
1406
1407 static LRESULT
1408 TOOLTIPS_GetCurrentToolW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1409 {
1410     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1411     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
1412     TTTOOL_INFO *toolPtr;
1413
1414     if (lpToolInfo) {
1415         if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1416             return FALSE;
1417
1418         if (infoPtr->nCurrentTool > -1) {
1419             toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
1420
1421             /* copy tool data */
1422             lpToolInfo->uFlags   = toolPtr->uFlags;
1423             lpToolInfo->rect     = toolPtr->rect;
1424             lpToolInfo->hinst    = toolPtr->hinst;
1425 /*          lpToolInfo->lpszText = toolPtr->lpszText; */
1426             lpToolInfo->lpszText = NULL;  /* FIXME */
1427
1428             if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOW))
1429                 lpToolInfo->lParam = toolPtr->lParam;
1430
1431             return TRUE;
1432         }
1433         else
1434             return FALSE;
1435     }
1436     else
1437         return (infoPtr->nCurrentTool != -1);
1438 }
1439
1440
1441 static LRESULT
1442 TOOLTIPS_GetDelayTime (HWND hwnd, WPARAM wParam, LPARAM lParam)
1443 {
1444     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1445
1446     switch (wParam) {
1447     case TTDT_RESHOW:
1448         return infoPtr->nReshowTime;
1449
1450     case TTDT_AUTOPOP:
1451         return infoPtr->nAutoPopTime;
1452
1453     case TTDT_INITIAL:
1454     case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1455         return infoPtr->nInitialTime;
1456
1457     default:
1458         WARN("Invalid wParam %lx\n", wParam);
1459         break;
1460     }
1461
1462     return -1;
1463 }
1464
1465
1466 static LRESULT
1467 TOOLTIPS_GetMargin (HWND hwnd, WPARAM wParam, LPARAM lParam)
1468 {
1469     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1470     LPRECT lpRect = (LPRECT)lParam;
1471
1472     lpRect->left   = infoPtr->rcMargin.left;
1473     lpRect->right  = infoPtr->rcMargin.right;
1474     lpRect->bottom = infoPtr->rcMargin.bottom;
1475     lpRect->top    = infoPtr->rcMargin.top;
1476
1477     return 0;
1478 }
1479
1480
1481 static inline LRESULT
1482 TOOLTIPS_GetMaxTipWidth (HWND hwnd, WPARAM wParam, LPARAM lParam)
1483 {
1484     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1485
1486     return infoPtr->nMaxTipWidth;
1487 }
1488
1489
1490 static LRESULT
1491 TOOLTIPS_GetTextA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1492 {
1493     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1494     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
1495     INT nTool;
1496
1497     if (lpToolInfo == NULL)
1498         return 0;
1499     if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
1500         return 0;
1501
1502     nTool = TOOLTIPS_GetToolFromInfoA (infoPtr, lpToolInfo);
1503     if (nTool == -1) return 0;
1504
1505     /* NB this API is broken, there is no way for the app to determine
1506        what size buffer it requires nor a way to specify how long the
1507        one it supplies is.  We'll assume it's up to INFOTIPSIZE */
1508
1509     WideCharToMultiByte(CP_ACP, 0, infoPtr->tools[nTool].lpszText, -1,
1510                         lpToolInfo->lpszText, INFOTIPSIZE, NULL, NULL);
1511
1512     return 0;
1513 }
1514
1515
1516 static LRESULT
1517 TOOLTIPS_GetTextW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1518 {
1519     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1520     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
1521     INT nTool;
1522
1523     if (lpToolInfo == NULL)
1524         return 0;
1525     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1526         return 0;
1527
1528     nTool = TOOLTIPS_GetToolFromInfoW (infoPtr, lpToolInfo);
1529     if (nTool == -1) return 0;
1530
1531     strcpyW (lpToolInfo->lpszText, infoPtr->tools[nTool].lpszText);
1532
1533     return 0;
1534 }
1535
1536
1537 static inline LRESULT
1538 TOOLTIPS_GetTipBkColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
1539 {
1540     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1541     return infoPtr->clrBk;
1542 }
1543
1544
1545 static inline LRESULT
1546 TOOLTIPS_GetTipTextColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
1547 {
1548     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1549     return infoPtr->clrText;
1550 }
1551
1552
1553 static inline LRESULT
1554 TOOLTIPS_GetToolCount (HWND hwnd, WPARAM wParam, LPARAM lParam)
1555 {
1556     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1557     return infoPtr->uNumTools;
1558 }
1559
1560
1561 static LRESULT
1562 TOOLTIPS_GetToolInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1563 {
1564     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1565     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
1566     TTTOOL_INFO *toolPtr;
1567     INT nTool;
1568
1569     if (lpToolInfo == NULL)
1570         return FALSE;
1571     if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
1572         return FALSE;
1573     if (infoPtr->uNumTools == 0)
1574         return FALSE;
1575
1576     nTool = TOOLTIPS_GetToolFromInfoA (infoPtr, lpToolInfo);
1577     if (nTool == -1)
1578         return FALSE;
1579
1580     TRACE("tool %d\n", nTool);
1581
1582     toolPtr = &infoPtr->tools[nTool];
1583
1584     /* copy tool data */
1585     lpToolInfo->uFlags   = toolPtr->uFlags;
1586     lpToolInfo->rect     = toolPtr->rect;
1587     lpToolInfo->hinst    = toolPtr->hinst;
1588 /*    lpToolInfo->lpszText = toolPtr->lpszText; */
1589     lpToolInfo->lpszText = NULL;  /* FIXME */
1590
1591     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOA))
1592         lpToolInfo->lParam = toolPtr->lParam;
1593
1594     return TRUE;
1595 }
1596
1597
1598 static LRESULT
1599 TOOLTIPS_GetToolInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1600 {
1601     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1602     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
1603     TTTOOL_INFO *toolPtr;
1604     INT nTool;
1605
1606     if (lpToolInfo == NULL)
1607         return FALSE;
1608     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1609         return FALSE;
1610     if (infoPtr->uNumTools == 0)
1611         return FALSE;
1612
1613     nTool = TOOLTIPS_GetToolFromInfoW (infoPtr, lpToolInfo);
1614     if (nTool == -1)
1615         return FALSE;
1616
1617     TRACE("tool %d\n", nTool);
1618
1619     toolPtr = &infoPtr->tools[nTool];
1620
1621     /* copy tool data */
1622     lpToolInfo->uFlags   = toolPtr->uFlags;
1623     lpToolInfo->rect     = toolPtr->rect;
1624     lpToolInfo->hinst    = toolPtr->hinst;
1625 /*    lpToolInfo->lpszText = toolPtr->lpszText; */
1626     lpToolInfo->lpszText = NULL;  /* FIXME */
1627
1628     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOW))
1629         lpToolInfo->lParam = toolPtr->lParam;
1630
1631     return TRUE;
1632 }
1633
1634
1635 static LRESULT
1636 TOOLTIPS_HitTestA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1637 {
1638     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1639     LPTTHITTESTINFOA lptthit = (LPTTHITTESTINFOA)lParam;
1640     TTTOOL_INFO *toolPtr;
1641     INT nTool;
1642
1643     if (lptthit == 0)
1644         return FALSE;
1645
1646     nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1647     if (nTool == -1)
1648         return FALSE;
1649
1650     TRACE("tool %d!\n", nTool);
1651
1652     /* copy tool data */
1653     if (lptthit->ti.cbSize >= sizeof(TTTOOLINFOA)) {
1654         toolPtr = &infoPtr->tools[nTool];
1655
1656         lptthit->ti.uFlags   = toolPtr->uFlags;
1657         lptthit->ti.hwnd     = toolPtr->hwnd;
1658         lptthit->ti.uId      = toolPtr->uId;
1659         lptthit->ti.rect     = toolPtr->rect;
1660         lptthit->ti.hinst    = toolPtr->hinst;
1661 /*      lptthit->ti.lpszText = toolPtr->lpszText; */
1662         lptthit->ti.lpszText = NULL;  /* FIXME */
1663         lptthit->ti.lParam   = toolPtr->lParam;
1664     }
1665
1666     return TRUE;
1667 }
1668
1669
1670 static LRESULT
1671 TOOLTIPS_HitTestW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1672 {
1673     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1674     LPTTHITTESTINFOW lptthit = (LPTTHITTESTINFOW)lParam;
1675     TTTOOL_INFO *toolPtr;
1676     INT nTool;
1677
1678     if (lptthit == 0)
1679         return FALSE;
1680
1681     nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1682     if (nTool == -1)
1683         return FALSE;
1684
1685     TRACE("tool %d!\n", nTool);
1686
1687     /* copy tool data */
1688     if (lptthit->ti.cbSize >= sizeof(TTTOOLINFOW)) {
1689         toolPtr = &infoPtr->tools[nTool];
1690
1691         lptthit->ti.uFlags   = toolPtr->uFlags;
1692         lptthit->ti.hwnd     = toolPtr->hwnd;
1693         lptthit->ti.uId      = toolPtr->uId;
1694         lptthit->ti.rect     = toolPtr->rect;
1695         lptthit->ti.hinst    = toolPtr->hinst;
1696 /*      lptthit->ti.lpszText = toolPtr->lpszText; */
1697         lptthit->ti.lpszText = NULL;  /* FIXME */
1698         lptthit->ti.lParam   = toolPtr->lParam;
1699     }
1700
1701     return TRUE;
1702 }
1703
1704
1705 static LRESULT
1706 TOOLTIPS_NewToolRectA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1707 {
1708     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1709     LPTTTOOLINFOA lpti = (LPTTTOOLINFOA)lParam;
1710     INT nTool;
1711
1712     if (lpti == NULL)
1713         return 0;
1714     if (lpti->cbSize < TTTOOLINFOA_V1_SIZE)
1715         return FALSE;
1716
1717     nTool = TOOLTIPS_GetToolFromInfoA (infoPtr, lpti);
1718
1719     TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&lpti->rect));
1720
1721     if (nTool == -1) return 0;
1722
1723     infoPtr->tools[nTool].rect = lpti->rect;
1724
1725     return 0;
1726 }
1727
1728
1729 static LRESULT
1730 TOOLTIPS_NewToolRectW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1731 {
1732     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1733     LPTTTOOLINFOW lpti = (LPTTTOOLINFOW)lParam;
1734     INT nTool;
1735
1736     if (lpti == NULL)
1737         return 0;
1738     if (lpti->cbSize < TTTOOLINFOW_V1_SIZE)
1739         return FALSE;
1740
1741     nTool = TOOLTIPS_GetToolFromInfoW (infoPtr, lpti);
1742
1743     TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&lpti->rect));
1744
1745     if (nTool == -1) return 0;
1746
1747     infoPtr->tools[nTool].rect = lpti->rect;
1748
1749     return 0;
1750 }
1751
1752
1753 static inline LRESULT
1754 TOOLTIPS_Pop (HWND hwnd, WPARAM wParam, LPARAM lParam)
1755 {
1756     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1757     TOOLTIPS_Hide (hwnd, infoPtr);
1758
1759     return 0;
1760 }
1761
1762
1763 static LRESULT
1764 TOOLTIPS_RelayEvent (HWND hwnd, WPARAM wParam, LPARAM lParam)
1765 {
1766     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1767     LPMSG lpMsg = (LPMSG)lParam;
1768     POINT pt;
1769     INT nOldTool;
1770
1771     if (lParam == 0) {
1772         ERR("lpMsg == NULL!\n");
1773         return 0;
1774     }
1775
1776     switch (lpMsg->message) {
1777         case WM_LBUTTONDOWN:
1778         case WM_LBUTTONUP:
1779         case WM_MBUTTONDOWN:
1780         case WM_MBUTTONUP:
1781         case WM_RBUTTONDOWN:
1782         case WM_RBUTTONUP:
1783             TOOLTIPS_Hide (hwnd, infoPtr);
1784             break;
1785
1786         case WM_MOUSEMOVE:
1787             pt.x = (short)LOWORD(lpMsg->lParam);
1788             pt.y = (short)HIWORD(lpMsg->lParam);
1789             nOldTool = infoPtr->nTool;
1790             infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1791                                                        &pt);
1792             TRACE("tool (%p) %d %d %d\n", hwnd, nOldTool,
1793                   infoPtr->nTool, infoPtr->nCurrentTool);
1794             TRACE("WM_MOUSEMOVE (%p %d %d)\n", hwnd, pt.x, pt.y);
1795
1796             if (infoPtr->nTool != nOldTool) {
1797                 if(infoPtr->nTool == -1) { /* Moved out of all tools */
1798                     TOOLTIPS_Hide(hwnd, infoPtr);
1799                     KillTimer(hwnd, ID_TIMERLEAVE);
1800                 } else if (nOldTool == -1) { /* Moved from outside */
1801                     if(infoPtr->bActive) {
1802                         SetTimer(hwnd, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1803                         TRACE("timer 1 started!\n");
1804                     }
1805                 } else { /* Moved from one to another */
1806                     TOOLTIPS_Hide (hwnd, infoPtr);
1807                     KillTimer(hwnd, ID_TIMERLEAVE);
1808                     if(infoPtr->bActive) {
1809                         SetTimer (hwnd, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1810                         TRACE("timer 1 started!\n");
1811                     }
1812                 }
1813             } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1814                 KillTimer(hwnd, ID_TIMERPOP);
1815                 SetTimer(hwnd, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1816                 TRACE("timer 2 restarted\n");
1817             } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1818                 /* previous show attempt didn't result in tooltip so try again */
1819                 SetTimer(hwnd, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1820                 TRACE("timer 1 started!\n");
1821             }
1822             break;
1823     }
1824
1825     return 0;
1826 }
1827
1828
1829 static LRESULT
1830 TOOLTIPS_SetDelayTime (HWND hwnd, WPARAM wParam, LPARAM lParam)
1831 {
1832     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1833     INT nTime = (INT)LOWORD(lParam);
1834
1835     switch (wParam) {
1836     case TTDT_AUTOMATIC:
1837         if (nTime <= 0)
1838             nTime = GetDoubleClickTime();
1839         infoPtr->nReshowTime    = nTime / 5;
1840         infoPtr->nAutoPopTime   = nTime * 10;
1841         infoPtr->nInitialTime   = nTime;
1842         break;
1843
1844     case TTDT_RESHOW:
1845         if(nTime < 0)
1846             nTime = GetDoubleClickTime() / 5;
1847         infoPtr->nReshowTime = nTime;
1848         break;
1849
1850     case TTDT_AUTOPOP:
1851         if(nTime < 0)
1852             nTime = GetDoubleClickTime() * 10;
1853         infoPtr->nAutoPopTime = nTime;
1854         break;
1855
1856     case TTDT_INITIAL:
1857         if(nTime < 0)
1858             nTime = GetDoubleClickTime();
1859         infoPtr->nInitialTime = nTime;
1860             break;
1861
1862     default:
1863         WARN("Invalid wParam %lx\n", wParam);
1864         break;
1865     }
1866
1867     return 0;
1868 }
1869
1870
1871 static LRESULT
1872 TOOLTIPS_SetMargin (HWND hwnd, WPARAM wParam, LPARAM lParam)
1873 {
1874     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1875     LPRECT lpRect = (LPRECT)lParam;
1876
1877     infoPtr->rcMargin.left   = lpRect->left;
1878     infoPtr->rcMargin.right  = lpRect->right;
1879     infoPtr->rcMargin.bottom = lpRect->bottom;
1880     infoPtr->rcMargin.top    = lpRect->top;
1881
1882     return 0;
1883 }
1884
1885
1886 static inline LRESULT
1887 TOOLTIPS_SetMaxTipWidth (HWND hwnd, WPARAM wParam, LPARAM lParam)
1888 {
1889     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1890     INT nTemp = infoPtr->nMaxTipWidth;
1891
1892     infoPtr->nMaxTipWidth = (INT)lParam;
1893
1894     return nTemp;
1895 }
1896
1897
1898 static inline LRESULT
1899 TOOLTIPS_SetTipBkColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
1900 {
1901     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1902
1903     infoPtr->clrBk = (COLORREF)wParam;
1904
1905     return 0;
1906 }
1907
1908
1909 static inline LRESULT
1910 TOOLTIPS_SetTipTextColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
1911 {
1912     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1913
1914     infoPtr->clrText = (COLORREF)wParam;
1915
1916     return 0;
1917 }
1918
1919
1920 static LRESULT
1921 TOOLTIPS_SetTitleA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1922 {
1923     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1924     LPCSTR pszTitle = (LPCSTR)lParam;
1925     UINT_PTR uTitleIcon = (UINT_PTR)wParam;
1926     UINT size;
1927
1928     TRACE("hwnd = %p, title = %s, icon = %p\n", hwnd, debugstr_a(pszTitle),
1929         (void*)uTitleIcon);
1930
1931     Free(infoPtr->pszTitle);
1932
1933     if (pszTitle)
1934     {
1935         size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, pszTitle, -1, NULL, 0);
1936         infoPtr->pszTitle = Alloc(size);
1937         if (!infoPtr->pszTitle)
1938             return FALSE;
1939         MultiByteToWideChar(CP_ACP, 0, pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1940     }
1941     else
1942         infoPtr->pszTitle = NULL;
1943
1944     if (uTitleIcon <= TTI_ERROR)
1945         infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1946     else
1947         infoPtr->hTitleIcon = CopyIcon((HICON)wParam);
1948
1949     return TRUE;
1950 }
1951
1952
1953 static LRESULT
1954 TOOLTIPS_SetTitleW (HWND hwnd, WPARAM wParam, LPARAM lParam)
1955 {
1956     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1957     LPCWSTR pszTitle = (LPCWSTR)lParam;
1958     UINT_PTR uTitleIcon = (UINT_PTR)wParam;
1959     UINT size;
1960
1961     TRACE("hwnd = %p, title = %s, icon = %p\n", hwnd, debugstr_w(pszTitle),
1962         (void*)uTitleIcon);
1963
1964     Free(infoPtr->pszTitle);
1965
1966     if (pszTitle)
1967     {
1968         size = (strlenW(pszTitle)+1)*sizeof(WCHAR);
1969         infoPtr->pszTitle = Alloc(size);
1970         if (!infoPtr->pszTitle)
1971             return FALSE;
1972         memcpy(infoPtr->pszTitle, pszTitle, size);
1973     }
1974     else
1975         infoPtr->pszTitle = NULL;
1976
1977     if (uTitleIcon <= TTI_ERROR)
1978         infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1979     else
1980         infoPtr->hTitleIcon = CopyIcon((HICON)wParam);
1981
1982     TRACE("icon = %p\n", infoPtr->hTitleIcon);
1983
1984     return TRUE;
1985 }
1986
1987
1988 static LRESULT
1989 TOOLTIPS_SetToolInfoA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1990 {
1991     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
1992     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
1993     TTTOOL_INFO *toolPtr;
1994     INT nTool;
1995
1996     if (lpToolInfo == NULL)
1997         return 0;
1998     if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
1999         return 0;
2000
2001     nTool = TOOLTIPS_GetToolFromInfoA (infoPtr, lpToolInfo);
2002     if (nTool == -1) return 0;
2003
2004     TRACE("tool %d\n", nTool);
2005
2006     toolPtr = &infoPtr->tools[nTool];
2007
2008     /* copy tool data */
2009     toolPtr->uFlags = lpToolInfo->uFlags;
2010     toolPtr->hwnd   = lpToolInfo->hwnd;
2011     toolPtr->uId    = lpToolInfo->uId;
2012     toolPtr->rect   = lpToolInfo->rect;
2013     toolPtr->hinst  = lpToolInfo->hinst;
2014
2015     if (IS_INTRESOURCE(lpToolInfo->lpszText)) {
2016         TRACE("set string id %x\n", LOWORD(lpToolInfo->lpszText));
2017         toolPtr->lpszText = (LPWSTR)lpToolInfo->lpszText;
2018     }
2019     else if (lpToolInfo->lpszText) {
2020         if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKA)
2021             toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
2022         else {
2023             if ( (toolPtr->lpszText) &&
2024                  !IS_INTRESOURCE(toolPtr->lpszText) ) {
2025                 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
2026                     Free (toolPtr->lpszText);
2027                 toolPtr->lpszText = NULL;
2028             }
2029             if (lpToolInfo->lpszText) {
2030                 INT len = MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText,
2031                                               -1, NULL, 0);
2032                 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
2033                 MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
2034                                     toolPtr->lpszText, len);
2035             }
2036         }
2037     }
2038
2039     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOA))
2040         toolPtr->lParam = lpToolInfo->lParam;
2041
2042     return 0;
2043 }
2044
2045
2046 static LRESULT
2047 TOOLTIPS_SetToolInfoW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2048 {
2049     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2050     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
2051     TTTOOL_INFO *toolPtr;
2052     INT nTool;
2053
2054     if (lpToolInfo == NULL)
2055         return 0;
2056     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
2057         return 0;
2058
2059     nTool = TOOLTIPS_GetToolFromInfoW (infoPtr, lpToolInfo);
2060     if (nTool == -1) return 0;
2061
2062     TRACE("tool %d\n", nTool);
2063
2064     toolPtr = &infoPtr->tools[nTool];
2065
2066     /* copy tool data */
2067     toolPtr->uFlags = lpToolInfo->uFlags;
2068     toolPtr->hwnd   = lpToolInfo->hwnd;
2069     toolPtr->uId    = lpToolInfo->uId;
2070     toolPtr->rect   = lpToolInfo->rect;
2071     toolPtr->hinst  = lpToolInfo->hinst;
2072
2073     if (IS_INTRESOURCE(lpToolInfo->lpszText)) {
2074         TRACE("set string id %x!\n", LOWORD(lpToolInfo->lpszText));
2075         toolPtr->lpszText = lpToolInfo->lpszText;
2076     }
2077     else {
2078         if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKW)
2079             toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
2080         else {
2081             if ( (toolPtr->lpszText) &&
2082                  !IS_INTRESOURCE(toolPtr->lpszText) ) {
2083                 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
2084                     Free (toolPtr->lpszText);
2085                 toolPtr->lpszText = NULL;
2086             }
2087             if (lpToolInfo->lpszText) {
2088                 INT len = lstrlenW (lpToolInfo->lpszText);
2089                 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
2090                 strcpyW (toolPtr->lpszText, lpToolInfo->lpszText);
2091             }
2092         }
2093     }
2094
2095     if (lpToolInfo->cbSize >= sizeof(TTTOOLINFOW))
2096         toolPtr->lParam = lpToolInfo->lParam;
2097
2098     if (infoPtr->nCurrentTool == nTool)
2099     {
2100         TOOLTIPS_GetTipText (hwnd, infoPtr, infoPtr->nCurrentTool);
2101
2102         if (infoPtr->szTipText[0] == 0)
2103             TOOLTIPS_Hide(hwnd, infoPtr);
2104         else
2105             TOOLTIPS_Show (hwnd, infoPtr, FALSE);
2106     }
2107
2108     return 0;
2109 }
2110
2111
2112 static LRESULT
2113 TOOLTIPS_TrackActivate (HWND hwnd, WPARAM wParam, LPARAM lParam)
2114 {
2115     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2116
2117     if ((BOOL)wParam) {
2118         LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
2119
2120         if (lpToolInfo == NULL)
2121             return 0;
2122         if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
2123             return FALSE;
2124
2125         /* activate */
2126         infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoA (infoPtr, lpToolInfo);
2127         if (infoPtr->nTrackTool != -1) {
2128             TRACE("activated!\n");
2129             infoPtr->bTrackActive = TRUE;
2130             TOOLTIPS_TrackShow (hwnd, infoPtr);
2131         }
2132     }
2133     else {
2134         /* deactivate */
2135         TOOLTIPS_TrackHide (hwnd, infoPtr);
2136
2137         infoPtr->bTrackActive = FALSE;
2138         infoPtr->nTrackTool = -1;
2139
2140         TRACE("deactivated!\n");
2141     }
2142
2143     return 0;
2144 }
2145
2146
2147 static LRESULT
2148 TOOLTIPS_TrackPosition (HWND hwnd, WPARAM wParam, LPARAM lParam)
2149 {
2150     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2151
2152     infoPtr->xTrackPos = (INT)LOWORD(lParam);
2153     infoPtr->yTrackPos = (INT)HIWORD(lParam);
2154
2155     if (infoPtr->bTrackActive) {
2156         TRACE("[%d %d]\n",
2157                infoPtr->xTrackPos, infoPtr->yTrackPos);
2158
2159         TOOLTIPS_TrackShow (hwnd, infoPtr);
2160     }
2161
2162     return 0;
2163 }
2164
2165
2166 static LRESULT
2167 TOOLTIPS_Update (HWND hwnd, WPARAM wParam, LPARAM lParam)
2168 {
2169     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2170
2171     if (infoPtr->nCurrentTool != -1)
2172         UpdateWindow (hwnd);
2173
2174     return 0;
2175 }
2176
2177
2178 static LRESULT
2179 TOOLTIPS_UpdateTipTextA (HWND hwnd, WPARAM wParam, LPARAM lParam)
2180 {
2181     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2182     LPTTTOOLINFOA lpToolInfo = (LPTTTOOLINFOA)lParam;
2183     TTTOOL_INFO *toolPtr;
2184     INT nTool;
2185
2186     if (lpToolInfo == NULL)
2187         return 0;
2188     if (lpToolInfo->cbSize < TTTOOLINFOA_V1_SIZE)
2189         return FALSE;
2190
2191     nTool = TOOLTIPS_GetToolFromInfoA (infoPtr, lpToolInfo);
2192     if (nTool == -1) return 0;
2193
2194     TRACE("tool %d\n", nTool);
2195
2196     toolPtr = &infoPtr->tools[nTool];
2197
2198     /* copy tool text */
2199     toolPtr->hinst  = lpToolInfo->hinst;
2200
2201     if (IS_INTRESOURCE(lpToolInfo->lpszText)){
2202         toolPtr->lpszText = (LPWSTR)lpToolInfo->lpszText;
2203     }
2204     else if (lpToolInfo->lpszText) {
2205         if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKA)
2206             toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
2207         else {
2208             if ( (toolPtr->lpszText) &&
2209                  !IS_INTRESOURCE(toolPtr->lpszText) ) {
2210                 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
2211                     Free (toolPtr->lpszText);
2212                 toolPtr->lpszText = NULL;
2213             }
2214             if (lpToolInfo->lpszText) {
2215                 INT len = MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText,
2216                                               -1, NULL, 0);
2217                 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
2218                 MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
2219                                     toolPtr->lpszText, len);
2220             }
2221         }
2222     }
2223
2224     if(infoPtr->nCurrentTool == -1) return 0;
2225     /* force repaint */
2226     if (infoPtr->bActive)
2227         TOOLTIPS_Show (hwnd, infoPtr, FALSE);
2228     else if (infoPtr->bTrackActive)
2229         TOOLTIPS_TrackShow (hwnd, infoPtr);
2230
2231     return 0;
2232 }
2233
2234
2235 static LRESULT
2236 TOOLTIPS_UpdateTipTextW (HWND hwnd, WPARAM wParam, LPARAM lParam)
2237 {
2238     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2239     LPTTTOOLINFOW lpToolInfo = (LPTTTOOLINFOW)lParam;
2240     TTTOOL_INFO *toolPtr;
2241     INT nTool;
2242
2243     if (lpToolInfo == NULL)
2244         return 0;
2245     if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
2246         return FALSE;
2247
2248     nTool = TOOLTIPS_GetToolFromInfoW (infoPtr, lpToolInfo);
2249     if (nTool == -1)
2250         return 0;
2251
2252     TRACE("tool %d\n", nTool);
2253
2254     toolPtr = &infoPtr->tools[nTool];
2255
2256     /* copy tool text */
2257     toolPtr->hinst  = lpToolInfo->hinst;
2258
2259     if (IS_INTRESOURCE(lpToolInfo->lpszText)){
2260         toolPtr->lpszText = lpToolInfo->lpszText;
2261     }
2262     else if (lpToolInfo->lpszText) {
2263         if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKW)
2264             toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
2265         else {
2266             if ( (toolPtr->lpszText)  &&
2267                  !IS_INTRESOURCE(toolPtr->lpszText) ) {
2268                 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
2269                     Free (toolPtr->lpszText);
2270                 toolPtr->lpszText = NULL;
2271             }
2272             if (lpToolInfo->lpszText) {
2273                 INT len = lstrlenW (lpToolInfo->lpszText);
2274                 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
2275                 strcpyW (toolPtr->lpszText, lpToolInfo->lpszText);
2276             }
2277         }
2278     }
2279
2280     if(infoPtr->nCurrentTool == -1) return 0;
2281     /* force repaint */
2282     if (infoPtr->bActive)
2283         TOOLTIPS_Show (hwnd, infoPtr, FALSE);
2284     else if (infoPtr->bTrackActive)
2285         TOOLTIPS_Show (hwnd, infoPtr, TRUE);
2286
2287     return 0;
2288 }
2289
2290
2291 static LRESULT
2292 TOOLTIPS_WindowFromPoint (HWND hwnd, WPARAM wParam, LPARAM lParam)
2293 {
2294     return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2295 }
2296
2297
2298
2299 static LRESULT
2300 TOOLTIPS_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
2301 {
2302     TOOLTIPS_INFO *infoPtr;
2303
2304     /* allocate memory for info structure */
2305     infoPtr = (TOOLTIPS_INFO *)Alloc (sizeof(TOOLTIPS_INFO));
2306     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
2307
2308     /* initialize info structure */
2309     infoPtr->bActive = TRUE;
2310     infoPtr->bTrackActive = FALSE;
2311
2312     infoPtr->nMaxTipWidth = -1;
2313     infoPtr->nTool = -1;
2314     infoPtr->nCurrentTool = -1;
2315     infoPtr->nTrackTool = -1;
2316
2317     /* initialize colours and fonts */
2318     TOOLTIPS_InitSystemSettings(infoPtr);
2319
2320     TOOLTIPS_SetDelayTime(hwnd, TTDT_AUTOMATIC, 0L);
2321
2322     SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
2323
2324     return 0;
2325 }
2326
2327
2328 static LRESULT
2329 TOOLTIPS_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
2330 {
2331     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2332     TTTOOL_INFO *toolPtr;
2333     UINT i;
2334
2335     /* free tools */
2336     if (infoPtr->tools) {
2337         for (i = 0; i < infoPtr->uNumTools; i++) {
2338             toolPtr = &infoPtr->tools[i];
2339             if (toolPtr->lpszText) {
2340                 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
2341                      !IS_INTRESOURCE(toolPtr->lpszText) )
2342                 {
2343                     Free (toolPtr->lpszText);
2344                     toolPtr->lpszText = NULL;
2345                 }
2346             }
2347
2348             /* remove subclassing */
2349         if (toolPtr->uFlags & TTF_SUBCLASS) {
2350             if (toolPtr->uFlags & TTF_IDISHWND) {
2351                 RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
2352             }
2353             else {
2354                 RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
2355             }
2356         }
2357     }
2358         Free (infoPtr->tools);
2359     }
2360
2361     /* free title string */
2362     Free (infoPtr->pszTitle);
2363     /* free title icon if not a standard one */
2364     if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
2365         DeleteObject(infoPtr->hTitleIcon);
2366
2367     /* delete fonts */
2368     DeleteObject (infoPtr->hFont);
2369     DeleteObject (infoPtr->hTitleFont);
2370
2371     /* free tool tips info data */
2372     Free (infoPtr);
2373     SetWindowLongPtrW(hwnd, 0, 0);
2374     return 0;
2375 }
2376
2377
2378 static LRESULT
2379 TOOLTIPS_GetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
2380 {
2381     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2382
2383     return (LRESULT)infoPtr->hFont;
2384 }
2385
2386
2387 static LRESULT
2388 TOOLTIPS_MouseMessage (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2389 {
2390     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2391
2392     TOOLTIPS_Hide (hwnd, infoPtr);
2393
2394     return 0;
2395 }
2396
2397
2398 static LRESULT
2399 TOOLTIPS_NCCreate (HWND hwnd, WPARAM wParam, LPARAM lParam)
2400 {
2401     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
2402     DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
2403
2404     dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
2405     dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
2406
2407     /* WS_BORDER only draws a border round the window rect, not the
2408      * window region, therefore it is useless to us in balloon mode */
2409     if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
2410
2411     SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
2412
2413     dwExStyle |= WS_EX_TOOLWINDOW;
2414     SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
2415
2416     return TRUE;
2417 }
2418
2419
2420 static LRESULT
2421 TOOLTIPS_NCHitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
2422 {
2423     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2424     INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
2425
2426     TRACE(" nTool=%d\n", nTool);
2427
2428     if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
2429         if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
2430             TRACE("-- in transparent mode!\n");
2431             return HTTRANSPARENT;
2432         }
2433     }
2434
2435     return DefWindowProcW (hwnd, WM_NCHITTEST, wParam, lParam);
2436 }
2437
2438
2439 static LRESULT
2440 TOOLTIPS_NotifyFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
2441 {
2442     FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", hwnd, wParam, lParam);
2443
2444     return 0;
2445 }
2446
2447
2448 static LRESULT
2449 TOOLTIPS_Paint (HWND hwnd, WPARAM wParam, LPARAM lParam)
2450 {
2451     HDC hdc;
2452     PAINTSTRUCT ps;
2453
2454     hdc = (wParam == 0) ? BeginPaint (hwnd, &ps) : (HDC)wParam;
2455     TOOLTIPS_Refresh (hwnd, hdc);
2456     if (!wParam)
2457         EndPaint (hwnd, &ps);
2458     return 0;
2459 }
2460
2461
2462 static LRESULT
2463 TOOLTIPS_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
2464 {
2465     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2466     LOGFONTW lf;
2467
2468     if(!GetObjectW((HFONT)wParam, sizeof(lf), &lf))
2469         return 0;
2470
2471     DeleteObject (infoPtr->hFont);
2472     infoPtr->hFont = CreateFontIndirectW(&lf);
2473
2474     DeleteObject (infoPtr->hTitleFont);
2475     lf.lfWeight = FW_BOLD;
2476     infoPtr->hTitleFont = CreateFontIndirectW(&lf);
2477
2478     if ((LOWORD(lParam)) & (infoPtr->nCurrentTool != -1)) {
2479         FIXME("full redraw needed!\n");
2480     }
2481
2482     return 0;
2483 }
2484
2485 /******************************************************************
2486  * TOOLTIPS_GetTextLength
2487  *
2488  * This function is called when the tooltip receive a
2489  * WM_GETTEXTLENGTH message.
2490  * wParam : not used
2491  * lParam : not used
2492  *
2493  * returns the length, in characters, of the tip text
2494  */
2495 static LRESULT
2496 TOOLTIPS_GetTextLength(HWND hwnd, WPARAM wParam, LPARAM lParam)
2497 {
2498     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2499     return strlenW(infoPtr->szTipText);
2500 }
2501
2502 /******************************************************************
2503  * TOOLTIPS_OnWMGetText
2504  *
2505  * This function is called when the tooltip receive a
2506  * WM_GETTEXT message.
2507  * wParam : specifies the maximum number of characters to be copied
2508  * lParam : is the pointer to the buffer that will receive
2509  *          the tip text
2510  *
2511  * returns the number of characters copied
2512  */
2513 static LRESULT
2514 TOOLTIPS_OnWMGetText (HWND hwnd, WPARAM wParam, LPARAM lParam)
2515 {
2516     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2517     LRESULT res;
2518     LPWSTR pszText = (LPWSTR)lParam;
2519
2520     if(!infoPtr->szTipText || !wParam)
2521         return 0;
2522
2523     res = min(strlenW(infoPtr->szTipText)+1, wParam);
2524     memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2525     pszText[res-1] = '\0';
2526     return res-1;
2527 }
2528
2529 static LRESULT
2530 TOOLTIPS_Timer (HWND hwnd, WPARAM wParam, LPARAM lParam)
2531 {
2532     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2533     INT nOldTool;
2534
2535     TRACE("timer %ld (%p) expired!\n", wParam, hwnd);
2536
2537     switch (wParam) {
2538     case ID_TIMERSHOW:
2539         KillTimer (hwnd, ID_TIMERSHOW);
2540         nOldTool = infoPtr->nTool;
2541         if ((infoPtr->nTool = TOOLTIPS_CheckTool (hwnd, TRUE)) == nOldTool)
2542             TOOLTIPS_Show (hwnd, infoPtr, FALSE);
2543         break;
2544
2545     case ID_TIMERPOP:
2546         TOOLTIPS_Hide (hwnd, infoPtr);
2547         break;
2548
2549     case ID_TIMERLEAVE:
2550         nOldTool = infoPtr->nTool;
2551         infoPtr->nTool = TOOLTIPS_CheckTool (hwnd, FALSE);
2552         TRACE("tool (%p) %d %d %d\n", hwnd, nOldTool,
2553               infoPtr->nTool, infoPtr->nCurrentTool);
2554         if (infoPtr->nTool != nOldTool) {
2555             if(infoPtr->nTool == -1) { /* Moved out of all tools */
2556                 TOOLTIPS_Hide(hwnd, infoPtr);
2557                 KillTimer(hwnd, ID_TIMERLEAVE);
2558             } else if (nOldTool == -1) { /* Moved from outside */
2559                 ERR("How did this happen?\n");
2560             } else { /* Moved from one to another */
2561                 TOOLTIPS_Hide (hwnd, infoPtr);
2562                 KillTimer(hwnd, ID_TIMERLEAVE);
2563                 if(infoPtr->bActive) {
2564                     SetTimer (hwnd, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2565                     TRACE("timer 1 started!\n");
2566                 }
2567             }
2568         }
2569         break;
2570
2571     default:
2572         ERR("Unknown timer id %ld\n", wParam);
2573         break;
2574     }
2575     return 0;
2576 }
2577
2578
2579 static LRESULT
2580 TOOLTIPS_WinIniChange (HWND hwnd, WPARAM wParam, LPARAM lParam)
2581 {
2582     TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2583
2584     TOOLTIPS_InitSystemSettings (infoPtr);
2585
2586     return 0;
2587 }
2588
2589
2590 static LRESULT CALLBACK
2591 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2592 {
2593     MSG msg;
2594
2595     switch(uMsg) {
2596     case WM_MOUSEMOVE:
2597     case WM_LBUTTONDOWN:
2598     case WM_LBUTTONUP:
2599     case WM_MBUTTONDOWN:
2600     case WM_MBUTTONUP:
2601     case WM_RBUTTONDOWN:
2602     case WM_RBUTTONUP:
2603         msg.hwnd = hwnd;
2604         msg.message = uMsg;
2605         msg.wParam = wParam;
2606         msg.lParam = lParam;
2607         TOOLTIPS_RelayEvent((HWND)dwRef, 0, (LPARAM)&msg);
2608         break;
2609
2610     default:
2611         break;
2612     }
2613     return DefSubclassProc(hwnd, uMsg, wParam, lParam);
2614 }
2615
2616
2617 static LRESULT CALLBACK
2618 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2619 {
2620     TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2621     if (!TOOLTIPS_GetInfoPtr(hwnd) && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2622         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2623     switch (uMsg)
2624     {
2625         case TTM_ACTIVATE:
2626             return TOOLTIPS_Activate (hwnd, wParam, lParam);
2627
2628         case TTM_ADDTOOLA:
2629             return TOOLTIPS_AddToolA (hwnd, wParam, lParam);
2630
2631         case TTM_ADDTOOLW:
2632             return TOOLTIPS_AddToolW (hwnd, wParam, lParam);
2633
2634         case TTM_DELTOOLA:
2635             return TOOLTIPS_DelToolA (hwnd, wParam, lParam);
2636
2637         case TTM_DELTOOLW:
2638             return TOOLTIPS_DelToolW (hwnd, wParam, lParam);
2639
2640         case TTM_ENUMTOOLSA:
2641             return TOOLTIPS_EnumToolsA (hwnd, wParam, lParam);
2642
2643         case TTM_ENUMTOOLSW:
2644             return TOOLTIPS_EnumToolsW (hwnd, wParam, lParam);
2645
2646         case TTM_GETBUBBLESIZE:
2647             return TOOLTIPS_GetBubbleSize (hwnd, wParam, lParam);
2648
2649         case TTM_GETCURRENTTOOLA:
2650             return TOOLTIPS_GetCurrentToolA (hwnd, wParam, lParam);
2651
2652         case TTM_GETCURRENTTOOLW:
2653             return TOOLTIPS_GetCurrentToolW (hwnd, wParam, lParam);
2654
2655         case TTM_GETDELAYTIME:
2656             return TOOLTIPS_GetDelayTime (hwnd, wParam, lParam);
2657
2658         case TTM_GETMARGIN:
2659             return TOOLTIPS_GetMargin (hwnd, wParam, lParam);
2660
2661         case TTM_GETMAXTIPWIDTH:
2662             return TOOLTIPS_GetMaxTipWidth (hwnd, wParam, lParam);
2663
2664         case TTM_GETTEXTA:
2665             return TOOLTIPS_GetTextA (hwnd, wParam, lParam);
2666
2667         case TTM_GETTEXTW:
2668             return TOOLTIPS_GetTextW (hwnd, wParam, lParam);
2669
2670         case TTM_GETTIPBKCOLOR:
2671             return TOOLTIPS_GetTipBkColor (hwnd, wParam, lParam);
2672
2673         case TTM_GETTIPTEXTCOLOR:
2674             return TOOLTIPS_GetTipTextColor (hwnd, wParam, lParam);
2675
2676         case TTM_GETTOOLCOUNT:
2677             return TOOLTIPS_GetToolCount (hwnd, wParam, lParam);
2678
2679         case TTM_GETTOOLINFOA:
2680             return TOOLTIPS_GetToolInfoA (hwnd, wParam, lParam);
2681
2682         case TTM_GETTOOLINFOW:
2683             return TOOLTIPS_GetToolInfoW (hwnd, wParam, lParam);
2684
2685         case TTM_HITTESTA:
2686             return TOOLTIPS_HitTestA (hwnd, wParam, lParam);
2687
2688         case TTM_HITTESTW:
2689             return TOOLTIPS_HitTestW (hwnd, wParam, lParam);
2690
2691         case TTM_NEWTOOLRECTA:
2692             return TOOLTIPS_NewToolRectA (hwnd, wParam, lParam);
2693
2694         case TTM_NEWTOOLRECTW:
2695             return TOOLTIPS_NewToolRectW (hwnd, wParam, lParam);
2696
2697         case TTM_POP:
2698             return TOOLTIPS_Pop (hwnd, wParam, lParam);
2699
2700         case TTM_RELAYEVENT:
2701             return TOOLTIPS_RelayEvent (hwnd, wParam, lParam);
2702
2703         case TTM_SETDELAYTIME:
2704             return TOOLTIPS_SetDelayTime (hwnd, wParam, lParam);
2705
2706         case TTM_SETMARGIN:
2707             return TOOLTIPS_SetMargin (hwnd, wParam, lParam);
2708
2709         case TTM_SETMAXTIPWIDTH:
2710             return TOOLTIPS_SetMaxTipWidth (hwnd, wParam, lParam);
2711
2712         case TTM_SETTIPBKCOLOR:
2713             return TOOLTIPS_SetTipBkColor (hwnd, wParam, lParam);
2714
2715         case TTM_SETTIPTEXTCOLOR:
2716             return TOOLTIPS_SetTipTextColor (hwnd, wParam, lParam);
2717
2718         case TTM_SETTITLEA:
2719             return TOOLTIPS_SetTitleA (hwnd, wParam, lParam);
2720
2721         case TTM_SETTITLEW:
2722             return TOOLTIPS_SetTitleW (hwnd, wParam, lParam);
2723
2724         case TTM_SETTOOLINFOA:
2725             return TOOLTIPS_SetToolInfoA (hwnd, wParam, lParam);
2726
2727         case TTM_SETTOOLINFOW:
2728             return TOOLTIPS_SetToolInfoW (hwnd, wParam, lParam);
2729
2730         case TTM_TRACKACTIVATE:
2731             return TOOLTIPS_TrackActivate (hwnd, wParam, lParam);
2732
2733         case TTM_TRACKPOSITION:
2734             return TOOLTIPS_TrackPosition (hwnd, wParam, lParam);
2735
2736         case TTM_UPDATE:
2737             return TOOLTIPS_Update (hwnd, wParam, lParam);
2738
2739         case TTM_UPDATETIPTEXTA:
2740             return TOOLTIPS_UpdateTipTextA (hwnd, wParam, lParam);
2741
2742         case TTM_UPDATETIPTEXTW:
2743             return TOOLTIPS_UpdateTipTextW (hwnd, wParam, lParam);
2744
2745         case TTM_WINDOWFROMPOINT:
2746             return TOOLTIPS_WindowFromPoint (hwnd, wParam, lParam);
2747
2748
2749         case WM_CREATE:
2750             return TOOLTIPS_Create (hwnd, (LPCREATESTRUCTW)lParam);
2751
2752         case WM_DESTROY:
2753             return TOOLTIPS_Destroy (hwnd, wParam, lParam);
2754
2755         case WM_ERASEBKGND:
2756             /* we draw the background in WM_PAINT */
2757             return 0;
2758
2759         case WM_GETFONT:
2760             return TOOLTIPS_GetFont (hwnd, wParam, lParam);
2761
2762         case WM_GETTEXT:
2763             return TOOLTIPS_OnWMGetText (hwnd, wParam, lParam);
2764
2765         case WM_GETTEXTLENGTH:
2766             return TOOLTIPS_GetTextLength (hwnd, wParam, lParam);
2767
2768         case WM_LBUTTONDOWN:
2769         case WM_LBUTTONUP:
2770         case WM_MBUTTONDOWN:
2771         case WM_MBUTTONUP:
2772         case WM_RBUTTONDOWN:
2773         case WM_RBUTTONUP:
2774         case WM_MOUSEMOVE:
2775             return TOOLTIPS_MouseMessage (hwnd, uMsg, wParam, lParam);
2776
2777         case WM_NCCREATE:
2778             return TOOLTIPS_NCCreate (hwnd, wParam, lParam);
2779
2780         case WM_NCHITTEST:
2781             return TOOLTIPS_NCHitTest (hwnd, wParam, lParam);
2782
2783         case WM_NOTIFYFORMAT:
2784             return TOOLTIPS_NotifyFormat (hwnd, wParam, lParam);
2785
2786         case WM_PRINTCLIENT:
2787         case WM_PAINT:
2788             return TOOLTIPS_Paint (hwnd, wParam, lParam);
2789
2790         case WM_SETFONT:
2791             return TOOLTIPS_SetFont (hwnd, wParam, lParam);
2792
2793         case WM_TIMER:
2794             return TOOLTIPS_Timer (hwnd, wParam, lParam);
2795
2796         case WM_WININICHANGE:
2797             return TOOLTIPS_WinIniChange (hwnd, wParam, lParam);
2798
2799         default:
2800             if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2801                 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2802                      uMsg, wParam, lParam);
2803             return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2804     }
2805 }
2806
2807
2808 VOID
2809 TOOLTIPS_Register (void)
2810 {
2811     WNDCLASSW wndClass;
2812
2813     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2814     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2815     wndClass.lpfnWndProc   = TOOLTIPS_WindowProc;
2816     wndClass.cbClsExtra    = 0;
2817     wndClass.cbWndExtra    = sizeof(TOOLTIPS_INFO *);
2818     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2819     wndClass.hbrBackground = 0;
2820     wndClass.lpszClassName = TOOLTIPS_CLASSW;
2821
2822     RegisterClassW (&wndClass);
2823
2824     hTooltipIcons[TTI_NONE] = NULL;
2825     hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2826         (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2827     hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2828         (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2829     hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2830         (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2831 }
2832
2833
2834 VOID
2835 TOOLTIPS_Unregister (void)
2836 {
2837     int i;
2838     for (i = TTI_INFO; i <= TTI_ERROR; i++)
2839         DestroyIcon(hTooltipIcons[i]);
2840     UnregisterClassW (TOOLTIPS_CLASSW, NULL);
2841 }