secur32/tests: Use importlib for functions available since Windows XP.
[wine.git] / dlls / comctl32 / tooltips.c
blob56dc743f53f4a7004b38c4690d8e0b5097456368
1 /*
2 * Tool tip control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 2004 Robert Shearman
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.
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.
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
21 * NOTES
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.
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.
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
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.
50 * Timer logic.
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.
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.
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.
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.
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.
94 #include <stdarg.h>
95 #include <string.h>
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"
107 WINE_DEFAULT_DEBUG_CHANNEL(tooltips);
109 static HICON hTooltipIcons[TTI_ERROR+1];
111 typedef struct
113 UINT uFlags;
114 UINT uInternalFlags;
115 HWND hwnd;
116 BOOL bNotifyUnicode;
117 UINT_PTR uId;
118 RECT rect;
119 HINSTANCE hinst;
120 LPWSTR lpszText;
121 LPARAM lParam;
122 } TTTOOL_INFO;
125 typedef struct
127 HWND hwndSelf;
128 WCHAR szTipText[INFOTIPSIZE];
129 BOOL bActive;
130 BOOL bTrackActive;
131 UINT uNumTools;
132 COLORREF clrBk;
133 COLORREF clrText;
134 HFONT hFont;
135 HFONT hTitleFont;
136 INT xTrackPos;
137 INT yTrackPos;
138 INT nMaxTipWidth;
139 INT nTool; /* tool that mouse was on on last relayed mouse move */
140 INT nCurrentTool;
141 INT nTrackTool;
142 INT nReshowTime;
143 INT nAutoPopTime;
144 INT nInitialTime;
145 RECT rcMargin;
146 BOOL bToolBelow;
147 LPWSTR pszTitle;
148 HICON hTitleIcon;
150 TTTOOL_INFO *tools;
151 } TOOLTIPS_INFO;
153 #define ID_TIMERSHOW 1 /* show delay timer */
154 #define ID_TIMERPOP 2 /* auto pop timer */
155 #define ID_TIMERLEAVE 3 /* tool leave timer */
158 #define TOOLTIPS_GetInfoPtr(hWindow) ((TOOLTIPS_INFO *)GetWindowLongPtrW (hWindow, 0))
160 /* offsets from window edge to start of text */
161 #define NORMAL_TEXT_MARGIN 2
162 #define BALLOON_TEXT_MARGIN (NORMAL_TEXT_MARGIN+8)
163 /* value used for CreateRoundRectRgn that specifies how much
164 * each corner is curved */
165 #define BALLOON_ROUNDEDNESS 20
166 #define BALLOON_STEMHEIGHT 13
167 #define BALLOON_STEMWIDTH 10
168 #define BALLOON_STEMINDENT 20
170 #define BALLOON_ICON_TITLE_SPACING 8 /* horizontal spacing between icon and title */
171 #define BALLOON_TITLE_TEXT_SPACING 8 /* vertical spacing between icon/title and main text */
172 #define ICON_HEIGHT 16
173 #define ICON_WIDTH 16
175 #define MAX_TEXT_SIZE_A 80 /* maximum retrieving text size by ANSI message */
177 static LRESULT CALLBACK
178 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uId, DWORD_PTR dwRef);
181 static inline BOOL TOOLTIPS_IsCallbackString(LPCWSTR str, BOOL isW)
183 if (isW)
184 return str == LPSTR_TEXTCALLBACKW;
185 else
186 return (LPCSTR)str == LPSTR_TEXTCALLBACKA;
189 static inline UINT_PTR
190 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
192 UINT i;
193 for (i = 0; i <= TTI_ERROR; i++)
194 if (hTooltipIcons[i] == hIcon)
195 return i;
196 return (UINT_PTR)hIcon;
199 static void
200 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
202 NONCLIENTMETRICSW nclm;
204 infoPtr->clrBk = comctl32_color.clrInfoBk;
205 infoPtr->clrText = comctl32_color.clrInfoText;
207 DeleteObject (infoPtr->hFont);
208 nclm.cbSize = sizeof(nclm);
209 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
210 infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
212 DeleteObject (infoPtr->hTitleFont);
213 nclm.lfStatusFont.lfWeight = FW_BOLD;
214 infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
217 /* Custom draw routines */
218 static void
219 TOOLTIPS_customdraw_fill(const TOOLTIPS_INFO *infoPtr, NMTTCUSTOMDRAW *lpnmttcd,
220 HDC hdc, const RECT *rcBounds, UINT uFlags)
222 ZeroMemory(lpnmttcd, sizeof(NMTTCUSTOMDRAW));
223 lpnmttcd->uDrawFlags = uFlags;
224 lpnmttcd->nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
225 lpnmttcd->nmcd.hdr.code = NM_CUSTOMDRAW;
226 if (infoPtr->nCurrentTool != -1) {
227 TTTOOL_INFO *toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
228 lpnmttcd->nmcd.hdr.idFrom = toolPtr->uId;
230 lpnmttcd->nmcd.hdc = hdc;
231 lpnmttcd->nmcd.rc = *rcBounds;
232 /* FIXME - dwItemSpec, uItemState, lItemlParam */
235 static inline DWORD
236 TOOLTIPS_notify_customdraw (DWORD dwDrawStage, NMTTCUSTOMDRAW *lpnmttcd)
238 LRESULT result;
239 lpnmttcd->nmcd.dwDrawStage = dwDrawStage;
241 TRACE("Notifying stage %d, flags %x, id %x\n", lpnmttcd->nmcd.dwDrawStage,
242 lpnmttcd->uDrawFlags, lpnmttcd->nmcd.hdr.code);
244 result = SendMessageW(GetParent(lpnmttcd->nmcd.hdr.hwndFrom), WM_NOTIFY,
245 0, (LPARAM)lpnmttcd);
247 TRACE("Notify result %x\n", (unsigned int)result);
249 return result;
252 static void
253 TOOLTIPS_Refresh (const TOOLTIPS_INFO *infoPtr, HDC hdc)
255 RECT rc;
256 INT oldBkMode;
257 HFONT hOldFont;
258 HBRUSH hBrush;
259 UINT uFlags = DT_EXTERNALLEADING;
260 HRGN hRgn = NULL;
261 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
262 NMTTCUSTOMDRAW nmttcd;
263 DWORD cdmode;
265 if (infoPtr->nMaxTipWidth > -1)
266 uFlags |= DT_WORDBREAK;
267 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)
268 uFlags |= DT_NOPREFIX;
269 GetClientRect (infoPtr->hwndSelf, &rc);
271 hBrush = CreateSolidBrush(infoPtr->clrBk);
273 oldBkMode = SetBkMode (hdc, TRANSPARENT);
274 SetTextColor (hdc, infoPtr->clrText);
275 hOldFont = SelectObject (hdc, infoPtr->hFont);
277 /* Custom draw - Call PrePaint once initial properties set up */
278 /* Note: Contrary to MSDN, CDRF_SKIPDEFAULT still draws a tooltip */
279 TOOLTIPS_customdraw_fill(infoPtr, &nmttcd, hdc, &rc, uFlags);
280 cdmode = TOOLTIPS_notify_customdraw(CDDS_PREPAINT, &nmttcd);
281 uFlags = nmttcd.uDrawFlags;
283 if (dwStyle & TTS_BALLOON)
285 /* create a region to store result into */
286 hRgn = CreateRectRgn(0, 0, 0, 0);
288 GetWindowRgn(infoPtr->hwndSelf, hRgn);
290 /* fill the background */
291 FillRgn(hdc, hRgn, hBrush);
292 DeleteObject(hBrush);
293 hBrush = NULL;
295 else
297 /* fill the background */
298 FillRect(hdc, &rc, hBrush);
299 DeleteObject(hBrush);
300 hBrush = NULL;
303 if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
305 /* calculate text rectangle */
306 rc.left += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
307 rc.top += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
308 rc.right -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
309 rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
310 if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
312 if (infoPtr->pszTitle)
314 RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
315 int height;
316 BOOL icon_present;
317 HFONT prevFont;
319 /* draw icon */
320 icon_present = infoPtr->hTitleIcon &&
321 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
322 ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
323 if (icon_present)
324 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
326 rcTitle.bottom = rc.top + ICON_HEIGHT;
328 /* draw title text */
329 prevFont = SelectObject (hdc, infoPtr->hTitleFont);
330 height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
331 SelectObject (hdc, prevFont);
332 rc.top += height + BALLOON_TITLE_TEXT_SPACING;
335 else
337 /* calculate text rectangle */
338 rc.left += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
339 rc.top += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
340 rc.right -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
341 rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
344 /* draw text */
345 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
347 /* Custom draw - Call PostPaint after drawing */
348 if (cdmode & CDRF_NOTIFYPOSTPAINT) {
349 TOOLTIPS_notify_customdraw(CDDS_POSTPAINT, &nmttcd);
352 /* be polite and reset the things we changed in the dc */
353 SelectObject (hdc, hOldFont);
354 SetBkMode (hdc, oldBkMode);
356 if (dwStyle & TTS_BALLOON)
358 /* frame region because default window proc doesn't do it */
359 INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
360 INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
362 hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
363 FrameRgn(hdc, hRgn, hBrush, width, height);
366 if (hRgn)
367 DeleteObject(hRgn);
370 static void TOOLTIPS_GetDispInfoA(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
372 NMTTDISPINFOA ttnmdi;
374 /* fill NMHDR struct */
375 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
376 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
377 ttnmdi.hdr.idFrom = toolPtr->uId;
378 ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
379 ttnmdi.lpszText = ttnmdi.szText;
380 ttnmdi.uFlags = toolPtr->uFlags;
381 ttnmdi.lParam = toolPtr->lParam;
383 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
384 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
386 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
387 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
388 buffer, INFOTIPSIZE);
389 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
390 toolPtr->hinst = ttnmdi.hinst;
391 toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
394 else if (ttnmdi.lpszText == 0) {
395 buffer[0] = '\0';
397 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
398 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
399 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
400 toolPtr->hinst = 0;
401 toolPtr->lpszText = NULL;
402 Str_SetPtrW(&toolPtr->lpszText, buffer);
405 else {
406 ERR("recursive text callback!\n");
407 buffer[0] = '\0';
410 /* no text available - try calling parent instead as per native */
411 /* FIXME: Unsure if SETITEM should save the value or not */
412 if (buffer[0] == 0x00) {
414 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
416 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
417 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
418 buffer, INFOTIPSIZE);
419 } else if (ttnmdi.lpszText &&
420 ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
421 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
426 static void TOOLTIPS_GetDispInfoW(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
428 NMTTDISPINFOW ttnmdi;
430 /* fill NMHDR struct */
431 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
432 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
433 ttnmdi.hdr.idFrom = toolPtr->uId;
434 ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
435 ttnmdi.lpszText = ttnmdi.szText;
436 ttnmdi.uFlags = toolPtr->uFlags;
437 ttnmdi.lParam = toolPtr->lParam;
439 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
440 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
442 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
443 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
444 buffer, INFOTIPSIZE);
445 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
446 toolPtr->hinst = ttnmdi.hinst;
447 toolPtr->lpszText = ttnmdi.lpszText;
450 else if (ttnmdi.lpszText == 0) {
451 buffer[0] = '\0';
453 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
454 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
455 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
456 toolPtr->hinst = 0;
457 toolPtr->lpszText = NULL;
458 Str_SetPtrW(&toolPtr->lpszText, buffer);
461 else {
462 ERR("recursive text callback!\n");
463 buffer[0] = '\0';
466 /* no text available - try calling parent instead as per native */
467 /* FIXME: Unsure if SETITEM should save the value or not */
468 if (buffer[0] == 0x00) {
470 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
472 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
473 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
474 buffer, INFOTIPSIZE);
475 } else if (ttnmdi.lpszText &&
476 ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
477 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
483 static void
484 TOOLTIPS_GetTipText (const TOOLTIPS_INFO *infoPtr, INT nTool, WCHAR *buffer)
486 TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
488 if (IS_INTRESOURCE(toolPtr->lpszText)) {
489 /* load a resource */
490 TRACE("load res string %p %x\n",
491 toolPtr->hinst, LOWORD(toolPtr->lpszText));
492 if (!LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText), buffer, INFOTIPSIZE))
493 buffer[0] = '\0';
495 else if (toolPtr->lpszText) {
496 if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
497 if (toolPtr->bNotifyUnicode)
498 TOOLTIPS_GetDispInfoW(infoPtr, toolPtr, buffer);
499 else
500 TOOLTIPS_GetDispInfoA(infoPtr, toolPtr, buffer);
502 else {
503 /* the item is a usual (unicode) text */
504 lstrcpynW (buffer, toolPtr->lpszText, INFOTIPSIZE);
507 else {
508 /* no text available */
509 buffer[0] = '\0';
512 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)) {
513 WCHAR *ptrW;
514 if ((ptrW = strchrW(buffer, '\t')))
515 *ptrW = 0;
518 TRACE("%s\n", debugstr_w(buffer));
522 static void
523 TOOLTIPS_CalcTipSize (const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
525 HDC hdc;
526 HFONT hOldFont;
527 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
528 UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
529 RECT rc = {0, 0, 0, 0};
530 SIZE title = {0, 0};
532 if (infoPtr->nMaxTipWidth > -1) {
533 rc.right = infoPtr->nMaxTipWidth;
534 uFlags |= DT_WORDBREAK;
536 if (style & TTS_NOPREFIX)
537 uFlags |= DT_NOPREFIX;
538 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
540 hdc = GetDC (infoPtr->hwndSelf);
541 if (infoPtr->pszTitle)
543 RECT rcTitle = {0, 0, 0, 0};
544 TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
545 if (infoPtr->hTitleIcon)
547 title.cx = ICON_WIDTH;
548 title.cy = ICON_HEIGHT;
550 if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
551 hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
552 DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
553 SelectObject (hdc, hOldFont);
554 title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
555 title.cx += (rcTitle.right - rcTitle.left);
557 hOldFont = SelectObject (hdc, infoPtr->hFont);
558 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
559 SelectObject (hdc, hOldFont);
560 ReleaseDC (infoPtr->hwndSelf, hdc);
562 if ((style & TTS_BALLOON) || infoPtr->pszTitle)
564 lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
565 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
566 lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
567 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
568 BALLOON_STEMHEIGHT;
570 else
572 lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
573 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
574 lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
575 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
580 static void
581 TOOLTIPS_Show (TOOLTIPS_INFO *infoPtr, BOOL track_activate)
583 TTTOOL_INFO *toolPtr;
584 HMONITOR monitor;
585 MONITORINFO mon_info;
586 RECT rect;
587 SIZE size;
588 NMHDR hdr;
589 int ptfx = 0;
590 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
591 INT nTool;
593 if (track_activate)
595 if (infoPtr->nTrackTool == -1)
597 TRACE("invalid tracking tool (-1)!\n");
598 return;
600 nTool = infoPtr->nTrackTool;
602 else
604 if (infoPtr->nTool == -1)
606 TRACE("invalid tool (-1)!\n");
607 return;
609 nTool = infoPtr->nTool;
612 TRACE("Show tooltip pre %d! (%p)\n", nTool, infoPtr->hwndSelf);
614 TOOLTIPS_GetTipText (infoPtr, nTool, infoPtr->szTipText);
616 if (infoPtr->szTipText[0] == '\0')
617 return;
619 toolPtr = &infoPtr->tools[nTool];
621 if (!track_activate)
622 infoPtr->nCurrentTool = infoPtr->nTool;
624 TRACE("Show tooltip %d!\n", nTool);
626 hdr.hwndFrom = infoPtr->hwndSelf;
627 hdr.idFrom = toolPtr->uId;
628 hdr.code = TTN_SHOW;
629 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
631 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
633 TOOLTIPS_CalcTipSize (infoPtr, &size);
634 TRACE("size %d x %d\n", size.cx, size.cy);
636 if (track_activate && (toolPtr->uFlags & TTF_TRACK))
638 rect.left = infoPtr->xTrackPos;
639 rect.top = infoPtr->yTrackPos;
640 ptfx = rect.left;
642 if (toolPtr->uFlags & TTF_CENTERTIP)
644 rect.left -= (size.cx / 2);
645 if (!(style & TTS_BALLOON))
646 rect.top -= (size.cy / 2);
648 if (!(infoPtr->bToolBelow = (infoPtr->yTrackPos + size.cy <= GetSystemMetrics(SM_CYSCREEN))))
649 rect.top -= size.cy;
651 if (!(toolPtr->uFlags & TTF_ABSOLUTE))
653 if (style & TTS_BALLOON)
654 rect.left -= BALLOON_STEMINDENT;
655 else
657 RECT rcTool;
659 if (toolPtr->uFlags & TTF_IDISHWND)
660 GetWindowRect ((HWND)toolPtr->uId, &rcTool);
661 else
663 rcTool = toolPtr->rect;
664 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
667 /* smart placement */
668 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
669 (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
670 rect.left = rcTool.right;
674 else
676 if (toolPtr->uFlags & TTF_CENTERTIP)
678 RECT rc;
680 if (toolPtr->uFlags & TTF_IDISHWND)
681 GetWindowRect ((HWND)toolPtr->uId, &rc);
682 else {
683 rc = toolPtr->rect;
684 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
686 rect.left = (rc.left + rc.right - size.cx) / 2;
687 if (style & TTS_BALLOON)
689 ptfx = rc.left + ((rc.right - rc.left) / 2);
691 /* CENTERTIP ballon tooltips default to below the field
692 * if they fit on the screen */
693 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
695 rect.top = rc.top - size.cy;
696 infoPtr->bToolBelow = FALSE;
698 else
700 infoPtr->bToolBelow = TRUE;
701 rect.top = rc.bottom;
703 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
705 else
707 rect.top = rc.bottom + 2;
708 infoPtr->bToolBelow = TRUE;
711 else
713 GetCursorPos ((LPPOINT)&rect);
714 if (style & TTS_BALLOON)
716 ptfx = rect.left;
717 if(rect.top - size.cy >= 0)
719 rect.top -= size.cy;
720 infoPtr->bToolBelow = FALSE;
722 else
724 infoPtr->bToolBelow = TRUE;
725 rect.top += 20;
727 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
729 else
731 rect.top += 20;
732 infoPtr->bToolBelow = TRUE;
737 TRACE("pos %d - %d\n", rect.left, rect.top);
739 rect.right = rect.left + size.cx;
740 rect.bottom = rect.top + size.cy;
742 /* check position */
744 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
745 mon_info.cbSize = sizeof(mon_info);
746 GetMonitorInfoW( monitor, &mon_info );
748 if( rect.right > mon_info.rcWork.right ) {
749 rect.left -= rect.right - mon_info.rcWork.right + 2;
750 rect.right = mon_info.rcWork.right - 2;
752 if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
754 if( rect.bottom > mon_info.rcWork.bottom ) {
755 RECT rc;
757 if (toolPtr->uFlags & TTF_IDISHWND)
758 GetWindowRect ((HWND)toolPtr->uId, &rc);
759 else {
760 rc = toolPtr->rect;
761 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
763 rect.bottom = rc.top - 2;
764 rect.top = rect.bottom - size.cy;
767 AdjustWindowRectEx (&rect, GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE),
768 FALSE, GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE));
770 if (style & TTS_BALLOON)
772 HRGN hRgn;
773 HRGN hrStem;
774 POINT pts[3];
776 ptfx -= rect.left;
778 if(infoPtr->bToolBelow)
780 pts[0].x = ptfx;
781 pts[0].y = 0;
782 pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
783 pts[1].y = BALLOON_STEMHEIGHT;
784 pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
785 pts[2].y = pts[1].y;
786 if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
788 pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
789 pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
792 else
794 pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
795 pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
796 pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
797 pts[1].y = pts[0].y;
798 pts[2].x = ptfx;
799 pts[2].y = (rect.bottom - rect.top);
800 if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
802 pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
803 pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
807 hrStem = CreatePolygonRgn(pts, sizeof(pts) / sizeof(pts[0]), ALTERNATE);
809 hRgn = CreateRoundRectRgn(0,
810 (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
811 rect.right - rect.left,
812 (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
813 BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
815 CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
816 DeleteObject(hrStem);
818 SetWindowRgn(infoPtr->hwndSelf, hRgn, FALSE);
819 /* we don't free the region handle as the system deletes it when
820 * it is no longer needed */
823 SetWindowPos (infoPtr->hwndSelf, HWND_TOPMOST, rect.left, rect.top,
824 rect.right - rect.left, rect.bottom - rect.top,
825 SWP_SHOWWINDOW | SWP_NOACTIVATE);
827 /* repaint the tooltip */
828 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
829 UpdateWindow(infoPtr->hwndSelf);
831 if (!track_activate)
833 SetTimer (infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
834 TRACE("timer 2 started!\n");
835 SetTimer (infoPtr->hwndSelf, ID_TIMERLEAVE, infoPtr->nReshowTime, 0);
836 TRACE("timer 3 started!\n");
841 static void
842 TOOLTIPS_Hide (TOOLTIPS_INFO *infoPtr)
844 TTTOOL_INFO *toolPtr;
845 NMHDR hdr;
847 TRACE("Hide tooltip %d! (%p)\n", infoPtr->nCurrentTool, infoPtr->hwndSelf);
849 if (infoPtr->nCurrentTool == -1)
850 return;
852 toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
853 KillTimer (infoPtr->hwndSelf, ID_TIMERPOP);
855 hdr.hwndFrom = infoPtr->hwndSelf;
856 hdr.idFrom = toolPtr->uId;
857 hdr.code = TTN_POP;
858 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
860 infoPtr->nCurrentTool = -1;
862 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
863 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
867 static void
868 TOOLTIPS_TrackShow (TOOLTIPS_INFO *infoPtr)
870 TOOLTIPS_Show(infoPtr, TRUE);
874 static void
875 TOOLTIPS_TrackHide (const TOOLTIPS_INFO *infoPtr)
877 TTTOOL_INFO *toolPtr;
878 NMHDR hdr;
880 TRACE("hide tracking tooltip %d\n", infoPtr->nTrackTool);
882 if (infoPtr->nTrackTool == -1)
883 return;
885 toolPtr = &infoPtr->tools[infoPtr->nTrackTool];
887 hdr.hwndFrom = infoPtr->hwndSelf;
888 hdr.idFrom = toolPtr->uId;
889 hdr.code = TTN_POP;
890 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
892 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
893 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
896 /* Structure layout is the same for TTTOOLINFOW and TTTOOLINFOA,
897 this helper is used in both cases. */
898 static INT
899 TOOLTIPS_GetToolFromInfoT (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
901 TTTOOL_INFO *toolPtr;
902 UINT nTool;
904 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
905 toolPtr = &infoPtr->tools[nTool];
907 if (!(toolPtr->uFlags & TTF_IDISHWND) &&
908 (lpToolInfo->hwnd == toolPtr->hwnd) &&
909 (lpToolInfo->uId == toolPtr->uId))
910 return nTool;
913 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
914 toolPtr = &infoPtr->tools[nTool];
916 if ((toolPtr->uFlags & TTF_IDISHWND) &&
917 (lpToolInfo->uId == toolPtr->uId))
918 return nTool;
921 return -1;
925 static INT
926 TOOLTIPS_GetToolFromPoint (const TOOLTIPS_INFO *infoPtr, HWND hwnd, const POINT *lpPt)
928 TTTOOL_INFO *toolPtr;
929 UINT nTool;
931 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
932 toolPtr = &infoPtr->tools[nTool];
934 if (!(toolPtr->uFlags & TTF_IDISHWND)) {
935 if (hwnd != toolPtr->hwnd)
936 continue;
937 if (!PtInRect (&toolPtr->rect, *lpPt))
938 continue;
939 return nTool;
943 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
944 toolPtr = &infoPtr->tools[nTool];
946 if (toolPtr->uFlags & TTF_IDISHWND) {
947 if ((HWND)toolPtr->uId == hwnd)
948 return nTool;
952 return -1;
955 static inline void
956 TOOLTIPS_CopyInfoT (const TTTOOL_INFO *toolPtr, TTTOOLINFOW *ti, BOOL isW)
958 if (ti->lpszText) {
959 if (toolPtr->lpszText == NULL ||
960 IS_INTRESOURCE(toolPtr->lpszText) ||
961 toolPtr->lpszText == LPSTR_TEXTCALLBACKW)
962 ti->lpszText = toolPtr->lpszText;
963 else if (isW)
964 strcpyW (ti->lpszText, toolPtr->lpszText);
965 else
966 /* ANSI version, the buffer is maximum 80 bytes without null. */
967 WideCharToMultiByte(CP_ACP, 0, toolPtr->lpszText, -1,
968 (LPSTR)ti->lpszText, MAX_TEXT_SIZE_A, NULL, NULL);
972 static BOOL
973 TOOLTIPS_IsWindowActive (HWND hwnd)
975 HWND hwndActive = GetActiveWindow ();
976 if (!hwndActive)
977 return FALSE;
978 if (hwndActive == hwnd)
979 return TRUE;
980 return IsChild (hwndActive, hwnd);
984 static INT
985 TOOLTIPS_CheckTool (const TOOLTIPS_INFO *infoPtr, BOOL bShowTest)
987 POINT pt;
988 HWND hwndTool;
989 INT nTool;
991 GetCursorPos (&pt);
992 hwndTool = (HWND)SendMessageW (infoPtr->hwndSelf, TTM_WINDOWFROMPOINT, 0, (LPARAM)&pt);
993 if (hwndTool == 0)
994 return -1;
996 ScreenToClient (hwndTool, &pt);
997 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, hwndTool, &pt);
998 if (nTool == -1)
999 return -1;
1001 if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_ALWAYSTIP) && bShowTest)
1003 TTTOOL_INFO *ti = &infoPtr->tools[nTool];
1004 HWND hwnd = (ti->uFlags & TTF_IDISHWND) ? (HWND)ti->uId : ti->hwnd;
1006 if (!TOOLTIPS_IsWindowActive(hwnd))
1008 TRACE("not active: hwnd %p, parent %p, active %p\n",
1009 hwnd, GetParent(hwnd), GetActiveWindow());
1010 return -1;
1014 TRACE("tool %d\n", nTool);
1016 return nTool;
1020 static LRESULT
1021 TOOLTIPS_Activate (TOOLTIPS_INFO *infoPtr, BOOL activate)
1023 infoPtr->bActive = activate;
1025 if (infoPtr->bActive)
1026 TRACE("activate!\n");
1028 if (!(infoPtr->bActive) && (infoPtr->nCurrentTool != -1))
1029 TOOLTIPS_Hide (infoPtr);
1031 return 0;
1035 static LRESULT
1036 TOOLTIPS_AddToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1038 TTTOOL_INFO *toolPtr;
1039 INT nResult;
1041 if (!ti) return FALSE;
1043 TRACE("add tool (%p) %p %ld%s!\n",
1044 infoPtr->hwndSelf, ti->hwnd, ti->uId,
1045 (ti->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
1047 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE && !ti->lpszText && isW)
1048 return FALSE;
1050 if (infoPtr->uNumTools == 0) {
1051 infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
1052 toolPtr = infoPtr->tools;
1054 else {
1055 TTTOOL_INFO *oldTools = infoPtr->tools;
1056 infoPtr->tools =
1057 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1058 memcpy (infoPtr->tools, oldTools,
1059 infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1060 Free (oldTools);
1061 toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1064 infoPtr->uNumTools++;
1066 /* copy tool data */
1067 toolPtr->uFlags = ti->uFlags;
1068 toolPtr->uInternalFlags = (ti->uFlags & (TTF_SUBCLASS | TTF_IDISHWND));
1069 toolPtr->hwnd = ti->hwnd;
1070 toolPtr->uId = ti->uId;
1071 toolPtr->rect = ti->rect;
1072 toolPtr->hinst = ti->hinst;
1074 if (ti->cbSize >= TTTOOLINFOW_V1_SIZE) {
1075 if (IS_INTRESOURCE(ti->lpszText)) {
1076 TRACE("add string id %x\n", LOWORD(ti->lpszText));
1077 toolPtr->lpszText = ti->lpszText;
1079 else if (ti->lpszText) {
1080 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW)) {
1081 TRACE("add CALLBACK!\n");
1082 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1084 else if (isW) {
1085 INT len = lstrlenW (ti->lpszText);
1086 TRACE("add text %s!\n", debugstr_w(ti->lpszText));
1087 toolPtr->lpszText = Alloc ((len + 1)*sizeof(WCHAR));
1088 strcpyW (toolPtr->lpszText, ti->lpszText);
1090 else {
1091 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, NULL, 0);
1092 TRACE("add text \"%s\"!\n", (LPSTR)ti->lpszText);
1093 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1094 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, toolPtr->lpszText, len);
1099 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1100 toolPtr->lParam = ti->lParam;
1102 /* install subclassing hook */
1103 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1104 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1105 SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1106 (DWORD_PTR)infoPtr->hwndSelf);
1108 else {
1109 SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1110 (DWORD_PTR)infoPtr->hwndSelf);
1112 TRACE("subclassing installed!\n");
1115 nResult = SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1116 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1117 if (nResult == NFR_ANSI) {
1118 toolPtr->bNotifyUnicode = FALSE;
1119 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1120 } else if (nResult == NFR_UNICODE) {
1121 toolPtr->bNotifyUnicode = TRUE;
1122 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1123 } else {
1124 TRACE (" -- WM_NOTIFYFORMAT returns: error!\n");
1127 return TRUE;
1131 static LRESULT
1132 TOOLTIPS_DelToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1134 TTTOOL_INFO *toolPtr;
1135 INT nTool;
1137 if (!ti) return 0;
1138 if (isW && ti->cbSize > TTTOOLINFOW_V2_SIZE &&
1139 ti->cbSize != TTTOOLINFOW_V3_SIZE)
1140 return 0;
1141 if (infoPtr->uNumTools == 0)
1142 return 0;
1144 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1146 TRACE("tool %d\n", nTool);
1148 if (nTool == -1)
1149 return 0;
1151 /* make sure the tooltip has disappeared before deleting it */
1152 TOOLTIPS_Hide(infoPtr);
1154 /* delete text string */
1155 toolPtr = &infoPtr->tools[nTool];
1156 if (toolPtr->lpszText) {
1157 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1158 !IS_INTRESOURCE(toolPtr->lpszText) )
1159 Free (toolPtr->lpszText);
1162 /* remove subclassing */
1163 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1164 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1165 RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
1167 else {
1168 RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
1172 /* delete tool from tool list */
1173 if (infoPtr->uNumTools == 1) {
1174 Free (infoPtr->tools);
1175 infoPtr->tools = NULL;
1177 else {
1178 TTTOOL_INFO *oldTools = infoPtr->tools;
1179 infoPtr->tools =
1180 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1182 if (nTool > 0)
1183 memcpy (&infoPtr->tools[0], &oldTools[0],
1184 nTool * sizeof(TTTOOL_INFO));
1186 if (nTool < infoPtr->uNumTools - 1)
1187 memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1188 (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1190 Free (oldTools);
1193 /* update any indices affected by delete */
1195 /* destroying tool that mouse was on on last relayed mouse move */
1196 if (infoPtr->nTool == nTool)
1197 /* -1 means no current tool (0 means first tool) */
1198 infoPtr->nTool = -1;
1199 else if (infoPtr->nTool > nTool)
1200 infoPtr->nTool--;
1202 if (infoPtr->nTrackTool == nTool)
1203 /* -1 means no current tool (0 means first tool) */
1204 infoPtr->nTrackTool = -1;
1205 else if (infoPtr->nTrackTool > nTool)
1206 infoPtr->nTrackTool--;
1208 if (infoPtr->nCurrentTool == nTool)
1209 /* -1 means no current tool (0 means first tool) */
1210 infoPtr->nCurrentTool = -1;
1211 else if (infoPtr->nCurrentTool > nTool)
1212 infoPtr->nCurrentTool--;
1214 infoPtr->uNumTools--;
1216 return 0;
1219 static LRESULT
1220 TOOLTIPS_EnumToolsT (const TOOLTIPS_INFO *infoPtr, UINT uIndex, TTTOOLINFOW *ti,
1221 BOOL isW)
1223 TTTOOL_INFO *toolPtr;
1225 if (!ti) return FALSE;
1226 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1227 return FALSE;
1228 if (uIndex >= infoPtr->uNumTools)
1229 return FALSE;
1231 TRACE("index=%u\n", uIndex);
1233 toolPtr = &infoPtr->tools[uIndex];
1235 /* copy tool data */
1236 ti->uFlags = toolPtr->uFlags;
1237 ti->hwnd = toolPtr->hwnd;
1238 ti->uId = toolPtr->uId;
1239 ti->rect = toolPtr->rect;
1240 ti->hinst = toolPtr->hinst;
1241 TOOLTIPS_CopyInfoT (toolPtr, ti, isW);
1243 if (ti->cbSize >= TTTOOLINFOA_V2_SIZE)
1244 ti->lParam = toolPtr->lParam;
1246 return TRUE;
1249 static LRESULT
1250 TOOLTIPS_GetBubbleSize (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
1252 INT nTool;
1253 SIZE size;
1255 if (lpToolInfo == NULL)
1256 return FALSE;
1257 if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1258 return FALSE;
1260 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, lpToolInfo);
1261 if (nTool == -1) return 0;
1263 TRACE("tool %d\n", nTool);
1265 TOOLTIPS_CalcTipSize (infoPtr, &size);
1266 TRACE("size %d x %d\n", size.cx, size.cy);
1268 return MAKELRESULT(size.cx, size.cy);
1271 static LRESULT
1272 TOOLTIPS_GetCurrentToolT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1274 TTTOOL_INFO *toolPtr;
1276 if (ti) {
1277 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1278 return FALSE;
1280 if (infoPtr->nCurrentTool > -1) {
1281 toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
1283 /* copy tool data */
1284 ti->uFlags = toolPtr->uFlags;
1285 ti->rect = toolPtr->rect;
1286 ti->hinst = toolPtr->hinst;
1287 TOOLTIPS_CopyInfoT (toolPtr, ti, isW);
1289 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1290 ti->lParam = toolPtr->lParam;
1292 return TRUE;
1294 else
1295 return FALSE;
1297 else
1298 return (infoPtr->nCurrentTool != -1);
1302 static LRESULT
1303 TOOLTIPS_GetDelayTime (const TOOLTIPS_INFO *infoPtr, DWORD duration)
1305 switch (duration) {
1306 case TTDT_RESHOW:
1307 return infoPtr->nReshowTime;
1309 case TTDT_AUTOPOP:
1310 return infoPtr->nAutoPopTime;
1312 case TTDT_INITIAL:
1313 case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1314 return infoPtr->nInitialTime;
1316 default:
1317 WARN("Invalid duration flag %x\n", duration);
1318 break;
1321 return -1;
1325 static LRESULT
1326 TOOLTIPS_GetMargin (const TOOLTIPS_INFO *infoPtr, RECT *rect)
1328 if (rect)
1329 *rect = infoPtr->rcMargin;
1331 return 0;
1335 static inline LRESULT
1336 TOOLTIPS_GetMaxTipWidth (const TOOLTIPS_INFO *infoPtr)
1338 return infoPtr->nMaxTipWidth;
1342 static LRESULT
1343 TOOLTIPS_GetTextT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1345 INT nTool;
1347 if (!ti) return 0;
1348 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1349 return 0;
1351 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1352 if (nTool == -1) return 0;
1354 if (infoPtr->tools[nTool].lpszText == NULL)
1355 return 0;
1357 if (isW) {
1358 ti->lpszText[0] = '\0';
1359 TOOLTIPS_GetTipText(infoPtr, nTool, ti->lpszText);
1361 else {
1362 WCHAR buffer[INFOTIPSIZE];
1364 /* NB this API is broken, there is no way for the app to determine
1365 what size buffer it requires nor a way to specify how long the
1366 one it supplies is. According to the test result, it's up to
1367 80 bytes by the ANSI version. */
1369 buffer[0] = '\0';
1370 TOOLTIPS_GetTipText(infoPtr, nTool, buffer);
1371 WideCharToMultiByte(CP_ACP, 0, buffer, -1, (LPSTR)ti->lpszText,
1372 MAX_TEXT_SIZE_A, NULL, NULL);
1375 return 0;
1379 static inline LRESULT
1380 TOOLTIPS_GetTipBkColor (const TOOLTIPS_INFO *infoPtr)
1382 return infoPtr->clrBk;
1386 static inline LRESULT
1387 TOOLTIPS_GetTipTextColor (const TOOLTIPS_INFO *infoPtr)
1389 return infoPtr->clrText;
1393 static inline LRESULT
1394 TOOLTIPS_GetToolCount (const TOOLTIPS_INFO *infoPtr)
1396 return infoPtr->uNumTools;
1400 static LRESULT
1401 TOOLTIPS_GetToolInfoT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1403 TTTOOL_INFO *toolPtr;
1404 INT nTool;
1406 if (!ti) return FALSE;
1407 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1408 return FALSE;
1409 if (infoPtr->uNumTools == 0)
1410 return FALSE;
1412 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1413 if (nTool == -1)
1414 return FALSE;
1416 TRACE("tool %d\n", nTool);
1418 toolPtr = &infoPtr->tools[nTool];
1420 /* copy tool data */
1421 ti->uFlags = toolPtr->uFlags;
1422 ti->rect = toolPtr->rect;
1423 ti->hinst = toolPtr->hinst;
1424 TOOLTIPS_CopyInfoT (toolPtr, ti, isW);
1426 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1427 ti->lParam = toolPtr->lParam;
1429 return TRUE;
1433 static LRESULT
1434 TOOLTIPS_HitTestT (const TOOLTIPS_INFO *infoPtr, LPTTHITTESTINFOW lptthit,
1435 BOOL isW)
1437 TTTOOL_INFO *toolPtr;
1438 INT nTool;
1440 if (lptthit == 0)
1441 return FALSE;
1443 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1444 if (nTool == -1)
1445 return FALSE;
1447 TRACE("tool %d!\n", nTool);
1449 /* copy tool data */
1450 if (lptthit->ti.cbSize >= TTTOOLINFOW_V1_SIZE) {
1451 toolPtr = &infoPtr->tools[nTool];
1453 lptthit->ti.uFlags = toolPtr->uFlags;
1454 lptthit->ti.hwnd = toolPtr->hwnd;
1455 lptthit->ti.uId = toolPtr->uId;
1456 lptthit->ti.rect = toolPtr->rect;
1457 lptthit->ti.hinst = toolPtr->hinst;
1458 TOOLTIPS_CopyInfoT (toolPtr, &lptthit->ti, isW);
1459 if (lptthit->ti.cbSize >= TTTOOLINFOW_V2_SIZE)
1460 lptthit->ti.lParam = toolPtr->lParam;
1463 return TRUE;
1467 static LRESULT
1468 TOOLTIPS_NewToolRectT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti)
1470 INT nTool;
1472 if (!ti) return 0;
1473 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1474 return FALSE;
1476 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1478 TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&ti->rect));
1480 if (nTool == -1) return 0;
1482 infoPtr->tools[nTool].rect = ti->rect;
1484 return 0;
1488 static inline LRESULT
1489 TOOLTIPS_Pop (TOOLTIPS_INFO *infoPtr)
1491 TOOLTIPS_Hide (infoPtr);
1493 return 0;
1497 static LRESULT
1498 TOOLTIPS_RelayEvent (TOOLTIPS_INFO *infoPtr, LPMSG lpMsg)
1500 POINT pt;
1501 INT nOldTool;
1503 if (!lpMsg) {
1504 ERR("lpMsg == NULL!\n");
1505 return 0;
1508 switch (lpMsg->message) {
1509 case WM_LBUTTONDOWN:
1510 case WM_LBUTTONUP:
1511 case WM_MBUTTONDOWN:
1512 case WM_MBUTTONUP:
1513 case WM_RBUTTONDOWN:
1514 case WM_RBUTTONUP:
1515 TOOLTIPS_Hide (infoPtr);
1516 break;
1518 case WM_MOUSEMOVE:
1519 pt.x = (short)LOWORD(lpMsg->lParam);
1520 pt.y = (short)HIWORD(lpMsg->lParam);
1521 nOldTool = infoPtr->nTool;
1522 infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1523 &pt);
1524 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
1525 infoPtr->nTool, infoPtr->nCurrentTool);
1526 TRACE("WM_MOUSEMOVE (%p %s)\n", infoPtr->hwndSelf, wine_dbgstr_point(&pt));
1528 if (infoPtr->nTool != nOldTool) {
1529 if(infoPtr->nTool == -1) { /* Moved out of all tools */
1530 TOOLTIPS_Hide(infoPtr);
1531 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1532 } else if (nOldTool == -1) { /* Moved from outside */
1533 if(infoPtr->bActive) {
1534 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1535 TRACE("timer 1 started!\n");
1537 } else { /* Moved from one to another */
1538 TOOLTIPS_Hide (infoPtr);
1539 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1540 if(infoPtr->bActive) {
1541 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1542 TRACE("timer 1 started!\n");
1545 } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1546 KillTimer(infoPtr->hwndSelf, ID_TIMERPOP);
1547 SetTimer(infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1548 TRACE("timer 2 restarted\n");
1549 } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1550 /* previous show attempt didn't result in tooltip so try again */
1551 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1552 TRACE("timer 1 started!\n");
1554 break;
1557 return 0;
1561 static LRESULT
1562 TOOLTIPS_SetDelayTime (TOOLTIPS_INFO *infoPtr, DWORD duration, INT nTime)
1564 switch (duration) {
1565 case TTDT_AUTOMATIC:
1566 if (nTime <= 0)
1567 nTime = GetDoubleClickTime();
1568 infoPtr->nReshowTime = nTime / 5;
1569 infoPtr->nAutoPopTime = nTime * 10;
1570 infoPtr->nInitialTime = nTime;
1571 break;
1573 case TTDT_RESHOW:
1574 if(nTime < 0)
1575 nTime = GetDoubleClickTime() / 5;
1576 infoPtr->nReshowTime = nTime;
1577 break;
1579 case TTDT_AUTOPOP:
1580 if(nTime < 0)
1581 nTime = GetDoubleClickTime() * 10;
1582 infoPtr->nAutoPopTime = nTime;
1583 break;
1585 case TTDT_INITIAL:
1586 if(nTime < 0)
1587 nTime = GetDoubleClickTime();
1588 infoPtr->nInitialTime = nTime;
1589 break;
1591 default:
1592 WARN("Invalid duration flag %x\n", duration);
1593 break;
1596 return 0;
1600 static LRESULT
1601 TOOLTIPS_SetMargin (TOOLTIPS_INFO *infoPtr, const RECT *rect)
1603 if (rect)
1604 infoPtr->rcMargin = *rect;
1606 return 0;
1610 static inline LRESULT
1611 TOOLTIPS_SetMaxTipWidth (TOOLTIPS_INFO *infoPtr, INT MaxWidth)
1613 INT nTemp = infoPtr->nMaxTipWidth;
1615 infoPtr->nMaxTipWidth = MaxWidth;
1617 return nTemp;
1621 static inline LRESULT
1622 TOOLTIPS_SetTipBkColor (TOOLTIPS_INFO *infoPtr, COLORREF clrBk)
1624 infoPtr->clrBk = clrBk;
1626 return 0;
1630 static inline LRESULT
1631 TOOLTIPS_SetTipTextColor (TOOLTIPS_INFO *infoPtr, COLORREF clrText)
1633 infoPtr->clrText = clrText;
1635 return 0;
1639 static LRESULT
1640 TOOLTIPS_SetTitleT (TOOLTIPS_INFO *infoPtr, UINT_PTR uTitleIcon, LPCWSTR pszTitle,
1641 BOOL isW)
1643 UINT size;
1645 TRACE("hwnd = %p, title = %s, icon = %p\n", infoPtr->hwndSelf, debugstr_w(pszTitle),
1646 (void*)uTitleIcon);
1648 Free(infoPtr->pszTitle);
1650 if (pszTitle)
1652 if (isW)
1654 size = (strlenW(pszTitle)+1)*sizeof(WCHAR);
1655 infoPtr->pszTitle = Alloc(size);
1656 if (!infoPtr->pszTitle)
1657 return FALSE;
1658 memcpy(infoPtr->pszTitle, pszTitle, size);
1660 else
1662 size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, NULL, 0);
1663 infoPtr->pszTitle = Alloc(size);
1664 if (!infoPtr->pszTitle)
1665 return FALSE;
1666 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1669 else
1670 infoPtr->pszTitle = NULL;
1672 if (uTitleIcon <= TTI_ERROR)
1673 infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1674 else
1675 infoPtr->hTitleIcon = CopyIcon((HICON)uTitleIcon);
1677 TRACE("icon = %p\n", infoPtr->hTitleIcon);
1679 return TRUE;
1683 static LRESULT
1684 TOOLTIPS_SetToolInfoT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1686 TTTOOL_INFO *toolPtr;
1687 INT nTool;
1689 if (!ti) return 0;
1690 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1691 return 0;
1693 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1694 if (nTool == -1) return 0;
1696 TRACE("tool %d\n", nTool);
1698 toolPtr = &infoPtr->tools[nTool];
1700 /* copy tool data */
1701 toolPtr->uFlags = ti->uFlags;
1702 toolPtr->hwnd = ti->hwnd;
1703 toolPtr->uId = ti->uId;
1704 toolPtr->rect = ti->rect;
1705 toolPtr->hinst = ti->hinst;
1707 if (IS_INTRESOURCE(ti->lpszText)) {
1708 TRACE("set string id %x!\n", LOWORD(ti->lpszText));
1709 toolPtr->lpszText = ti->lpszText;
1711 else {
1712 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1713 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1714 else {
1715 if ( (toolPtr->lpszText) &&
1716 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1717 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1718 Free (toolPtr->lpszText);
1719 toolPtr->lpszText = NULL;
1721 if (ti->lpszText) {
1722 if (isW) {
1723 INT len = lstrlenW (ti->lpszText);
1724 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1725 strcpyW (toolPtr->lpszText, ti->lpszText);
1727 else {
1728 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1729 -1, NULL, 0);
1730 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1731 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1732 toolPtr->lpszText, len);
1738 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1739 toolPtr->lParam = ti->lParam;
1741 if (infoPtr->nCurrentTool == nTool)
1743 TOOLTIPS_GetTipText (infoPtr, infoPtr->nCurrentTool, infoPtr->szTipText);
1745 if (infoPtr->szTipText[0] == 0)
1746 TOOLTIPS_Hide(infoPtr);
1747 else
1748 TOOLTIPS_Show (infoPtr, FALSE);
1751 return 0;
1755 static LRESULT
1756 TOOLTIPS_TrackActivate (TOOLTIPS_INFO *infoPtr, BOOL track_activate, const TTTOOLINFOA *ti)
1758 if (track_activate) {
1760 if (!ti) return 0;
1761 if (ti->cbSize < TTTOOLINFOA_V1_SIZE)
1762 return FALSE;
1764 /* activate */
1765 infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoT (infoPtr, (const TTTOOLINFOW*)ti);
1766 if (infoPtr->nTrackTool != -1) {
1767 TRACE("activated!\n");
1768 infoPtr->bTrackActive = TRUE;
1769 TOOLTIPS_TrackShow (infoPtr);
1772 else {
1773 /* deactivate */
1774 TOOLTIPS_TrackHide (infoPtr);
1776 infoPtr->bTrackActive = FALSE;
1777 infoPtr->nTrackTool = -1;
1779 TRACE("deactivated!\n");
1782 return 0;
1786 static LRESULT
1787 TOOLTIPS_TrackPosition (TOOLTIPS_INFO *infoPtr, LPARAM coord)
1789 infoPtr->xTrackPos = (INT)LOWORD(coord);
1790 infoPtr->yTrackPos = (INT)HIWORD(coord);
1792 if (infoPtr->bTrackActive) {
1793 TRACE("[%d %d]\n",
1794 infoPtr->xTrackPos, infoPtr->yTrackPos);
1796 TOOLTIPS_TrackShow (infoPtr);
1799 return 0;
1803 static LRESULT
1804 TOOLTIPS_Update (TOOLTIPS_INFO *infoPtr)
1806 if (infoPtr->nCurrentTool != -1)
1807 UpdateWindow (infoPtr->hwndSelf);
1809 return 0;
1813 static LRESULT
1814 TOOLTIPS_UpdateTipTextT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1816 TTTOOL_INFO *toolPtr;
1817 INT nTool;
1819 if (!ti) return 0;
1820 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1821 return FALSE;
1823 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1824 if (nTool == -1)
1825 return 0;
1827 TRACE("tool %d\n", nTool);
1829 toolPtr = &infoPtr->tools[nTool];
1831 /* copy tool text */
1832 toolPtr->hinst = ti->hinst;
1834 if (IS_INTRESOURCE(ti->lpszText)){
1835 toolPtr->lpszText = ti->lpszText;
1837 else if (ti->lpszText) {
1838 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1839 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1840 else {
1841 if ( (toolPtr->lpszText) &&
1842 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1843 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1844 Free (toolPtr->lpszText);
1845 toolPtr->lpszText = NULL;
1847 if (ti->lpszText) {
1848 if (isW) {
1849 INT len = lstrlenW (ti->lpszText);
1850 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1851 strcpyW (toolPtr->lpszText, ti->lpszText);
1853 else {
1854 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1855 -1, NULL, 0);
1856 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1857 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1858 toolPtr->lpszText, len);
1864 if(infoPtr->nCurrentTool == -1) return 0;
1865 /* force repaint */
1866 if (infoPtr->bActive)
1867 TOOLTIPS_Show (infoPtr, FALSE);
1868 else if (infoPtr->bTrackActive)
1869 TOOLTIPS_Show (infoPtr, TRUE);
1871 return 0;
1875 static LRESULT
1876 TOOLTIPS_Create (HWND hwnd)
1878 TOOLTIPS_INFO *infoPtr;
1880 /* allocate memory for info structure */
1881 infoPtr = Alloc (sizeof(TOOLTIPS_INFO));
1882 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1884 /* initialize info structure */
1885 infoPtr->bActive = TRUE;
1886 infoPtr->bTrackActive = FALSE;
1888 infoPtr->nMaxTipWidth = -1;
1889 infoPtr->nTool = -1;
1890 infoPtr->nCurrentTool = -1;
1891 infoPtr->nTrackTool = -1;
1892 infoPtr->hwndSelf = hwnd;
1894 /* initialize colours and fonts */
1895 TOOLTIPS_InitSystemSettings(infoPtr);
1897 TOOLTIPS_SetDelayTime(infoPtr, TTDT_AUTOMATIC, 0);
1899 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
1901 return 0;
1905 static LRESULT
1906 TOOLTIPS_Destroy (TOOLTIPS_INFO *infoPtr)
1908 TTTOOL_INFO *toolPtr;
1909 UINT i;
1911 /* free tools */
1912 if (infoPtr->tools) {
1913 for (i = 0; i < infoPtr->uNumTools; i++) {
1914 toolPtr = &infoPtr->tools[i];
1915 if (toolPtr->lpszText) {
1916 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1917 !IS_INTRESOURCE(toolPtr->lpszText) )
1919 Free (toolPtr->lpszText);
1920 toolPtr->lpszText = NULL;
1924 /* remove subclassing */
1925 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1926 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1927 RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
1929 else {
1930 RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
1934 Free (infoPtr->tools);
1937 /* free title string */
1938 Free (infoPtr->pszTitle);
1939 /* free title icon if not a standard one */
1940 if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
1941 DeleteObject(infoPtr->hTitleIcon);
1943 /* delete fonts */
1944 DeleteObject (infoPtr->hFont);
1945 DeleteObject (infoPtr->hTitleFont);
1947 /* free tool tips info data */
1948 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1949 Free (infoPtr);
1951 return 0;
1955 static inline LRESULT
1956 TOOLTIPS_GetFont (const TOOLTIPS_INFO *infoPtr)
1958 return (LRESULT)infoPtr->hFont;
1962 static LRESULT
1963 TOOLTIPS_MouseMessage (TOOLTIPS_INFO *infoPtr)
1965 TOOLTIPS_Hide (infoPtr);
1967 return 0;
1971 static LRESULT
1972 TOOLTIPS_NCCreate (HWND hwnd)
1974 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1975 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
1977 dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
1978 dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
1980 /* WS_BORDER only draws a border round the window rect, not the
1981 * window region, therefore it is useless to us in balloon mode */
1982 if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
1984 SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
1986 dwExStyle |= WS_EX_TOOLWINDOW;
1987 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
1989 return TRUE;
1993 static LRESULT
1994 TOOLTIPS_NCHitTest (const TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1996 INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
1998 TRACE(" nTool=%d\n", nTool);
2000 if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
2001 if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
2002 TRACE("-- in transparent mode!\n");
2003 return HTTRANSPARENT;
2007 return DefWindowProcW (infoPtr->hwndSelf, WM_NCHITTEST, wParam, lParam);
2011 static LRESULT
2012 TOOLTIPS_NotifyFormat (TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2014 FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", infoPtr->hwndSelf, wParam, lParam);
2016 return 0;
2020 static LRESULT
2021 TOOLTIPS_Paint (const TOOLTIPS_INFO *infoPtr, HDC hDC)
2023 HDC hdc;
2024 PAINTSTRUCT ps;
2026 hdc = (hDC == NULL) ? BeginPaint (infoPtr->hwndSelf, &ps) : hDC;
2027 TOOLTIPS_Refresh (infoPtr, hdc);
2028 if (!hDC)
2029 EndPaint (infoPtr->hwndSelf, &ps);
2030 return 0;
2034 static LRESULT
2035 TOOLTIPS_SetFont (TOOLTIPS_INFO *infoPtr, HFONT hFont, BOOL redraw)
2037 LOGFONTW lf;
2039 if(!GetObjectW(hFont, sizeof(lf), &lf))
2040 return 0;
2042 DeleteObject (infoPtr->hFont);
2043 infoPtr->hFont = CreateFontIndirectW(&lf);
2045 DeleteObject (infoPtr->hTitleFont);
2046 lf.lfWeight = FW_BOLD;
2047 infoPtr->hTitleFont = CreateFontIndirectW(&lf);
2049 if (redraw && infoPtr->nCurrentTool != -1) {
2050 FIXME("full redraw needed!\n");
2053 return 0;
2056 /******************************************************************
2057 * TOOLTIPS_GetTextLength
2059 * This function is called when the tooltip receive a
2060 * WM_GETTEXTLENGTH message.
2062 * returns the length, in characters, of the tip text
2064 static inline LRESULT
2065 TOOLTIPS_GetTextLength(const TOOLTIPS_INFO *infoPtr)
2067 return strlenW(infoPtr->szTipText);
2070 /******************************************************************
2071 * TOOLTIPS_OnWMGetText
2073 * This function is called when the tooltip receive a
2074 * WM_GETTEXT message.
2075 * wParam : specifies the maximum number of characters to be copied
2076 * lParam : is the pointer to the buffer that will receive
2077 * the tip text
2079 * returns the number of characters copied
2081 static LRESULT
2082 TOOLTIPS_OnWMGetText (const TOOLTIPS_INFO *infoPtr, WPARAM size, LPWSTR pszText)
2084 LRESULT res;
2086 if(!size)
2087 return 0;
2089 res = min(strlenW(infoPtr->szTipText)+1, size);
2090 memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2091 pszText[res-1] = '\0';
2092 return res-1;
2095 static LRESULT
2096 TOOLTIPS_Timer (TOOLTIPS_INFO *infoPtr, INT iTimer)
2098 INT nOldTool;
2100 TRACE("timer %d (%p) expired!\n", iTimer, infoPtr->hwndSelf);
2102 switch (iTimer) {
2103 case ID_TIMERSHOW:
2104 KillTimer (infoPtr->hwndSelf, ID_TIMERSHOW);
2105 nOldTool = infoPtr->nTool;
2106 if ((infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, TRUE)) == nOldTool)
2107 TOOLTIPS_Show (infoPtr, FALSE);
2108 break;
2110 case ID_TIMERPOP:
2111 TOOLTIPS_Hide (infoPtr);
2112 break;
2114 case ID_TIMERLEAVE:
2115 nOldTool = infoPtr->nTool;
2116 infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, FALSE);
2117 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
2118 infoPtr->nTool, infoPtr->nCurrentTool);
2119 if (infoPtr->nTool != nOldTool) {
2120 if(infoPtr->nTool == -1) { /* Moved out of all tools */
2121 TOOLTIPS_Hide(infoPtr);
2122 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2123 } else if (nOldTool == -1) { /* Moved from outside */
2124 ERR("How did this happen?\n");
2125 } else { /* Moved from one to another */
2126 TOOLTIPS_Hide (infoPtr);
2127 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2128 if(infoPtr->bActive) {
2129 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2130 TRACE("timer 1 started!\n");
2134 break;
2136 default:
2137 ERR("Unknown timer id %d\n", iTimer);
2138 break;
2140 return 0;
2144 static LRESULT
2145 TOOLTIPS_WinIniChange (TOOLTIPS_INFO *infoPtr)
2147 TOOLTIPS_InitSystemSettings (infoPtr);
2149 return 0;
2153 static LRESULT CALLBACK
2154 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2156 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr ((HWND)dwRef);
2157 MSG msg;
2159 switch(uMsg) {
2160 case WM_MOUSEMOVE:
2161 case WM_LBUTTONDOWN:
2162 case WM_LBUTTONUP:
2163 case WM_MBUTTONDOWN:
2164 case WM_MBUTTONUP:
2165 case WM_RBUTTONDOWN:
2166 case WM_RBUTTONUP:
2167 msg.hwnd = hwnd;
2168 msg.message = uMsg;
2169 msg.wParam = wParam;
2170 msg.lParam = lParam;
2171 TOOLTIPS_RelayEvent(infoPtr, &msg);
2172 break;
2174 default:
2175 break;
2177 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
2181 static LRESULT CALLBACK
2182 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2184 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2186 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2187 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2188 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2189 switch (uMsg)
2191 case TTM_ACTIVATE:
2192 return TOOLTIPS_Activate (infoPtr, (BOOL)wParam);
2194 case TTM_ADDTOOLA:
2195 case TTM_ADDTOOLW:
2196 return TOOLTIPS_AddToolT (infoPtr, (LPTTTOOLINFOW)lParam, uMsg == TTM_ADDTOOLW);
2198 case TTM_DELTOOLA:
2199 case TTM_DELTOOLW:
2200 return TOOLTIPS_DelToolT (infoPtr, (LPTOOLINFOW)lParam,
2201 uMsg == TTM_DELTOOLW);
2202 case TTM_ENUMTOOLSA:
2203 case TTM_ENUMTOOLSW:
2204 return TOOLTIPS_EnumToolsT (infoPtr, (UINT)wParam, (LPTTTOOLINFOW)lParam,
2205 uMsg == TTM_ENUMTOOLSW);
2206 case TTM_GETBUBBLESIZE:
2207 return TOOLTIPS_GetBubbleSize (infoPtr, (LPTTTOOLINFOW)lParam);
2209 case TTM_GETCURRENTTOOLA:
2210 case TTM_GETCURRENTTOOLW:
2211 return TOOLTIPS_GetCurrentToolT (infoPtr, (LPTTTOOLINFOW)lParam,
2212 uMsg == TTM_GETCURRENTTOOLW);
2214 case TTM_GETDELAYTIME:
2215 return TOOLTIPS_GetDelayTime (infoPtr, (DWORD)wParam);
2217 case TTM_GETMARGIN:
2218 return TOOLTIPS_GetMargin (infoPtr, (LPRECT)lParam);
2220 case TTM_GETMAXTIPWIDTH:
2221 return TOOLTIPS_GetMaxTipWidth (infoPtr);
2223 case TTM_GETTEXTA:
2224 case TTM_GETTEXTW:
2225 return TOOLTIPS_GetTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2226 uMsg == TTM_GETTEXTW);
2228 case TTM_GETTIPBKCOLOR:
2229 return TOOLTIPS_GetTipBkColor (infoPtr);
2231 case TTM_GETTIPTEXTCOLOR:
2232 return TOOLTIPS_GetTipTextColor (infoPtr);
2234 case TTM_GETTOOLCOUNT:
2235 return TOOLTIPS_GetToolCount (infoPtr);
2237 case TTM_GETTOOLINFOA:
2238 case TTM_GETTOOLINFOW:
2239 return TOOLTIPS_GetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2240 uMsg == TTM_GETTOOLINFOW);
2242 case TTM_HITTESTA:
2243 case TTM_HITTESTW:
2244 return TOOLTIPS_HitTestT (infoPtr, (LPTTHITTESTINFOW)lParam,
2245 uMsg == TTM_HITTESTW);
2246 case TTM_NEWTOOLRECTA:
2247 case TTM_NEWTOOLRECTW:
2248 return TOOLTIPS_NewToolRectT (infoPtr, (LPTTTOOLINFOW)lParam);
2250 case TTM_POP:
2251 return TOOLTIPS_Pop (infoPtr);
2253 case TTM_RELAYEVENT:
2254 return TOOLTIPS_RelayEvent (infoPtr, (LPMSG)lParam);
2256 case TTM_SETDELAYTIME:
2257 return TOOLTIPS_SetDelayTime (infoPtr, (DWORD)wParam, (INT)LOWORD(lParam));
2259 case TTM_SETMARGIN:
2260 return TOOLTIPS_SetMargin (infoPtr, (LPRECT)lParam);
2262 case TTM_SETMAXTIPWIDTH:
2263 return TOOLTIPS_SetMaxTipWidth (infoPtr, (INT)lParam);
2265 case TTM_SETTIPBKCOLOR:
2266 return TOOLTIPS_SetTipBkColor (infoPtr, (COLORREF)wParam);
2268 case TTM_SETTIPTEXTCOLOR:
2269 return TOOLTIPS_SetTipTextColor (infoPtr, (COLORREF)wParam);
2271 case TTM_SETTITLEA:
2272 case TTM_SETTITLEW:
2273 return TOOLTIPS_SetTitleT (infoPtr, (UINT_PTR)wParam, (LPCWSTR)lParam,
2274 uMsg == TTM_SETTITLEW);
2276 case TTM_SETTOOLINFOA:
2277 case TTM_SETTOOLINFOW:
2278 return TOOLTIPS_SetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2279 uMsg == TTM_SETTOOLINFOW);
2281 case TTM_TRACKACTIVATE:
2282 return TOOLTIPS_TrackActivate (infoPtr, (BOOL)wParam, (LPTTTOOLINFOA)lParam);
2284 case TTM_TRACKPOSITION:
2285 return TOOLTIPS_TrackPosition (infoPtr, lParam);
2287 case TTM_UPDATE:
2288 return TOOLTIPS_Update (infoPtr);
2290 case TTM_UPDATETIPTEXTA:
2291 case TTM_UPDATETIPTEXTW:
2292 return TOOLTIPS_UpdateTipTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2293 uMsg == TTM_UPDATETIPTEXTW);
2295 case TTM_WINDOWFROMPOINT:
2296 return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2298 case WM_CREATE:
2299 return TOOLTIPS_Create (hwnd);
2301 case WM_DESTROY:
2302 return TOOLTIPS_Destroy (infoPtr);
2304 case WM_ERASEBKGND:
2305 /* we draw the background in WM_PAINT */
2306 return 0;
2308 case WM_GETFONT:
2309 return TOOLTIPS_GetFont (infoPtr);
2311 case WM_GETTEXT:
2312 return TOOLTIPS_OnWMGetText (infoPtr, wParam, (LPWSTR)lParam);
2314 case WM_GETTEXTLENGTH:
2315 return TOOLTIPS_GetTextLength (infoPtr);
2317 case WM_LBUTTONDOWN:
2318 case WM_LBUTTONUP:
2319 case WM_MBUTTONDOWN:
2320 case WM_MBUTTONUP:
2321 case WM_RBUTTONDOWN:
2322 case WM_RBUTTONUP:
2323 case WM_MOUSEMOVE:
2324 return TOOLTIPS_MouseMessage (infoPtr);
2326 case WM_NCCREATE:
2327 return TOOLTIPS_NCCreate (hwnd);
2329 case WM_NCHITTEST:
2330 return TOOLTIPS_NCHitTest (infoPtr, wParam, lParam);
2332 case WM_NOTIFYFORMAT:
2333 return TOOLTIPS_NotifyFormat (infoPtr, wParam, lParam);
2335 case WM_PRINTCLIENT:
2336 case WM_PAINT:
2337 return TOOLTIPS_Paint (infoPtr, (HDC)wParam);
2339 case WM_SETFONT:
2340 return TOOLTIPS_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
2342 case WM_SYSCOLORCHANGE:
2343 COMCTL32_RefreshSysColors();
2344 return 0;
2346 case WM_TIMER:
2347 return TOOLTIPS_Timer (infoPtr, (INT)wParam);
2349 case WM_WININICHANGE:
2350 return TOOLTIPS_WinIniChange (infoPtr);
2352 default:
2353 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2354 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2355 uMsg, wParam, lParam);
2356 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2361 VOID
2362 TOOLTIPS_Register (void)
2364 WNDCLASSW wndClass;
2366 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2367 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2368 wndClass.lpfnWndProc = TOOLTIPS_WindowProc;
2369 wndClass.cbClsExtra = 0;
2370 wndClass.cbWndExtra = sizeof(TOOLTIPS_INFO *);
2371 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2372 wndClass.hbrBackground = 0;
2373 wndClass.lpszClassName = TOOLTIPS_CLASSW;
2375 RegisterClassW (&wndClass);
2377 hTooltipIcons[TTI_NONE] = NULL;
2378 hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2379 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2380 hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2381 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2382 hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2383 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2387 VOID
2388 TOOLTIPS_Unregister (void)
2390 int i;
2391 for (i = TTI_INFO; i <= TTI_ERROR; i++)
2392 DestroyIcon(hTooltipIcons[i]);
2393 UnregisterClassW (TOOLTIPS_CLASSW, NULL);