wineconsole: Try harder to get a scalable font.
[wine.git] / dlls / comctl32 / tooltips.c
blob1aa55d12e3825d1737ae83b5ee6d512d0399ae7a
1 /*
2 * Tool tip control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 2004 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Sep. 08, 2004, by Robert Shearman.
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features or bugs please note them below.
30 * TODO:
31 * - Custom draw support.
32 * - Animation.
33 * - Links.
34 * - Messages:
35 * o TTM_ADJUSTRECT
36 * o TTM_GETTITLEA
37 * o TTM_GETTTILEW
38 * o TTM_POPUP
39 * - Styles:
40 * o TTS_NOANIMATE
41 * o TTS_NOFADE
42 * o TTS_CLOSE
44 * Testing:
45 * - Run tests using Waite Group Windows95 API Bible Volume 2.
46 * The second cdrom (chapter 3) contains executables activate.exe,
47 * curtool.exe, deltool.exe, enumtools.exe, getinfo.exe, getiptxt.exe,
48 * hittest.exe, needtext.exe, newrect.exe, updtext.exe and winfrpt.exe.
50 * Timer logic.
52 * One important point to remember is that tools don't necessarily get
53 * a WM_MOUSEMOVE once the cursor leaves the tool, an example is when
54 * a tool sets TTF_IDISHWND (i.e. an entire window is a tool) because
55 * here WM_MOUSEMOVEs only get sent when the cursor is inside the
56 * client area. Therefore the only reliable way to know that the
57 * cursor has left a tool is to keep a timer running and check the
58 * position every time it expires. This is the role of timer
59 * ID_TIMERLEAVE.
62 * On entering a tool (detected in a relayed WM_MOUSEMOVE) we start
63 * ID_TIMERSHOW, if this times out and we're still in the tool we show
64 * the tip. On showing a tip we start both ID_TIMERPOP and
65 * ID_TIMERLEAVE. On hiding a tooltip we kill ID_TIMERPOP.
66 * ID_TIMERPOP is restarted on every relayed WM_MOUSEMOVE. If
67 * ID_TIMERPOP expires the tool is hidden and ID_TIMERPOP is killed.
68 * ID_TIMERLEAVE remains running - this is important as we need to
69 * determine when the cursor leaves the tool.
71 * When ID_TIMERLEAVE expires or on a relayed WM_MOUSEMOVE if we're
72 * still in the tool do nothing (apart from restart ID_TIMERPOP if
73 * this is a WM_MOUSEMOVE) (ID_TIMERLEAVE remains running). If we've
74 * left the tool and entered another one then hide the tip and start
75 * ID_TIMERSHOW with time ReshowTime and kill ID_TIMERLEAVE. If we're
76 * outside all tools hide the tip and kill ID_TIMERLEAVE. On Relayed
77 * mouse button messages hide the tip but leave ID_TIMERLEAVE running,
78 * this again will let us keep track of when the cursor leaves the
79 * tool.
82 * infoPtr->nTool is the tool the mouse was on on the last relayed MM
83 * or timer expiry or -1 if the mouse was not on a tool.
85 * infoPtr->nCurrentTool is the tool for which the tip is currently
86 * displaying text for or -1 if the tip is not shown. Actually this
87 * will only ever be infoPtr-nTool or -1, so it could be changed to a
88 * BOOL.
94 #include <stdarg.h>
95 #include <string.h>
97 #include "windef.h"
98 #include "winbase.h"
99 #include "wine/unicode.h"
100 #include "wingdi.h"
101 #include "winuser.h"
102 #include "winnls.h"
103 #include "commctrl.h"
104 #include "comctl32.h"
105 #include "wine/debug.h"
107 WINE_DEFAULT_DEBUG_CHANNEL(tooltips);
109 static HICON hTooltipIcons[TTI_ERROR+1];
111 typedef struct
113 UINT uFlags;
114 UINT uInternalFlags;
115 HWND hwnd;
116 BOOL bNotifyUnicode;
117 UINT_PTR uId;
118 RECT rect;
119 HINSTANCE hinst;
120 LPWSTR lpszText;
121 LPARAM lParam;
122 } TTTOOL_INFO;
125 typedef struct
127 HWND hwndSelf;
128 WCHAR szTipText[INFOTIPSIZE];
129 BOOL bActive;
130 BOOL bTrackActive;
131 UINT uNumTools;
132 COLORREF clrBk;
133 COLORREF clrText;
134 HFONT hFont;
135 HFONT hTitleFont;
136 INT xTrackPos;
137 INT yTrackPos;
138 INT nMaxTipWidth;
139 INT nTool; /* tool that mouse was on on last relayed mouse move */
140 INT nCurrentTool;
141 INT nTrackTool;
142 INT nReshowTime;
143 INT nAutoPopTime;
144 INT nInitialTime;
145 RECT rcMargin;
146 BOOL bToolBelow;
147 LPWSTR pszTitle;
148 HICON hTitleIcon;
150 TTTOOL_INFO *tools;
151 } TOOLTIPS_INFO;
153 #define ID_TIMERSHOW 1 /* show delay timer */
154 #define ID_TIMERPOP 2 /* auto pop timer */
155 #define ID_TIMERLEAVE 3 /* tool leave timer */
158 #define TOOLTIPS_GetInfoPtr(hWindow) ((TOOLTIPS_INFO *)GetWindowLongPtrW (hWindow, 0))
160 /* offsets from window edge to start of text */
161 #define NORMAL_TEXT_MARGIN 2
162 #define BALLOON_TEXT_MARGIN (NORMAL_TEXT_MARGIN+8)
163 /* value used for CreateRoundRectRgn that specifies how much
164 * each corner is curved */
165 #define BALLOON_ROUNDEDNESS 20
166 #define BALLOON_STEMHEIGHT 13
167 #define BALLOON_STEMWIDTH 10
168 #define BALLOON_STEMINDENT 20
170 #define BALLOON_ICON_TITLE_SPACING 8 /* horizontal spacing between icon and title */
171 #define BALLOON_TITLE_TEXT_SPACING 8 /* vertical spacing between icon/title and main text */
172 #define ICON_HEIGHT 16
173 #define ICON_WIDTH 16
175 #define MAX_TEXT_SIZE_A 80 /* maximum retrieving text size by ANSI message */
177 static LRESULT CALLBACK
178 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uId, DWORD_PTR dwRef);
180 static inline UINT_PTR
181 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
183 UINT i;
184 for (i = 0; i <= TTI_ERROR; i++)
185 if (hTooltipIcons[i] == hIcon)
186 return i;
187 return (UINT_PTR)hIcon;
190 static void
191 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
193 NONCLIENTMETRICSW nclm;
195 infoPtr->clrBk = comctl32_color.clrInfoBk;
196 infoPtr->clrText = comctl32_color.clrInfoText;
198 DeleteObject (infoPtr->hFont);
199 nclm.cbSize = sizeof(nclm);
200 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
201 infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
203 DeleteObject (infoPtr->hTitleFont);
204 nclm.lfStatusFont.lfWeight = FW_BOLD;
205 infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
208 /* Custom draw routines */
209 static void
210 TOOLTIPS_customdraw_fill(const TOOLTIPS_INFO *infoPtr, NMTTCUSTOMDRAW *lpnmttcd,
211 HDC hdc, const RECT *rcBounds, UINT uFlags)
213 ZeroMemory(lpnmttcd, sizeof(NMTTCUSTOMDRAW));
214 lpnmttcd->uDrawFlags = uFlags;
215 lpnmttcd->nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
216 lpnmttcd->nmcd.hdr.code = NM_CUSTOMDRAW;
217 if (infoPtr->nCurrentTool != -1) {
218 TTTOOL_INFO *toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
219 lpnmttcd->nmcd.hdr.idFrom = toolPtr->uId;
221 lpnmttcd->nmcd.hdc = hdc;
222 lpnmttcd->nmcd.rc = *rcBounds;
223 /* FIXME - dwItemSpec, uItemState, lItemlParam */
226 static inline DWORD
227 TOOLTIPS_notify_customdraw (DWORD dwDrawStage, NMTTCUSTOMDRAW *lpnmttcd)
229 LRESULT result;
230 lpnmttcd->nmcd.dwDrawStage = dwDrawStage;
232 TRACE("Notifying stage %d, flags %x, id %x\n", lpnmttcd->nmcd.dwDrawStage,
233 lpnmttcd->uDrawFlags, lpnmttcd->nmcd.hdr.code);
235 result = SendMessageW(GetParent(lpnmttcd->nmcd.hdr.hwndFrom), WM_NOTIFY,
236 0, (LPARAM)lpnmttcd);
238 TRACE("Notify result %x\n", (unsigned int)result);
240 return result;
243 static void
244 TOOLTIPS_Refresh (const TOOLTIPS_INFO *infoPtr, HDC hdc)
246 RECT rc;
247 INT oldBkMode;
248 HFONT hOldFont;
249 HBRUSH hBrush;
250 UINT uFlags = DT_EXTERNALLEADING;
251 HRGN hRgn = NULL;
252 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
253 NMTTCUSTOMDRAW nmttcd;
254 DWORD cdmode;
256 if (infoPtr->nMaxTipWidth > -1)
257 uFlags |= DT_WORDBREAK;
258 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)
259 uFlags |= DT_NOPREFIX;
260 GetClientRect (infoPtr->hwndSelf, &rc);
262 hBrush = CreateSolidBrush(infoPtr->clrBk);
264 oldBkMode = SetBkMode (hdc, TRANSPARENT);
265 SetTextColor (hdc, infoPtr->clrText);
266 hOldFont = SelectObject (hdc, infoPtr->hFont);
268 /* Custom draw - Call PrePaint once initial properties set up */
269 /* Note: Contrary to MSDN, CDRF_SKIPDEFAULT still draws a tooltip */
270 TOOLTIPS_customdraw_fill(infoPtr, &nmttcd, hdc, &rc, uFlags);
271 cdmode = TOOLTIPS_notify_customdraw(CDDS_PREPAINT, &nmttcd);
272 uFlags = nmttcd.uDrawFlags;
274 if (dwStyle & TTS_BALLOON)
276 /* create a region to store result into */
277 hRgn = CreateRectRgn(0, 0, 0, 0);
279 GetWindowRgn(infoPtr->hwndSelf, hRgn);
281 /* fill the background */
282 FillRgn(hdc, hRgn, hBrush);
283 DeleteObject(hBrush);
284 hBrush = NULL;
286 else
288 /* fill the background */
289 FillRect(hdc, &rc, hBrush);
290 DeleteObject(hBrush);
291 hBrush = NULL;
294 if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
296 /* calculate text rectangle */
297 rc.left += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
298 rc.top += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
299 rc.right -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
300 rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
301 if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
303 if (infoPtr->pszTitle)
305 RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
306 int height;
307 BOOL icon_present;
308 HFONT prevFont;
310 /* draw icon */
311 icon_present = infoPtr->hTitleIcon &&
312 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
313 ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
314 if (icon_present)
315 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
317 rcTitle.bottom = rc.top + ICON_HEIGHT;
319 /* draw title text */
320 prevFont = SelectObject (hdc, infoPtr->hTitleFont);
321 height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
322 SelectObject (hdc, prevFont);
323 rc.top += height + BALLOON_TITLE_TEXT_SPACING;
326 else
328 /* calculate text rectangle */
329 rc.left += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
330 rc.top += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
331 rc.right -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
332 rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
335 /* draw text */
336 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
338 /* Custom draw - Call PostPaint after drawing */
339 if (cdmode & CDRF_NOTIFYPOSTPAINT) {
340 TOOLTIPS_notify_customdraw(CDDS_POSTPAINT, &nmttcd);
343 /* be polite and reset the things we changed in the dc */
344 SelectObject (hdc, hOldFont);
345 SetBkMode (hdc, oldBkMode);
347 if (dwStyle & TTS_BALLOON)
349 /* frame region because default window proc doesn't do it */
350 INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
351 INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
353 hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
354 FrameRgn(hdc, hRgn, hBrush, width, height);
357 if (hRgn)
358 DeleteObject(hRgn);
361 static void TOOLTIPS_GetDispInfoA(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
363 NMTTDISPINFOA ttnmdi;
365 /* fill NMHDR struct */
366 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
367 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
368 ttnmdi.hdr.idFrom = toolPtr->uId;
369 ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
370 ttnmdi.lpszText = ttnmdi.szText;
371 ttnmdi.uFlags = toolPtr->uFlags;
372 ttnmdi.lParam = toolPtr->lParam;
374 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
375 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
377 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
378 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
379 buffer, INFOTIPSIZE);
380 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
381 toolPtr->hinst = ttnmdi.hinst;
382 toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
385 else if (ttnmdi.lpszText == 0) {
386 buffer[0] = '\0';
388 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
389 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
390 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
391 toolPtr->hinst = 0;
392 toolPtr->lpszText = NULL;
393 Str_SetPtrW(&toolPtr->lpszText, buffer);
396 else {
397 ERR("recursive text callback\n");
398 buffer[0] = '\0';
401 /* no text available - try calling parent instead as per native */
402 /* FIXME: Unsure if SETITEM should save the value or not */
403 if (buffer[0] == 0x00) {
405 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
407 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
408 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
409 buffer, INFOTIPSIZE);
410 } else if (ttnmdi.lpszText &&
411 ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
412 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
417 static void TOOLTIPS_GetDispInfoW(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
419 NMTTDISPINFOW ttnmdi;
421 /* fill NMHDR struct */
422 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
423 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
424 ttnmdi.hdr.idFrom = toolPtr->uId;
425 ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
426 ttnmdi.lpszText = ttnmdi.szText;
427 ttnmdi.uFlags = toolPtr->uFlags;
428 ttnmdi.lParam = toolPtr->lParam;
430 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
431 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
433 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
434 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
435 buffer, INFOTIPSIZE);
436 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
437 toolPtr->hinst = ttnmdi.hinst;
438 toolPtr->lpszText = ttnmdi.lpszText;
441 else if (ttnmdi.lpszText == 0) {
442 buffer[0] = '\0';
444 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
445 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
446 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
447 toolPtr->hinst = 0;
448 toolPtr->lpszText = NULL;
449 Str_SetPtrW(&toolPtr->lpszText, buffer);
452 else {
453 ERR("recursive text callback\n");
454 buffer[0] = '\0';
457 /* no text available - try calling parent instead as per native */
458 /* FIXME: Unsure if SETITEM should save the value or not */
459 if (buffer[0] == 0x00) {
461 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
463 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
464 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
465 buffer, INFOTIPSIZE);
466 } else if (ttnmdi.lpszText &&
467 ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
468 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
474 static void
475 TOOLTIPS_GetTipText (const TOOLTIPS_INFO *infoPtr, INT nTool, WCHAR *buffer)
477 TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
479 if (IS_INTRESOURCE(toolPtr->lpszText)) {
480 /* load a resource */
481 TRACE("load res string %p %x\n",
482 toolPtr->hinst, LOWORD(toolPtr->lpszText));
483 if (!LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText), buffer, INFOTIPSIZE))
484 buffer[0] = '\0';
486 else if (toolPtr->lpszText) {
487 if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
488 if (toolPtr->bNotifyUnicode)
489 TOOLTIPS_GetDispInfoW(infoPtr, toolPtr, buffer);
490 else
491 TOOLTIPS_GetDispInfoA(infoPtr, toolPtr, buffer);
493 else {
494 /* the item is a usual (unicode) text */
495 lstrcpynW (buffer, toolPtr->lpszText, INFOTIPSIZE);
498 else {
499 /* no text available */
500 buffer[0] = '\0';
503 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)) {
504 WCHAR *ptrW;
505 if ((ptrW = strchrW(buffer, '\t')))
506 *ptrW = 0;
509 TRACE("%s\n", debugstr_w(buffer));
513 static void
514 TOOLTIPS_CalcTipSize (const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
516 HDC hdc;
517 HFONT hOldFont;
518 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
519 UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
520 RECT rc = {0, 0, 0, 0};
521 SIZE title = {0, 0};
523 if (infoPtr->nMaxTipWidth > -1) {
524 rc.right = infoPtr->nMaxTipWidth;
525 uFlags |= DT_WORDBREAK;
527 if (style & TTS_NOPREFIX)
528 uFlags |= DT_NOPREFIX;
529 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
531 hdc = GetDC (infoPtr->hwndSelf);
532 if (infoPtr->pszTitle)
534 RECT rcTitle = {0, 0, 0, 0};
535 TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
536 if (infoPtr->hTitleIcon)
538 title.cx = ICON_WIDTH;
539 title.cy = ICON_HEIGHT;
541 if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
542 hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
543 DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
544 SelectObject (hdc, hOldFont);
545 title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
546 title.cx += (rcTitle.right - rcTitle.left);
548 hOldFont = SelectObject (hdc, infoPtr->hFont);
549 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
550 SelectObject (hdc, hOldFont);
551 ReleaseDC (infoPtr->hwndSelf, hdc);
553 if ((style & TTS_BALLOON) || infoPtr->pszTitle)
555 lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
556 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
557 lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
558 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
559 BALLOON_STEMHEIGHT;
561 else
563 lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
564 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
565 lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
566 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
571 static void
572 TOOLTIPS_Show (TOOLTIPS_INFO *infoPtr, BOOL track_activate)
574 TTTOOL_INFO *toolPtr;
575 HMONITOR monitor;
576 MONITORINFO mon_info;
577 RECT rect;
578 SIZE size;
579 NMHDR hdr;
580 int ptfx = 0;
581 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
582 INT nTool, current;
584 if (track_activate)
586 if (infoPtr->nTrackTool == -1)
588 TRACE("invalid tracking tool %d\n", infoPtr->nTrackTool);
589 return;
591 nTool = infoPtr->nTrackTool;
593 else
595 if (infoPtr->nTool == -1)
597 TRACE("invalid tool %d\n", infoPtr->nTool);
598 return;
600 nTool = infoPtr->nTool;
603 TRACE("Show tooltip pre %d, %p\n", nTool, infoPtr->hwndSelf);
605 current = infoPtr->nCurrentTool;
606 if (!track_activate)
607 infoPtr->nCurrentTool = infoPtr->nTool;
609 TOOLTIPS_GetTipText (infoPtr, nTool, infoPtr->szTipText);
611 if (infoPtr->szTipText[0] == '\0')
613 infoPtr->nCurrentTool = current;
614 return;
617 toolPtr = &infoPtr->tools[nTool];
618 TOOLTIPS_CalcTipSize (infoPtr, &size);
620 TRACE("Show tooltip %d, %s, size %d x %d\n", nTool, debugstr_w(infoPtr->szTipText),
621 size.cx, size.cy);
623 if (track_activate && (toolPtr->uFlags & TTF_TRACK))
625 rect.left = infoPtr->xTrackPos;
626 rect.top = infoPtr->yTrackPos;
627 ptfx = rect.left;
629 if (toolPtr->uFlags & TTF_CENTERTIP)
631 rect.left -= (size.cx / 2);
632 if (!(style & TTS_BALLOON))
633 rect.top -= (size.cy / 2);
635 if (!(infoPtr->bToolBelow = (infoPtr->yTrackPos + size.cy <= GetSystemMetrics(SM_CYSCREEN))))
636 rect.top -= size.cy;
638 if (!(toolPtr->uFlags & TTF_ABSOLUTE))
640 if (style & TTS_BALLOON)
641 rect.left -= BALLOON_STEMINDENT;
642 else
644 RECT rcTool;
646 if (toolPtr->uFlags & TTF_IDISHWND)
647 GetWindowRect ((HWND)toolPtr->uId, &rcTool);
648 else
650 rcTool = toolPtr->rect;
651 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
654 /* smart placement */
655 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
656 (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
657 rect.left = rcTool.right;
661 else
663 if (toolPtr->uFlags & TTF_CENTERTIP)
665 RECT rc;
667 if (toolPtr->uFlags & TTF_IDISHWND)
668 GetWindowRect ((HWND)toolPtr->uId, &rc);
669 else {
670 rc = toolPtr->rect;
671 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
673 rect.left = (rc.left + rc.right - size.cx) / 2;
674 if (style & TTS_BALLOON)
676 ptfx = rc.left + ((rc.right - rc.left) / 2);
678 /* CENTERTIP ballon tooltips default to below the field
679 * if they fit on the screen */
680 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
682 rect.top = rc.top - size.cy;
683 infoPtr->bToolBelow = FALSE;
685 else
687 infoPtr->bToolBelow = TRUE;
688 rect.top = rc.bottom;
690 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
692 else
694 rect.top = rc.bottom + 2;
695 infoPtr->bToolBelow = TRUE;
698 else
700 GetCursorPos ((LPPOINT)&rect);
701 if (style & TTS_BALLOON)
703 ptfx = rect.left;
704 if(rect.top - size.cy >= 0)
706 rect.top -= size.cy;
707 infoPtr->bToolBelow = FALSE;
709 else
711 infoPtr->bToolBelow = TRUE;
712 rect.top += 20;
714 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
716 else
718 rect.top += 20;
719 infoPtr->bToolBelow = TRUE;
724 TRACE("pos %d - %d\n", rect.left, rect.top);
726 rect.right = rect.left + size.cx;
727 rect.bottom = rect.top + size.cy;
729 /* check position */
731 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
732 mon_info.cbSize = sizeof(mon_info);
733 GetMonitorInfoW( monitor, &mon_info );
735 if( rect.right > mon_info.rcWork.right ) {
736 rect.left -= rect.right - mon_info.rcWork.right + 2;
737 rect.right = mon_info.rcWork.right - 2;
739 if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
741 if( rect.bottom > mon_info.rcWork.bottom ) {
742 RECT rc;
744 if (toolPtr->uFlags & TTF_IDISHWND)
745 GetWindowRect ((HWND)toolPtr->uId, &rc);
746 else {
747 rc = toolPtr->rect;
748 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
750 rect.bottom = rc.top - 2;
751 rect.top = rect.bottom - size.cy;
754 AdjustWindowRectEx (&rect, GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE),
755 FALSE, GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE));
757 if (style & TTS_BALLOON)
759 HRGN hRgn;
760 HRGN hrStem;
761 POINT pts[3];
763 ptfx -= rect.left;
765 if(infoPtr->bToolBelow)
767 pts[0].x = ptfx;
768 pts[0].y = 0;
769 pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
770 pts[1].y = BALLOON_STEMHEIGHT;
771 pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
772 pts[2].y = pts[1].y;
773 if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
775 pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
776 pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
779 else
781 pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
782 pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
783 pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
784 pts[1].y = pts[0].y;
785 pts[2].x = ptfx;
786 pts[2].y = (rect.bottom - rect.top);
787 if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
789 pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
790 pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
794 hrStem = CreatePolygonRgn(pts, ARRAY_SIZE(pts), ALTERNATE);
796 hRgn = CreateRoundRectRgn(0,
797 (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
798 rect.right - rect.left,
799 (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
800 BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
802 CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
803 DeleteObject(hrStem);
805 SetWindowRgn(infoPtr->hwndSelf, hRgn, FALSE);
806 /* we don't free the region handle as the system deletes it when
807 * it is no longer needed */
810 SetWindowPos (infoPtr->hwndSelf, NULL, rect.left, rect.top,
811 rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
813 hdr.hwndFrom = infoPtr->hwndSelf;
814 hdr.idFrom = toolPtr->uId;
815 hdr.code = TTN_SHOW;
816 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
818 SetWindowPos (infoPtr->hwndSelf, HWND_TOPMOST, 0, 0, 0, 0,
819 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
821 /* repaint the tooltip */
822 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
823 UpdateWindow(infoPtr->hwndSelf);
825 if (!track_activate)
827 SetTimer (infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
828 TRACE("timer 2 started\n");
829 SetTimer (infoPtr->hwndSelf, ID_TIMERLEAVE, infoPtr->nReshowTime, 0);
830 TRACE("timer 3 started\n");
835 static void
836 TOOLTIPS_Hide (TOOLTIPS_INFO *infoPtr)
838 TTTOOL_INFO *toolPtr;
839 NMHDR hdr;
841 TRACE("Hide tooltip %d, %p.\n", infoPtr->nCurrentTool, infoPtr->hwndSelf);
843 if (infoPtr->nCurrentTool == -1)
844 return;
846 toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
847 KillTimer (infoPtr->hwndSelf, ID_TIMERPOP);
849 hdr.hwndFrom = infoPtr->hwndSelf;
850 hdr.idFrom = toolPtr->uId;
851 hdr.code = TTN_POP;
852 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
854 infoPtr->nCurrentTool = -1;
856 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
857 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
861 static void
862 TOOLTIPS_TrackShow (TOOLTIPS_INFO *infoPtr)
864 TOOLTIPS_Show(infoPtr, TRUE);
868 static void
869 TOOLTIPS_TrackHide (const TOOLTIPS_INFO *infoPtr)
871 TTTOOL_INFO *toolPtr;
872 NMHDR hdr;
874 TRACE("hide tracking tooltip %d\n", infoPtr->nTrackTool);
876 if (infoPtr->nTrackTool == -1)
877 return;
879 toolPtr = &infoPtr->tools[infoPtr->nTrackTool];
881 hdr.hwndFrom = infoPtr->hwndSelf;
882 hdr.idFrom = toolPtr->uId;
883 hdr.code = TTN_POP;
884 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
886 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
887 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
890 /* Structure layout is the same for TTTOOLINFOW and TTTOOLINFOA,
891 this helper is used in both cases. */
892 static INT
893 TOOLTIPS_GetToolFromInfoT (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
895 TTTOOL_INFO *toolPtr;
896 UINT nTool;
898 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
899 toolPtr = &infoPtr->tools[nTool];
901 if (!(toolPtr->uFlags & TTF_IDISHWND) &&
902 (lpToolInfo->hwnd == toolPtr->hwnd) &&
903 (lpToolInfo->uId == toolPtr->uId))
904 return nTool;
907 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
908 toolPtr = &infoPtr->tools[nTool];
910 if ((toolPtr->uFlags & TTF_IDISHWND) &&
911 (lpToolInfo->uId == toolPtr->uId))
912 return nTool;
915 return -1;
919 static INT
920 TOOLTIPS_GetToolFromPoint (const TOOLTIPS_INFO *infoPtr, HWND hwnd, const POINT *lpPt)
922 TTTOOL_INFO *toolPtr;
923 UINT nTool;
925 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
926 toolPtr = &infoPtr->tools[nTool];
928 if (!(toolPtr->uFlags & TTF_IDISHWND)) {
929 if (hwnd != toolPtr->hwnd)
930 continue;
931 if (!PtInRect (&toolPtr->rect, *lpPt))
932 continue;
933 return nTool;
937 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
938 toolPtr = &infoPtr->tools[nTool];
940 if (toolPtr->uFlags & TTF_IDISHWND) {
941 if ((HWND)toolPtr->uId == hwnd)
942 return nTool;
946 return -1;
949 static inline void
950 TOOLTIPS_CopyInfoT (const TOOLTIPS_INFO *infoPtr, INT index, TTTOOLINFOW *ti, BOOL isW)
952 const TTTOOL_INFO *toolPtr = &infoPtr->tools[index];
954 ti->uFlags = toolPtr->uFlags;
955 ti->hwnd = toolPtr->hwnd;
956 ti->uId = toolPtr->uId;
957 ti->rect = toolPtr->rect;
958 ti->hinst = toolPtr->hinst;
960 if (ti->lpszText) {
961 if (toolPtr->lpszText == NULL ||
962 IS_INTRESOURCE(toolPtr->lpszText) ||
963 toolPtr->lpszText == LPSTR_TEXTCALLBACKW)
964 ti->lpszText = toolPtr->lpszText;
965 else if (isW)
966 strcpyW (ti->lpszText, toolPtr->lpszText);
967 else
968 /* ANSI version, the buffer is maximum 80 bytes without null. */
969 WideCharToMultiByte(CP_ACP, 0, toolPtr->lpszText, -1,
970 (LPSTR)ti->lpszText, MAX_TEXT_SIZE_A, NULL, NULL);
973 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
974 ti->lParam = toolPtr->lParam;
976 /* lpReserved is intentionally not set. */
979 static BOOL
980 TOOLTIPS_IsWindowActive (HWND hwnd)
982 HWND hwndActive = GetActiveWindow ();
983 if (!hwndActive)
984 return FALSE;
985 if (hwndActive == hwnd)
986 return TRUE;
987 return IsChild (hwndActive, hwnd);
991 static INT
992 TOOLTIPS_CheckTool (const TOOLTIPS_INFO *infoPtr, BOOL bShowTest)
994 POINT pt;
995 HWND hwndTool;
996 INT nTool;
998 GetCursorPos (&pt);
999 hwndTool = (HWND)SendMessageW (infoPtr->hwndSelf, TTM_WINDOWFROMPOINT, 0, (LPARAM)&pt);
1000 if (hwndTool == 0)
1001 return -1;
1003 ScreenToClient (hwndTool, &pt);
1004 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, hwndTool, &pt);
1005 if (nTool == -1)
1006 return -1;
1008 if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_ALWAYSTIP) && bShowTest)
1010 TTTOOL_INFO *ti = &infoPtr->tools[nTool];
1011 HWND hwnd = (ti->uFlags & TTF_IDISHWND) ? (HWND)ti->uId : ti->hwnd;
1013 if (!TOOLTIPS_IsWindowActive(hwnd))
1015 TRACE("not active: hwnd %p, parent %p, active %p\n",
1016 hwnd, GetParent(hwnd), GetActiveWindow());
1017 return -1;
1021 TRACE("tool %d\n", nTool);
1023 return nTool;
1027 static LRESULT
1028 TOOLTIPS_Activate (TOOLTIPS_INFO *infoPtr, BOOL activate)
1030 infoPtr->bActive = activate;
1032 TRACE("activate %d\n", activate);
1034 if (!(infoPtr->bActive) && (infoPtr->nCurrentTool != -1))
1035 TOOLTIPS_Hide (infoPtr);
1037 return 0;
1041 static LRESULT
1042 TOOLTIPS_AddToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1044 TTTOOL_INFO *toolPtr;
1045 INT nResult;
1047 if (!ti) return FALSE;
1049 TRACE("add tool (%p) %p %ld%s\n", infoPtr->hwndSelf, ti->hwnd, ti->uId,
1050 (ti->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
1052 if (ti->cbSize > TTTOOLINFOW_V3_SIZE && isW)
1053 return FALSE;
1055 if (infoPtr->uNumTools == 0) {
1056 infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
1057 toolPtr = infoPtr->tools;
1059 else {
1060 TTTOOL_INFO *oldTools = infoPtr->tools;
1061 infoPtr->tools =
1062 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1063 memcpy (infoPtr->tools, oldTools,
1064 infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1065 Free (oldTools);
1066 toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1069 infoPtr->uNumTools++;
1071 /* copy tool data */
1072 toolPtr->uFlags = ti->uFlags;
1073 toolPtr->uInternalFlags = (ti->uFlags & (TTF_SUBCLASS | TTF_IDISHWND));
1074 toolPtr->hwnd = ti->hwnd;
1075 toolPtr->uId = ti->uId;
1076 toolPtr->rect = ti->rect;
1077 toolPtr->hinst = ti->hinst;
1079 if (ti->cbSize >= TTTOOLINFOW_V1_SIZE) {
1080 if (IS_INTRESOURCE(ti->lpszText)) {
1081 TRACE("add string id %x\n", LOWORD(ti->lpszText));
1082 toolPtr->lpszText = ti->lpszText;
1084 else if (ti->lpszText) {
1085 if (ti->lpszText == LPSTR_TEXTCALLBACKW) {
1086 TRACE("add CALLBACK\n");
1087 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1089 else if (isW) {
1090 INT len = lstrlenW (ti->lpszText);
1091 TRACE("add text %s\n", debugstr_w(ti->lpszText));
1092 toolPtr->lpszText = Alloc ((len + 1)*sizeof(WCHAR));
1093 strcpyW (toolPtr->lpszText, ti->lpszText);
1095 else {
1096 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, NULL, 0);
1097 TRACE("add text \"%s\"\n", debugstr_a((char *)ti->lpszText));
1098 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1099 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, toolPtr->lpszText, len);
1104 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1105 toolPtr->lParam = ti->lParam;
1107 /* install subclassing hook */
1108 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1109 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1110 SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1111 (DWORD_PTR)infoPtr->hwndSelf);
1113 else {
1114 SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1115 (DWORD_PTR)infoPtr->hwndSelf);
1117 TRACE("subclassing installed\n");
1120 nResult = SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1121 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1122 if (nResult == NFR_ANSI) {
1123 toolPtr->bNotifyUnicode = FALSE;
1124 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1125 } else if (nResult == NFR_UNICODE) {
1126 toolPtr->bNotifyUnicode = TRUE;
1127 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1128 } else {
1129 TRACE (" -- WM_NOTIFYFORMAT returns: %d\n", nResult);
1132 return TRUE;
1135 static void TOOLTIPS_ResetSubclass (const TTTOOL_INFO *toolPtr)
1137 /* Reset subclassing data. */
1138 if (toolPtr->uInternalFlags & TTF_SUBCLASS)
1139 SetWindowSubclass(toolPtr->uInternalFlags & TTF_IDISHWND ? (HWND)toolPtr->uId : toolPtr->hwnd,
1140 TOOLTIPS_SubclassProc, 1, 0);
1143 static LRESULT
1144 TOOLTIPS_DelToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1146 TTTOOL_INFO *toolPtr;
1147 INT nTool;
1149 if (!ti) return 0;
1150 if (isW && ti->cbSize > TTTOOLINFOW_V2_SIZE &&
1151 ti->cbSize != TTTOOLINFOW_V3_SIZE)
1152 return 0;
1153 if (infoPtr->uNumTools == 0)
1154 return 0;
1156 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1158 TRACE("tool %d\n", nTool);
1160 if (nTool == -1)
1161 return 0;
1163 /* make sure the tooltip has disappeared before deleting it */
1164 TOOLTIPS_Hide(infoPtr);
1166 /* delete text string */
1167 toolPtr = &infoPtr->tools[nTool];
1168 if (toolPtr->lpszText) {
1169 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1170 !IS_INTRESOURCE(toolPtr->lpszText) )
1171 Free (toolPtr->lpszText);
1174 TOOLTIPS_ResetSubclass (toolPtr);
1176 /* delete tool from tool list */
1177 if (infoPtr->uNumTools == 1) {
1178 Free (infoPtr->tools);
1179 infoPtr->tools = NULL;
1181 else {
1182 TTTOOL_INFO *oldTools = infoPtr->tools;
1183 infoPtr->tools =
1184 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1186 if (nTool > 0)
1187 memcpy (&infoPtr->tools[0], &oldTools[0],
1188 nTool * sizeof(TTTOOL_INFO));
1190 if (nTool < infoPtr->uNumTools - 1)
1191 memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1192 (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1194 Free (oldTools);
1197 /* update any indices affected by delete */
1199 /* destroying tool that mouse was on on last relayed mouse move */
1200 if (infoPtr->nTool == nTool)
1201 /* -1 means no current tool (0 means first tool) */
1202 infoPtr->nTool = -1;
1203 else if (infoPtr->nTool > nTool)
1204 infoPtr->nTool--;
1206 if (infoPtr->nTrackTool == nTool)
1207 /* -1 means no current tool (0 means first tool) */
1208 infoPtr->nTrackTool = -1;
1209 else if (infoPtr->nTrackTool > nTool)
1210 infoPtr->nTrackTool--;
1212 if (infoPtr->nCurrentTool == nTool)
1213 /* -1 means no current tool (0 means first tool) */
1214 infoPtr->nCurrentTool = -1;
1215 else if (infoPtr->nCurrentTool > nTool)
1216 infoPtr->nCurrentTool--;
1218 infoPtr->uNumTools--;
1220 return 0;
1223 static LRESULT
1224 TOOLTIPS_EnumToolsT (const TOOLTIPS_INFO *infoPtr, UINT uIndex, TTTOOLINFOW *ti,
1225 BOOL isW)
1227 if (!ti || ti->cbSize < TTTOOLINFOW_V1_SIZE)
1228 return FALSE;
1229 if (uIndex >= infoPtr->uNumTools)
1230 return FALSE;
1232 TRACE("index=%u\n", uIndex);
1234 TOOLTIPS_CopyInfoT (infoPtr, uIndex, ti, isW);
1236 return TRUE;
1239 static LRESULT
1240 TOOLTIPS_GetBubbleSize (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
1242 INT nTool;
1243 SIZE size;
1245 if (lpToolInfo == NULL)
1246 return FALSE;
1247 if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1248 return FALSE;
1250 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, lpToolInfo);
1251 if (nTool == -1) return 0;
1253 TRACE("tool %d\n", nTool);
1255 TOOLTIPS_CalcTipSize (infoPtr, &size);
1256 TRACE("size %d x %d\n", size.cx, size.cy);
1258 return MAKELRESULT(size.cx, size.cy);
1261 static LRESULT
1262 TOOLTIPS_GetCurrentToolT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1264 if (ti) {
1265 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1266 return FALSE;
1268 if (infoPtr->nCurrentTool != -1)
1269 TOOLTIPS_CopyInfoT (infoPtr, infoPtr->nCurrentTool, ti, isW);
1272 return infoPtr->nCurrentTool != -1;
1276 static LRESULT
1277 TOOLTIPS_GetDelayTime (const TOOLTIPS_INFO *infoPtr, DWORD duration)
1279 switch (duration) {
1280 case TTDT_RESHOW:
1281 return infoPtr->nReshowTime;
1283 case TTDT_AUTOPOP:
1284 return infoPtr->nAutoPopTime;
1286 case TTDT_INITIAL:
1287 case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1288 return infoPtr->nInitialTime;
1290 default:
1291 WARN("Invalid duration flag %x\n", duration);
1292 break;
1295 return -1;
1299 static LRESULT
1300 TOOLTIPS_GetMargin (const TOOLTIPS_INFO *infoPtr, RECT *rect)
1302 if (rect)
1303 *rect = infoPtr->rcMargin;
1305 return 0;
1309 static inline LRESULT
1310 TOOLTIPS_GetMaxTipWidth (const TOOLTIPS_INFO *infoPtr)
1312 return infoPtr->nMaxTipWidth;
1316 static LRESULT
1317 TOOLTIPS_GetTextT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1319 INT nTool;
1321 if (!ti) return 0;
1322 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1323 return 0;
1325 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1326 if (nTool == -1) return 0;
1328 if (infoPtr->tools[nTool].lpszText == NULL)
1329 return 0;
1331 if (isW) {
1332 ti->lpszText[0] = '\0';
1333 TOOLTIPS_GetTipText(infoPtr, nTool, ti->lpszText);
1335 else {
1336 WCHAR buffer[INFOTIPSIZE];
1338 /* NB this API is broken, there is no way for the app to determine
1339 what size buffer it requires nor a way to specify how long the
1340 one it supplies is. According to the test result, it's up to
1341 80 bytes by the ANSI version. */
1343 buffer[0] = '\0';
1344 TOOLTIPS_GetTipText(infoPtr, nTool, buffer);
1345 WideCharToMultiByte(CP_ACP, 0, buffer, -1, (LPSTR)ti->lpszText,
1346 MAX_TEXT_SIZE_A, NULL, NULL);
1349 return 0;
1353 static inline LRESULT
1354 TOOLTIPS_GetTipBkColor (const TOOLTIPS_INFO *infoPtr)
1356 return infoPtr->clrBk;
1360 static inline LRESULT
1361 TOOLTIPS_GetTipTextColor (const TOOLTIPS_INFO *infoPtr)
1363 return infoPtr->clrText;
1367 static inline LRESULT
1368 TOOLTIPS_GetToolCount (const TOOLTIPS_INFO *infoPtr)
1370 return infoPtr->uNumTools;
1374 static LRESULT
1375 TOOLTIPS_GetToolInfoT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1377 INT nTool;
1378 HWND hwnd;
1380 if (!ti) return FALSE;
1381 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1382 return FALSE;
1383 if (infoPtr->uNumTools == 0)
1384 return FALSE;
1386 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1387 if (nTool == -1)
1388 return FALSE;
1390 TRACE("tool %d\n", nTool);
1392 hwnd = ti->hwnd;
1393 TOOLTIPS_CopyInfoT (infoPtr, nTool, ti, isW);
1394 ti->hwnd = hwnd;
1396 return TRUE;
1400 static LRESULT
1401 TOOLTIPS_HitTestT (const TOOLTIPS_INFO *infoPtr, LPTTHITTESTINFOW lptthit,
1402 BOOL isW)
1404 INT nTool;
1406 if (lptthit == 0)
1407 return FALSE;
1409 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1410 if (nTool == -1)
1411 return FALSE;
1413 TRACE("tool %d\n", nTool);
1415 /* copy tool data */
1416 if (lptthit->ti.cbSize >= TTTOOLINFOW_V1_SIZE)
1417 TOOLTIPS_CopyInfoT (infoPtr, nTool, &lptthit->ti, isW);
1419 return TRUE;
1423 static LRESULT
1424 TOOLTIPS_NewToolRectT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti)
1426 INT nTool;
1428 if (!ti) return 0;
1429 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1430 return FALSE;
1432 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1434 TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&ti->rect));
1436 if (nTool == -1) return 0;
1438 infoPtr->tools[nTool].rect = ti->rect;
1440 return 0;
1444 static inline LRESULT
1445 TOOLTIPS_Pop (TOOLTIPS_INFO *infoPtr)
1447 TOOLTIPS_Hide (infoPtr);
1449 return 0;
1453 static LRESULT
1454 TOOLTIPS_RelayEvent (TOOLTIPS_INFO *infoPtr, LPMSG lpMsg)
1456 POINT pt;
1457 INT nOldTool;
1459 if (!lpMsg) {
1460 ERR("lpMsg == NULL\n");
1461 return 0;
1464 switch (lpMsg->message) {
1465 case WM_LBUTTONDOWN:
1466 case WM_LBUTTONUP:
1467 case WM_MBUTTONDOWN:
1468 case WM_MBUTTONUP:
1469 case WM_RBUTTONDOWN:
1470 case WM_RBUTTONUP:
1471 TOOLTIPS_Hide (infoPtr);
1472 break;
1474 case WM_MOUSEMOVE:
1475 pt.x = (short)LOWORD(lpMsg->lParam);
1476 pt.y = (short)HIWORD(lpMsg->lParam);
1477 nOldTool = infoPtr->nTool;
1478 infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1479 &pt);
1480 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
1481 infoPtr->nTool, infoPtr->nCurrentTool);
1482 TRACE("WM_MOUSEMOVE (%p %s)\n", infoPtr->hwndSelf, wine_dbgstr_point(&pt));
1484 if (infoPtr->nTool != nOldTool) {
1485 if(infoPtr->nTool == -1) { /* Moved out of all tools */
1486 TOOLTIPS_Hide(infoPtr);
1487 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1488 } else if (nOldTool == -1) { /* Moved from outside */
1489 if(infoPtr->bActive) {
1490 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1491 TRACE("timer 1 started\n");
1493 } else { /* Moved from one to another */
1494 TOOLTIPS_Hide (infoPtr);
1495 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1496 if(infoPtr->bActive) {
1497 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1498 TRACE("timer 1 started\n");
1501 } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1502 KillTimer(infoPtr->hwndSelf, ID_TIMERPOP);
1503 SetTimer(infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1504 TRACE("timer 2 restarted\n");
1505 } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1506 /* previous show attempt didn't result in tooltip so try again */
1507 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1508 TRACE("timer 1 started\n");
1510 break;
1513 return 0;
1517 static LRESULT
1518 TOOLTIPS_SetDelayTime (TOOLTIPS_INFO *infoPtr, DWORD duration, INT nTime)
1520 switch (duration) {
1521 case TTDT_AUTOMATIC:
1522 if (nTime <= 0)
1523 nTime = GetDoubleClickTime();
1524 infoPtr->nReshowTime = nTime / 5;
1525 infoPtr->nAutoPopTime = nTime * 10;
1526 infoPtr->nInitialTime = nTime;
1527 break;
1529 case TTDT_RESHOW:
1530 if(nTime < 0)
1531 nTime = GetDoubleClickTime() / 5;
1532 infoPtr->nReshowTime = nTime;
1533 break;
1535 case TTDT_AUTOPOP:
1536 if(nTime < 0)
1537 nTime = GetDoubleClickTime() * 10;
1538 infoPtr->nAutoPopTime = nTime;
1539 break;
1541 case TTDT_INITIAL:
1542 if(nTime < 0)
1543 nTime = GetDoubleClickTime();
1544 infoPtr->nInitialTime = nTime;
1545 break;
1547 default:
1548 WARN("Invalid duration flag %x\n", duration);
1549 break;
1552 return 0;
1556 static LRESULT
1557 TOOLTIPS_SetMargin (TOOLTIPS_INFO *infoPtr, const RECT *rect)
1559 if (rect)
1560 infoPtr->rcMargin = *rect;
1562 return 0;
1566 static inline LRESULT
1567 TOOLTIPS_SetMaxTipWidth (TOOLTIPS_INFO *infoPtr, INT MaxWidth)
1569 INT nTemp = infoPtr->nMaxTipWidth;
1571 infoPtr->nMaxTipWidth = MaxWidth;
1573 return nTemp;
1577 static inline LRESULT
1578 TOOLTIPS_SetTipBkColor (TOOLTIPS_INFO *infoPtr, COLORREF clrBk)
1580 infoPtr->clrBk = clrBk;
1582 return 0;
1586 static inline LRESULT
1587 TOOLTIPS_SetTipTextColor (TOOLTIPS_INFO *infoPtr, COLORREF clrText)
1589 infoPtr->clrText = clrText;
1591 return 0;
1595 static LRESULT
1596 TOOLTIPS_SetTitleT (TOOLTIPS_INFO *infoPtr, UINT_PTR uTitleIcon, LPCWSTR pszTitle,
1597 BOOL isW)
1599 UINT size;
1601 TRACE("hwnd = %p, title = %s, icon = %p\n", infoPtr->hwndSelf, debugstr_w(pszTitle),
1602 (void*)uTitleIcon);
1604 Free(infoPtr->pszTitle);
1606 if (pszTitle)
1608 if (isW)
1610 size = (strlenW(pszTitle)+1)*sizeof(WCHAR);
1611 infoPtr->pszTitle = Alloc(size);
1612 if (!infoPtr->pszTitle)
1613 return FALSE;
1614 memcpy(infoPtr->pszTitle, pszTitle, size);
1616 else
1618 size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, NULL, 0);
1619 infoPtr->pszTitle = Alloc(size);
1620 if (!infoPtr->pszTitle)
1621 return FALSE;
1622 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1625 else
1626 infoPtr->pszTitle = NULL;
1628 if (uTitleIcon <= TTI_ERROR)
1629 infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1630 else
1631 infoPtr->hTitleIcon = CopyIcon((HICON)uTitleIcon);
1633 TRACE("icon = %p\n", infoPtr->hTitleIcon);
1635 return TRUE;
1639 static LRESULT
1640 TOOLTIPS_SetToolInfoT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1642 TTTOOL_INFO *toolPtr;
1643 INT nTool;
1645 if (!ti) return 0;
1646 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1647 return 0;
1649 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1650 if (nTool == -1) return 0;
1652 TRACE("tool %d\n", nTool);
1654 toolPtr = &infoPtr->tools[nTool];
1656 /* copy tool data */
1657 toolPtr->uFlags = ti->uFlags;
1658 toolPtr->hwnd = ti->hwnd;
1659 toolPtr->uId = ti->uId;
1660 toolPtr->rect = ti->rect;
1661 toolPtr->hinst = ti->hinst;
1663 if (IS_INTRESOURCE(ti->lpszText)) {
1664 TRACE("set string id %x\n", LOWORD(ti->lpszText));
1665 toolPtr->lpszText = ti->lpszText;
1667 else {
1668 if (ti->lpszText == LPSTR_TEXTCALLBACKW)
1669 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1670 else {
1671 if ( (toolPtr->lpszText) &&
1672 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1673 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1674 Free (toolPtr->lpszText);
1675 toolPtr->lpszText = NULL;
1677 if (ti->lpszText) {
1678 if (isW) {
1679 INT len = lstrlenW (ti->lpszText);
1680 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1681 strcpyW (toolPtr->lpszText, ti->lpszText);
1683 else {
1684 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1685 -1, NULL, 0);
1686 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1687 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1688 toolPtr->lpszText, len);
1694 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1695 toolPtr->lParam = ti->lParam;
1697 if (infoPtr->nCurrentTool == nTool)
1699 TOOLTIPS_GetTipText (infoPtr, infoPtr->nCurrentTool, infoPtr->szTipText);
1701 if (infoPtr->szTipText[0] == 0)
1702 TOOLTIPS_Hide(infoPtr);
1703 else
1704 TOOLTIPS_Show (infoPtr, FALSE);
1707 return 0;
1711 static LRESULT
1712 TOOLTIPS_TrackActivate (TOOLTIPS_INFO *infoPtr, BOOL track_activate, const TTTOOLINFOA *ti)
1714 if (track_activate) {
1716 if (!ti) return 0;
1717 if (ti->cbSize < TTTOOLINFOA_V1_SIZE)
1718 return FALSE;
1720 /* activate */
1721 infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoT (infoPtr, (const TTTOOLINFOW*)ti);
1722 if (infoPtr->nTrackTool != -1) {
1723 TRACE("activated\n");
1724 infoPtr->bTrackActive = TRUE;
1725 TOOLTIPS_TrackShow (infoPtr);
1728 else {
1729 /* deactivate */
1730 TOOLTIPS_TrackHide (infoPtr);
1732 infoPtr->bTrackActive = FALSE;
1733 infoPtr->nTrackTool = -1;
1735 TRACE("deactivated\n");
1738 return 0;
1742 static LRESULT
1743 TOOLTIPS_TrackPosition (TOOLTIPS_INFO *infoPtr, LPARAM coord)
1745 infoPtr->xTrackPos = (INT)LOWORD(coord);
1746 infoPtr->yTrackPos = (INT)HIWORD(coord);
1748 if (infoPtr->bTrackActive) {
1749 TRACE("[%d %d]\n",
1750 infoPtr->xTrackPos, infoPtr->yTrackPos);
1752 TOOLTIPS_TrackShow (infoPtr);
1755 return 0;
1759 static LRESULT
1760 TOOLTIPS_Update (TOOLTIPS_INFO *infoPtr)
1762 if (infoPtr->nCurrentTool != -1)
1763 UpdateWindow (infoPtr->hwndSelf);
1765 return 0;
1769 static LRESULT
1770 TOOLTIPS_UpdateTipTextT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1772 TTTOOL_INFO *toolPtr;
1773 INT nTool;
1775 if (!ti) return 0;
1776 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1777 return FALSE;
1779 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1780 if (nTool == -1)
1781 return 0;
1783 TRACE("tool %d\n", nTool);
1785 toolPtr = &infoPtr->tools[nTool];
1787 /* copy tool text */
1788 toolPtr->hinst = ti->hinst;
1790 if (IS_INTRESOURCE(ti->lpszText)){
1791 toolPtr->lpszText = ti->lpszText;
1793 else if (ti->lpszText) {
1794 if (ti->lpszText == LPSTR_TEXTCALLBACKW)
1795 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1796 else {
1797 if ( (toolPtr->lpszText) &&
1798 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1799 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1800 Free (toolPtr->lpszText);
1801 toolPtr->lpszText = NULL;
1803 if (ti->lpszText) {
1804 if (isW) {
1805 INT len = lstrlenW (ti->lpszText);
1806 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1807 strcpyW (toolPtr->lpszText, ti->lpszText);
1809 else {
1810 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1811 -1, NULL, 0);
1812 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1813 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1814 toolPtr->lpszText, len);
1820 if(infoPtr->nCurrentTool == -1) return 0;
1821 /* force repaint */
1822 if (infoPtr->bActive)
1823 TOOLTIPS_Show (infoPtr, FALSE);
1824 else if (infoPtr->bTrackActive)
1825 TOOLTIPS_Show (infoPtr, TRUE);
1827 return 0;
1831 static LRESULT
1832 TOOLTIPS_Create (HWND hwnd)
1834 TOOLTIPS_INFO *infoPtr;
1836 /* allocate memory for info structure */
1837 infoPtr = Alloc (sizeof(TOOLTIPS_INFO));
1838 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1840 /* initialize info structure */
1841 infoPtr->bActive = TRUE;
1842 infoPtr->bTrackActive = FALSE;
1844 infoPtr->nMaxTipWidth = -1;
1845 infoPtr->nTool = -1;
1846 infoPtr->nCurrentTool = -1;
1847 infoPtr->nTrackTool = -1;
1848 infoPtr->hwndSelf = hwnd;
1850 /* initialize colours and fonts */
1851 TOOLTIPS_InitSystemSettings(infoPtr);
1853 TOOLTIPS_SetDelayTime(infoPtr, TTDT_AUTOMATIC, 0);
1855 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
1857 return 0;
1861 static LRESULT
1862 TOOLTIPS_Destroy (TOOLTIPS_INFO *infoPtr)
1864 TTTOOL_INFO *toolPtr;
1865 UINT i;
1867 /* free tools */
1868 if (infoPtr->tools) {
1869 for (i = 0; i < infoPtr->uNumTools; i++) {
1870 toolPtr = &infoPtr->tools[i];
1871 if (toolPtr->lpszText) {
1872 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1873 !IS_INTRESOURCE(toolPtr->lpszText) )
1875 Free (toolPtr->lpszText);
1876 toolPtr->lpszText = NULL;
1880 TOOLTIPS_ResetSubclass (toolPtr);
1883 Free (infoPtr->tools);
1886 /* free title string */
1887 Free (infoPtr->pszTitle);
1888 /* free title icon if not a standard one */
1889 if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
1890 DeleteObject(infoPtr->hTitleIcon);
1892 /* delete fonts */
1893 DeleteObject (infoPtr->hFont);
1894 DeleteObject (infoPtr->hTitleFont);
1896 /* free tool tips info data */
1897 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1898 Free (infoPtr);
1900 return 0;
1904 static inline LRESULT
1905 TOOLTIPS_GetFont (const TOOLTIPS_INFO *infoPtr)
1907 return (LRESULT)infoPtr->hFont;
1911 static LRESULT
1912 TOOLTIPS_MouseMessage (TOOLTIPS_INFO *infoPtr)
1914 TOOLTIPS_Hide (infoPtr);
1916 return 0;
1920 static LRESULT
1921 TOOLTIPS_NCCreate (HWND hwnd)
1923 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1924 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
1926 dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
1927 dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
1929 /* WS_BORDER only draws a border round the window rect, not the
1930 * window region, therefore it is useless to us in balloon mode */
1931 if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
1933 SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
1935 dwExStyle |= WS_EX_TOOLWINDOW;
1936 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
1938 return TRUE;
1942 static LRESULT
1943 TOOLTIPS_NCHitTest (const TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1945 INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
1947 TRACE(" nTool=%d\n", nTool);
1949 if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
1950 if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
1951 TRACE("-- in transparent mode\n");
1952 return HTTRANSPARENT;
1956 return DefWindowProcW (infoPtr->hwndSelf, WM_NCHITTEST, wParam, lParam);
1960 static LRESULT
1961 TOOLTIPS_NotifyFormat (TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1963 FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", infoPtr->hwndSelf, wParam, lParam);
1965 return 0;
1969 static LRESULT
1970 TOOLTIPS_Paint (const TOOLTIPS_INFO *infoPtr, HDC hDC)
1972 HDC hdc;
1973 PAINTSTRUCT ps;
1975 hdc = (hDC == NULL) ? BeginPaint (infoPtr->hwndSelf, &ps) : hDC;
1976 TOOLTIPS_Refresh (infoPtr, hdc);
1977 if (!hDC)
1978 EndPaint (infoPtr->hwndSelf, &ps);
1979 return 0;
1983 static LRESULT
1984 TOOLTIPS_SetFont (TOOLTIPS_INFO *infoPtr, HFONT hFont, BOOL redraw)
1986 LOGFONTW lf;
1988 if(!GetObjectW(hFont, sizeof(lf), &lf))
1989 return 0;
1991 DeleteObject (infoPtr->hFont);
1992 infoPtr->hFont = CreateFontIndirectW(&lf);
1994 DeleteObject (infoPtr->hTitleFont);
1995 lf.lfWeight = FW_BOLD;
1996 infoPtr->hTitleFont = CreateFontIndirectW(&lf);
1998 if (redraw && infoPtr->nCurrentTool != -1)
1999 FIXME("full redraw needed\n");
2001 return 0;
2004 /******************************************************************
2005 * TOOLTIPS_GetTextLength
2007 * This function is called when the tooltip receive a
2008 * WM_GETTEXTLENGTH message.
2010 * returns the length, in characters, of the tip text
2012 static inline LRESULT
2013 TOOLTIPS_GetTextLength(const TOOLTIPS_INFO *infoPtr)
2015 return strlenW(infoPtr->szTipText);
2018 /******************************************************************
2019 * TOOLTIPS_OnWMGetText
2021 * This function is called when the tooltip receive a
2022 * WM_GETTEXT message.
2023 * wParam : specifies the maximum number of characters to be copied
2024 * lParam : is the pointer to the buffer that will receive
2025 * the tip text
2027 * returns the number of characters copied
2029 static LRESULT
2030 TOOLTIPS_OnWMGetText (const TOOLTIPS_INFO *infoPtr, WPARAM size, LPWSTR pszText)
2032 LRESULT res;
2034 if(!size)
2035 return 0;
2037 res = min(strlenW(infoPtr->szTipText)+1, size);
2038 memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2039 pszText[res-1] = '\0';
2040 return res-1;
2043 static LRESULT
2044 TOOLTIPS_Timer (TOOLTIPS_INFO *infoPtr, INT iTimer)
2046 INT nOldTool;
2048 TRACE("timer %d (%p) expired\n", iTimer, infoPtr->hwndSelf);
2050 switch (iTimer) {
2051 case ID_TIMERSHOW:
2052 KillTimer (infoPtr->hwndSelf, ID_TIMERSHOW);
2053 nOldTool = infoPtr->nTool;
2054 if ((infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, TRUE)) == nOldTool)
2055 TOOLTIPS_Show (infoPtr, FALSE);
2056 break;
2058 case ID_TIMERPOP:
2059 TOOLTIPS_Hide (infoPtr);
2060 break;
2062 case ID_TIMERLEAVE:
2063 nOldTool = infoPtr->nTool;
2064 infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, FALSE);
2065 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
2066 infoPtr->nTool, infoPtr->nCurrentTool);
2067 if (infoPtr->nTool != nOldTool) {
2068 if(infoPtr->nTool == -1) { /* Moved out of all tools */
2069 TOOLTIPS_Hide(infoPtr);
2070 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2071 } else if (nOldTool == -1) { /* Moved from outside */
2072 ERR("How did this happen?\n");
2073 } else { /* Moved from one to another */
2074 TOOLTIPS_Hide (infoPtr);
2075 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2076 if(infoPtr->bActive) {
2077 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2078 TRACE("timer 1 started!\n");
2082 break;
2084 default:
2085 ERR("Unknown timer id %d\n", iTimer);
2086 break;
2088 return 0;
2092 static LRESULT
2093 TOOLTIPS_WinIniChange (TOOLTIPS_INFO *infoPtr)
2095 TOOLTIPS_InitSystemSettings (infoPtr);
2097 return 0;
2101 static LRESULT CALLBACK
2102 TOOLTIPS_SubclassProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2104 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr ((HWND)dwRef);
2105 MSG msg;
2107 switch (message)
2109 case WM_MOUSEMOVE:
2110 case WM_LBUTTONDOWN:
2111 case WM_LBUTTONUP:
2112 case WM_MBUTTONDOWN:
2113 case WM_MBUTTONUP:
2114 case WM_RBUTTONDOWN:
2115 case WM_RBUTTONUP:
2116 if (infoPtr)
2118 msg.hwnd = hwnd;
2119 msg.message = message;
2120 msg.wParam = wParam;
2121 msg.lParam = lParam;
2122 TOOLTIPS_RelayEvent(infoPtr, &msg);
2124 break;
2125 case WM_NCDESTROY:
2126 RemoveWindowSubclass(hwnd, TOOLTIPS_SubclassProc, 1);
2127 break;
2128 default:
2129 break;
2132 return DefSubclassProc(hwnd, message, wParam, lParam);
2136 static LRESULT CALLBACK
2137 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2139 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2141 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2142 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2143 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2144 switch (uMsg)
2146 case TTM_ACTIVATE:
2147 return TOOLTIPS_Activate (infoPtr, (BOOL)wParam);
2149 case TTM_ADDTOOLA:
2150 case TTM_ADDTOOLW:
2151 return TOOLTIPS_AddToolT (infoPtr, (LPTTTOOLINFOW)lParam, uMsg == TTM_ADDTOOLW);
2153 case TTM_DELTOOLA:
2154 case TTM_DELTOOLW:
2155 return TOOLTIPS_DelToolT (infoPtr, (LPTOOLINFOW)lParam,
2156 uMsg == TTM_DELTOOLW);
2157 case TTM_ENUMTOOLSA:
2158 case TTM_ENUMTOOLSW:
2159 return TOOLTIPS_EnumToolsT (infoPtr, (UINT)wParam, (LPTTTOOLINFOW)lParam,
2160 uMsg == TTM_ENUMTOOLSW);
2161 case TTM_GETBUBBLESIZE:
2162 return TOOLTIPS_GetBubbleSize (infoPtr, (LPTTTOOLINFOW)lParam);
2164 case TTM_GETCURRENTTOOLA:
2165 case TTM_GETCURRENTTOOLW:
2166 return TOOLTIPS_GetCurrentToolT (infoPtr, (LPTTTOOLINFOW)lParam,
2167 uMsg == TTM_GETCURRENTTOOLW);
2169 case TTM_GETDELAYTIME:
2170 return TOOLTIPS_GetDelayTime (infoPtr, (DWORD)wParam);
2172 case TTM_GETMARGIN:
2173 return TOOLTIPS_GetMargin (infoPtr, (LPRECT)lParam);
2175 case TTM_GETMAXTIPWIDTH:
2176 return TOOLTIPS_GetMaxTipWidth (infoPtr);
2178 case TTM_GETTEXTA:
2179 case TTM_GETTEXTW:
2180 return TOOLTIPS_GetTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2181 uMsg == TTM_GETTEXTW);
2183 case TTM_GETTIPBKCOLOR:
2184 return TOOLTIPS_GetTipBkColor (infoPtr);
2186 case TTM_GETTIPTEXTCOLOR:
2187 return TOOLTIPS_GetTipTextColor (infoPtr);
2189 case TTM_GETTOOLCOUNT:
2190 return TOOLTIPS_GetToolCount (infoPtr);
2192 case TTM_GETTOOLINFOA:
2193 case TTM_GETTOOLINFOW:
2194 return TOOLTIPS_GetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2195 uMsg == TTM_GETTOOLINFOW);
2197 case TTM_HITTESTA:
2198 case TTM_HITTESTW:
2199 return TOOLTIPS_HitTestT (infoPtr, (LPTTHITTESTINFOW)lParam,
2200 uMsg == TTM_HITTESTW);
2201 case TTM_NEWTOOLRECTA:
2202 case TTM_NEWTOOLRECTW:
2203 return TOOLTIPS_NewToolRectT (infoPtr, (LPTTTOOLINFOW)lParam);
2205 case TTM_POP:
2206 return TOOLTIPS_Pop (infoPtr);
2208 case TTM_RELAYEVENT:
2209 return TOOLTIPS_RelayEvent (infoPtr, (LPMSG)lParam);
2211 case TTM_SETDELAYTIME:
2212 return TOOLTIPS_SetDelayTime (infoPtr, (DWORD)wParam, (INT)LOWORD(lParam));
2214 case TTM_SETMARGIN:
2215 return TOOLTIPS_SetMargin (infoPtr, (LPRECT)lParam);
2217 case TTM_SETMAXTIPWIDTH:
2218 return TOOLTIPS_SetMaxTipWidth (infoPtr, (INT)lParam);
2220 case TTM_SETTIPBKCOLOR:
2221 return TOOLTIPS_SetTipBkColor (infoPtr, (COLORREF)wParam);
2223 case TTM_SETTIPTEXTCOLOR:
2224 return TOOLTIPS_SetTipTextColor (infoPtr, (COLORREF)wParam);
2226 case TTM_SETTITLEA:
2227 case TTM_SETTITLEW:
2228 return TOOLTIPS_SetTitleT (infoPtr, (UINT_PTR)wParam, (LPCWSTR)lParam,
2229 uMsg == TTM_SETTITLEW);
2231 case TTM_SETTOOLINFOA:
2232 case TTM_SETTOOLINFOW:
2233 return TOOLTIPS_SetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2234 uMsg == TTM_SETTOOLINFOW);
2236 case TTM_TRACKACTIVATE:
2237 return TOOLTIPS_TrackActivate (infoPtr, (BOOL)wParam, (LPTTTOOLINFOA)lParam);
2239 case TTM_TRACKPOSITION:
2240 return TOOLTIPS_TrackPosition (infoPtr, lParam);
2242 case TTM_UPDATE:
2243 return TOOLTIPS_Update (infoPtr);
2245 case TTM_UPDATETIPTEXTA:
2246 case TTM_UPDATETIPTEXTW:
2247 return TOOLTIPS_UpdateTipTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2248 uMsg == TTM_UPDATETIPTEXTW);
2250 case TTM_WINDOWFROMPOINT:
2251 return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2253 case WM_CREATE:
2254 return TOOLTIPS_Create (hwnd);
2256 case WM_DESTROY:
2257 return TOOLTIPS_Destroy (infoPtr);
2259 case WM_ERASEBKGND:
2260 /* we draw the background in WM_PAINT */
2261 return 0;
2263 case WM_GETFONT:
2264 return TOOLTIPS_GetFont (infoPtr);
2266 case WM_GETTEXT:
2267 return TOOLTIPS_OnWMGetText (infoPtr, wParam, (LPWSTR)lParam);
2269 case WM_GETTEXTLENGTH:
2270 return TOOLTIPS_GetTextLength (infoPtr);
2272 case WM_LBUTTONDOWN:
2273 case WM_LBUTTONUP:
2274 case WM_MBUTTONDOWN:
2275 case WM_MBUTTONUP:
2276 case WM_RBUTTONDOWN:
2277 case WM_RBUTTONUP:
2278 case WM_MOUSEMOVE:
2279 return TOOLTIPS_MouseMessage (infoPtr);
2281 case WM_NCCREATE:
2282 return TOOLTIPS_NCCreate (hwnd);
2284 case WM_NCHITTEST:
2285 return TOOLTIPS_NCHitTest (infoPtr, wParam, lParam);
2287 case WM_NOTIFYFORMAT:
2288 return TOOLTIPS_NotifyFormat (infoPtr, wParam, lParam);
2290 case WM_PRINTCLIENT:
2291 case WM_PAINT:
2292 return TOOLTIPS_Paint (infoPtr, (HDC)wParam);
2294 case WM_SETFONT:
2295 return TOOLTIPS_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
2297 case WM_SYSCOLORCHANGE:
2298 COMCTL32_RefreshSysColors();
2299 return 0;
2301 case WM_TIMER:
2302 return TOOLTIPS_Timer (infoPtr, (INT)wParam);
2304 case WM_WININICHANGE:
2305 return TOOLTIPS_WinIniChange (infoPtr);
2307 default:
2308 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2309 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2310 uMsg, wParam, lParam);
2311 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2316 VOID
2317 TOOLTIPS_Register (void)
2319 WNDCLASSW wndClass;
2321 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2322 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2323 wndClass.lpfnWndProc = TOOLTIPS_WindowProc;
2324 wndClass.cbClsExtra = 0;
2325 wndClass.cbWndExtra = sizeof(TOOLTIPS_INFO *);
2326 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2327 wndClass.hbrBackground = 0;
2328 wndClass.lpszClassName = TOOLTIPS_CLASSW;
2330 RegisterClassW (&wndClass);
2332 hTooltipIcons[TTI_NONE] = NULL;
2333 hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2334 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2335 hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2336 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2337 hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2338 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2342 VOID
2343 TOOLTIPS_Unregister (void)
2345 int i;
2346 for (i = TTI_INFO; i <= TTI_ERROR; i++)
2347 DestroyIcon(hTooltipIcons[i]);
2348 UnregisterClassW (TOOLTIPS_CLASSW, NULL);