wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / comctl32 / tooltips.c
blob2a68beafd7d78865158dd84987bb74312cf98b0e
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 "wingdi.h"
100 #include "winuser.h"
101 #include "winnls.h"
102 #include "commctrl.h"
103 #include "comctl32.h"
104 #include "wine/debug.h"
106 WINE_DEFAULT_DEBUG_CHANNEL(tooltips);
108 static HICON hTooltipIcons[TTI_ERROR+1];
110 typedef struct
112 UINT uFlags;
113 UINT uInternalFlags;
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);
179 static inline UINT_PTR
180 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
182 UINT i;
183 for (i = 0; i <= TTI_ERROR; i++)
184 if (hTooltipIcons[i] == hIcon)
185 return i;
186 return (UINT_PTR)hIcon;
189 static void
190 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
192 NONCLIENTMETRICSW nclm;
194 infoPtr->clrBk = comctl32_color.clrInfoBk;
195 infoPtr->clrText = comctl32_color.clrInfoText;
197 DeleteObject (infoPtr->hFont);
198 nclm.cbSize = sizeof(nclm);
199 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
200 infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
202 DeleteObject (infoPtr->hTitleFont);
203 nclm.lfStatusFont.lfWeight = FW_BOLD;
204 infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
207 /* Custom draw routines */
208 static void
209 TOOLTIPS_customdraw_fill(const TOOLTIPS_INFO *infoPtr, NMTTCUSTOMDRAW *lpnmttcd,
210 HDC hdc, const RECT *rcBounds, UINT uFlags)
212 ZeroMemory(lpnmttcd, sizeof(NMTTCUSTOMDRAW));
213 lpnmttcd->uDrawFlags = uFlags;
214 lpnmttcd->nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
215 lpnmttcd->nmcd.hdr.code = NM_CUSTOMDRAW;
216 if (infoPtr->nCurrentTool != -1) {
217 TTTOOL_INFO *toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
218 lpnmttcd->nmcd.hdr.idFrom = toolPtr->uId;
220 lpnmttcd->nmcd.hdc = hdc;
221 lpnmttcd->nmcd.rc = *rcBounds;
222 /* FIXME - dwItemSpec, uItemState, lItemlParam */
225 static inline DWORD
226 TOOLTIPS_notify_customdraw (DWORD dwDrawStage, NMTTCUSTOMDRAW *lpnmttcd)
228 LRESULT result;
229 lpnmttcd->nmcd.dwDrawStage = dwDrawStage;
231 TRACE("Notifying stage %d, flags %x, id %x\n", lpnmttcd->nmcd.dwDrawStage,
232 lpnmttcd->uDrawFlags, lpnmttcd->nmcd.hdr.code);
234 result = SendMessageW(GetParent(lpnmttcd->nmcd.hdr.hwndFrom), WM_NOTIFY,
235 0, (LPARAM)lpnmttcd);
237 TRACE("Notify result %x\n", (unsigned int)result);
239 return result;
242 static void
243 TOOLTIPS_Refresh (const TOOLTIPS_INFO *infoPtr, HDC hdc)
245 RECT rc;
246 INT oldBkMode;
247 HFONT hOldFont;
248 HBRUSH hBrush;
249 UINT uFlags = DT_EXTERNALLEADING;
250 HRGN hRgn = NULL;
251 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
252 NMTTCUSTOMDRAW nmttcd;
253 DWORD cdmode;
255 if (infoPtr->nMaxTipWidth > -1)
256 uFlags |= DT_WORDBREAK;
257 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)
258 uFlags |= DT_NOPREFIX;
259 GetClientRect (infoPtr->hwndSelf, &rc);
261 hBrush = CreateSolidBrush(infoPtr->clrBk);
263 oldBkMode = SetBkMode (hdc, TRANSPARENT);
264 SetTextColor (hdc, infoPtr->clrText);
265 hOldFont = SelectObject (hdc, infoPtr->hFont);
267 /* Custom draw - Call PrePaint once initial properties set up */
268 /* Note: Contrary to MSDN, CDRF_SKIPDEFAULT still draws a tooltip */
269 TOOLTIPS_customdraw_fill(infoPtr, &nmttcd, hdc, &rc, uFlags);
270 cdmode = TOOLTIPS_notify_customdraw(CDDS_PREPAINT, &nmttcd);
271 uFlags = nmttcd.uDrawFlags;
273 if (dwStyle & TTS_BALLOON)
275 /* create a region to store result into */
276 hRgn = CreateRectRgn(0, 0, 0, 0);
278 GetWindowRgn(infoPtr->hwndSelf, hRgn);
280 /* fill the background */
281 FillRgn(hdc, hRgn, hBrush);
282 DeleteObject(hBrush);
283 hBrush = NULL;
285 else
287 /* fill the background */
288 FillRect(hdc, &rc, hBrush);
289 DeleteObject(hBrush);
290 hBrush = NULL;
293 if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
295 /* calculate text rectangle */
296 rc.left += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
297 rc.top += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
298 rc.right -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
299 rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
300 if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
302 if (infoPtr->pszTitle)
304 RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
305 int height;
306 BOOL icon_present;
307 HFONT prevFont;
309 /* draw icon */
310 icon_present = infoPtr->hTitleIcon &&
311 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
312 ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
313 if (icon_present)
314 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
316 rcTitle.bottom = rc.top + ICON_HEIGHT;
318 /* draw title text */
319 prevFont = SelectObject (hdc, infoPtr->hTitleFont);
320 height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
321 SelectObject (hdc, prevFont);
322 rc.top += height + BALLOON_TITLE_TEXT_SPACING;
325 else
327 /* calculate text rectangle */
328 rc.left += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
329 rc.top += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
330 rc.right -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
331 rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
334 /* draw text */
335 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
337 /* Custom draw - Call PostPaint after drawing */
338 if (cdmode & CDRF_NOTIFYPOSTPAINT) {
339 TOOLTIPS_notify_customdraw(CDDS_POSTPAINT, &nmttcd);
342 /* be polite and reset the things we changed in the dc */
343 SelectObject (hdc, hOldFont);
344 SetBkMode (hdc, oldBkMode);
346 if (dwStyle & TTS_BALLOON)
348 /* frame region because default window proc doesn't do it */
349 INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
350 INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
352 hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
353 FrameRgn(hdc, hRgn, hBrush, width, height);
356 if (hRgn)
357 DeleteObject(hRgn);
360 static void TOOLTIPS_GetDispInfoA(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
362 NMTTDISPINFOA ttnmdi;
364 /* fill NMHDR struct */
365 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
366 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
367 ttnmdi.hdr.idFrom = toolPtr->uId;
368 ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
369 ttnmdi.lpszText = ttnmdi.szText;
370 ttnmdi.uFlags = toolPtr->uFlags;
371 ttnmdi.lParam = toolPtr->lParam;
373 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
374 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
376 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
377 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
378 buffer, INFOTIPSIZE);
379 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
380 toolPtr->hinst = ttnmdi.hinst;
381 toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
384 else if (ttnmdi.lpszText == 0) {
385 buffer[0] = '\0';
387 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
388 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
389 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
390 toolPtr->hinst = 0;
391 toolPtr->lpszText = NULL;
392 Str_SetPtrW(&toolPtr->lpszText, buffer);
395 else {
396 ERR("recursive text callback\n");
397 buffer[0] = '\0';
400 /* no text available - try calling parent instead as per native */
401 /* FIXME: Unsure if SETITEM should save the value or not */
402 if (buffer[0] == 0x00) {
404 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
406 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
407 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
408 buffer, INFOTIPSIZE);
409 } else if (ttnmdi.lpszText &&
410 ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
411 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
416 static void TOOLTIPS_GetDispInfoW(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
418 NMTTDISPINFOW ttnmdi;
420 /* fill NMHDR struct */
421 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
422 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
423 ttnmdi.hdr.idFrom = toolPtr->uId;
424 ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
425 ttnmdi.lpszText = ttnmdi.szText;
426 ttnmdi.uFlags = toolPtr->uFlags;
427 ttnmdi.lParam = toolPtr->lParam;
429 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
430 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
432 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
433 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
434 buffer, INFOTIPSIZE);
435 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
436 toolPtr->hinst = ttnmdi.hinst;
437 toolPtr->lpszText = ttnmdi.lpszText;
440 else if (ttnmdi.lpszText == 0) {
441 buffer[0] = '\0';
443 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
444 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
445 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
446 toolPtr->hinst = 0;
447 toolPtr->lpszText = NULL;
448 Str_SetPtrW(&toolPtr->lpszText, buffer);
451 else {
452 ERR("recursive text callback\n");
453 buffer[0] = '\0';
456 /* no text available - try calling parent instead as per native */
457 /* FIXME: Unsure if SETITEM should save the value or not */
458 if (buffer[0] == 0x00) {
460 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
462 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
463 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
464 buffer, INFOTIPSIZE);
465 } else if (ttnmdi.lpszText &&
466 ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
467 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
473 static void
474 TOOLTIPS_GetTipText (const TOOLTIPS_INFO *infoPtr, INT nTool, WCHAR *buffer)
476 TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
478 if (IS_INTRESOURCE(toolPtr->lpszText)) {
479 /* load a resource */
480 TRACE("load res string %p %x\n",
481 toolPtr->hinst, LOWORD(toolPtr->lpszText));
482 if (!LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText), buffer, INFOTIPSIZE))
483 buffer[0] = '\0';
485 else if (toolPtr->lpszText) {
486 if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
487 if (toolPtr->bNotifyUnicode)
488 TOOLTIPS_GetDispInfoW(infoPtr, toolPtr, buffer);
489 else
490 TOOLTIPS_GetDispInfoA(infoPtr, toolPtr, buffer);
492 else {
493 /* the item is a usual (unicode) text */
494 lstrcpynW (buffer, toolPtr->lpszText, INFOTIPSIZE);
497 else {
498 /* no text available */
499 buffer[0] = '\0';
502 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)) {
503 WCHAR *ptrW;
504 if ((ptrW = wcschr(buffer, '\t')))
505 *ptrW = 0;
508 TRACE("%s\n", debugstr_w(buffer));
512 static void
513 TOOLTIPS_CalcTipSize (const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
515 HDC hdc;
516 HFONT hOldFont;
517 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
518 UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
519 RECT rc = {0, 0, 0, 0};
520 SIZE title = {0, 0};
522 if (infoPtr->nMaxTipWidth > -1) {
523 rc.right = infoPtr->nMaxTipWidth;
524 uFlags |= DT_WORDBREAK;
526 if (style & TTS_NOPREFIX)
527 uFlags |= DT_NOPREFIX;
528 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
530 hdc = GetDC (infoPtr->hwndSelf);
531 if (infoPtr->pszTitle)
533 RECT rcTitle = {0, 0, 0, 0};
534 TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
535 if (infoPtr->hTitleIcon)
537 title.cx = ICON_WIDTH;
538 title.cy = ICON_HEIGHT;
540 if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
541 hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
542 DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
543 SelectObject (hdc, hOldFont);
544 title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
545 title.cx += (rcTitle.right - rcTitle.left);
547 hOldFont = SelectObject (hdc, infoPtr->hFont);
548 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
549 SelectObject (hdc, hOldFont);
550 ReleaseDC (infoPtr->hwndSelf, hdc);
552 if ((style & TTS_BALLOON) || infoPtr->pszTitle)
554 lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
555 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
556 lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
557 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
558 BALLOON_STEMHEIGHT;
560 else
562 lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
563 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
564 lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
565 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
570 static void
571 TOOLTIPS_Show (TOOLTIPS_INFO *infoPtr, BOOL track_activate)
573 TTTOOL_INFO *toolPtr;
574 HMONITOR monitor;
575 MONITORINFO mon_info;
576 RECT rect;
577 SIZE size;
578 NMHDR hdr;
579 int ptfx = 0;
580 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
581 INT nTool, current;
583 if (track_activate)
585 if (infoPtr->nTrackTool == -1)
587 TRACE("invalid tracking tool %d\n", infoPtr->nTrackTool);
588 return;
590 nTool = infoPtr->nTrackTool;
592 else
594 if (infoPtr->nTool == -1)
596 TRACE("invalid tool %d\n", infoPtr->nTool);
597 return;
599 nTool = infoPtr->nTool;
602 TRACE("Show tooltip pre %d, %p\n", nTool, infoPtr->hwndSelf);
604 current = infoPtr->nCurrentTool;
605 if (!track_activate)
606 infoPtr->nCurrentTool = infoPtr->nTool;
608 TOOLTIPS_GetTipText (infoPtr, nTool, infoPtr->szTipText);
610 if (infoPtr->szTipText[0] == '\0')
612 infoPtr->nCurrentTool = current;
613 return;
616 toolPtr = &infoPtr->tools[nTool];
617 TOOLTIPS_CalcTipSize (infoPtr, &size);
619 TRACE("Show tooltip %d, %s, size %d x %d\n", nTool, debugstr_w(infoPtr->szTipText),
620 size.cx, size.cy);
622 if (track_activate && (toolPtr->uFlags & TTF_TRACK))
624 rect.left = infoPtr->xTrackPos;
625 rect.top = infoPtr->yTrackPos;
626 ptfx = rect.left;
628 if (toolPtr->uFlags & TTF_CENTERTIP)
630 rect.left -= (size.cx / 2);
631 if (!(style & TTS_BALLOON))
632 rect.top -= (size.cy / 2);
634 if (!(infoPtr->bToolBelow = (infoPtr->yTrackPos + size.cy <= GetSystemMetrics(SM_CYSCREEN))))
635 rect.top -= size.cy;
637 if (!(toolPtr->uFlags & TTF_ABSOLUTE))
639 if (style & TTS_BALLOON)
640 rect.left -= BALLOON_STEMINDENT;
641 else
643 RECT rcTool;
645 if (toolPtr->uFlags & TTF_IDISHWND)
646 GetWindowRect ((HWND)toolPtr->uId, &rcTool);
647 else
649 rcTool = toolPtr->rect;
650 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
653 /* smart placement */
654 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
655 (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
656 rect.left = rcTool.right;
660 else
662 if (toolPtr->uFlags & TTF_CENTERTIP)
664 RECT rc;
666 if (toolPtr->uFlags & TTF_IDISHWND)
667 GetWindowRect ((HWND)toolPtr->uId, &rc);
668 else {
669 rc = toolPtr->rect;
670 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
672 rect.left = (rc.left + rc.right - size.cx) / 2;
673 if (style & TTS_BALLOON)
675 ptfx = rc.left + ((rc.right - rc.left) / 2);
677 /* CENTERTIP ballon tooltips default to below the field
678 * if they fit on the screen */
679 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
681 rect.top = rc.top - size.cy;
682 infoPtr->bToolBelow = FALSE;
684 else
686 infoPtr->bToolBelow = TRUE;
687 rect.top = rc.bottom;
689 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
691 else
693 rect.top = rc.bottom + 2;
694 infoPtr->bToolBelow = TRUE;
697 else
699 GetCursorPos ((LPPOINT)&rect);
700 if (style & TTS_BALLOON)
702 ptfx = rect.left;
703 if(rect.top - size.cy >= 0)
705 rect.top -= size.cy;
706 infoPtr->bToolBelow = FALSE;
708 else
710 infoPtr->bToolBelow = TRUE;
711 rect.top += 20;
713 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
715 else
717 rect.top += 20;
718 infoPtr->bToolBelow = TRUE;
723 TRACE("pos %d - %d\n", rect.left, rect.top);
725 rect.right = rect.left + size.cx;
726 rect.bottom = rect.top + size.cy;
728 /* check position */
730 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
731 mon_info.cbSize = sizeof(mon_info);
732 GetMonitorInfoW( monitor, &mon_info );
734 if( rect.right > mon_info.rcWork.right ) {
735 rect.left -= rect.right - mon_info.rcWork.right + 2;
736 rect.right = mon_info.rcWork.right - 2;
738 if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
740 if( rect.bottom > mon_info.rcWork.bottom ) {
741 RECT rc;
743 if (toolPtr->uFlags & TTF_IDISHWND)
744 GetWindowRect ((HWND)toolPtr->uId, &rc);
745 else {
746 rc = toolPtr->rect;
747 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
749 rect.bottom = rc.top - 2;
750 rect.top = rect.bottom - size.cy;
753 AdjustWindowRectEx (&rect, GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE),
754 FALSE, GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE));
756 if (style & TTS_BALLOON)
758 HRGN hRgn;
759 HRGN hrStem;
760 POINT pts[3];
762 ptfx -= rect.left;
764 if(infoPtr->bToolBelow)
766 pts[0].x = ptfx;
767 pts[0].y = 0;
768 pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
769 pts[1].y = BALLOON_STEMHEIGHT;
770 pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
771 pts[2].y = pts[1].y;
772 if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
774 pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
775 pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
778 else
780 pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
781 pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
782 pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
783 pts[1].y = pts[0].y;
784 pts[2].x = ptfx;
785 pts[2].y = (rect.bottom - rect.top);
786 if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
788 pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
789 pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
793 hrStem = CreatePolygonRgn(pts, ARRAY_SIZE(pts), ALTERNATE);
795 hRgn = CreateRoundRectRgn(0,
796 (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
797 rect.right - rect.left,
798 (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
799 BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
801 CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
802 DeleteObject(hrStem);
804 SetWindowRgn(infoPtr->hwndSelf, hRgn, FALSE);
805 /* we don't free the region handle as the system deletes it when
806 * it is no longer needed */
809 SetWindowPos (infoPtr->hwndSelf, NULL, rect.left, rect.top,
810 rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
812 hdr.hwndFrom = infoPtr->hwndSelf;
813 hdr.idFrom = toolPtr->uId;
814 hdr.code = TTN_SHOW;
815 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
817 SetWindowPos (infoPtr->hwndSelf, HWND_TOPMOST, 0, 0, 0, 0,
818 SWP_NOSIZE | SWP_NOMOVE | 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 TOOLTIPS_INFO *infoPtr, INT index, TTTOOLINFOW *ti, BOOL isW)
951 const TTTOOL_INFO *toolPtr = &infoPtr->tools[index];
953 ti->uFlags = toolPtr->uFlags;
954 ti->hwnd = toolPtr->hwnd;
955 ti->uId = toolPtr->uId;
956 ti->rect = toolPtr->rect;
957 ti->hinst = toolPtr->hinst;
959 if (ti->lpszText) {
960 if (toolPtr->lpszText == NULL ||
961 IS_INTRESOURCE(toolPtr->lpszText) ||
962 toolPtr->lpszText == LPSTR_TEXTCALLBACKW)
963 ti->lpszText = toolPtr->lpszText;
964 else if (isW)
965 lstrcpyW (ti->lpszText, toolPtr->lpszText);
966 else
967 /* ANSI version, the buffer is maximum 80 bytes without null. */
968 WideCharToMultiByte(CP_ACP, 0, toolPtr->lpszText, -1,
969 (LPSTR)ti->lpszText, MAX_TEXT_SIZE_A, NULL, NULL);
972 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
973 ti->lParam = toolPtr->lParam;
975 /* lpReserved is intentionally not set. */
978 static BOOL
979 TOOLTIPS_IsWindowActive (HWND hwnd)
981 HWND hwndActive = GetActiveWindow ();
982 if (!hwndActive)
983 return FALSE;
984 if (hwndActive == hwnd)
985 return TRUE;
986 return IsChild (hwndActive, hwnd);
990 static INT
991 TOOLTIPS_CheckTool (const TOOLTIPS_INFO *infoPtr, BOOL bShowTest)
993 POINT pt;
994 HWND hwndTool;
995 INT nTool;
997 GetCursorPos (&pt);
998 hwndTool = (HWND)SendMessageW (infoPtr->hwndSelf, TTM_WINDOWFROMPOINT, 0, (LPARAM)&pt);
999 if (hwndTool == 0)
1000 return -1;
1002 ScreenToClient (hwndTool, &pt);
1003 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, hwndTool, &pt);
1004 if (nTool == -1)
1005 return -1;
1007 if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_ALWAYSTIP) && bShowTest)
1009 TTTOOL_INFO *ti = &infoPtr->tools[nTool];
1010 HWND hwnd = (ti->uFlags & TTF_IDISHWND) ? (HWND)ti->uId : ti->hwnd;
1012 if (!TOOLTIPS_IsWindowActive(hwnd))
1014 TRACE("not active: hwnd %p, parent %p, active %p\n",
1015 hwnd, GetParent(hwnd), GetActiveWindow());
1016 return -1;
1020 TRACE("tool %d\n", nTool);
1022 return nTool;
1026 static LRESULT
1027 TOOLTIPS_Activate (TOOLTIPS_INFO *infoPtr, BOOL activate)
1029 infoPtr->bActive = activate;
1031 TRACE("activate %d\n", activate);
1033 if (!(infoPtr->bActive) && (infoPtr->nCurrentTool != -1))
1034 TOOLTIPS_Hide (infoPtr);
1036 return 0;
1040 static LRESULT
1041 TOOLTIPS_AddToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1043 TTTOOL_INFO *toolPtr;
1044 INT nResult;
1046 if (!ti) return FALSE;
1048 TRACE("add tool (%p) %p %ld%s\n", infoPtr->hwndSelf, ti->hwnd, ti->uId,
1049 (ti->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
1051 if (ti->cbSize > TTTOOLINFOW_V3_SIZE && isW)
1052 return FALSE;
1054 if (infoPtr->uNumTools == 0) {
1055 infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
1056 toolPtr = infoPtr->tools;
1058 else {
1059 TTTOOL_INFO *oldTools = infoPtr->tools;
1060 infoPtr->tools =
1061 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1062 memcpy (infoPtr->tools, oldTools,
1063 infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1064 Free (oldTools);
1065 toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1068 infoPtr->uNumTools++;
1070 /* copy tool data */
1071 toolPtr->uFlags = ti->uFlags;
1072 toolPtr->uInternalFlags = (ti->uFlags & (TTF_SUBCLASS | TTF_IDISHWND));
1073 toolPtr->hwnd = ti->hwnd;
1074 toolPtr->uId = ti->uId;
1075 toolPtr->rect = ti->rect;
1076 toolPtr->hinst = ti->hinst;
1078 if (ti->cbSize >= TTTOOLINFOW_V1_SIZE) {
1079 if (IS_INTRESOURCE(ti->lpszText)) {
1080 TRACE("add string id %x\n", LOWORD(ti->lpszText));
1081 toolPtr->lpszText = ti->lpszText;
1083 else if (ti->lpszText) {
1084 if (ti->lpszText == LPSTR_TEXTCALLBACKW) {
1085 TRACE("add CALLBACK\n");
1086 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1088 else if (isW) {
1089 INT len = lstrlenW (ti->lpszText);
1090 TRACE("add text %s\n", debugstr_w(ti->lpszText));
1091 toolPtr->lpszText = Alloc ((len + 1)*sizeof(WCHAR));
1092 lstrcpyW (toolPtr->lpszText, ti->lpszText);
1094 else {
1095 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, NULL, 0);
1096 TRACE("add text \"%s\"\n", debugstr_a((char *)ti->lpszText));
1097 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1098 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, toolPtr->lpszText, len);
1103 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1104 toolPtr->lParam = ti->lParam;
1106 /* install subclassing hook */
1107 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1108 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1109 SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1110 (DWORD_PTR)infoPtr->hwndSelf);
1112 else {
1113 SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1114 (DWORD_PTR)infoPtr->hwndSelf);
1116 TRACE("subclassing installed\n");
1119 nResult = SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1120 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1121 if (nResult == NFR_ANSI) {
1122 toolPtr->bNotifyUnicode = FALSE;
1123 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1124 } else if (nResult == NFR_UNICODE) {
1125 toolPtr->bNotifyUnicode = TRUE;
1126 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1127 } else {
1128 TRACE (" -- WM_NOTIFYFORMAT returns: %d\n", nResult);
1131 return TRUE;
1134 static void TOOLTIPS_ResetSubclass (const TTTOOL_INFO *toolPtr)
1136 /* Reset subclassing data. */
1137 if (toolPtr->uInternalFlags & TTF_SUBCLASS)
1138 SetWindowSubclass(toolPtr->uInternalFlags & TTF_IDISHWND ? (HWND)toolPtr->uId : toolPtr->hwnd,
1139 TOOLTIPS_SubclassProc, 1, 0);
1142 static void TOOLTIPS_FreeToolText(TTTOOL_INFO *toolPtr)
1144 if (toolPtr->lpszText)
1146 if (!IS_INTRESOURCE(toolPtr->lpszText) && toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1147 Free(toolPtr->lpszText);
1148 toolPtr->lpszText = NULL;
1152 static void TOOLTIPS_SetToolText(TTTOOL_INFO *toolPtr, WCHAR *text, BOOL is_unicode)
1154 int len;
1156 TOOLTIPS_FreeToolText (toolPtr);
1158 if (IS_INTRESOURCE(text))
1159 toolPtr->lpszText = text;
1160 else if (text == LPSTR_TEXTCALLBACKW)
1161 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1162 else if (text)
1164 if (is_unicode)
1166 len = lstrlenW(text);
1167 toolPtr->lpszText = Alloc ((len + 1) * sizeof(WCHAR));
1168 if (toolPtr->lpszText)
1169 lstrcpyW (toolPtr->lpszText, text);
1171 else
1173 len = MultiByteToWideChar(CP_ACP, 0, (char *)text, -1, NULL, 0);
1174 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1175 if (toolPtr->lpszText)
1176 MultiByteToWideChar(CP_ACP, 0, (char *)text, -1, toolPtr->lpszText, len);
1181 static LRESULT
1182 TOOLTIPS_DelToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1184 TTTOOL_INFO *toolPtr;
1185 INT nTool;
1187 if (!ti) return 0;
1188 if (isW && ti->cbSize > TTTOOLINFOW_V2_SIZE &&
1189 ti->cbSize != TTTOOLINFOW_V3_SIZE)
1190 return 0;
1191 if (infoPtr->uNumTools == 0)
1192 return 0;
1194 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1196 TRACE("tool %d\n", nTool);
1198 if (nTool == -1)
1199 return 0;
1201 /* make sure the tooltip has disappeared before deleting it */
1202 TOOLTIPS_Hide(infoPtr);
1204 toolPtr = &infoPtr->tools[nTool];
1205 TOOLTIPS_FreeToolText (toolPtr);
1206 TOOLTIPS_ResetSubclass (toolPtr);
1208 /* delete tool from tool list */
1209 if (infoPtr->uNumTools == 1) {
1210 Free (infoPtr->tools);
1211 infoPtr->tools = NULL;
1213 else {
1214 TTTOOL_INFO *oldTools = infoPtr->tools;
1215 infoPtr->tools =
1216 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1218 if (nTool > 0)
1219 memcpy (&infoPtr->tools[0], &oldTools[0],
1220 nTool * sizeof(TTTOOL_INFO));
1222 if (nTool < infoPtr->uNumTools - 1)
1223 memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1224 (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1226 Free (oldTools);
1229 /* update any indices affected by delete */
1231 /* destroying tool that mouse was on on last relayed mouse move */
1232 if (infoPtr->nTool == nTool)
1233 /* -1 means no current tool (0 means first tool) */
1234 infoPtr->nTool = -1;
1235 else if (infoPtr->nTool > nTool)
1236 infoPtr->nTool--;
1238 if (infoPtr->nTrackTool == nTool)
1239 /* -1 means no current tool (0 means first tool) */
1240 infoPtr->nTrackTool = -1;
1241 else if (infoPtr->nTrackTool > nTool)
1242 infoPtr->nTrackTool--;
1244 if (infoPtr->nCurrentTool == nTool)
1245 /* -1 means no current tool (0 means first tool) */
1246 infoPtr->nCurrentTool = -1;
1247 else if (infoPtr->nCurrentTool > nTool)
1248 infoPtr->nCurrentTool--;
1250 infoPtr->uNumTools--;
1252 return 0;
1255 static LRESULT
1256 TOOLTIPS_EnumToolsT (const TOOLTIPS_INFO *infoPtr, UINT uIndex, TTTOOLINFOW *ti,
1257 BOOL isW)
1259 if (!ti || ti->cbSize < TTTOOLINFOW_V1_SIZE)
1260 return FALSE;
1261 if (uIndex >= infoPtr->uNumTools)
1262 return FALSE;
1264 TRACE("index=%u\n", uIndex);
1266 TOOLTIPS_CopyInfoT (infoPtr, uIndex, ti, isW);
1268 return TRUE;
1271 static LRESULT
1272 TOOLTIPS_GetBubbleSize (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
1274 INT nTool;
1275 SIZE size;
1277 if (lpToolInfo == NULL)
1278 return FALSE;
1279 if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1280 return FALSE;
1282 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, lpToolInfo);
1283 if (nTool == -1) return 0;
1285 TRACE("tool %d\n", nTool);
1287 TOOLTIPS_CalcTipSize (infoPtr, &size);
1288 TRACE("size %d x %d\n", size.cx, size.cy);
1290 return MAKELRESULT(size.cx, size.cy);
1293 static LRESULT
1294 TOOLTIPS_GetCurrentToolT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1296 if (ti) {
1297 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1298 return FALSE;
1300 if (infoPtr->nCurrentTool != -1)
1301 TOOLTIPS_CopyInfoT (infoPtr, infoPtr->nCurrentTool, ti, isW);
1304 return infoPtr->nCurrentTool != -1;
1308 static LRESULT
1309 TOOLTIPS_GetDelayTime (const TOOLTIPS_INFO *infoPtr, DWORD duration)
1311 switch (duration) {
1312 case TTDT_RESHOW:
1313 return infoPtr->nReshowTime;
1315 case TTDT_AUTOPOP:
1316 return infoPtr->nAutoPopTime;
1318 case TTDT_INITIAL:
1319 case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1320 return infoPtr->nInitialTime;
1322 default:
1323 WARN("Invalid duration flag %x\n", duration);
1324 break;
1327 return -1;
1331 static LRESULT
1332 TOOLTIPS_GetMargin (const TOOLTIPS_INFO *infoPtr, RECT *rect)
1334 if (rect)
1335 *rect = infoPtr->rcMargin;
1337 return 0;
1341 static inline LRESULT
1342 TOOLTIPS_GetMaxTipWidth (const TOOLTIPS_INFO *infoPtr)
1344 return infoPtr->nMaxTipWidth;
1348 static LRESULT
1349 TOOLTIPS_GetTextT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1351 INT nTool;
1353 if (!ti) return 0;
1354 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1355 return 0;
1357 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1358 if (nTool == -1) return 0;
1360 if (infoPtr->tools[nTool].lpszText == NULL)
1361 return 0;
1363 if (isW) {
1364 ti->lpszText[0] = '\0';
1365 TOOLTIPS_GetTipText(infoPtr, nTool, ti->lpszText);
1367 else {
1368 WCHAR buffer[INFOTIPSIZE];
1370 /* NB this API is broken, there is no way for the app to determine
1371 what size buffer it requires nor a way to specify how long the
1372 one it supplies is. According to the test result, it's up to
1373 80 bytes by the ANSI version. */
1375 buffer[0] = '\0';
1376 TOOLTIPS_GetTipText(infoPtr, nTool, buffer);
1377 WideCharToMultiByte(CP_ACP, 0, buffer, -1, (LPSTR)ti->lpszText,
1378 MAX_TEXT_SIZE_A, NULL, NULL);
1381 return 0;
1385 static inline LRESULT
1386 TOOLTIPS_GetTipBkColor (const TOOLTIPS_INFO *infoPtr)
1388 return infoPtr->clrBk;
1392 static inline LRESULT
1393 TOOLTIPS_GetTipTextColor (const TOOLTIPS_INFO *infoPtr)
1395 return infoPtr->clrText;
1399 static inline LRESULT
1400 TOOLTIPS_GetToolCount (const TOOLTIPS_INFO *infoPtr)
1402 return infoPtr->uNumTools;
1406 static LRESULT
1407 TOOLTIPS_GetToolInfoT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1409 INT nTool;
1410 HWND hwnd;
1412 if (!ti) return FALSE;
1413 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1414 return FALSE;
1415 if (infoPtr->uNumTools == 0)
1416 return FALSE;
1418 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1419 if (nTool == -1)
1420 return FALSE;
1422 TRACE("tool %d\n", nTool);
1424 hwnd = ti->hwnd;
1425 TOOLTIPS_CopyInfoT (infoPtr, nTool, ti, isW);
1426 ti->hwnd = hwnd;
1428 return TRUE;
1432 static LRESULT
1433 TOOLTIPS_HitTestT (const TOOLTIPS_INFO *infoPtr, LPTTHITTESTINFOW lptthit,
1434 BOOL isW)
1436 INT nTool;
1438 if (lptthit == 0)
1439 return FALSE;
1441 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1442 if (nTool == -1)
1443 return FALSE;
1445 TRACE("tool %d\n", nTool);
1447 /* copy tool data */
1448 if (lptthit->ti.cbSize >= TTTOOLINFOW_V1_SIZE)
1449 TOOLTIPS_CopyInfoT (infoPtr, nTool, &lptthit->ti, isW);
1451 return TRUE;
1455 static LRESULT
1456 TOOLTIPS_NewToolRectT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti)
1458 INT nTool;
1460 if (!ti) return 0;
1461 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1462 return FALSE;
1464 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1466 TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&ti->rect));
1468 if (nTool == -1) return 0;
1470 infoPtr->tools[nTool].rect = ti->rect;
1472 return 0;
1476 static inline LRESULT
1477 TOOLTIPS_Pop (TOOLTIPS_INFO *infoPtr)
1479 TOOLTIPS_Hide (infoPtr);
1481 return 0;
1485 static LRESULT
1486 TOOLTIPS_RelayEvent (TOOLTIPS_INFO *infoPtr, LPMSG lpMsg)
1488 POINT pt;
1489 INT nOldTool;
1491 if (!lpMsg) {
1492 ERR("lpMsg == NULL\n");
1493 return 0;
1496 switch (lpMsg->message) {
1497 case WM_LBUTTONDOWN:
1498 case WM_LBUTTONUP:
1499 case WM_MBUTTONDOWN:
1500 case WM_MBUTTONUP:
1501 case WM_RBUTTONDOWN:
1502 case WM_RBUTTONUP:
1503 TOOLTIPS_Hide (infoPtr);
1504 break;
1506 case WM_MOUSEMOVE:
1507 pt.x = (short)LOWORD(lpMsg->lParam);
1508 pt.y = (short)HIWORD(lpMsg->lParam);
1509 nOldTool = infoPtr->nTool;
1510 infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1511 &pt);
1512 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
1513 infoPtr->nTool, infoPtr->nCurrentTool);
1514 TRACE("WM_MOUSEMOVE (%p %s)\n", infoPtr->hwndSelf, wine_dbgstr_point(&pt));
1516 if (infoPtr->nTool != nOldTool) {
1517 if(infoPtr->nTool == -1) { /* Moved out of all tools */
1518 TOOLTIPS_Hide(infoPtr);
1519 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1520 } else if (nOldTool == -1) { /* Moved from outside */
1521 if(infoPtr->bActive) {
1522 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1523 TRACE("timer 1 started\n");
1525 } else { /* Moved from one to another */
1526 TOOLTIPS_Hide (infoPtr);
1527 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1528 if(infoPtr->bActive) {
1529 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1530 TRACE("timer 1 started\n");
1533 } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1534 KillTimer(infoPtr->hwndSelf, ID_TIMERPOP);
1535 SetTimer(infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1536 TRACE("timer 2 restarted\n");
1537 } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1538 /* previous show attempt didn't result in tooltip so try again */
1539 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1540 TRACE("timer 1 started\n");
1542 break;
1545 return 0;
1549 static LRESULT
1550 TOOLTIPS_SetDelayTime (TOOLTIPS_INFO *infoPtr, DWORD duration, INT nTime)
1552 switch (duration) {
1553 case TTDT_AUTOMATIC:
1554 if (nTime <= 0)
1555 nTime = GetDoubleClickTime();
1556 infoPtr->nReshowTime = nTime / 5;
1557 infoPtr->nAutoPopTime = nTime * 10;
1558 infoPtr->nInitialTime = nTime;
1559 break;
1561 case TTDT_RESHOW:
1562 if(nTime < 0)
1563 nTime = GetDoubleClickTime() / 5;
1564 infoPtr->nReshowTime = nTime;
1565 break;
1567 case TTDT_AUTOPOP:
1568 if(nTime < 0)
1569 nTime = GetDoubleClickTime() * 10;
1570 infoPtr->nAutoPopTime = nTime;
1571 break;
1573 case TTDT_INITIAL:
1574 if(nTime < 0)
1575 nTime = GetDoubleClickTime();
1576 infoPtr->nInitialTime = nTime;
1577 break;
1579 default:
1580 WARN("Invalid duration flag %x\n", duration);
1581 break;
1584 return 0;
1588 static LRESULT
1589 TOOLTIPS_SetMargin (TOOLTIPS_INFO *infoPtr, const RECT *rect)
1591 if (rect)
1592 infoPtr->rcMargin = *rect;
1594 return 0;
1598 static inline LRESULT
1599 TOOLTIPS_SetMaxTipWidth (TOOLTIPS_INFO *infoPtr, INT MaxWidth)
1601 INT nTemp = infoPtr->nMaxTipWidth;
1603 infoPtr->nMaxTipWidth = MaxWidth;
1605 return nTemp;
1609 static inline LRESULT
1610 TOOLTIPS_SetTipBkColor (TOOLTIPS_INFO *infoPtr, COLORREF clrBk)
1612 infoPtr->clrBk = clrBk;
1614 return 0;
1618 static inline LRESULT
1619 TOOLTIPS_SetTipTextColor (TOOLTIPS_INFO *infoPtr, COLORREF clrText)
1621 infoPtr->clrText = clrText;
1623 return 0;
1627 static LRESULT
1628 TOOLTIPS_SetTitleT (TOOLTIPS_INFO *infoPtr, UINT_PTR uTitleIcon, LPCWSTR pszTitle,
1629 BOOL isW)
1631 UINT size;
1633 TRACE("hwnd = %p, title = %s, icon = %p\n", infoPtr->hwndSelf, debugstr_w(pszTitle),
1634 (void*)uTitleIcon);
1636 Free(infoPtr->pszTitle);
1638 if (pszTitle)
1640 if (isW)
1642 size = (lstrlenW(pszTitle)+1)*sizeof(WCHAR);
1643 infoPtr->pszTitle = Alloc(size);
1644 if (!infoPtr->pszTitle)
1645 return FALSE;
1646 memcpy(infoPtr->pszTitle, pszTitle, size);
1648 else
1650 size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, NULL, 0);
1651 infoPtr->pszTitle = Alloc(size);
1652 if (!infoPtr->pszTitle)
1653 return FALSE;
1654 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1657 else
1658 infoPtr->pszTitle = NULL;
1660 if (uTitleIcon <= TTI_ERROR)
1661 infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1662 else
1663 infoPtr->hTitleIcon = CopyIcon((HICON)uTitleIcon);
1665 TRACE("icon = %p\n", infoPtr->hTitleIcon);
1667 return TRUE;
1670 static LRESULT
1671 TOOLTIPS_SetToolInfoT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1673 TTTOOL_INFO *toolPtr;
1674 INT nTool;
1676 if (!ti) return 0;
1677 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1678 return 0;
1680 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1681 if (nTool == -1) return 0;
1683 TRACE("tool %d\n", nTool);
1685 toolPtr = &infoPtr->tools[nTool];
1687 /* copy tool data */
1688 toolPtr->uFlags = ti->uFlags;
1689 toolPtr->hwnd = ti->hwnd;
1690 toolPtr->uId = ti->uId;
1691 toolPtr->rect = ti->rect;
1692 toolPtr->hinst = ti->hinst;
1694 TOOLTIPS_SetToolText (toolPtr, ti->lpszText, isW);
1696 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1697 toolPtr->lParam = ti->lParam;
1699 if (infoPtr->nCurrentTool == nTool)
1701 TOOLTIPS_GetTipText (infoPtr, infoPtr->nCurrentTool, infoPtr->szTipText);
1703 if (infoPtr->szTipText[0] == 0)
1704 TOOLTIPS_Hide(infoPtr);
1705 else
1706 TOOLTIPS_Show (infoPtr, FALSE);
1709 return 0;
1713 static LRESULT
1714 TOOLTIPS_TrackActivate (TOOLTIPS_INFO *infoPtr, BOOL track_activate, const TTTOOLINFOA *ti)
1716 if (track_activate) {
1718 if (!ti) return 0;
1719 if (ti->cbSize < TTTOOLINFOA_V1_SIZE)
1720 return FALSE;
1722 /* activate */
1723 infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoT (infoPtr, (const TTTOOLINFOW*)ti);
1724 if (infoPtr->nTrackTool != -1) {
1725 TRACE("activated\n");
1726 infoPtr->bTrackActive = TRUE;
1727 TOOLTIPS_TrackShow (infoPtr);
1730 else {
1731 /* deactivate */
1732 TOOLTIPS_TrackHide (infoPtr);
1734 infoPtr->bTrackActive = FALSE;
1735 infoPtr->nTrackTool = -1;
1737 TRACE("deactivated\n");
1740 return 0;
1744 static LRESULT
1745 TOOLTIPS_TrackPosition (TOOLTIPS_INFO *infoPtr, LPARAM coord)
1747 infoPtr->xTrackPos = (INT)LOWORD(coord);
1748 infoPtr->yTrackPos = (INT)HIWORD(coord);
1750 if (infoPtr->bTrackActive) {
1751 TRACE("[%d %d]\n",
1752 infoPtr->xTrackPos, infoPtr->yTrackPos);
1754 TOOLTIPS_TrackShow (infoPtr);
1757 return 0;
1761 static LRESULT
1762 TOOLTIPS_Update (TOOLTIPS_INFO *infoPtr)
1764 if (infoPtr->nCurrentTool != -1)
1765 UpdateWindow (infoPtr->hwndSelf);
1767 return 0;
1771 static LRESULT
1772 TOOLTIPS_UpdateTipTextT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1774 TTTOOL_INFO *toolPtr;
1775 INT nTool;
1777 if (!ti) return 0;
1778 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1779 return FALSE;
1781 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1782 if (nTool == -1)
1783 return 0;
1785 TRACE("tool %d\n", nTool);
1787 toolPtr = &infoPtr->tools[nTool];
1789 toolPtr->hinst = ti->hinst;
1791 TOOLTIPS_SetToolText(toolPtr, ti->lpszText, isW);
1793 if(infoPtr->nCurrentTool == -1) return 0;
1794 /* force repaint */
1795 if (infoPtr->bActive)
1796 TOOLTIPS_Show (infoPtr, FALSE);
1797 else if (infoPtr->bTrackActive)
1798 TOOLTIPS_Show (infoPtr, TRUE);
1800 return 0;
1804 static LRESULT
1805 TOOLTIPS_Create (HWND hwnd)
1807 TOOLTIPS_INFO *infoPtr;
1809 /* allocate memory for info structure */
1810 infoPtr = Alloc (sizeof(TOOLTIPS_INFO));
1811 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1813 /* initialize info structure */
1814 infoPtr->bActive = TRUE;
1815 infoPtr->bTrackActive = FALSE;
1817 infoPtr->nMaxTipWidth = -1;
1818 infoPtr->nTool = -1;
1819 infoPtr->nCurrentTool = -1;
1820 infoPtr->nTrackTool = -1;
1821 infoPtr->hwndSelf = hwnd;
1823 /* initialize colours and fonts */
1824 TOOLTIPS_InitSystemSettings(infoPtr);
1826 TOOLTIPS_SetDelayTime(infoPtr, TTDT_AUTOMATIC, 0);
1828 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
1830 return 0;
1834 static LRESULT
1835 TOOLTIPS_Destroy (TOOLTIPS_INFO *infoPtr)
1837 TTTOOL_INFO *toolPtr;
1838 UINT i;
1840 for (i = 0; i < infoPtr->uNumTools; i++)
1842 toolPtr = &infoPtr->tools[i];
1844 TOOLTIPS_FreeToolText (toolPtr);
1845 TOOLTIPS_ResetSubclass (toolPtr);
1848 Free (infoPtr->tools);
1850 /* free title string */
1851 Free (infoPtr->pszTitle);
1852 /* free title icon if not a standard one */
1853 if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
1854 DeleteObject(infoPtr->hTitleIcon);
1856 /* delete fonts */
1857 DeleteObject (infoPtr->hFont);
1858 DeleteObject (infoPtr->hTitleFont);
1860 /* free tool tips info data */
1861 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1862 Free (infoPtr);
1864 return 0;
1868 static inline LRESULT
1869 TOOLTIPS_GetFont (const TOOLTIPS_INFO *infoPtr)
1871 return (LRESULT)infoPtr->hFont;
1875 static LRESULT
1876 TOOLTIPS_MouseMessage (TOOLTIPS_INFO *infoPtr)
1878 TOOLTIPS_Hide (infoPtr);
1880 return 0;
1884 static LRESULT
1885 TOOLTIPS_NCCreate (HWND hwnd)
1887 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1888 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
1890 dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
1891 dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
1893 /* WS_BORDER only draws a border round the window rect, not the
1894 * window region, therefore it is useless to us in balloon mode */
1895 if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
1897 SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
1899 dwExStyle |= WS_EX_TOOLWINDOW;
1900 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
1902 return TRUE;
1906 static LRESULT
1907 TOOLTIPS_NCHitTest (const TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1909 INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
1911 TRACE(" nTool=%d\n", nTool);
1913 if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
1914 if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
1915 TRACE("-- in transparent mode\n");
1916 return HTTRANSPARENT;
1920 return DefWindowProcW (infoPtr->hwndSelf, WM_NCHITTEST, wParam, lParam);
1924 static LRESULT
1925 TOOLTIPS_NotifyFormat (TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1927 FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", infoPtr->hwndSelf, wParam, lParam);
1929 return 0;
1933 static LRESULT
1934 TOOLTIPS_Paint (const TOOLTIPS_INFO *infoPtr, HDC hDC)
1936 HDC hdc;
1937 PAINTSTRUCT ps;
1939 hdc = (hDC == NULL) ? BeginPaint (infoPtr->hwndSelf, &ps) : hDC;
1940 TOOLTIPS_Refresh (infoPtr, hdc);
1941 if (!hDC)
1942 EndPaint (infoPtr->hwndSelf, &ps);
1943 return 0;
1947 static LRESULT
1948 TOOLTIPS_SetFont (TOOLTIPS_INFO *infoPtr, HFONT hFont, BOOL redraw)
1950 LOGFONTW lf;
1952 if(!GetObjectW(hFont, sizeof(lf), &lf))
1953 return 0;
1955 DeleteObject (infoPtr->hFont);
1956 infoPtr->hFont = CreateFontIndirectW(&lf);
1958 DeleteObject (infoPtr->hTitleFont);
1959 lf.lfWeight = FW_BOLD;
1960 infoPtr->hTitleFont = CreateFontIndirectW(&lf);
1962 if (redraw && infoPtr->nCurrentTool != -1)
1963 FIXME("full redraw needed\n");
1965 return 0;
1968 /******************************************************************
1969 * TOOLTIPS_GetTextLength
1971 * This function is called when the tooltip receive a
1972 * WM_GETTEXTLENGTH message.
1974 * returns the length, in characters, of the tip text
1976 static inline LRESULT
1977 TOOLTIPS_GetTextLength(const TOOLTIPS_INFO *infoPtr)
1979 return lstrlenW(infoPtr->szTipText);
1982 /******************************************************************
1983 * TOOLTIPS_OnWMGetText
1985 * This function is called when the tooltip receive a
1986 * WM_GETTEXT message.
1987 * wParam : specifies the maximum number of characters to be copied
1988 * lParam : is the pointer to the buffer that will receive
1989 * the tip text
1991 * returns the number of characters copied
1993 static LRESULT
1994 TOOLTIPS_OnWMGetText (const TOOLTIPS_INFO *infoPtr, WPARAM size, LPWSTR pszText)
1996 LRESULT res;
1998 if(!size)
1999 return 0;
2001 res = min(lstrlenW(infoPtr->szTipText)+1, size);
2002 memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2003 pszText[res-1] = '\0';
2004 return res-1;
2007 static LRESULT
2008 TOOLTIPS_Timer (TOOLTIPS_INFO *infoPtr, INT iTimer)
2010 INT nOldTool;
2012 TRACE("timer %d (%p) expired\n", iTimer, infoPtr->hwndSelf);
2014 switch (iTimer) {
2015 case ID_TIMERSHOW:
2016 KillTimer (infoPtr->hwndSelf, ID_TIMERSHOW);
2017 nOldTool = infoPtr->nTool;
2018 if ((infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, TRUE)) == nOldTool)
2019 TOOLTIPS_Show (infoPtr, FALSE);
2020 break;
2022 case ID_TIMERPOP:
2023 TOOLTIPS_Hide (infoPtr);
2024 break;
2026 case ID_TIMERLEAVE:
2027 nOldTool = infoPtr->nTool;
2028 infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, FALSE);
2029 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
2030 infoPtr->nTool, infoPtr->nCurrentTool);
2031 if (infoPtr->nTool != nOldTool) {
2032 if(infoPtr->nTool == -1) { /* Moved out of all tools */
2033 TOOLTIPS_Hide(infoPtr);
2034 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2035 } else if (nOldTool == -1) { /* Moved from outside */
2036 ERR("How did this happen?\n");
2037 } else { /* Moved from one to another */
2038 TOOLTIPS_Hide (infoPtr);
2039 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2040 if(infoPtr->bActive) {
2041 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2042 TRACE("timer 1 started!\n");
2046 break;
2048 default:
2049 ERR("Unknown timer id %d\n", iTimer);
2050 break;
2052 return 0;
2056 static LRESULT
2057 TOOLTIPS_WinIniChange (TOOLTIPS_INFO *infoPtr)
2059 TOOLTIPS_InitSystemSettings (infoPtr);
2061 return 0;
2065 static LRESULT CALLBACK
2066 TOOLTIPS_SubclassProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2068 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr ((HWND)dwRef);
2069 MSG msg;
2071 switch (message)
2073 case WM_MOUSEMOVE:
2074 case WM_LBUTTONDOWN:
2075 case WM_LBUTTONUP:
2076 case WM_MBUTTONDOWN:
2077 case WM_MBUTTONUP:
2078 case WM_RBUTTONDOWN:
2079 case WM_RBUTTONUP:
2080 if (infoPtr)
2082 msg.hwnd = hwnd;
2083 msg.message = message;
2084 msg.wParam = wParam;
2085 msg.lParam = lParam;
2086 TOOLTIPS_RelayEvent(infoPtr, &msg);
2088 break;
2089 case WM_NCDESTROY:
2090 RemoveWindowSubclass(hwnd, TOOLTIPS_SubclassProc, 1);
2091 break;
2092 default:
2093 break;
2096 return DefSubclassProc(hwnd, message, wParam, lParam);
2100 static LRESULT CALLBACK
2101 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2103 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2105 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2106 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2107 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2108 switch (uMsg)
2110 case TTM_ACTIVATE:
2111 return TOOLTIPS_Activate (infoPtr, (BOOL)wParam);
2113 case TTM_ADDTOOLA:
2114 case TTM_ADDTOOLW:
2115 return TOOLTIPS_AddToolT (infoPtr, (LPTTTOOLINFOW)lParam, uMsg == TTM_ADDTOOLW);
2117 case TTM_DELTOOLA:
2118 case TTM_DELTOOLW:
2119 return TOOLTIPS_DelToolT (infoPtr, (LPTOOLINFOW)lParam,
2120 uMsg == TTM_DELTOOLW);
2121 case TTM_ENUMTOOLSA:
2122 case TTM_ENUMTOOLSW:
2123 return TOOLTIPS_EnumToolsT (infoPtr, (UINT)wParam, (LPTTTOOLINFOW)lParam,
2124 uMsg == TTM_ENUMTOOLSW);
2125 case TTM_GETBUBBLESIZE:
2126 return TOOLTIPS_GetBubbleSize (infoPtr, (LPTTTOOLINFOW)lParam);
2128 case TTM_GETCURRENTTOOLA:
2129 case TTM_GETCURRENTTOOLW:
2130 return TOOLTIPS_GetCurrentToolT (infoPtr, (LPTTTOOLINFOW)lParam,
2131 uMsg == TTM_GETCURRENTTOOLW);
2133 case TTM_GETDELAYTIME:
2134 return TOOLTIPS_GetDelayTime (infoPtr, (DWORD)wParam);
2136 case TTM_GETMARGIN:
2137 return TOOLTIPS_GetMargin (infoPtr, (LPRECT)lParam);
2139 case TTM_GETMAXTIPWIDTH:
2140 return TOOLTIPS_GetMaxTipWidth (infoPtr);
2142 case TTM_GETTEXTA:
2143 case TTM_GETTEXTW:
2144 return TOOLTIPS_GetTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2145 uMsg == TTM_GETTEXTW);
2147 case TTM_GETTIPBKCOLOR:
2148 return TOOLTIPS_GetTipBkColor (infoPtr);
2150 case TTM_GETTIPTEXTCOLOR:
2151 return TOOLTIPS_GetTipTextColor (infoPtr);
2153 case TTM_GETTOOLCOUNT:
2154 return TOOLTIPS_GetToolCount (infoPtr);
2156 case TTM_GETTOOLINFOA:
2157 case TTM_GETTOOLINFOW:
2158 return TOOLTIPS_GetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2159 uMsg == TTM_GETTOOLINFOW);
2161 case TTM_HITTESTA:
2162 case TTM_HITTESTW:
2163 return TOOLTIPS_HitTestT (infoPtr, (LPTTHITTESTINFOW)lParam,
2164 uMsg == TTM_HITTESTW);
2165 case TTM_NEWTOOLRECTA:
2166 case TTM_NEWTOOLRECTW:
2167 return TOOLTIPS_NewToolRectT (infoPtr, (LPTTTOOLINFOW)lParam);
2169 case TTM_POP:
2170 return TOOLTIPS_Pop (infoPtr);
2172 case TTM_RELAYEVENT:
2173 return TOOLTIPS_RelayEvent (infoPtr, (LPMSG)lParam);
2175 case TTM_SETDELAYTIME:
2176 return TOOLTIPS_SetDelayTime (infoPtr, (DWORD)wParam, (INT)LOWORD(lParam));
2178 case TTM_SETMARGIN:
2179 return TOOLTIPS_SetMargin (infoPtr, (LPRECT)lParam);
2181 case TTM_SETMAXTIPWIDTH:
2182 return TOOLTIPS_SetMaxTipWidth (infoPtr, (INT)lParam);
2184 case TTM_SETTIPBKCOLOR:
2185 return TOOLTIPS_SetTipBkColor (infoPtr, (COLORREF)wParam);
2187 case TTM_SETTIPTEXTCOLOR:
2188 return TOOLTIPS_SetTipTextColor (infoPtr, (COLORREF)wParam);
2190 case TTM_SETTITLEA:
2191 case TTM_SETTITLEW:
2192 return TOOLTIPS_SetTitleT (infoPtr, (UINT_PTR)wParam, (LPCWSTR)lParam,
2193 uMsg == TTM_SETTITLEW);
2195 case TTM_SETTOOLINFOA:
2196 case TTM_SETTOOLINFOW:
2197 return TOOLTIPS_SetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2198 uMsg == TTM_SETTOOLINFOW);
2200 case TTM_TRACKACTIVATE:
2201 return TOOLTIPS_TrackActivate (infoPtr, (BOOL)wParam, (LPTTTOOLINFOA)lParam);
2203 case TTM_TRACKPOSITION:
2204 return TOOLTIPS_TrackPosition (infoPtr, lParam);
2206 case TTM_UPDATE:
2207 return TOOLTIPS_Update (infoPtr);
2209 case TTM_UPDATETIPTEXTA:
2210 case TTM_UPDATETIPTEXTW:
2211 return TOOLTIPS_UpdateTipTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2212 uMsg == TTM_UPDATETIPTEXTW);
2214 case TTM_WINDOWFROMPOINT:
2215 return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2217 case WM_CREATE:
2218 return TOOLTIPS_Create (hwnd);
2220 case WM_DESTROY:
2221 return TOOLTIPS_Destroy (infoPtr);
2223 case WM_ERASEBKGND:
2224 /* we draw the background in WM_PAINT */
2225 return 0;
2227 case WM_GETFONT:
2228 return TOOLTIPS_GetFont (infoPtr);
2230 case WM_GETTEXT:
2231 return TOOLTIPS_OnWMGetText (infoPtr, wParam, (LPWSTR)lParam);
2233 case WM_GETTEXTLENGTH:
2234 return TOOLTIPS_GetTextLength (infoPtr);
2236 case WM_LBUTTONDOWN:
2237 case WM_LBUTTONUP:
2238 case WM_MBUTTONDOWN:
2239 case WM_MBUTTONUP:
2240 case WM_RBUTTONDOWN:
2241 case WM_RBUTTONUP:
2242 case WM_MOUSEMOVE:
2243 return TOOLTIPS_MouseMessage (infoPtr);
2245 case WM_NCCREATE:
2246 return TOOLTIPS_NCCreate (hwnd);
2248 case WM_NCHITTEST:
2249 return TOOLTIPS_NCHitTest (infoPtr, wParam, lParam);
2251 case WM_NOTIFYFORMAT:
2252 return TOOLTIPS_NotifyFormat (infoPtr, wParam, lParam);
2254 case WM_PRINTCLIENT:
2255 case WM_PAINT:
2256 return TOOLTIPS_Paint (infoPtr, (HDC)wParam);
2258 case WM_SETFONT:
2259 return TOOLTIPS_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
2261 case WM_SYSCOLORCHANGE:
2262 COMCTL32_RefreshSysColors();
2263 return 0;
2265 case WM_TIMER:
2266 return TOOLTIPS_Timer (infoPtr, (INT)wParam);
2268 case WM_WININICHANGE:
2269 return TOOLTIPS_WinIniChange (infoPtr);
2271 default:
2272 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2273 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2274 uMsg, wParam, lParam);
2275 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2280 VOID
2281 TOOLTIPS_Register (void)
2283 WNDCLASSW wndClass;
2285 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2286 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2287 wndClass.lpfnWndProc = TOOLTIPS_WindowProc;
2288 wndClass.cbClsExtra = 0;
2289 wndClass.cbWndExtra = sizeof(TOOLTIPS_INFO *);
2290 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2291 wndClass.hbrBackground = 0;
2292 wndClass.lpszClassName = TOOLTIPS_CLASSW;
2294 RegisterClassW (&wndClass);
2296 hTooltipIcons[TTI_NONE] = NULL;
2297 hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2298 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2299 hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2300 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2301 hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2302 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2306 VOID
2307 TOOLTIPS_Unregister (void)
2309 int i;
2310 for (i = TTI_INFO; i <= TTI_ERROR; i++)
2311 DestroyIcon(hTooltipIcons[i]);
2312 UnregisterClassW (TOOLTIPS_CLASSW, NULL);