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