vbscript: Implemented Tan.
[wine.git] / dlls / comctl32 / tooltips.c
blobf74ea657c7d68eb84f7fe4cae0e851e6c4bc4463
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 HWND hwnd;
115 BOOL bNotifyUnicode;
116 UINT_PTR uId;
117 RECT rect;
118 HINSTANCE hinst;
119 LPWSTR lpszText;
120 LPARAM lParam;
121 } TTTOOL_INFO;
124 typedef struct
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;
149 TTTOOL_INFO *tools;
150 } TOOLTIPS_INFO;
152 #define ID_TIMERSHOW 1 /* show delay timer */
153 #define ID_TIMERPOP 2 /* auto pop timer */
154 #define ID_TIMERLEAVE 3 /* tool leave timer */
157 #define TOOLTIPS_GetInfoPtr(hWindow) ((TOOLTIPS_INFO *)GetWindowLongPtrW (hWindow, 0))
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
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
174 #define MAX_TEXT_SIZE_A 80 /* maximum retrieving text size by ANSI message */
176 static LRESULT CALLBACK
177 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uId, DWORD_PTR dwRef);
180 static inline BOOL TOOLTIPS_IsCallbackString(LPCWSTR str, BOOL isW)
182 if (isW)
183 return str == LPSTR_TEXTCALLBACKW;
184 else
185 return (LPCSTR)str == LPSTR_TEXTCALLBACKA;
188 static inline UINT_PTR
189 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
191 UINT i;
192 for (i = 0; i <= TTI_ERROR; i++)
193 if (hTooltipIcons[i] == hIcon)
194 return i;
195 return (UINT_PTR)hIcon;
198 static void
199 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
201 NONCLIENTMETRICSW nclm;
203 infoPtr->clrBk = comctl32_color.clrInfoBk;
204 infoPtr->clrText = comctl32_color.clrInfoText;
206 DeleteObject (infoPtr->hFont);
207 nclm.cbSize = sizeof(nclm);
208 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
209 infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
211 DeleteObject (infoPtr->hTitleFont);
212 nclm.lfStatusFont.lfWeight = FW_BOLD;
213 infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
216 /* Custom draw routines */
217 static void
218 TOOLTIPS_customdraw_fill(const TOOLTIPS_INFO *infoPtr, NMTTCUSTOMDRAW *lpnmttcd,
219 HDC hdc, const RECT *rcBounds, UINT uFlags)
221 ZeroMemory(lpnmttcd, sizeof(NMTTCUSTOMDRAW));
222 lpnmttcd->uDrawFlags = uFlags;
223 lpnmttcd->nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
224 lpnmttcd->nmcd.hdr.code = NM_CUSTOMDRAW;
225 if (infoPtr->nCurrentTool != -1) {
226 TTTOOL_INFO *toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
227 lpnmttcd->nmcd.hdr.idFrom = toolPtr->uId;
229 lpnmttcd->nmcd.hdc = hdc;
230 lpnmttcd->nmcd.rc = *rcBounds;
231 /* FIXME - dwItemSpec, uItemState, lItemlParam */
234 static inline DWORD
235 TOOLTIPS_notify_customdraw (DWORD dwDrawStage, NMTTCUSTOMDRAW *lpnmttcd)
237 LRESULT result = CDRF_DODEFAULT;
238 lpnmttcd->nmcd.dwDrawStage = dwDrawStage;
240 TRACE("Notifying stage %d, flags %x, id %x\n", lpnmttcd->nmcd.dwDrawStage,
241 lpnmttcd->uDrawFlags, lpnmttcd->nmcd.hdr.code);
243 result = SendMessageW(GetParent(lpnmttcd->nmcd.hdr.hwndFrom), WM_NOTIFY,
244 0, (LPARAM)lpnmttcd);
246 TRACE("Notify result %x\n", (unsigned int)result);
248 return result;
251 static void
252 TOOLTIPS_Refresh (const TOOLTIPS_INFO *infoPtr, HDC hdc)
254 RECT rc;
255 INT oldBkMode;
256 HFONT hOldFont;
257 HBRUSH hBrush;
258 UINT uFlags = DT_EXTERNALLEADING;
259 HRGN hRgn = NULL;
260 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
261 NMTTCUSTOMDRAW nmttcd;
262 DWORD cdmode;
264 if (infoPtr->nMaxTipWidth > -1)
265 uFlags |= DT_WORDBREAK;
266 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)
267 uFlags |= DT_NOPREFIX;
268 GetClientRect (infoPtr->hwndSelf, &rc);
270 hBrush = CreateSolidBrush(infoPtr->clrBk);
272 oldBkMode = SetBkMode (hdc, TRANSPARENT);
273 SetTextColor (hdc, infoPtr->clrText);
274 hOldFont = SelectObject (hdc, infoPtr->hFont);
276 /* Custom draw - Call PrePaint once initial properties set up */
277 /* Note: Contrary to MSDN, CDRF_SKIPDEFAULT still draws a tooltip */
278 TOOLTIPS_customdraw_fill(infoPtr, &nmttcd, hdc, &rc, uFlags);
279 cdmode = TOOLTIPS_notify_customdraw(CDDS_PREPAINT, &nmttcd);
280 uFlags = nmttcd.uDrawFlags;
282 if (dwStyle & TTS_BALLOON)
284 /* create a region to store result into */
285 hRgn = CreateRectRgn(0, 0, 0, 0);
287 GetWindowRgn(infoPtr->hwndSelf, hRgn);
289 /* fill the background */
290 FillRgn(hdc, hRgn, hBrush);
291 DeleteObject(hBrush);
292 hBrush = NULL;
294 else
296 /* fill the background */
297 FillRect(hdc, &rc, hBrush);
298 DeleteObject(hBrush);
299 hBrush = NULL;
302 if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
304 /* calculate text rectangle */
305 rc.left += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
306 rc.top += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
307 rc.right -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
308 rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
309 if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
311 if (infoPtr->pszTitle)
313 RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
314 int height;
315 BOOL icon_present;
316 HFONT prevFont;
318 /* draw icon */
319 icon_present = infoPtr->hTitleIcon &&
320 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
321 ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
322 if (icon_present)
323 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
325 rcTitle.bottom = rc.top + ICON_HEIGHT;
327 /* draw title text */
328 prevFont = SelectObject (hdc, infoPtr->hTitleFont);
329 height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
330 SelectObject (hdc, prevFont);
331 rc.top += height + BALLOON_TITLE_TEXT_SPACING;
334 else
336 /* calculate text rectangle */
337 rc.left += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
338 rc.top += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
339 rc.right -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
340 rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
343 /* draw text */
344 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
346 /* Custom draw - Call PostPaint after drawing */
347 if (cdmode & CDRF_NOTIFYPOSTPAINT) {
348 TOOLTIPS_notify_customdraw(CDDS_POSTPAINT, &nmttcd);
351 /* be polite and reset the things we changed in the dc */
352 SelectObject (hdc, hOldFont);
353 SetBkMode (hdc, oldBkMode);
355 if (dwStyle & TTS_BALLOON)
357 /* frame region because default window proc doesn't do it */
358 INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
359 INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
361 hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
362 FrameRgn(hdc, hRgn, hBrush, width, height);
365 if (hRgn)
366 DeleteObject(hRgn);
369 static void TOOLTIPS_GetDispInfoA(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
371 NMTTDISPINFOA ttnmdi;
373 /* fill NMHDR struct */
374 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
375 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
376 ttnmdi.hdr.idFrom = toolPtr->uId;
377 ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
378 ttnmdi.lpszText = ttnmdi.szText;
379 ttnmdi.uFlags = toolPtr->uFlags;
380 ttnmdi.lParam = toolPtr->lParam;
382 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
383 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
385 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
386 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
387 buffer, INFOTIPSIZE);
388 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
389 toolPtr->hinst = ttnmdi.hinst;
390 toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
393 else if (ttnmdi.lpszText == 0) {
394 buffer[0] = '\0';
396 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
397 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
398 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
399 toolPtr->hinst = 0;
400 toolPtr->lpszText = NULL;
401 Str_SetPtrW(&toolPtr->lpszText, buffer);
404 else {
405 ERR("recursive text callback!\n");
406 buffer[0] = '\0';
409 /* no text available - try calling parent instead as per native */
410 /* FIXME: Unsure if SETITEM should save the value or not */
411 if (buffer[0] == 0x00) {
413 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
415 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
416 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
417 buffer, INFOTIPSIZE);
418 } else if (ttnmdi.lpszText &&
419 ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
420 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
425 static void TOOLTIPS_GetDispInfoW(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
427 NMTTDISPINFOW ttnmdi;
429 /* fill NMHDR struct */
430 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
431 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
432 ttnmdi.hdr.idFrom = toolPtr->uId;
433 ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
434 ttnmdi.lpszText = ttnmdi.szText;
435 ttnmdi.uFlags = toolPtr->uFlags;
436 ttnmdi.lParam = toolPtr->lParam;
438 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
439 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
441 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
442 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
443 buffer, INFOTIPSIZE);
444 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
445 toolPtr->hinst = ttnmdi.hinst;
446 toolPtr->lpszText = ttnmdi.lpszText;
449 else if (ttnmdi.lpszText == 0) {
450 buffer[0] = '\0';
452 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
453 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
454 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
455 toolPtr->hinst = 0;
456 toolPtr->lpszText = NULL;
457 Str_SetPtrW(&toolPtr->lpszText, buffer);
460 else {
461 ERR("recursive text callback!\n");
462 buffer[0] = '\0';
465 /* no text available - try calling parent instead as per native */
466 /* FIXME: Unsure if SETITEM should save the value or not */
467 if (buffer[0] == 0x00) {
469 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
471 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
472 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
473 buffer, INFOTIPSIZE);
474 } else if (ttnmdi.lpszText &&
475 ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
476 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
482 static void
483 TOOLTIPS_GetTipText (const TOOLTIPS_INFO *infoPtr, INT nTool, WCHAR *buffer)
485 TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
487 if (IS_INTRESOURCE(toolPtr->lpszText) && toolPtr->hinst) {
488 /* load a resource */
489 TRACE("load res string %p %x\n",
490 toolPtr->hinst, LOWORD(toolPtr->lpszText));
491 LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText),
492 buffer, INFOTIPSIZE);
494 else if (toolPtr->lpszText) {
495 if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
496 if (toolPtr->bNotifyUnicode)
497 TOOLTIPS_GetDispInfoW(infoPtr, toolPtr, buffer);
498 else
499 TOOLTIPS_GetDispInfoA(infoPtr, toolPtr, buffer);
501 else {
502 /* the item is a usual (unicode) text */
503 lstrcpynW (buffer, toolPtr->lpszText, INFOTIPSIZE);
506 else {
507 /* no text available */
508 buffer[0] = '\0';
511 TRACE("%s\n", debugstr_w(buffer));
515 static void
516 TOOLTIPS_CalcTipSize (const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
518 HDC hdc;
519 HFONT hOldFont;
520 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
521 UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
522 RECT rc = {0, 0, 0, 0};
523 SIZE title = {0, 0};
525 if (infoPtr->nMaxTipWidth > -1) {
526 rc.right = infoPtr->nMaxTipWidth;
527 uFlags |= DT_WORDBREAK;
529 if (style & TTS_NOPREFIX)
530 uFlags |= DT_NOPREFIX;
531 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
533 hdc = GetDC (infoPtr->hwndSelf);
534 if (infoPtr->pszTitle)
536 RECT rcTitle = {0, 0, 0, 0};
537 TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
538 if (infoPtr->hTitleIcon)
540 title.cx = ICON_WIDTH;
541 title.cy = ICON_HEIGHT;
543 if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
544 hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
545 DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
546 SelectObject (hdc, hOldFont);
547 title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
548 title.cx += (rcTitle.right - rcTitle.left);
550 hOldFont = SelectObject (hdc, infoPtr->hFont);
551 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
552 SelectObject (hdc, hOldFont);
553 ReleaseDC (infoPtr->hwndSelf, hdc);
555 if ((style & TTS_BALLOON) || infoPtr->pszTitle)
557 lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
558 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
559 lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
560 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
561 BALLOON_STEMHEIGHT;
563 else
565 lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
566 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
567 lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
568 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
573 static void
574 TOOLTIPS_Show (TOOLTIPS_INFO *infoPtr, BOOL track_activate)
576 TTTOOL_INFO *toolPtr;
577 HMONITOR monitor;
578 MONITORINFO mon_info;
579 RECT rect;
580 SIZE size;
581 NMHDR hdr;
582 int ptfx = 0;
583 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
584 INT nTool;
586 if (track_activate)
588 if (infoPtr->nTrackTool == -1)
590 TRACE("invalid tracking tool (-1)!\n");
591 return;
593 nTool = infoPtr->nTrackTool;
595 else
597 if (infoPtr->nTool == -1)
599 TRACE("invalid tool (-1)!\n");
600 return;
602 nTool = infoPtr->nTool;
605 TRACE("Show tooltip pre %d! (%p)\n", nTool, infoPtr->hwndSelf);
607 TOOLTIPS_GetTipText (infoPtr, nTool, infoPtr->szTipText);
609 if (infoPtr->szTipText[0] == '\0')
610 return;
612 toolPtr = &infoPtr->tools[nTool];
614 if (!track_activate)
615 infoPtr->nCurrentTool = infoPtr->nTool;
617 TRACE("Show tooltip %d!\n", nTool);
619 hdr.hwndFrom = infoPtr->hwndSelf;
620 hdr.idFrom = toolPtr->uId;
621 hdr.code = TTN_SHOW;
622 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
624 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
626 TOOLTIPS_CalcTipSize (infoPtr, &size);
627 TRACE("size %d x %d\n", size.cx, size.cy);
629 if (track_activate && (toolPtr->uFlags & TTF_TRACK))
631 rect.left = infoPtr->xTrackPos;
632 rect.top = infoPtr->yTrackPos;
633 ptfx = rect.left;
635 if (toolPtr->uFlags & TTF_CENTERTIP)
637 rect.left -= (size.cx / 2);
638 if (!(style & TTS_BALLOON))
639 rect.top -= (size.cy / 2);
641 if (!(infoPtr->bToolBelow = (infoPtr->yTrackPos + size.cy <= GetSystemMetrics(SM_CYSCREEN))))
642 rect.top -= size.cy;
644 if (!(toolPtr->uFlags & TTF_ABSOLUTE))
646 if (style & TTS_BALLOON)
647 rect.left -= BALLOON_STEMINDENT;
648 else
650 RECT rcTool;
652 if (toolPtr->uFlags & TTF_IDISHWND)
653 GetWindowRect ((HWND)toolPtr->uId, &rcTool);
654 else
656 rcTool = toolPtr->rect;
657 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
660 /* smart placement */
661 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
662 (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
663 rect.left = rcTool.right;
667 else
669 if (toolPtr->uFlags & TTF_CENTERTIP)
671 RECT rc;
673 if (toolPtr->uFlags & TTF_IDISHWND)
674 GetWindowRect ((HWND)toolPtr->uId, &rc);
675 else {
676 rc = toolPtr->rect;
677 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
679 rect.left = (rc.left + rc.right - size.cx) / 2;
680 if (style & TTS_BALLOON)
682 ptfx = rc.left + ((rc.right - rc.left) / 2);
684 /* CENTERTIP ballon tooltips default to below the field
685 * if they fit on the screen */
686 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
688 rect.top = rc.top - size.cy;
689 infoPtr->bToolBelow = FALSE;
691 else
693 infoPtr->bToolBelow = TRUE;
694 rect.top = rc.bottom;
696 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
698 else
700 rect.top = rc.bottom + 2;
701 infoPtr->bToolBelow = TRUE;
704 else
706 GetCursorPos ((LPPOINT)&rect);
707 if (style & TTS_BALLOON)
709 ptfx = rect.left;
710 if(rect.top - size.cy >= 0)
712 rect.top -= size.cy;
713 infoPtr->bToolBelow = FALSE;
715 else
717 infoPtr->bToolBelow = TRUE;
718 rect.top += 20;
720 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
722 else
724 rect.top += 20;
725 infoPtr->bToolBelow = TRUE;
730 TRACE("pos %d - %d\n", rect.left, rect.top);
732 rect.right = rect.left + size.cx;
733 rect.bottom = rect.top + size.cy;
735 /* check position */
737 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
738 mon_info.cbSize = sizeof(mon_info);
739 GetMonitorInfoW( monitor, &mon_info );
741 if( rect.right > mon_info.rcWork.right ) {
742 rect.left -= rect.right - mon_info.rcWork.right + 2;
743 rect.right = mon_info.rcWork.right - 2;
745 if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
747 if( rect.bottom > mon_info.rcWork.bottom ) {
748 RECT rc;
750 if (toolPtr->uFlags & TTF_IDISHWND)
751 GetWindowRect ((HWND)toolPtr->uId, &rc);
752 else {
753 rc = toolPtr->rect;
754 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
756 rect.bottom = rc.top - 2;
757 rect.top = rect.bottom - size.cy;
760 AdjustWindowRectEx (&rect, GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE),
761 FALSE, GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE));
763 if (style & TTS_BALLOON)
765 HRGN hRgn;
766 HRGN hrStem;
767 POINT pts[3];
769 ptfx -= rect.left;
771 if(infoPtr->bToolBelow)
773 pts[0].x = ptfx;
774 pts[0].y = 0;
775 pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
776 pts[1].y = BALLOON_STEMHEIGHT;
777 pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
778 pts[2].y = pts[1].y;
779 if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
781 pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
782 pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
785 else
787 pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
788 pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
789 pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
790 pts[1].y = pts[0].y;
791 pts[2].x = ptfx;
792 pts[2].y = (rect.bottom - rect.top);
793 if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
795 pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
796 pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
800 hrStem = CreatePolygonRgn(pts, sizeof(pts) / sizeof(pts[0]), ALTERNATE);
802 hRgn = CreateRoundRectRgn(0,
803 (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
804 rect.right - rect.left,
805 (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
806 BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
808 CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
809 DeleteObject(hrStem);
811 SetWindowRgn(infoPtr->hwndSelf, hRgn, FALSE);
812 /* we don't free the region handle as the system deletes it when
813 * it is no longer needed */
816 SetWindowPos (infoPtr->hwndSelf, HWND_TOPMOST, rect.left, rect.top,
817 rect.right - rect.left, rect.bottom - rect.top,
818 SWP_SHOWWINDOW | SWP_NOACTIVATE);
820 /* repaint the tooltip */
821 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
822 UpdateWindow(infoPtr->hwndSelf);
824 if (!track_activate)
826 SetTimer (infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
827 TRACE("timer 2 started!\n");
828 SetTimer (infoPtr->hwndSelf, ID_TIMERLEAVE, infoPtr->nReshowTime, 0);
829 TRACE("timer 3 started!\n");
834 static void
835 TOOLTIPS_Hide (TOOLTIPS_INFO *infoPtr)
837 TTTOOL_INFO *toolPtr;
838 NMHDR hdr;
840 TRACE("Hide tooltip %d! (%p)\n", infoPtr->nCurrentTool, infoPtr->hwndSelf);
842 if (infoPtr->nCurrentTool == -1)
843 return;
845 toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
846 KillTimer (infoPtr->hwndSelf, ID_TIMERPOP);
848 hdr.hwndFrom = infoPtr->hwndSelf;
849 hdr.idFrom = toolPtr->uId;
850 hdr.code = TTN_POP;
851 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
853 infoPtr->nCurrentTool = -1;
855 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
856 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
860 static void
861 TOOLTIPS_TrackShow (TOOLTIPS_INFO *infoPtr)
863 TOOLTIPS_Show(infoPtr, TRUE);
867 static void
868 TOOLTIPS_TrackHide (const TOOLTIPS_INFO *infoPtr)
870 TTTOOL_INFO *toolPtr;
871 NMHDR hdr;
873 TRACE("hide tracking tooltip %d\n", infoPtr->nTrackTool);
875 if (infoPtr->nTrackTool == -1)
876 return;
878 toolPtr = &infoPtr->tools[infoPtr->nTrackTool];
880 hdr.hwndFrom = infoPtr->hwndSelf;
881 hdr.idFrom = toolPtr->uId;
882 hdr.code = TTN_POP;
883 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
885 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
886 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
889 /* Structure layout is the same for TTTOOLINFOW and TTTOOLINFOA,
890 this helper is used in both cases. */
891 static INT
892 TOOLTIPS_GetToolFromInfoT (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
894 TTTOOL_INFO *toolPtr;
895 UINT nTool;
897 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
898 toolPtr = &infoPtr->tools[nTool];
900 if (!(toolPtr->uFlags & TTF_IDISHWND) &&
901 (lpToolInfo->hwnd == toolPtr->hwnd) &&
902 (lpToolInfo->uId == toolPtr->uId))
903 return nTool;
906 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
907 toolPtr = &infoPtr->tools[nTool];
909 if ((toolPtr->uFlags & TTF_IDISHWND) &&
910 (lpToolInfo->uId == toolPtr->uId))
911 return nTool;
914 return -1;
918 static INT
919 TOOLTIPS_GetToolFromPoint (const TOOLTIPS_INFO *infoPtr, HWND hwnd, const POINT *lpPt)
921 TTTOOL_INFO *toolPtr;
922 UINT nTool;
924 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
925 toolPtr = &infoPtr->tools[nTool];
927 if (!(toolPtr->uFlags & TTF_IDISHWND)) {
928 if (hwnd != toolPtr->hwnd)
929 continue;
930 if (!PtInRect (&toolPtr->rect, *lpPt))
931 continue;
932 return nTool;
936 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
937 toolPtr = &infoPtr->tools[nTool];
939 if (toolPtr->uFlags & TTF_IDISHWND) {
940 if ((HWND)toolPtr->uId == hwnd)
941 return nTool;
945 return -1;
948 static inline void
949 TOOLTIPS_CopyInfoT (const TTTOOL_INFO *toolPtr, TTTOOLINFOW *ti, BOOL isW)
951 if (ti->lpszText) {
952 if (toolPtr->lpszText == NULL ||
953 IS_INTRESOURCE(toolPtr->lpszText) ||
954 toolPtr->lpszText == LPSTR_TEXTCALLBACKW)
955 ti->lpszText = toolPtr->lpszText;
956 else if (isW)
957 strcpyW (ti->lpszText, toolPtr->lpszText);
958 else
959 /* ANSI version, the buffer is maximum 80 bytes without null. */
960 WideCharToMultiByte(CP_ACP, 0, toolPtr->lpszText, -1,
961 (LPSTR)ti->lpszText, MAX_TEXT_SIZE_A, NULL, NULL);
965 static BOOL
966 TOOLTIPS_IsWindowActive (HWND hwnd)
968 HWND hwndActive = GetActiveWindow ();
969 if (!hwndActive)
970 return FALSE;
971 if (hwndActive == hwnd)
972 return TRUE;
973 return IsChild (hwndActive, hwnd);
977 static INT
978 TOOLTIPS_CheckTool (const TOOLTIPS_INFO *infoPtr, BOOL bShowTest)
980 POINT pt;
981 HWND hwndTool;
982 INT nTool;
984 GetCursorPos (&pt);
985 hwndTool = (HWND)SendMessageW (infoPtr->hwndSelf, TTM_WINDOWFROMPOINT, 0, (LPARAM)&pt);
986 if (hwndTool == 0)
987 return -1;
989 ScreenToClient (hwndTool, &pt);
990 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, hwndTool, &pt);
991 if (nTool == -1)
992 return -1;
994 if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_ALWAYSTIP) && bShowTest)
996 TTTOOL_INFO *ti = &infoPtr->tools[nTool];
997 HWND hwnd = (ti->uFlags & TTF_IDISHWND) ? (HWND)ti->uId : ti->hwnd;
999 if (!TOOLTIPS_IsWindowActive(hwnd))
1001 TRACE("not active: hwnd %p, parent %p, active %p\n",
1002 hwnd, GetParent(hwnd), GetActiveWindow());
1003 return -1;
1007 TRACE("tool %d\n", nTool);
1009 return nTool;
1013 static LRESULT
1014 TOOLTIPS_Activate (TOOLTIPS_INFO *infoPtr, BOOL activate)
1016 infoPtr->bActive = activate;
1018 if (infoPtr->bActive)
1019 TRACE("activate!\n");
1021 if (!(infoPtr->bActive) && (infoPtr->nCurrentTool != -1))
1022 TOOLTIPS_Hide (infoPtr);
1024 return 0;
1028 static LRESULT
1029 TOOLTIPS_AddToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1031 TTTOOL_INFO *toolPtr;
1032 INT nResult;
1034 if (!ti) return FALSE;
1036 TRACE("add tool (%p) %p %ld%s!\n",
1037 infoPtr->hwndSelf, ti->hwnd, ti->uId,
1038 (ti->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
1040 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE && !ti->lpszText && isW)
1041 return FALSE;
1043 if (infoPtr->uNumTools == 0) {
1044 infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
1045 toolPtr = infoPtr->tools;
1047 else {
1048 TTTOOL_INFO *oldTools = infoPtr->tools;
1049 infoPtr->tools =
1050 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1051 memcpy (infoPtr->tools, oldTools,
1052 infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1053 Free (oldTools);
1054 toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1057 infoPtr->uNumTools++;
1059 /* copy tool data */
1060 toolPtr->uFlags = ti->uFlags;
1061 toolPtr->hwnd = ti->hwnd;
1062 toolPtr->uId = ti->uId;
1063 toolPtr->rect = ti->rect;
1064 toolPtr->hinst = ti->hinst;
1066 if (ti->cbSize >= TTTOOLINFOW_V1_SIZE) {
1067 if (IS_INTRESOURCE(ti->lpszText)) {
1068 TRACE("add string id %x\n", LOWORD(ti->lpszText));
1069 toolPtr->lpszText = ti->lpszText;
1071 else if (ti->lpszText) {
1072 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW)) {
1073 TRACE("add CALLBACK!\n");
1074 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1076 else if (isW) {
1077 INT len = lstrlenW (ti->lpszText);
1078 TRACE("add text %s!\n", debugstr_w(ti->lpszText));
1079 toolPtr->lpszText = Alloc ((len + 1)*sizeof(WCHAR));
1080 strcpyW (toolPtr->lpszText, ti->lpszText);
1082 else {
1083 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, NULL, 0);
1084 TRACE("add text \"%s\"!\n", (LPSTR)ti->lpszText);
1085 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1086 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, toolPtr->lpszText, len);
1091 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1092 toolPtr->lParam = ti->lParam;
1094 /* install subclassing hook */
1095 if (toolPtr->uFlags & TTF_SUBCLASS) {
1096 if (toolPtr->uFlags & TTF_IDISHWND) {
1097 SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1098 (DWORD_PTR)infoPtr->hwndSelf);
1100 else {
1101 SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1102 (DWORD_PTR)infoPtr->hwndSelf);
1104 TRACE("subclassing installed!\n");
1107 nResult = SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1108 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1109 if (nResult == NFR_ANSI) {
1110 toolPtr->bNotifyUnicode = FALSE;
1111 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1112 } else if (nResult == NFR_UNICODE) {
1113 toolPtr->bNotifyUnicode = TRUE;
1114 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1115 } else {
1116 TRACE (" -- WM_NOTIFYFORMAT returns: error!\n");
1119 return TRUE;
1123 static LRESULT
1124 TOOLTIPS_DelToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1126 TTTOOL_INFO *toolPtr;
1127 INT nTool;
1129 if (!ti) return 0;
1130 if (isW && ti->cbSize > TTTOOLINFOW_V2_SIZE &&
1131 ti->cbSize != TTTOOLINFOW_V3_SIZE)
1132 return 0;
1133 if (infoPtr->uNumTools == 0)
1134 return 0;
1136 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1138 TRACE("tool %d\n", nTool);
1140 if (nTool == -1)
1141 return 0;
1143 /* make sure the tooltip has disappeared before deleting it */
1144 TOOLTIPS_Hide(infoPtr);
1146 /* delete text string */
1147 toolPtr = &infoPtr->tools[nTool];
1148 if (toolPtr->lpszText) {
1149 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1150 !IS_INTRESOURCE(toolPtr->lpszText) )
1151 Free (toolPtr->lpszText);
1154 /* remove subclassing */
1155 if (toolPtr->uFlags & TTF_SUBCLASS) {
1156 if (toolPtr->uFlags & TTF_IDISHWND) {
1157 RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
1159 else {
1160 RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
1164 /* delete tool from tool list */
1165 if (infoPtr->uNumTools == 1) {
1166 Free (infoPtr->tools);
1167 infoPtr->tools = NULL;
1169 else {
1170 TTTOOL_INFO *oldTools = infoPtr->tools;
1171 infoPtr->tools =
1172 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1174 if (nTool > 0)
1175 memcpy (&infoPtr->tools[0], &oldTools[0],
1176 nTool * sizeof(TTTOOL_INFO));
1178 if (nTool < infoPtr->uNumTools - 1)
1179 memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1180 (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1182 Free (oldTools);
1185 /* update any indices affected by delete */
1187 /* destroying tool that mouse was on on last relayed mouse move */
1188 if (infoPtr->nTool == nTool)
1189 /* -1 means no current tool (0 means first tool) */
1190 infoPtr->nTool = -1;
1191 else if (infoPtr->nTool > nTool)
1192 infoPtr->nTool--;
1194 if (infoPtr->nTrackTool == nTool)
1195 /* -1 means no current tool (0 means first tool) */
1196 infoPtr->nTrackTool = -1;
1197 else if (infoPtr->nTrackTool > nTool)
1198 infoPtr->nTrackTool--;
1200 if (infoPtr->nCurrentTool == nTool)
1201 /* -1 means no current tool (0 means first tool) */
1202 infoPtr->nCurrentTool = -1;
1203 else if (infoPtr->nCurrentTool > nTool)
1204 infoPtr->nCurrentTool--;
1206 infoPtr->uNumTools--;
1208 return 0;
1211 static LRESULT
1212 TOOLTIPS_EnumToolsT (const TOOLTIPS_INFO *infoPtr, UINT uIndex, TTTOOLINFOW *ti,
1213 BOOL isW)
1215 TTTOOL_INFO *toolPtr;
1217 if (!ti) return FALSE;
1218 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1219 return FALSE;
1220 if (uIndex >= infoPtr->uNumTools)
1221 return FALSE;
1223 TRACE("index=%u\n", uIndex);
1225 toolPtr = &infoPtr->tools[uIndex];
1227 /* copy tool data */
1228 ti->uFlags = toolPtr->uFlags;
1229 ti->hwnd = toolPtr->hwnd;
1230 ti->uId = toolPtr->uId;
1231 ti->rect = toolPtr->rect;
1232 ti->hinst = toolPtr->hinst;
1233 TOOLTIPS_CopyInfoT (toolPtr, ti, isW);
1235 if (ti->cbSize >= TTTOOLINFOA_V2_SIZE)
1236 ti->lParam = toolPtr->lParam;
1238 return TRUE;
1241 static LRESULT
1242 TOOLTIPS_GetBubbleSize (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
1244 INT nTool;
1245 SIZE size;
1247 if (lpToolInfo == NULL)
1248 return FALSE;
1249 if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1250 return FALSE;
1252 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, lpToolInfo);
1253 if (nTool == -1) return 0;
1255 TRACE("tool %d\n", nTool);
1257 TOOLTIPS_CalcTipSize (infoPtr, &size);
1258 TRACE("size %d x %d\n", size.cx, size.cy);
1260 return MAKELRESULT(size.cx, size.cy);
1263 static LRESULT
1264 TOOLTIPS_GetCurrentToolT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1266 TTTOOL_INFO *toolPtr;
1268 if (ti) {
1269 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1270 return FALSE;
1272 if (infoPtr->nCurrentTool > -1) {
1273 toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
1275 /* copy tool data */
1276 ti->uFlags = toolPtr->uFlags;
1277 ti->rect = toolPtr->rect;
1278 ti->hinst = toolPtr->hinst;
1279 TOOLTIPS_CopyInfoT (toolPtr, ti, isW);
1281 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1282 ti->lParam = toolPtr->lParam;
1284 return TRUE;
1286 else
1287 return FALSE;
1289 else
1290 return (infoPtr->nCurrentTool != -1);
1294 static LRESULT
1295 TOOLTIPS_GetDelayTime (const TOOLTIPS_INFO *infoPtr, DWORD duration)
1297 switch (duration) {
1298 case TTDT_RESHOW:
1299 return infoPtr->nReshowTime;
1301 case TTDT_AUTOPOP:
1302 return infoPtr->nAutoPopTime;
1304 case TTDT_INITIAL:
1305 case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1306 return infoPtr->nInitialTime;
1308 default:
1309 WARN("Invalid duration flag %x\n", duration);
1310 break;
1313 return -1;
1317 static LRESULT
1318 TOOLTIPS_GetMargin (const TOOLTIPS_INFO *infoPtr, LPRECT lpRect)
1320 lpRect->left = infoPtr->rcMargin.left;
1321 lpRect->right = infoPtr->rcMargin.right;
1322 lpRect->bottom = infoPtr->rcMargin.bottom;
1323 lpRect->top = infoPtr->rcMargin.top;
1325 return 0;
1329 static inline LRESULT
1330 TOOLTIPS_GetMaxTipWidth (const TOOLTIPS_INFO *infoPtr)
1332 return infoPtr->nMaxTipWidth;
1336 static LRESULT
1337 TOOLTIPS_GetTextT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1339 INT nTool;
1341 if (!ti) return 0;
1342 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1343 return 0;
1345 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1346 if (nTool == -1) return 0;
1348 if (infoPtr->tools[nTool].lpszText == NULL)
1349 return 0;
1351 if (isW) {
1352 ti->lpszText[0] = '\0';
1353 TOOLTIPS_GetTipText(infoPtr, nTool, ti->lpszText);
1355 else {
1356 WCHAR buffer[INFOTIPSIZE];
1358 /* NB this API is broken, there is no way for the app to determine
1359 what size buffer it requires nor a way to specify how long the
1360 one it supplies is. According to the test result, it's up to
1361 80 bytes by the ANSI version. */
1363 buffer[0] = '\0';
1364 TOOLTIPS_GetTipText(infoPtr, nTool, buffer);
1365 WideCharToMultiByte(CP_ACP, 0, buffer, -1, (LPSTR)ti->lpszText,
1366 MAX_TEXT_SIZE_A, NULL, NULL);
1369 return 0;
1373 static inline LRESULT
1374 TOOLTIPS_GetTipBkColor (const TOOLTIPS_INFO *infoPtr)
1376 return infoPtr->clrBk;
1380 static inline LRESULT
1381 TOOLTIPS_GetTipTextColor (const TOOLTIPS_INFO *infoPtr)
1383 return infoPtr->clrText;
1387 static inline LRESULT
1388 TOOLTIPS_GetToolCount (const TOOLTIPS_INFO *infoPtr)
1390 return infoPtr->uNumTools;
1394 static LRESULT
1395 TOOLTIPS_GetToolInfoT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1397 TTTOOL_INFO *toolPtr;
1398 INT nTool;
1400 if (!ti) return FALSE;
1401 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1402 return FALSE;
1403 if (infoPtr->uNumTools == 0)
1404 return FALSE;
1406 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1407 if (nTool == -1)
1408 return FALSE;
1410 TRACE("tool %d\n", nTool);
1412 toolPtr = &infoPtr->tools[nTool];
1414 /* copy tool data */
1415 ti->uFlags = toolPtr->uFlags;
1416 ti->rect = toolPtr->rect;
1417 ti->hinst = toolPtr->hinst;
1418 TOOLTIPS_CopyInfoT (toolPtr, ti, isW);
1420 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1421 ti->lParam = toolPtr->lParam;
1423 return TRUE;
1427 static LRESULT
1428 TOOLTIPS_HitTestT (const TOOLTIPS_INFO *infoPtr, LPTTHITTESTINFOW lptthit,
1429 BOOL isW)
1431 TTTOOL_INFO *toolPtr;
1432 INT nTool;
1434 if (lptthit == 0)
1435 return FALSE;
1437 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1438 if (nTool == -1)
1439 return FALSE;
1441 TRACE("tool %d!\n", nTool);
1443 /* copy tool data */
1444 if (lptthit->ti.cbSize >= TTTOOLINFOW_V1_SIZE) {
1445 toolPtr = &infoPtr->tools[nTool];
1447 lptthit->ti.uFlags = toolPtr->uFlags;
1448 lptthit->ti.hwnd = toolPtr->hwnd;
1449 lptthit->ti.uId = toolPtr->uId;
1450 lptthit->ti.rect = toolPtr->rect;
1451 lptthit->ti.hinst = toolPtr->hinst;
1452 TOOLTIPS_CopyInfoT (toolPtr, &lptthit->ti, isW);
1453 if (lptthit->ti.cbSize >= TTTOOLINFOW_V2_SIZE)
1454 lptthit->ti.lParam = toolPtr->lParam;
1457 return TRUE;
1461 static LRESULT
1462 TOOLTIPS_NewToolRectT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti)
1464 INT nTool;
1466 if (!ti) return 0;
1467 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1468 return FALSE;
1470 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1472 TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&ti->rect));
1474 if (nTool == -1) return 0;
1476 infoPtr->tools[nTool].rect = ti->rect;
1478 return 0;
1482 static inline LRESULT
1483 TOOLTIPS_Pop (TOOLTIPS_INFO *infoPtr)
1485 TOOLTIPS_Hide (infoPtr);
1487 return 0;
1491 static LRESULT
1492 TOOLTIPS_RelayEvent (TOOLTIPS_INFO *infoPtr, LPMSG lpMsg)
1494 POINT pt;
1495 INT nOldTool;
1497 if (!lpMsg) {
1498 ERR("lpMsg == NULL!\n");
1499 return 0;
1502 switch (lpMsg->message) {
1503 case WM_LBUTTONDOWN:
1504 case WM_LBUTTONUP:
1505 case WM_MBUTTONDOWN:
1506 case WM_MBUTTONUP:
1507 case WM_RBUTTONDOWN:
1508 case WM_RBUTTONUP:
1509 TOOLTIPS_Hide (infoPtr);
1510 break;
1512 case WM_MOUSEMOVE:
1513 pt.x = (short)LOWORD(lpMsg->lParam);
1514 pt.y = (short)HIWORD(lpMsg->lParam);
1515 nOldTool = infoPtr->nTool;
1516 infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1517 &pt);
1518 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
1519 infoPtr->nTool, infoPtr->nCurrentTool);
1520 TRACE("WM_MOUSEMOVE (%p %d %d)\n", infoPtr->hwndSelf, pt.x, pt.y);
1522 if (infoPtr->nTool != nOldTool) {
1523 if(infoPtr->nTool == -1) { /* Moved out of all tools */
1524 TOOLTIPS_Hide(infoPtr);
1525 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1526 } else if (nOldTool == -1) { /* Moved from outside */
1527 if(infoPtr->bActive) {
1528 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1529 TRACE("timer 1 started!\n");
1531 } else { /* Moved from one to another */
1532 TOOLTIPS_Hide (infoPtr);
1533 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1534 if(infoPtr->bActive) {
1535 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1536 TRACE("timer 1 started!\n");
1539 } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1540 KillTimer(infoPtr->hwndSelf, ID_TIMERPOP);
1541 SetTimer(infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1542 TRACE("timer 2 restarted\n");
1543 } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1544 /* previous show attempt didn't result in tooltip so try again */
1545 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1546 TRACE("timer 1 started!\n");
1548 break;
1551 return 0;
1555 static LRESULT
1556 TOOLTIPS_SetDelayTime (TOOLTIPS_INFO *infoPtr, DWORD duration, INT nTime)
1558 switch (duration) {
1559 case TTDT_AUTOMATIC:
1560 if (nTime <= 0)
1561 nTime = GetDoubleClickTime();
1562 infoPtr->nReshowTime = nTime / 5;
1563 infoPtr->nAutoPopTime = nTime * 10;
1564 infoPtr->nInitialTime = nTime;
1565 break;
1567 case TTDT_RESHOW:
1568 if(nTime < 0)
1569 nTime = GetDoubleClickTime() / 5;
1570 infoPtr->nReshowTime = nTime;
1571 break;
1573 case TTDT_AUTOPOP:
1574 if(nTime < 0)
1575 nTime = GetDoubleClickTime() * 10;
1576 infoPtr->nAutoPopTime = nTime;
1577 break;
1579 case TTDT_INITIAL:
1580 if(nTime < 0)
1581 nTime = GetDoubleClickTime();
1582 infoPtr->nInitialTime = nTime;
1583 break;
1585 default:
1586 WARN("Invalid duration flag %x\n", duration);
1587 break;
1590 return 0;
1594 static LRESULT
1595 TOOLTIPS_SetMargin (TOOLTIPS_INFO *infoPtr, const RECT *lpRect)
1597 infoPtr->rcMargin.left = lpRect->left;
1598 infoPtr->rcMargin.right = lpRect->right;
1599 infoPtr->rcMargin.bottom = lpRect->bottom;
1600 infoPtr->rcMargin.top = lpRect->top;
1602 return 0;
1606 static inline LRESULT
1607 TOOLTIPS_SetMaxTipWidth (TOOLTIPS_INFO *infoPtr, INT MaxWidth)
1609 INT nTemp = infoPtr->nMaxTipWidth;
1611 infoPtr->nMaxTipWidth = MaxWidth;
1613 return nTemp;
1617 static inline LRESULT
1618 TOOLTIPS_SetTipBkColor (TOOLTIPS_INFO *infoPtr, COLORREF clrBk)
1620 infoPtr->clrBk = clrBk;
1622 return 0;
1626 static inline LRESULT
1627 TOOLTIPS_SetTipTextColor (TOOLTIPS_INFO *infoPtr, COLORREF clrText)
1629 infoPtr->clrText = clrText;
1631 return 0;
1635 static LRESULT
1636 TOOLTIPS_SetTitleT (TOOLTIPS_INFO *infoPtr, UINT_PTR uTitleIcon, LPCWSTR pszTitle,
1637 BOOL isW)
1639 UINT size;
1641 TRACE("hwnd = %p, title = %s, icon = %p\n", infoPtr->hwndSelf, debugstr_w(pszTitle),
1642 (void*)uTitleIcon);
1644 Free(infoPtr->pszTitle);
1646 if (pszTitle)
1648 if (isW)
1650 size = (strlenW(pszTitle)+1)*sizeof(WCHAR);
1651 infoPtr->pszTitle = Alloc(size);
1652 if (!infoPtr->pszTitle)
1653 return FALSE;
1654 memcpy(infoPtr->pszTitle, pszTitle, size);
1656 else
1658 size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, NULL, 0);
1659 infoPtr->pszTitle = Alloc(size);
1660 if (!infoPtr->pszTitle)
1661 return FALSE;
1662 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1665 else
1666 infoPtr->pszTitle = NULL;
1668 if (uTitleIcon <= TTI_ERROR)
1669 infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1670 else
1671 infoPtr->hTitleIcon = CopyIcon((HICON)uTitleIcon);
1673 TRACE("icon = %p\n", infoPtr->hTitleIcon);
1675 return TRUE;
1679 static LRESULT
1680 TOOLTIPS_SetToolInfoT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1682 TTTOOL_INFO *toolPtr;
1683 INT nTool;
1685 if (!ti) return 0;
1686 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1687 return 0;
1689 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1690 if (nTool == -1) return 0;
1692 TRACE("tool %d\n", nTool);
1694 toolPtr = &infoPtr->tools[nTool];
1696 /* copy tool data */
1697 toolPtr->uFlags = ti->uFlags;
1698 toolPtr->hwnd = ti->hwnd;
1699 toolPtr->uId = ti->uId;
1700 toolPtr->rect = ti->rect;
1701 toolPtr->hinst = ti->hinst;
1703 if (IS_INTRESOURCE(ti->lpszText)) {
1704 TRACE("set string id %x!\n", LOWORD(ti->lpszText));
1705 toolPtr->lpszText = ti->lpszText;
1707 else {
1708 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1709 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1710 else {
1711 if ( (toolPtr->lpszText) &&
1712 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1713 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1714 Free (toolPtr->lpszText);
1715 toolPtr->lpszText = NULL;
1717 if (ti->lpszText) {
1718 if (isW) {
1719 INT len = lstrlenW (ti->lpszText);
1720 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1721 strcpyW (toolPtr->lpszText, ti->lpszText);
1723 else {
1724 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1725 -1, NULL, 0);
1726 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1727 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1728 toolPtr->lpszText, len);
1734 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1735 toolPtr->lParam = ti->lParam;
1737 if (infoPtr->nCurrentTool == nTool)
1739 TOOLTIPS_GetTipText (infoPtr, infoPtr->nCurrentTool, infoPtr->szTipText);
1741 if (infoPtr->szTipText[0] == 0)
1742 TOOLTIPS_Hide(infoPtr);
1743 else
1744 TOOLTIPS_Show (infoPtr, FALSE);
1747 return 0;
1751 static LRESULT
1752 TOOLTIPS_TrackActivate (TOOLTIPS_INFO *infoPtr, BOOL track_activate, const TTTOOLINFOA *ti)
1754 if (track_activate) {
1756 if (!ti) return 0;
1757 if (ti->cbSize < TTTOOLINFOA_V1_SIZE)
1758 return FALSE;
1760 /* activate */
1761 infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoT (infoPtr, (const TTTOOLINFOW*)ti);
1762 if (infoPtr->nTrackTool != -1) {
1763 TRACE("activated!\n");
1764 infoPtr->bTrackActive = TRUE;
1765 TOOLTIPS_TrackShow (infoPtr);
1768 else {
1769 /* deactivate */
1770 TOOLTIPS_TrackHide (infoPtr);
1772 infoPtr->bTrackActive = FALSE;
1773 infoPtr->nTrackTool = -1;
1775 TRACE("deactivated!\n");
1778 return 0;
1782 static LRESULT
1783 TOOLTIPS_TrackPosition (TOOLTIPS_INFO *infoPtr, LPARAM coord)
1785 infoPtr->xTrackPos = (INT)LOWORD(coord);
1786 infoPtr->yTrackPos = (INT)HIWORD(coord);
1788 if (infoPtr->bTrackActive) {
1789 TRACE("[%d %d]\n",
1790 infoPtr->xTrackPos, infoPtr->yTrackPos);
1792 TOOLTIPS_TrackShow (infoPtr);
1795 return 0;
1799 static LRESULT
1800 TOOLTIPS_Update (TOOLTIPS_INFO *infoPtr)
1802 if (infoPtr->nCurrentTool != -1)
1803 UpdateWindow (infoPtr->hwndSelf);
1805 return 0;
1809 static LRESULT
1810 TOOLTIPS_UpdateTipTextT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1812 TTTOOL_INFO *toolPtr;
1813 INT nTool;
1815 if (!ti) return 0;
1816 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1817 return FALSE;
1819 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1820 if (nTool == -1)
1821 return 0;
1823 TRACE("tool %d\n", nTool);
1825 toolPtr = &infoPtr->tools[nTool];
1827 /* copy tool text */
1828 toolPtr->hinst = ti->hinst;
1830 if (IS_INTRESOURCE(ti->lpszText)){
1831 toolPtr->lpszText = ti->lpszText;
1833 else if (ti->lpszText) {
1834 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1835 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1836 else {
1837 if ( (toolPtr->lpszText) &&
1838 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1839 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1840 Free (toolPtr->lpszText);
1841 toolPtr->lpszText = NULL;
1843 if (ti->lpszText) {
1844 if (isW) {
1845 INT len = lstrlenW (ti->lpszText);
1846 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1847 strcpyW (toolPtr->lpszText, ti->lpszText);
1849 else {
1850 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1851 -1, NULL, 0);
1852 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1853 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1854 toolPtr->lpszText, len);
1860 if(infoPtr->nCurrentTool == -1) return 0;
1861 /* force repaint */
1862 if (infoPtr->bActive)
1863 TOOLTIPS_Show (infoPtr, FALSE);
1864 else if (infoPtr->bTrackActive)
1865 TOOLTIPS_Show (infoPtr, TRUE);
1867 return 0;
1871 static LRESULT
1872 TOOLTIPS_Create (HWND hwnd)
1874 TOOLTIPS_INFO *infoPtr;
1876 /* allocate memory for info structure */
1877 infoPtr = Alloc (sizeof(TOOLTIPS_INFO));
1878 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1880 /* initialize info structure */
1881 infoPtr->bActive = TRUE;
1882 infoPtr->bTrackActive = FALSE;
1884 infoPtr->nMaxTipWidth = -1;
1885 infoPtr->nTool = -1;
1886 infoPtr->nCurrentTool = -1;
1887 infoPtr->nTrackTool = -1;
1888 infoPtr->hwndSelf = hwnd;
1890 /* initialize colours and fonts */
1891 TOOLTIPS_InitSystemSettings(infoPtr);
1893 TOOLTIPS_SetDelayTime(infoPtr, TTDT_AUTOMATIC, 0);
1895 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
1897 return 0;
1901 static LRESULT
1902 TOOLTIPS_Destroy (TOOLTIPS_INFO *infoPtr)
1904 TTTOOL_INFO *toolPtr;
1905 UINT i;
1907 /* free tools */
1908 if (infoPtr->tools) {
1909 for (i = 0; i < infoPtr->uNumTools; i++) {
1910 toolPtr = &infoPtr->tools[i];
1911 if (toolPtr->lpszText) {
1912 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1913 !IS_INTRESOURCE(toolPtr->lpszText) )
1915 Free (toolPtr->lpszText);
1916 toolPtr->lpszText = NULL;
1920 /* remove subclassing */
1921 if (toolPtr->uFlags & TTF_SUBCLASS) {
1922 if (toolPtr->uFlags & TTF_IDISHWND) {
1923 RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
1925 else {
1926 RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
1930 Free (infoPtr->tools);
1933 /* free title string */
1934 Free (infoPtr->pszTitle);
1935 /* free title icon if not a standard one */
1936 if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
1937 DeleteObject(infoPtr->hTitleIcon);
1939 /* delete fonts */
1940 DeleteObject (infoPtr->hFont);
1941 DeleteObject (infoPtr->hTitleFont);
1943 /* free tool tips info data */
1944 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1945 Free (infoPtr);
1947 return 0;
1951 static inline LRESULT
1952 TOOLTIPS_GetFont (const TOOLTIPS_INFO *infoPtr)
1954 return (LRESULT)infoPtr->hFont;
1958 static LRESULT
1959 TOOLTIPS_MouseMessage (TOOLTIPS_INFO *infoPtr)
1961 TOOLTIPS_Hide (infoPtr);
1963 return 0;
1967 static LRESULT
1968 TOOLTIPS_NCCreate (HWND hwnd)
1970 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1971 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
1973 dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
1974 dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
1976 /* WS_BORDER only draws a border round the window rect, not the
1977 * window region, therefore it is useless to us in balloon mode */
1978 if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
1980 SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
1982 dwExStyle |= WS_EX_TOOLWINDOW;
1983 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
1985 return TRUE;
1989 static LRESULT
1990 TOOLTIPS_NCHitTest (const TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1992 INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
1994 TRACE(" nTool=%d\n", nTool);
1996 if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
1997 if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
1998 TRACE("-- in transparent mode!\n");
1999 return HTTRANSPARENT;
2003 return DefWindowProcW (infoPtr->hwndSelf, WM_NCHITTEST, wParam, lParam);
2007 static LRESULT
2008 TOOLTIPS_NotifyFormat (TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2010 FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", infoPtr->hwndSelf, wParam, lParam);
2012 return 0;
2016 static LRESULT
2017 TOOLTIPS_Paint (const TOOLTIPS_INFO *infoPtr, HDC hDC)
2019 HDC hdc;
2020 PAINTSTRUCT ps;
2022 hdc = (hDC == NULL) ? BeginPaint (infoPtr->hwndSelf, &ps) : hDC;
2023 TOOLTIPS_Refresh (infoPtr, hdc);
2024 if (!hDC)
2025 EndPaint (infoPtr->hwndSelf, &ps);
2026 return 0;
2030 static LRESULT
2031 TOOLTIPS_SetFont (TOOLTIPS_INFO *infoPtr, HFONT hFont, BOOL redraw)
2033 LOGFONTW lf;
2035 if(!GetObjectW(hFont, sizeof(lf), &lf))
2036 return 0;
2038 DeleteObject (infoPtr->hFont);
2039 infoPtr->hFont = CreateFontIndirectW(&lf);
2041 DeleteObject (infoPtr->hTitleFont);
2042 lf.lfWeight = FW_BOLD;
2043 infoPtr->hTitleFont = CreateFontIndirectW(&lf);
2045 if (redraw && infoPtr->nCurrentTool != -1) {
2046 FIXME("full redraw needed!\n");
2049 return 0;
2052 /******************************************************************
2053 * TOOLTIPS_GetTextLength
2055 * This function is called when the tooltip receive a
2056 * WM_GETTEXTLENGTH message.
2058 * returns the length, in characters, of the tip text
2060 static inline LRESULT
2061 TOOLTIPS_GetTextLength(const TOOLTIPS_INFO *infoPtr)
2063 return strlenW(infoPtr->szTipText);
2066 /******************************************************************
2067 * TOOLTIPS_OnWMGetText
2069 * This function is called when the tooltip receive a
2070 * WM_GETTEXT message.
2071 * wParam : specifies the maximum number of characters to be copied
2072 * lParam : is the pointer to the buffer that will receive
2073 * the tip text
2075 * returns the number of characters copied
2077 static LRESULT
2078 TOOLTIPS_OnWMGetText (const TOOLTIPS_INFO *infoPtr, WPARAM size, LPWSTR pszText)
2080 LRESULT res;
2082 if(!size)
2083 return 0;
2085 res = min(strlenW(infoPtr->szTipText)+1, size);
2086 memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2087 pszText[res-1] = '\0';
2088 return res-1;
2091 static LRESULT
2092 TOOLTIPS_Timer (TOOLTIPS_INFO *infoPtr, INT iTimer)
2094 INT nOldTool;
2096 TRACE("timer %d (%p) expired!\n", iTimer, infoPtr->hwndSelf);
2098 switch (iTimer) {
2099 case ID_TIMERSHOW:
2100 KillTimer (infoPtr->hwndSelf, ID_TIMERSHOW);
2101 nOldTool = infoPtr->nTool;
2102 if ((infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, TRUE)) == nOldTool)
2103 TOOLTIPS_Show (infoPtr, FALSE);
2104 break;
2106 case ID_TIMERPOP:
2107 TOOLTIPS_Hide (infoPtr);
2108 break;
2110 case ID_TIMERLEAVE:
2111 nOldTool = infoPtr->nTool;
2112 infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, FALSE);
2113 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
2114 infoPtr->nTool, infoPtr->nCurrentTool);
2115 if (infoPtr->nTool != nOldTool) {
2116 if(infoPtr->nTool == -1) { /* Moved out of all tools */
2117 TOOLTIPS_Hide(infoPtr);
2118 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2119 } else if (nOldTool == -1) { /* Moved from outside */
2120 ERR("How did this happen?\n");
2121 } else { /* Moved from one to another */
2122 TOOLTIPS_Hide (infoPtr);
2123 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2124 if(infoPtr->bActive) {
2125 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2126 TRACE("timer 1 started!\n");
2130 break;
2132 default:
2133 ERR("Unknown timer id %d\n", iTimer);
2134 break;
2136 return 0;
2140 static LRESULT
2141 TOOLTIPS_WinIniChange (TOOLTIPS_INFO *infoPtr)
2143 TOOLTIPS_InitSystemSettings (infoPtr);
2145 return 0;
2149 static LRESULT CALLBACK
2150 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2152 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr ((HWND)dwRef);
2153 MSG msg;
2155 switch(uMsg) {
2156 case WM_MOUSEMOVE:
2157 case WM_LBUTTONDOWN:
2158 case WM_LBUTTONUP:
2159 case WM_MBUTTONDOWN:
2160 case WM_MBUTTONUP:
2161 case WM_RBUTTONDOWN:
2162 case WM_RBUTTONUP:
2163 msg.hwnd = hwnd;
2164 msg.message = uMsg;
2165 msg.wParam = wParam;
2166 msg.lParam = lParam;
2167 TOOLTIPS_RelayEvent(infoPtr, &msg);
2168 break;
2170 default:
2171 break;
2173 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
2177 static LRESULT CALLBACK
2178 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2180 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2182 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2183 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2184 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2185 switch (uMsg)
2187 case TTM_ACTIVATE:
2188 return TOOLTIPS_Activate (infoPtr, (BOOL)wParam);
2190 case TTM_ADDTOOLA:
2191 case TTM_ADDTOOLW:
2192 return TOOLTIPS_AddToolT (infoPtr, (LPTTTOOLINFOW)lParam, uMsg == TTM_ADDTOOLW);
2194 case TTM_DELTOOLA:
2195 case TTM_DELTOOLW:
2196 return TOOLTIPS_DelToolT (infoPtr, (LPTOOLINFOW)lParam,
2197 uMsg == TTM_DELTOOLW);
2198 case TTM_ENUMTOOLSA:
2199 case TTM_ENUMTOOLSW:
2200 return TOOLTIPS_EnumToolsT (infoPtr, (UINT)wParam, (LPTTTOOLINFOW)lParam,
2201 uMsg == TTM_ENUMTOOLSW);
2202 case TTM_GETBUBBLESIZE:
2203 return TOOLTIPS_GetBubbleSize (infoPtr, (LPTTTOOLINFOW)lParam);
2205 case TTM_GETCURRENTTOOLA:
2206 case TTM_GETCURRENTTOOLW:
2207 return TOOLTIPS_GetCurrentToolT (infoPtr, (LPTTTOOLINFOW)lParam,
2208 uMsg == TTM_GETCURRENTTOOLW);
2210 case TTM_GETDELAYTIME:
2211 return TOOLTIPS_GetDelayTime (infoPtr, (DWORD)wParam);
2213 case TTM_GETMARGIN:
2214 return TOOLTIPS_GetMargin (infoPtr, (LPRECT)lParam);
2216 case TTM_GETMAXTIPWIDTH:
2217 return TOOLTIPS_GetMaxTipWidth (infoPtr);
2219 case TTM_GETTEXTA:
2220 case TTM_GETTEXTW:
2221 return TOOLTIPS_GetTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2222 uMsg == TTM_GETTEXTW);
2224 case TTM_GETTIPBKCOLOR:
2225 return TOOLTIPS_GetTipBkColor (infoPtr);
2227 case TTM_GETTIPTEXTCOLOR:
2228 return TOOLTIPS_GetTipTextColor (infoPtr);
2230 case TTM_GETTOOLCOUNT:
2231 return TOOLTIPS_GetToolCount (infoPtr);
2233 case TTM_GETTOOLINFOA:
2234 case TTM_GETTOOLINFOW:
2235 return TOOLTIPS_GetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2236 uMsg == TTM_GETTOOLINFOW);
2238 case TTM_HITTESTA:
2239 case TTM_HITTESTW:
2240 return TOOLTIPS_HitTestT (infoPtr, (LPTTHITTESTINFOW)lParam,
2241 uMsg == TTM_HITTESTW);
2242 case TTM_NEWTOOLRECTA:
2243 case TTM_NEWTOOLRECTW:
2244 return TOOLTIPS_NewToolRectT (infoPtr, (LPTTTOOLINFOW)lParam);
2246 case TTM_POP:
2247 return TOOLTIPS_Pop (infoPtr);
2249 case TTM_RELAYEVENT:
2250 return TOOLTIPS_RelayEvent (infoPtr, (LPMSG)lParam);
2252 case TTM_SETDELAYTIME:
2253 return TOOLTIPS_SetDelayTime (infoPtr, (DWORD)wParam, (INT)LOWORD(lParam));
2255 case TTM_SETMARGIN:
2256 return TOOLTIPS_SetMargin (infoPtr, (LPRECT)lParam);
2258 case TTM_SETMAXTIPWIDTH:
2259 return TOOLTIPS_SetMaxTipWidth (infoPtr, (INT)lParam);
2261 case TTM_SETTIPBKCOLOR:
2262 return TOOLTIPS_SetTipBkColor (infoPtr, (COLORREF)wParam);
2264 case TTM_SETTIPTEXTCOLOR:
2265 return TOOLTIPS_SetTipTextColor (infoPtr, (COLORREF)wParam);
2267 case TTM_SETTITLEA:
2268 case TTM_SETTITLEW:
2269 return TOOLTIPS_SetTitleT (infoPtr, (UINT_PTR)wParam, (LPCWSTR)lParam,
2270 uMsg == TTM_SETTITLEW);
2272 case TTM_SETTOOLINFOA:
2273 case TTM_SETTOOLINFOW:
2274 return TOOLTIPS_SetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2275 uMsg == TTM_SETTOOLINFOW);
2277 case TTM_TRACKACTIVATE:
2278 return TOOLTIPS_TrackActivate (infoPtr, (BOOL)wParam, (LPTTTOOLINFOA)lParam);
2280 case TTM_TRACKPOSITION:
2281 return TOOLTIPS_TrackPosition (infoPtr, lParam);
2283 case TTM_UPDATE:
2284 return TOOLTIPS_Update (infoPtr);
2286 case TTM_UPDATETIPTEXTA:
2287 case TTM_UPDATETIPTEXTW:
2288 return TOOLTIPS_UpdateTipTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2289 uMsg == TTM_UPDATETIPTEXTW);
2291 case TTM_WINDOWFROMPOINT:
2292 return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2294 case WM_CREATE:
2295 return TOOLTIPS_Create (hwnd);
2297 case WM_DESTROY:
2298 return TOOLTIPS_Destroy (infoPtr);
2300 case WM_ERASEBKGND:
2301 /* we draw the background in WM_PAINT */
2302 return 0;
2304 case WM_GETFONT:
2305 return TOOLTIPS_GetFont (infoPtr);
2307 case WM_GETTEXT:
2308 return TOOLTIPS_OnWMGetText (infoPtr, wParam, (LPWSTR)lParam);
2310 case WM_GETTEXTLENGTH:
2311 return TOOLTIPS_GetTextLength (infoPtr);
2313 case WM_LBUTTONDOWN:
2314 case WM_LBUTTONUP:
2315 case WM_MBUTTONDOWN:
2316 case WM_MBUTTONUP:
2317 case WM_RBUTTONDOWN:
2318 case WM_RBUTTONUP:
2319 case WM_MOUSEMOVE:
2320 return TOOLTIPS_MouseMessage (infoPtr);
2322 case WM_NCCREATE:
2323 return TOOLTIPS_NCCreate (hwnd);
2325 case WM_NCHITTEST:
2326 return TOOLTIPS_NCHitTest (infoPtr, wParam, lParam);
2328 case WM_NOTIFYFORMAT:
2329 return TOOLTIPS_NotifyFormat (infoPtr, wParam, lParam);
2331 case WM_PRINTCLIENT:
2332 case WM_PAINT:
2333 return TOOLTIPS_Paint (infoPtr, (HDC)wParam);
2335 case WM_SETFONT:
2336 return TOOLTIPS_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
2338 case WM_SYSCOLORCHANGE:
2339 COMCTL32_RefreshSysColors();
2340 return 0;
2342 case WM_TIMER:
2343 return TOOLTIPS_Timer (infoPtr, (INT)wParam);
2345 case WM_WININICHANGE:
2346 return TOOLTIPS_WinIniChange (infoPtr);
2348 default:
2349 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2350 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2351 uMsg, wParam, lParam);
2352 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2357 VOID
2358 TOOLTIPS_Register (void)
2360 WNDCLASSW wndClass;
2362 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2363 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2364 wndClass.lpfnWndProc = TOOLTIPS_WindowProc;
2365 wndClass.cbClsExtra = 0;
2366 wndClass.cbWndExtra = sizeof(TOOLTIPS_INFO *);
2367 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2368 wndClass.hbrBackground = 0;
2369 wndClass.lpszClassName = TOOLTIPS_CLASSW;
2371 RegisterClassW (&wndClass);
2373 hTooltipIcons[TTI_NONE] = NULL;
2374 hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2375 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2376 hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2377 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2378 hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2379 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2383 VOID
2384 TOOLTIPS_Unregister (void)
2386 int i;
2387 for (i = TTI_INFO; i <= TTI_ERROR; i++)
2388 DestroyIcon(hTooltipIcons[i]);
2389 UnregisterClassW (TOOLTIPS_CLASSW, NULL);