include: Use the hard-float calling convention for Windows APIs on ARM
[wine.git] / dlls / comctl32 / tooltips.c
blobbbf1693f4d2712bc69cb7ef874b6e96dc5cad4ed
1 /*
2 * Tool tip control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 2004 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Sep. 08, 2004, by Robert Shearman.
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features or bugs please note them below.
30 * TODO:
31 * - Custom draw support.
32 * - Animation.
33 * - Links.
34 * - Messages:
35 * o TTM_ADJUSTRECT
36 * o TTM_GETTITLEA
37 * o TTM_GETTTILEW
38 * o TTM_POPUP
39 * - Styles:
40 * o TTS_NOANIMATE
41 * o TTS_NOFADE
42 * o TTS_CLOSE
44 * Testing:
45 * - Run tests using Waite Group Windows95 API Bible Volume 2.
46 * The second cdrom (chapter 3) contains executables activate.exe,
47 * curtool.exe, deltool.exe, enumtools.exe, getinfo.exe, getiptxt.exe,
48 * hittest.exe, needtext.exe, newrect.exe, updtext.exe and winfrpt.exe.
50 * Timer logic.
52 * One important point to remember is that tools don't necessarily get
53 * a WM_MOUSEMOVE once the cursor leaves the tool, an example is when
54 * a tool sets TTF_IDISHWND (i.e. an entire window is a tool) because
55 * here WM_MOUSEMOVEs only get sent when the cursor is inside the
56 * client area. Therefore the only reliable way to know that the
57 * cursor has left a tool is to keep a timer running and check the
58 * position every time it expires. This is the role of timer
59 * ID_TIMERLEAVE.
62 * On entering a tool (detected in a relayed WM_MOUSEMOVE) we start
63 * ID_TIMERSHOW, if this times out and we're still in the tool we show
64 * the tip. On showing a tip we start both ID_TIMERPOP and
65 * ID_TIMERLEAVE. On hiding a tooltip we kill ID_TIMERPOP.
66 * ID_TIMERPOP is restarted on every relayed WM_MOUSEMOVE. If
67 * ID_TIMERPOP expires the tool is hidden and ID_TIMERPOP is killed.
68 * ID_TIMERLEAVE remains running - this is important as we need to
69 * determine when the cursor leaves the tool.
71 * When ID_TIMERLEAVE expires or on a relayed WM_MOUSEMOVE if we're
72 * still in the tool do nothing (apart from restart ID_TIMERPOP if
73 * this is a WM_MOUSEMOVE) (ID_TIMERLEAVE remains running). If we've
74 * left the tool and entered another one then hide the tip and start
75 * ID_TIMERSHOW with time ReshowTime and kill ID_TIMERLEAVE. If we're
76 * outside all tools hide the tip and kill ID_TIMERLEAVE. On Relayed
77 * mouse button messages hide the tip but leave ID_TIMERLEAVE running,
78 * this again will let us keep track of when the cursor leaves the
79 * tool.
82 * infoPtr->nTool is the tool the mouse was on on the last relayed MM
83 * or timer expiry or -1 if the mouse was not on a tool.
85 * infoPtr->nCurrentTool is the tool for which the tip is currently
86 * displaying text for or -1 if the tip is not shown. Actually this
87 * will only ever be infoPtr-nTool or -1, so it could be changed to a
88 * BOOL.
94 #include <stdarg.h>
95 #include <string.h>
97 #include "windef.h"
98 #include "winbase.h"
99 #include "wine/unicode.h"
100 #include "wingdi.h"
101 #include "winuser.h"
102 #include "winnls.h"
103 #include "commctrl.h"
104 #include "comctl32.h"
105 #include "wine/debug.h"
107 WINE_DEFAULT_DEBUG_CHANNEL(tooltips);
109 static HICON hTooltipIcons[TTI_ERROR+1];
111 typedef struct
113 UINT uFlags;
114 UINT uInternalFlags;
115 HWND hwnd;
116 BOOL bNotifyUnicode;
117 UINT_PTR uId;
118 RECT rect;
119 HINSTANCE hinst;
120 LPWSTR lpszText;
121 LPARAM lParam;
122 } TTTOOL_INFO;
125 typedef struct
127 HWND hwndSelf;
128 WCHAR szTipText[INFOTIPSIZE];
129 BOOL bActive;
130 BOOL bTrackActive;
131 UINT uNumTools;
132 COLORREF clrBk;
133 COLORREF clrText;
134 HFONT hFont;
135 HFONT hTitleFont;
136 INT xTrackPos;
137 INT yTrackPos;
138 INT nMaxTipWidth;
139 INT nTool; /* tool that mouse was on on last relayed mouse move */
140 INT nCurrentTool;
141 INT nTrackTool;
142 INT nReshowTime;
143 INT nAutoPopTime;
144 INT nInitialTime;
145 RECT rcMargin;
146 BOOL bToolBelow;
147 LPWSTR pszTitle;
148 HICON hTitleIcon;
150 TTTOOL_INFO *tools;
151 } TOOLTIPS_INFO;
153 #define ID_TIMERSHOW 1 /* show delay timer */
154 #define ID_TIMERPOP 2 /* auto pop timer */
155 #define ID_TIMERLEAVE 3 /* tool leave timer */
158 #define TOOLTIPS_GetInfoPtr(hWindow) ((TOOLTIPS_INFO *)GetWindowLongPtrW (hWindow, 0))
160 /* offsets from window edge to start of text */
161 #define NORMAL_TEXT_MARGIN 2
162 #define BALLOON_TEXT_MARGIN (NORMAL_TEXT_MARGIN+8)
163 /* value used for CreateRoundRectRgn that specifies how much
164 * each corner is curved */
165 #define BALLOON_ROUNDEDNESS 20
166 #define BALLOON_STEMHEIGHT 13
167 #define BALLOON_STEMWIDTH 10
168 #define BALLOON_STEMINDENT 20
170 #define BALLOON_ICON_TITLE_SPACING 8 /* horizontal spacing between icon and title */
171 #define BALLOON_TITLE_TEXT_SPACING 8 /* vertical spacing between icon/title and main text */
172 #define ICON_HEIGHT 16
173 #define ICON_WIDTH 16
175 #define MAX_TEXT_SIZE_A 80 /* maximum retrieving text size by ANSI message */
177 static LRESULT CALLBACK
178 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uId, DWORD_PTR dwRef);
181 static inline BOOL TOOLTIPS_IsCallbackString(LPCWSTR str, BOOL isW)
183 if (isW)
184 return str == LPSTR_TEXTCALLBACKW;
185 else
186 return (LPCSTR)str == LPSTR_TEXTCALLBACKA;
189 static inline UINT_PTR
190 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
192 UINT i;
193 for (i = 0; i <= TTI_ERROR; i++)
194 if (hTooltipIcons[i] == hIcon)
195 return i;
196 return (UINT_PTR)hIcon;
199 static void
200 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
202 NONCLIENTMETRICSW nclm;
204 infoPtr->clrBk = comctl32_color.clrInfoBk;
205 infoPtr->clrText = comctl32_color.clrInfoText;
207 DeleteObject (infoPtr->hFont);
208 nclm.cbSize = sizeof(nclm);
209 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
210 infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
212 DeleteObject (infoPtr->hTitleFont);
213 nclm.lfStatusFont.lfWeight = FW_BOLD;
214 infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
217 /* Custom draw routines */
218 static void
219 TOOLTIPS_customdraw_fill(const TOOLTIPS_INFO *infoPtr, NMTTCUSTOMDRAW *lpnmttcd,
220 HDC hdc, const RECT *rcBounds, UINT uFlags)
222 ZeroMemory(lpnmttcd, sizeof(NMTTCUSTOMDRAW));
223 lpnmttcd->uDrawFlags = uFlags;
224 lpnmttcd->nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
225 lpnmttcd->nmcd.hdr.code = NM_CUSTOMDRAW;
226 if (infoPtr->nCurrentTool != -1) {
227 TTTOOL_INFO *toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
228 lpnmttcd->nmcd.hdr.idFrom = toolPtr->uId;
230 lpnmttcd->nmcd.hdc = hdc;
231 lpnmttcd->nmcd.rc = *rcBounds;
232 /* FIXME - dwItemSpec, uItemState, lItemlParam */
235 static inline DWORD
236 TOOLTIPS_notify_customdraw (DWORD dwDrawStage, NMTTCUSTOMDRAW *lpnmttcd)
238 LRESULT result;
239 lpnmttcd->nmcd.dwDrawStage = dwDrawStage;
241 TRACE("Notifying stage %d, flags %x, id %x\n", lpnmttcd->nmcd.dwDrawStage,
242 lpnmttcd->uDrawFlags, lpnmttcd->nmcd.hdr.code);
244 result = SendMessageW(GetParent(lpnmttcd->nmcd.hdr.hwndFrom), WM_NOTIFY,
245 0, (LPARAM)lpnmttcd);
247 TRACE("Notify result %x\n", (unsigned int)result);
249 return result;
252 static void
253 TOOLTIPS_Refresh (const TOOLTIPS_INFO *infoPtr, HDC hdc)
255 RECT rc;
256 INT oldBkMode;
257 HFONT hOldFont;
258 HBRUSH hBrush;
259 UINT uFlags = DT_EXTERNALLEADING;
260 HRGN hRgn = NULL;
261 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
262 NMTTCUSTOMDRAW nmttcd;
263 DWORD cdmode;
265 if (infoPtr->nMaxTipWidth > -1)
266 uFlags |= DT_WORDBREAK;
267 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)
268 uFlags |= DT_NOPREFIX;
269 GetClientRect (infoPtr->hwndSelf, &rc);
271 hBrush = CreateSolidBrush(infoPtr->clrBk);
273 oldBkMode = SetBkMode (hdc, TRANSPARENT);
274 SetTextColor (hdc, infoPtr->clrText);
275 hOldFont = SelectObject (hdc, infoPtr->hFont);
277 /* Custom draw - Call PrePaint once initial properties set up */
278 /* Note: Contrary to MSDN, CDRF_SKIPDEFAULT still draws a tooltip */
279 TOOLTIPS_customdraw_fill(infoPtr, &nmttcd, hdc, &rc, uFlags);
280 cdmode = TOOLTIPS_notify_customdraw(CDDS_PREPAINT, &nmttcd);
281 uFlags = nmttcd.uDrawFlags;
283 if (dwStyle & TTS_BALLOON)
285 /* create a region to store result into */
286 hRgn = CreateRectRgn(0, 0, 0, 0);
288 GetWindowRgn(infoPtr->hwndSelf, hRgn);
290 /* fill the background */
291 FillRgn(hdc, hRgn, hBrush);
292 DeleteObject(hBrush);
293 hBrush = NULL;
295 else
297 /* fill the background */
298 FillRect(hdc, &rc, hBrush);
299 DeleteObject(hBrush);
300 hBrush = NULL;
303 if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
305 /* calculate text rectangle */
306 rc.left += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
307 rc.top += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
308 rc.right -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
309 rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
310 if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
312 if (infoPtr->pszTitle)
314 RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
315 int height;
316 BOOL icon_present;
317 HFONT prevFont;
319 /* draw icon */
320 icon_present = infoPtr->hTitleIcon &&
321 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
322 ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
323 if (icon_present)
324 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
326 rcTitle.bottom = rc.top + ICON_HEIGHT;
328 /* draw title text */
329 prevFont = SelectObject (hdc, infoPtr->hTitleFont);
330 height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
331 SelectObject (hdc, prevFont);
332 rc.top += height + BALLOON_TITLE_TEXT_SPACING;
335 else
337 /* calculate text rectangle */
338 rc.left += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
339 rc.top += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
340 rc.right -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
341 rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
344 /* draw text */
345 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
347 /* Custom draw - Call PostPaint after drawing */
348 if (cdmode & CDRF_NOTIFYPOSTPAINT) {
349 TOOLTIPS_notify_customdraw(CDDS_POSTPAINT, &nmttcd);
352 /* be polite and reset the things we changed in the dc */
353 SelectObject (hdc, hOldFont);
354 SetBkMode (hdc, oldBkMode);
356 if (dwStyle & TTS_BALLOON)
358 /* frame region because default window proc doesn't do it */
359 INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
360 INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
362 hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
363 FrameRgn(hdc, hRgn, hBrush, width, height);
366 if (hRgn)
367 DeleteObject(hRgn);
370 static void TOOLTIPS_GetDispInfoA(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
372 NMTTDISPINFOA ttnmdi;
374 /* fill NMHDR struct */
375 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
376 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
377 ttnmdi.hdr.idFrom = toolPtr->uId;
378 ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
379 ttnmdi.lpszText = ttnmdi.szText;
380 ttnmdi.uFlags = toolPtr->uFlags;
381 ttnmdi.lParam = toolPtr->lParam;
383 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
384 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
386 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
387 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
388 buffer, INFOTIPSIZE);
389 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
390 toolPtr->hinst = ttnmdi.hinst;
391 toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
394 else if (ttnmdi.lpszText == 0) {
395 buffer[0] = '\0';
397 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
398 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
399 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
400 toolPtr->hinst = 0;
401 toolPtr->lpszText = NULL;
402 Str_SetPtrW(&toolPtr->lpszText, buffer);
405 else {
406 ERR("recursive text callback\n");
407 buffer[0] = '\0';
410 /* no text available - try calling parent instead as per native */
411 /* FIXME: Unsure if SETITEM should save the value or not */
412 if (buffer[0] == 0x00) {
414 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
416 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
417 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
418 buffer, INFOTIPSIZE);
419 } else if (ttnmdi.lpszText &&
420 ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
421 Str_GetPtrAtoW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
426 static void TOOLTIPS_GetDispInfoW(const TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr, WCHAR *buffer)
428 NMTTDISPINFOW ttnmdi;
430 /* fill NMHDR struct */
431 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
432 ttnmdi.hdr.hwndFrom = infoPtr->hwndSelf;
433 ttnmdi.hdr.idFrom = toolPtr->uId;
434 ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
435 ttnmdi.lpszText = ttnmdi.szText;
436 ttnmdi.uFlags = toolPtr->uFlags;
437 ttnmdi.lParam = toolPtr->lParam;
439 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
440 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
442 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
443 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
444 buffer, INFOTIPSIZE);
445 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
446 toolPtr->hinst = ttnmdi.hinst;
447 toolPtr->lpszText = ttnmdi.lpszText;
450 else if (ttnmdi.lpszText == 0) {
451 buffer[0] = '\0';
453 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
454 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
455 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
456 toolPtr->hinst = 0;
457 toolPtr->lpszText = NULL;
458 Str_SetPtrW(&toolPtr->lpszText, buffer);
461 else {
462 ERR("recursive text callback\n");
463 buffer[0] = '\0';
466 /* no text available - try calling parent instead as per native */
467 /* FIXME: Unsure if SETITEM should save the value or not */
468 if (buffer[0] == 0x00) {
470 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
472 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
473 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
474 buffer, INFOTIPSIZE);
475 } else if (ttnmdi.lpszText &&
476 ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
477 Str_GetPtrW(ttnmdi.lpszText, buffer, INFOTIPSIZE);
483 static void
484 TOOLTIPS_GetTipText (const TOOLTIPS_INFO *infoPtr, INT nTool, WCHAR *buffer)
486 TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
488 if (IS_INTRESOURCE(toolPtr->lpszText)) {
489 /* load a resource */
490 TRACE("load res string %p %x\n",
491 toolPtr->hinst, LOWORD(toolPtr->lpszText));
492 if (!LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText), buffer, INFOTIPSIZE))
493 buffer[0] = '\0';
495 else if (toolPtr->lpszText) {
496 if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
497 if (toolPtr->bNotifyUnicode)
498 TOOLTIPS_GetDispInfoW(infoPtr, toolPtr, buffer);
499 else
500 TOOLTIPS_GetDispInfoA(infoPtr, toolPtr, buffer);
502 else {
503 /* the item is a usual (unicode) text */
504 lstrcpynW (buffer, toolPtr->lpszText, INFOTIPSIZE);
507 else {
508 /* no text available */
509 buffer[0] = '\0';
512 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & TTS_NOPREFIX)) {
513 WCHAR *ptrW;
514 if ((ptrW = strchrW(buffer, '\t')))
515 *ptrW = 0;
518 TRACE("%s\n", debugstr_w(buffer));
522 static void
523 TOOLTIPS_CalcTipSize (const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
525 HDC hdc;
526 HFONT hOldFont;
527 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
528 UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
529 RECT rc = {0, 0, 0, 0};
530 SIZE title = {0, 0};
532 if (infoPtr->nMaxTipWidth > -1) {
533 rc.right = infoPtr->nMaxTipWidth;
534 uFlags |= DT_WORDBREAK;
536 if (style & TTS_NOPREFIX)
537 uFlags |= DT_NOPREFIX;
538 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
540 hdc = GetDC (infoPtr->hwndSelf);
541 if (infoPtr->pszTitle)
543 RECT rcTitle = {0, 0, 0, 0};
544 TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
545 if (infoPtr->hTitleIcon)
547 title.cx = ICON_WIDTH;
548 title.cy = ICON_HEIGHT;
550 if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
551 hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
552 DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
553 SelectObject (hdc, hOldFont);
554 title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
555 title.cx += (rcTitle.right - rcTitle.left);
557 hOldFont = SelectObject (hdc, infoPtr->hFont);
558 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
559 SelectObject (hdc, hOldFont);
560 ReleaseDC (infoPtr->hwndSelf, hdc);
562 if ((style & TTS_BALLOON) || infoPtr->pszTitle)
564 lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
565 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
566 lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
567 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
568 BALLOON_STEMHEIGHT;
570 else
572 lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
573 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
574 lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
575 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
580 static void
581 TOOLTIPS_Show (TOOLTIPS_INFO *infoPtr, BOOL track_activate)
583 TTTOOL_INFO *toolPtr;
584 HMONITOR monitor;
585 MONITORINFO mon_info;
586 RECT rect;
587 SIZE size;
588 NMHDR hdr;
589 int ptfx = 0;
590 DWORD style = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
591 INT nTool, current;
593 if (track_activate)
595 if (infoPtr->nTrackTool == -1)
597 TRACE("invalid tracking tool %d\n", infoPtr->nTrackTool);
598 return;
600 nTool = infoPtr->nTrackTool;
602 else
604 if (infoPtr->nTool == -1)
606 TRACE("invalid tool %d\n", infoPtr->nTool);
607 return;
609 nTool = infoPtr->nTool;
612 TRACE("Show tooltip pre %d, %p\n", nTool, infoPtr->hwndSelf);
614 current = infoPtr->nCurrentTool;
615 if (!track_activate)
616 infoPtr->nCurrentTool = infoPtr->nTool;
618 TOOLTIPS_GetTipText (infoPtr, nTool, infoPtr->szTipText);
620 if (infoPtr->szTipText[0] == '\0')
622 infoPtr->nCurrentTool = current;
623 return;
626 toolPtr = &infoPtr->tools[nTool];
628 TRACE("Show tooltip %d\n", nTool);
630 hdr.hwndFrom = infoPtr->hwndSelf;
631 hdr.idFrom = toolPtr->uId;
632 hdr.code = TTN_SHOW;
633 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
635 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
637 TOOLTIPS_CalcTipSize (infoPtr, &size);
638 TRACE("size %d x %d\n", size.cx, size.cy);
640 if (track_activate && (toolPtr->uFlags & TTF_TRACK))
642 rect.left = infoPtr->xTrackPos;
643 rect.top = infoPtr->yTrackPos;
644 ptfx = rect.left;
646 if (toolPtr->uFlags & TTF_CENTERTIP)
648 rect.left -= (size.cx / 2);
649 if (!(style & TTS_BALLOON))
650 rect.top -= (size.cy / 2);
652 if (!(infoPtr->bToolBelow = (infoPtr->yTrackPos + size.cy <= GetSystemMetrics(SM_CYSCREEN))))
653 rect.top -= size.cy;
655 if (!(toolPtr->uFlags & TTF_ABSOLUTE))
657 if (style & TTS_BALLOON)
658 rect.left -= BALLOON_STEMINDENT;
659 else
661 RECT rcTool;
663 if (toolPtr->uFlags & TTF_IDISHWND)
664 GetWindowRect ((HWND)toolPtr->uId, &rcTool);
665 else
667 rcTool = toolPtr->rect;
668 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
671 /* smart placement */
672 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
673 (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
674 rect.left = rcTool.right;
678 else
680 if (toolPtr->uFlags & TTF_CENTERTIP)
682 RECT rc;
684 if (toolPtr->uFlags & TTF_IDISHWND)
685 GetWindowRect ((HWND)toolPtr->uId, &rc);
686 else {
687 rc = toolPtr->rect;
688 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
690 rect.left = (rc.left + rc.right - size.cx) / 2;
691 if (style & TTS_BALLOON)
693 ptfx = rc.left + ((rc.right - rc.left) / 2);
695 /* CENTERTIP ballon tooltips default to below the field
696 * if they fit on the screen */
697 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
699 rect.top = rc.top - size.cy;
700 infoPtr->bToolBelow = FALSE;
702 else
704 infoPtr->bToolBelow = TRUE;
705 rect.top = rc.bottom;
707 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
709 else
711 rect.top = rc.bottom + 2;
712 infoPtr->bToolBelow = TRUE;
715 else
717 GetCursorPos ((LPPOINT)&rect);
718 if (style & TTS_BALLOON)
720 ptfx = rect.left;
721 if(rect.top - size.cy >= 0)
723 rect.top -= size.cy;
724 infoPtr->bToolBelow = FALSE;
726 else
728 infoPtr->bToolBelow = TRUE;
729 rect.top += 20;
731 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
733 else
735 rect.top += 20;
736 infoPtr->bToolBelow = TRUE;
741 TRACE("pos %d - %d\n", rect.left, rect.top);
743 rect.right = rect.left + size.cx;
744 rect.bottom = rect.top + size.cy;
746 /* check position */
748 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
749 mon_info.cbSize = sizeof(mon_info);
750 GetMonitorInfoW( monitor, &mon_info );
752 if( rect.right > mon_info.rcWork.right ) {
753 rect.left -= rect.right - mon_info.rcWork.right + 2;
754 rect.right = mon_info.rcWork.right - 2;
756 if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
758 if( rect.bottom > mon_info.rcWork.bottom ) {
759 RECT rc;
761 if (toolPtr->uFlags & TTF_IDISHWND)
762 GetWindowRect ((HWND)toolPtr->uId, &rc);
763 else {
764 rc = toolPtr->rect;
765 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
767 rect.bottom = rc.top - 2;
768 rect.top = rect.bottom - size.cy;
771 AdjustWindowRectEx (&rect, GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE),
772 FALSE, GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE));
774 if (style & TTS_BALLOON)
776 HRGN hRgn;
777 HRGN hrStem;
778 POINT pts[3];
780 ptfx -= rect.left;
782 if(infoPtr->bToolBelow)
784 pts[0].x = ptfx;
785 pts[0].y = 0;
786 pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
787 pts[1].y = BALLOON_STEMHEIGHT;
788 pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
789 pts[2].y = pts[1].y;
790 if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
792 pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
793 pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
796 else
798 pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
799 pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
800 pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
801 pts[1].y = pts[0].y;
802 pts[2].x = ptfx;
803 pts[2].y = (rect.bottom - rect.top);
804 if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
806 pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
807 pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
811 hrStem = CreatePolygonRgn(pts, sizeof(pts) / sizeof(pts[0]), ALTERNATE);
813 hRgn = CreateRoundRectRgn(0,
814 (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
815 rect.right - rect.left,
816 (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
817 BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
819 CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
820 DeleteObject(hrStem);
822 SetWindowRgn(infoPtr->hwndSelf, hRgn, FALSE);
823 /* we don't free the region handle as the system deletes it when
824 * it is no longer needed */
827 SetWindowPos (infoPtr->hwndSelf, HWND_TOPMOST, rect.left, rect.top,
828 rect.right - rect.left, rect.bottom - rect.top,
829 SWP_SHOWWINDOW | SWP_NOACTIVATE);
831 /* repaint the tooltip */
832 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
833 UpdateWindow(infoPtr->hwndSelf);
835 if (!track_activate)
837 SetTimer (infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
838 TRACE("timer 2 started\n");
839 SetTimer (infoPtr->hwndSelf, ID_TIMERLEAVE, infoPtr->nReshowTime, 0);
840 TRACE("timer 3 started\n");
845 static void
846 TOOLTIPS_Hide (TOOLTIPS_INFO *infoPtr)
848 TTTOOL_INFO *toolPtr;
849 NMHDR hdr;
851 TRACE("Hide tooltip %d, %p.\n", infoPtr->nCurrentTool, infoPtr->hwndSelf);
853 if (infoPtr->nCurrentTool == -1)
854 return;
856 toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
857 KillTimer (infoPtr->hwndSelf, ID_TIMERPOP);
859 hdr.hwndFrom = infoPtr->hwndSelf;
860 hdr.idFrom = toolPtr->uId;
861 hdr.code = TTN_POP;
862 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
864 infoPtr->nCurrentTool = -1;
866 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
867 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
871 static void
872 TOOLTIPS_TrackShow (TOOLTIPS_INFO *infoPtr)
874 TOOLTIPS_Show(infoPtr, TRUE);
878 static void
879 TOOLTIPS_TrackHide (const TOOLTIPS_INFO *infoPtr)
881 TTTOOL_INFO *toolPtr;
882 NMHDR hdr;
884 TRACE("hide tracking tooltip %d\n", infoPtr->nTrackTool);
886 if (infoPtr->nTrackTool == -1)
887 return;
889 toolPtr = &infoPtr->tools[infoPtr->nTrackTool];
891 hdr.hwndFrom = infoPtr->hwndSelf;
892 hdr.idFrom = toolPtr->uId;
893 hdr.code = TTN_POP;
894 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
896 SetWindowPos (infoPtr->hwndSelf, HWND_TOP, 0, 0, 0, 0,
897 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
900 /* Structure layout is the same for TTTOOLINFOW and TTTOOLINFOA,
901 this helper is used in both cases. */
902 static INT
903 TOOLTIPS_GetToolFromInfoT (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
905 TTTOOL_INFO *toolPtr;
906 UINT nTool;
908 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
909 toolPtr = &infoPtr->tools[nTool];
911 if (!(toolPtr->uFlags & TTF_IDISHWND) &&
912 (lpToolInfo->hwnd == toolPtr->hwnd) &&
913 (lpToolInfo->uId == toolPtr->uId))
914 return nTool;
917 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
918 toolPtr = &infoPtr->tools[nTool];
920 if ((toolPtr->uFlags & TTF_IDISHWND) &&
921 (lpToolInfo->uId == toolPtr->uId))
922 return nTool;
925 return -1;
929 static INT
930 TOOLTIPS_GetToolFromPoint (const TOOLTIPS_INFO *infoPtr, HWND hwnd, const POINT *lpPt)
932 TTTOOL_INFO *toolPtr;
933 UINT nTool;
935 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
936 toolPtr = &infoPtr->tools[nTool];
938 if (!(toolPtr->uFlags & TTF_IDISHWND)) {
939 if (hwnd != toolPtr->hwnd)
940 continue;
941 if (!PtInRect (&toolPtr->rect, *lpPt))
942 continue;
943 return nTool;
947 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
948 toolPtr = &infoPtr->tools[nTool];
950 if (toolPtr->uFlags & TTF_IDISHWND) {
951 if ((HWND)toolPtr->uId == hwnd)
952 return nTool;
956 return -1;
959 static inline void
960 TOOLTIPS_CopyInfoT (const TOOLTIPS_INFO *infoPtr, INT index, TTTOOLINFOW *ti, BOOL isW)
962 const TTTOOL_INFO *toolPtr = &infoPtr->tools[index];
964 ti->uFlags = toolPtr->uFlags;
965 ti->hwnd = toolPtr->hwnd;
966 ti->uId = toolPtr->uId;
967 ti->rect = toolPtr->rect;
968 ti->hinst = toolPtr->hinst;
970 if (ti->lpszText) {
971 if (toolPtr->lpszText == NULL ||
972 IS_INTRESOURCE(toolPtr->lpszText) ||
973 toolPtr->lpszText == LPSTR_TEXTCALLBACKW)
974 ti->lpszText = toolPtr->lpszText;
975 else if (isW)
976 strcpyW (ti->lpszText, toolPtr->lpszText);
977 else
978 /* ANSI version, the buffer is maximum 80 bytes without null. */
979 WideCharToMultiByte(CP_ACP, 0, toolPtr->lpszText, -1,
980 (LPSTR)ti->lpszText, MAX_TEXT_SIZE_A, NULL, NULL);
983 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
984 ti->lParam = toolPtr->lParam;
986 /* lpReserved is intentionally not set. */
989 static BOOL
990 TOOLTIPS_IsWindowActive (HWND hwnd)
992 HWND hwndActive = GetActiveWindow ();
993 if (!hwndActive)
994 return FALSE;
995 if (hwndActive == hwnd)
996 return TRUE;
997 return IsChild (hwndActive, hwnd);
1001 static INT
1002 TOOLTIPS_CheckTool (const TOOLTIPS_INFO *infoPtr, BOOL bShowTest)
1004 POINT pt;
1005 HWND hwndTool;
1006 INT nTool;
1008 GetCursorPos (&pt);
1009 hwndTool = (HWND)SendMessageW (infoPtr->hwndSelf, TTM_WINDOWFROMPOINT, 0, (LPARAM)&pt);
1010 if (hwndTool == 0)
1011 return -1;
1013 ScreenToClient (hwndTool, &pt);
1014 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, hwndTool, &pt);
1015 if (nTool == -1)
1016 return -1;
1018 if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TTS_ALWAYSTIP) && bShowTest)
1020 TTTOOL_INFO *ti = &infoPtr->tools[nTool];
1021 HWND hwnd = (ti->uFlags & TTF_IDISHWND) ? (HWND)ti->uId : ti->hwnd;
1023 if (!TOOLTIPS_IsWindowActive(hwnd))
1025 TRACE("not active: hwnd %p, parent %p, active %p\n",
1026 hwnd, GetParent(hwnd), GetActiveWindow());
1027 return -1;
1031 TRACE("tool %d\n", nTool);
1033 return nTool;
1037 static LRESULT
1038 TOOLTIPS_Activate (TOOLTIPS_INFO *infoPtr, BOOL activate)
1040 infoPtr->bActive = activate;
1042 TRACE("activate %d\n", activate);
1044 if (!(infoPtr->bActive) && (infoPtr->nCurrentTool != -1))
1045 TOOLTIPS_Hide (infoPtr);
1047 return 0;
1051 static LRESULT
1052 TOOLTIPS_AddToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1054 TTTOOL_INFO *toolPtr;
1055 INT nResult;
1057 if (!ti) return FALSE;
1059 TRACE("add tool (%p) %p %ld%s\n", infoPtr->hwndSelf, ti->hwnd, ti->uId,
1060 (ti->uFlags & TTF_IDISHWND) ? " TTF_IDISHWND" : "");
1062 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE && !ti->lpszText && isW)
1063 return FALSE;
1065 if (infoPtr->uNumTools == 0) {
1066 infoPtr->tools = Alloc (sizeof(TTTOOL_INFO));
1067 toolPtr = infoPtr->tools;
1069 else {
1070 TTTOOL_INFO *oldTools = infoPtr->tools;
1071 infoPtr->tools =
1072 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools + 1));
1073 memcpy (infoPtr->tools, oldTools,
1074 infoPtr->uNumTools * sizeof(TTTOOL_INFO));
1075 Free (oldTools);
1076 toolPtr = &infoPtr->tools[infoPtr->uNumTools];
1079 infoPtr->uNumTools++;
1081 /* copy tool data */
1082 toolPtr->uFlags = ti->uFlags;
1083 toolPtr->uInternalFlags = (ti->uFlags & (TTF_SUBCLASS | TTF_IDISHWND));
1084 toolPtr->hwnd = ti->hwnd;
1085 toolPtr->uId = ti->uId;
1086 toolPtr->rect = ti->rect;
1087 toolPtr->hinst = ti->hinst;
1089 if (ti->cbSize >= TTTOOLINFOW_V1_SIZE) {
1090 if (IS_INTRESOURCE(ti->lpszText)) {
1091 TRACE("add string id %x\n", LOWORD(ti->lpszText));
1092 toolPtr->lpszText = ti->lpszText;
1094 else if (ti->lpszText) {
1095 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW)) {
1096 TRACE("add CALLBACK\n");
1097 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1099 else if (isW) {
1100 INT len = lstrlenW (ti->lpszText);
1101 TRACE("add text %s\n", debugstr_w(ti->lpszText));
1102 toolPtr->lpszText = Alloc ((len + 1)*sizeof(WCHAR));
1103 strcpyW (toolPtr->lpszText, ti->lpszText);
1105 else {
1106 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, NULL, 0);
1107 TRACE("add text \"%s\"\n", debugstr_a((char *)ti->lpszText));
1108 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1109 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1, toolPtr->lpszText, len);
1114 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1115 toolPtr->lParam = ti->lParam;
1117 /* install subclassing hook */
1118 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1119 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1120 SetWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1,
1121 (DWORD_PTR)infoPtr->hwndSelf);
1123 else {
1124 SetWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1,
1125 (DWORD_PTR)infoPtr->hwndSelf);
1127 TRACE("subclassing installed\n");
1130 nResult = SendMessageW (toolPtr->hwnd, WM_NOTIFYFORMAT,
1131 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
1132 if (nResult == NFR_ANSI) {
1133 toolPtr->bNotifyUnicode = FALSE;
1134 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_ANSI\n");
1135 } else if (nResult == NFR_UNICODE) {
1136 toolPtr->bNotifyUnicode = TRUE;
1137 TRACE(" -- WM_NOTIFYFORMAT returns: NFR_UNICODE\n");
1138 } else {
1139 TRACE (" -- WM_NOTIFYFORMAT returns: %d\n", nResult);
1142 return TRUE;
1146 static LRESULT
1147 TOOLTIPS_DelToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1149 TTTOOL_INFO *toolPtr;
1150 INT nTool;
1152 if (!ti) return 0;
1153 if (isW && ti->cbSize > TTTOOLINFOW_V2_SIZE &&
1154 ti->cbSize != TTTOOLINFOW_V3_SIZE)
1155 return 0;
1156 if (infoPtr->uNumTools == 0)
1157 return 0;
1159 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1161 TRACE("tool %d\n", nTool);
1163 if (nTool == -1)
1164 return 0;
1166 /* make sure the tooltip has disappeared before deleting it */
1167 TOOLTIPS_Hide(infoPtr);
1169 /* delete text string */
1170 toolPtr = &infoPtr->tools[nTool];
1171 if (toolPtr->lpszText) {
1172 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1173 !IS_INTRESOURCE(toolPtr->lpszText) )
1174 Free (toolPtr->lpszText);
1177 /* remove subclassing */
1178 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1179 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1180 RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
1182 else {
1183 RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
1187 /* delete tool from tool list */
1188 if (infoPtr->uNumTools == 1) {
1189 Free (infoPtr->tools);
1190 infoPtr->tools = NULL;
1192 else {
1193 TTTOOL_INFO *oldTools = infoPtr->tools;
1194 infoPtr->tools =
1195 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1197 if (nTool > 0)
1198 memcpy (&infoPtr->tools[0], &oldTools[0],
1199 nTool * sizeof(TTTOOL_INFO));
1201 if (nTool < infoPtr->uNumTools - 1)
1202 memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1203 (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1205 Free (oldTools);
1208 /* update any indices affected by delete */
1210 /* destroying tool that mouse was on on last relayed mouse move */
1211 if (infoPtr->nTool == nTool)
1212 /* -1 means no current tool (0 means first tool) */
1213 infoPtr->nTool = -1;
1214 else if (infoPtr->nTool > nTool)
1215 infoPtr->nTool--;
1217 if (infoPtr->nTrackTool == nTool)
1218 /* -1 means no current tool (0 means first tool) */
1219 infoPtr->nTrackTool = -1;
1220 else if (infoPtr->nTrackTool > nTool)
1221 infoPtr->nTrackTool--;
1223 if (infoPtr->nCurrentTool == nTool)
1224 /* -1 means no current tool (0 means first tool) */
1225 infoPtr->nCurrentTool = -1;
1226 else if (infoPtr->nCurrentTool > nTool)
1227 infoPtr->nCurrentTool--;
1229 infoPtr->uNumTools--;
1231 return 0;
1234 static LRESULT
1235 TOOLTIPS_EnumToolsT (const TOOLTIPS_INFO *infoPtr, UINT uIndex, TTTOOLINFOW *ti,
1236 BOOL isW)
1238 if (!ti || ti->cbSize < TTTOOLINFOW_V1_SIZE)
1239 return FALSE;
1240 if (uIndex >= infoPtr->uNumTools)
1241 return FALSE;
1243 TRACE("index=%u\n", uIndex);
1245 TOOLTIPS_CopyInfoT (infoPtr, uIndex, ti, isW);
1247 return TRUE;
1250 static LRESULT
1251 TOOLTIPS_GetBubbleSize (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
1253 INT nTool;
1254 SIZE size;
1256 if (lpToolInfo == NULL)
1257 return FALSE;
1258 if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1259 return FALSE;
1261 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, lpToolInfo);
1262 if (nTool == -1) return 0;
1264 TRACE("tool %d\n", nTool);
1266 TOOLTIPS_CalcTipSize (infoPtr, &size);
1267 TRACE("size %d x %d\n", size.cx, size.cy);
1269 return MAKELRESULT(size.cx, size.cy);
1272 static LRESULT
1273 TOOLTIPS_GetCurrentToolT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1275 if (ti) {
1276 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1277 return FALSE;
1279 if (infoPtr->nCurrentTool != -1)
1280 TOOLTIPS_CopyInfoT (infoPtr, infoPtr->nCurrentTool, ti, isW);
1283 return infoPtr->nCurrentTool != -1;
1287 static LRESULT
1288 TOOLTIPS_GetDelayTime (const TOOLTIPS_INFO *infoPtr, DWORD duration)
1290 switch (duration) {
1291 case TTDT_RESHOW:
1292 return infoPtr->nReshowTime;
1294 case TTDT_AUTOPOP:
1295 return infoPtr->nAutoPopTime;
1297 case TTDT_INITIAL:
1298 case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1299 return infoPtr->nInitialTime;
1301 default:
1302 WARN("Invalid duration flag %x\n", duration);
1303 break;
1306 return -1;
1310 static LRESULT
1311 TOOLTIPS_GetMargin (const TOOLTIPS_INFO *infoPtr, RECT *rect)
1313 if (rect)
1314 *rect = infoPtr->rcMargin;
1316 return 0;
1320 static inline LRESULT
1321 TOOLTIPS_GetMaxTipWidth (const TOOLTIPS_INFO *infoPtr)
1323 return infoPtr->nMaxTipWidth;
1327 static LRESULT
1328 TOOLTIPS_GetTextT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1330 INT nTool;
1332 if (!ti) return 0;
1333 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1334 return 0;
1336 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1337 if (nTool == -1) return 0;
1339 if (infoPtr->tools[nTool].lpszText == NULL)
1340 return 0;
1342 if (isW) {
1343 ti->lpszText[0] = '\0';
1344 TOOLTIPS_GetTipText(infoPtr, nTool, ti->lpszText);
1346 else {
1347 WCHAR buffer[INFOTIPSIZE];
1349 /* NB this API is broken, there is no way for the app to determine
1350 what size buffer it requires nor a way to specify how long the
1351 one it supplies is. According to the test result, it's up to
1352 80 bytes by the ANSI version. */
1354 buffer[0] = '\0';
1355 TOOLTIPS_GetTipText(infoPtr, nTool, buffer);
1356 WideCharToMultiByte(CP_ACP, 0, buffer, -1, (LPSTR)ti->lpszText,
1357 MAX_TEXT_SIZE_A, NULL, NULL);
1360 return 0;
1364 static inline LRESULT
1365 TOOLTIPS_GetTipBkColor (const TOOLTIPS_INFO *infoPtr)
1367 return infoPtr->clrBk;
1371 static inline LRESULT
1372 TOOLTIPS_GetTipTextColor (const TOOLTIPS_INFO *infoPtr)
1374 return infoPtr->clrText;
1378 static inline LRESULT
1379 TOOLTIPS_GetToolCount (const TOOLTIPS_INFO *infoPtr)
1381 return infoPtr->uNumTools;
1385 static LRESULT
1386 TOOLTIPS_GetToolInfoT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1388 INT nTool;
1389 HWND hwnd;
1391 if (!ti) return FALSE;
1392 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1393 return FALSE;
1394 if (infoPtr->uNumTools == 0)
1395 return FALSE;
1397 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1398 if (nTool == -1)
1399 return FALSE;
1401 TRACE("tool %d\n", nTool);
1403 hwnd = ti->hwnd;
1404 TOOLTIPS_CopyInfoT (infoPtr, nTool, ti, isW);
1405 ti->hwnd = hwnd;
1407 return TRUE;
1411 static LRESULT
1412 TOOLTIPS_HitTestT (const TOOLTIPS_INFO *infoPtr, LPTTHITTESTINFOW lptthit,
1413 BOOL isW)
1415 INT nTool;
1417 if (lptthit == 0)
1418 return FALSE;
1420 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1421 if (nTool == -1)
1422 return FALSE;
1424 TRACE("tool %d\n", nTool);
1426 /* copy tool data */
1427 if (lptthit->ti.cbSize >= TTTOOLINFOW_V1_SIZE)
1428 TOOLTIPS_CopyInfoT (infoPtr, nTool, &lptthit->ti, isW);
1430 return TRUE;
1434 static LRESULT
1435 TOOLTIPS_NewToolRectT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti)
1437 INT nTool;
1439 if (!ti) return 0;
1440 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1441 return FALSE;
1443 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1445 TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&ti->rect));
1447 if (nTool == -1) return 0;
1449 infoPtr->tools[nTool].rect = ti->rect;
1451 return 0;
1455 static inline LRESULT
1456 TOOLTIPS_Pop (TOOLTIPS_INFO *infoPtr)
1458 TOOLTIPS_Hide (infoPtr);
1460 return 0;
1464 static LRESULT
1465 TOOLTIPS_RelayEvent (TOOLTIPS_INFO *infoPtr, LPMSG lpMsg)
1467 POINT pt;
1468 INT nOldTool;
1470 if (!lpMsg) {
1471 ERR("lpMsg == NULL\n");
1472 return 0;
1475 switch (lpMsg->message) {
1476 case WM_LBUTTONDOWN:
1477 case WM_LBUTTONUP:
1478 case WM_MBUTTONDOWN:
1479 case WM_MBUTTONUP:
1480 case WM_RBUTTONDOWN:
1481 case WM_RBUTTONUP:
1482 TOOLTIPS_Hide (infoPtr);
1483 break;
1485 case WM_MOUSEMOVE:
1486 pt.x = (short)LOWORD(lpMsg->lParam);
1487 pt.y = (short)HIWORD(lpMsg->lParam);
1488 nOldTool = infoPtr->nTool;
1489 infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1490 &pt);
1491 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
1492 infoPtr->nTool, infoPtr->nCurrentTool);
1493 TRACE("WM_MOUSEMOVE (%p %s)\n", infoPtr->hwndSelf, wine_dbgstr_point(&pt));
1495 if (infoPtr->nTool != nOldTool) {
1496 if(infoPtr->nTool == -1) { /* Moved out of all tools */
1497 TOOLTIPS_Hide(infoPtr);
1498 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1499 } else if (nOldTool == -1) { /* Moved from outside */
1500 if(infoPtr->bActive) {
1501 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1502 TRACE("timer 1 started\n");
1504 } else { /* Moved from one to another */
1505 TOOLTIPS_Hide (infoPtr);
1506 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1507 if(infoPtr->bActive) {
1508 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1509 TRACE("timer 1 started\n");
1512 } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1513 KillTimer(infoPtr->hwndSelf, ID_TIMERPOP);
1514 SetTimer(infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1515 TRACE("timer 2 restarted\n");
1516 } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1517 /* previous show attempt didn't result in tooltip so try again */
1518 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1519 TRACE("timer 1 started\n");
1521 break;
1524 return 0;
1528 static LRESULT
1529 TOOLTIPS_SetDelayTime (TOOLTIPS_INFO *infoPtr, DWORD duration, INT nTime)
1531 switch (duration) {
1532 case TTDT_AUTOMATIC:
1533 if (nTime <= 0)
1534 nTime = GetDoubleClickTime();
1535 infoPtr->nReshowTime = nTime / 5;
1536 infoPtr->nAutoPopTime = nTime * 10;
1537 infoPtr->nInitialTime = nTime;
1538 break;
1540 case TTDT_RESHOW:
1541 if(nTime < 0)
1542 nTime = GetDoubleClickTime() / 5;
1543 infoPtr->nReshowTime = nTime;
1544 break;
1546 case TTDT_AUTOPOP:
1547 if(nTime < 0)
1548 nTime = GetDoubleClickTime() * 10;
1549 infoPtr->nAutoPopTime = nTime;
1550 break;
1552 case TTDT_INITIAL:
1553 if(nTime < 0)
1554 nTime = GetDoubleClickTime();
1555 infoPtr->nInitialTime = nTime;
1556 break;
1558 default:
1559 WARN("Invalid duration flag %x\n", duration);
1560 break;
1563 return 0;
1567 static LRESULT
1568 TOOLTIPS_SetMargin (TOOLTIPS_INFO *infoPtr, const RECT *rect)
1570 if (rect)
1571 infoPtr->rcMargin = *rect;
1573 return 0;
1577 static inline LRESULT
1578 TOOLTIPS_SetMaxTipWidth (TOOLTIPS_INFO *infoPtr, INT MaxWidth)
1580 INT nTemp = infoPtr->nMaxTipWidth;
1582 infoPtr->nMaxTipWidth = MaxWidth;
1584 return nTemp;
1588 static inline LRESULT
1589 TOOLTIPS_SetTipBkColor (TOOLTIPS_INFO *infoPtr, COLORREF clrBk)
1591 infoPtr->clrBk = clrBk;
1593 return 0;
1597 static inline LRESULT
1598 TOOLTIPS_SetTipTextColor (TOOLTIPS_INFO *infoPtr, COLORREF clrText)
1600 infoPtr->clrText = clrText;
1602 return 0;
1606 static LRESULT
1607 TOOLTIPS_SetTitleT (TOOLTIPS_INFO *infoPtr, UINT_PTR uTitleIcon, LPCWSTR pszTitle,
1608 BOOL isW)
1610 UINT size;
1612 TRACE("hwnd = %p, title = %s, icon = %p\n", infoPtr->hwndSelf, debugstr_w(pszTitle),
1613 (void*)uTitleIcon);
1615 Free(infoPtr->pszTitle);
1617 if (pszTitle)
1619 if (isW)
1621 size = (strlenW(pszTitle)+1)*sizeof(WCHAR);
1622 infoPtr->pszTitle = Alloc(size);
1623 if (!infoPtr->pszTitle)
1624 return FALSE;
1625 memcpy(infoPtr->pszTitle, pszTitle, size);
1627 else
1629 size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, NULL, 0);
1630 infoPtr->pszTitle = Alloc(size);
1631 if (!infoPtr->pszTitle)
1632 return FALSE;
1633 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1636 else
1637 infoPtr->pszTitle = NULL;
1639 if (uTitleIcon <= TTI_ERROR)
1640 infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1641 else
1642 infoPtr->hTitleIcon = CopyIcon((HICON)uTitleIcon);
1644 TRACE("icon = %p\n", infoPtr->hTitleIcon);
1646 return TRUE;
1650 static LRESULT
1651 TOOLTIPS_SetToolInfoT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1653 TTTOOL_INFO *toolPtr;
1654 INT nTool;
1656 if (!ti) return 0;
1657 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1658 return 0;
1660 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1661 if (nTool == -1) return 0;
1663 TRACE("tool %d\n", nTool);
1665 toolPtr = &infoPtr->tools[nTool];
1667 /* copy tool data */
1668 toolPtr->uFlags = ti->uFlags;
1669 toolPtr->hwnd = ti->hwnd;
1670 toolPtr->uId = ti->uId;
1671 toolPtr->rect = ti->rect;
1672 toolPtr->hinst = ti->hinst;
1674 if (IS_INTRESOURCE(ti->lpszText)) {
1675 TRACE("set string id %x\n", LOWORD(ti->lpszText));
1676 toolPtr->lpszText = ti->lpszText;
1678 else {
1679 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1680 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1681 else {
1682 if ( (toolPtr->lpszText) &&
1683 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1684 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1685 Free (toolPtr->lpszText);
1686 toolPtr->lpszText = NULL;
1688 if (ti->lpszText) {
1689 if (isW) {
1690 INT len = lstrlenW (ti->lpszText);
1691 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1692 strcpyW (toolPtr->lpszText, ti->lpszText);
1694 else {
1695 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1696 -1, NULL, 0);
1697 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1698 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1699 toolPtr->lpszText, len);
1705 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1706 toolPtr->lParam = ti->lParam;
1708 if (infoPtr->nCurrentTool == nTool)
1710 TOOLTIPS_GetTipText (infoPtr, infoPtr->nCurrentTool, infoPtr->szTipText);
1712 if (infoPtr->szTipText[0] == 0)
1713 TOOLTIPS_Hide(infoPtr);
1714 else
1715 TOOLTIPS_Show (infoPtr, FALSE);
1718 return 0;
1722 static LRESULT
1723 TOOLTIPS_TrackActivate (TOOLTIPS_INFO *infoPtr, BOOL track_activate, const TTTOOLINFOA *ti)
1725 if (track_activate) {
1727 if (!ti) return 0;
1728 if (ti->cbSize < TTTOOLINFOA_V1_SIZE)
1729 return FALSE;
1731 /* activate */
1732 infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoT (infoPtr, (const TTTOOLINFOW*)ti);
1733 if (infoPtr->nTrackTool != -1) {
1734 TRACE("activated\n");
1735 infoPtr->bTrackActive = TRUE;
1736 TOOLTIPS_TrackShow (infoPtr);
1739 else {
1740 /* deactivate */
1741 TOOLTIPS_TrackHide (infoPtr);
1743 infoPtr->bTrackActive = FALSE;
1744 infoPtr->nTrackTool = -1;
1746 TRACE("deactivated\n");
1749 return 0;
1753 static LRESULT
1754 TOOLTIPS_TrackPosition (TOOLTIPS_INFO *infoPtr, LPARAM coord)
1756 infoPtr->xTrackPos = (INT)LOWORD(coord);
1757 infoPtr->yTrackPos = (INT)HIWORD(coord);
1759 if (infoPtr->bTrackActive) {
1760 TRACE("[%d %d]\n",
1761 infoPtr->xTrackPos, infoPtr->yTrackPos);
1763 TOOLTIPS_TrackShow (infoPtr);
1766 return 0;
1770 static LRESULT
1771 TOOLTIPS_Update (TOOLTIPS_INFO *infoPtr)
1773 if (infoPtr->nCurrentTool != -1)
1774 UpdateWindow (infoPtr->hwndSelf);
1776 return 0;
1780 static LRESULT
1781 TOOLTIPS_UpdateTipTextT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1783 TTTOOL_INFO *toolPtr;
1784 INT nTool;
1786 if (!ti) return 0;
1787 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1788 return FALSE;
1790 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1791 if (nTool == -1)
1792 return 0;
1794 TRACE("tool %d\n", nTool);
1796 toolPtr = &infoPtr->tools[nTool];
1798 /* copy tool text */
1799 toolPtr->hinst = ti->hinst;
1801 if (IS_INTRESOURCE(ti->lpszText)){
1802 toolPtr->lpszText = ti->lpszText;
1804 else if (ti->lpszText) {
1805 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1806 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1807 else {
1808 if ( (toolPtr->lpszText) &&
1809 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1810 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1811 Free (toolPtr->lpszText);
1812 toolPtr->lpszText = NULL;
1814 if (ti->lpszText) {
1815 if (isW) {
1816 INT len = lstrlenW (ti->lpszText);
1817 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1818 strcpyW (toolPtr->lpszText, ti->lpszText);
1820 else {
1821 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1822 -1, NULL, 0);
1823 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1824 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1825 toolPtr->lpszText, len);
1831 if(infoPtr->nCurrentTool == -1) return 0;
1832 /* force repaint */
1833 if (infoPtr->bActive)
1834 TOOLTIPS_Show (infoPtr, FALSE);
1835 else if (infoPtr->bTrackActive)
1836 TOOLTIPS_Show (infoPtr, TRUE);
1838 return 0;
1842 static LRESULT
1843 TOOLTIPS_Create (HWND hwnd)
1845 TOOLTIPS_INFO *infoPtr;
1847 /* allocate memory for info structure */
1848 infoPtr = Alloc (sizeof(TOOLTIPS_INFO));
1849 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1851 /* initialize info structure */
1852 infoPtr->bActive = TRUE;
1853 infoPtr->bTrackActive = FALSE;
1855 infoPtr->nMaxTipWidth = -1;
1856 infoPtr->nTool = -1;
1857 infoPtr->nCurrentTool = -1;
1858 infoPtr->nTrackTool = -1;
1859 infoPtr->hwndSelf = hwnd;
1861 /* initialize colours and fonts */
1862 TOOLTIPS_InitSystemSettings(infoPtr);
1864 TOOLTIPS_SetDelayTime(infoPtr, TTDT_AUTOMATIC, 0);
1866 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
1868 return 0;
1872 static LRESULT
1873 TOOLTIPS_Destroy (TOOLTIPS_INFO *infoPtr)
1875 TTTOOL_INFO *toolPtr;
1876 UINT i;
1878 /* free tools */
1879 if (infoPtr->tools) {
1880 for (i = 0; i < infoPtr->uNumTools; i++) {
1881 toolPtr = &infoPtr->tools[i];
1882 if (toolPtr->lpszText) {
1883 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1884 !IS_INTRESOURCE(toolPtr->lpszText) )
1886 Free (toolPtr->lpszText);
1887 toolPtr->lpszText = NULL;
1891 /* remove subclassing */
1892 if (toolPtr->uInternalFlags & TTF_SUBCLASS) {
1893 if (toolPtr->uInternalFlags & TTF_IDISHWND) {
1894 RemoveWindowSubclass((HWND)toolPtr->uId, TOOLTIPS_SubclassProc, 1);
1896 else {
1897 RemoveWindowSubclass(toolPtr->hwnd, TOOLTIPS_SubclassProc, 1);
1901 Free (infoPtr->tools);
1904 /* free title string */
1905 Free (infoPtr->pszTitle);
1906 /* free title icon if not a standard one */
1907 if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
1908 DeleteObject(infoPtr->hTitleIcon);
1910 /* delete fonts */
1911 DeleteObject (infoPtr->hFont);
1912 DeleteObject (infoPtr->hTitleFont);
1914 /* free tool tips info data */
1915 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1916 Free (infoPtr);
1918 return 0;
1922 static inline LRESULT
1923 TOOLTIPS_GetFont (const TOOLTIPS_INFO *infoPtr)
1925 return (LRESULT)infoPtr->hFont;
1929 static LRESULT
1930 TOOLTIPS_MouseMessage (TOOLTIPS_INFO *infoPtr)
1932 TOOLTIPS_Hide (infoPtr);
1934 return 0;
1938 static LRESULT
1939 TOOLTIPS_NCCreate (HWND hwnd)
1941 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1942 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
1944 dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
1945 dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
1947 /* WS_BORDER only draws a border round the window rect, not the
1948 * window region, therefore it is useless to us in balloon mode */
1949 if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
1951 SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
1953 dwExStyle |= WS_EX_TOOLWINDOW;
1954 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
1956 return TRUE;
1960 static LRESULT
1961 TOOLTIPS_NCHitTest (const TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1963 INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
1965 TRACE(" nTool=%d\n", nTool);
1967 if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
1968 if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
1969 TRACE("-- in transparent mode\n");
1970 return HTTRANSPARENT;
1974 return DefWindowProcW (infoPtr->hwndSelf, WM_NCHITTEST, wParam, lParam);
1978 static LRESULT
1979 TOOLTIPS_NotifyFormat (TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1981 FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", infoPtr->hwndSelf, wParam, lParam);
1983 return 0;
1987 static LRESULT
1988 TOOLTIPS_Paint (const TOOLTIPS_INFO *infoPtr, HDC hDC)
1990 HDC hdc;
1991 PAINTSTRUCT ps;
1993 hdc = (hDC == NULL) ? BeginPaint (infoPtr->hwndSelf, &ps) : hDC;
1994 TOOLTIPS_Refresh (infoPtr, hdc);
1995 if (!hDC)
1996 EndPaint (infoPtr->hwndSelf, &ps);
1997 return 0;
2001 static LRESULT
2002 TOOLTIPS_SetFont (TOOLTIPS_INFO *infoPtr, HFONT hFont, BOOL redraw)
2004 LOGFONTW lf;
2006 if(!GetObjectW(hFont, sizeof(lf), &lf))
2007 return 0;
2009 DeleteObject (infoPtr->hFont);
2010 infoPtr->hFont = CreateFontIndirectW(&lf);
2012 DeleteObject (infoPtr->hTitleFont);
2013 lf.lfWeight = FW_BOLD;
2014 infoPtr->hTitleFont = CreateFontIndirectW(&lf);
2016 if (redraw && infoPtr->nCurrentTool != -1)
2017 FIXME("full redraw needed\n");
2019 return 0;
2022 /******************************************************************
2023 * TOOLTIPS_GetTextLength
2025 * This function is called when the tooltip receive a
2026 * WM_GETTEXTLENGTH message.
2028 * returns the length, in characters, of the tip text
2030 static inline LRESULT
2031 TOOLTIPS_GetTextLength(const TOOLTIPS_INFO *infoPtr)
2033 return strlenW(infoPtr->szTipText);
2036 /******************************************************************
2037 * TOOLTIPS_OnWMGetText
2039 * This function is called when the tooltip receive a
2040 * WM_GETTEXT message.
2041 * wParam : specifies the maximum number of characters to be copied
2042 * lParam : is the pointer to the buffer that will receive
2043 * the tip text
2045 * returns the number of characters copied
2047 static LRESULT
2048 TOOLTIPS_OnWMGetText (const TOOLTIPS_INFO *infoPtr, WPARAM size, LPWSTR pszText)
2050 LRESULT res;
2052 if(!size)
2053 return 0;
2055 res = min(strlenW(infoPtr->szTipText)+1, size);
2056 memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2057 pszText[res-1] = '\0';
2058 return res-1;
2061 static LRESULT
2062 TOOLTIPS_Timer (TOOLTIPS_INFO *infoPtr, INT iTimer)
2064 INT nOldTool;
2066 TRACE("timer %d (%p) expired\n", iTimer, infoPtr->hwndSelf);
2068 switch (iTimer) {
2069 case ID_TIMERSHOW:
2070 KillTimer (infoPtr->hwndSelf, ID_TIMERSHOW);
2071 nOldTool = infoPtr->nTool;
2072 if ((infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, TRUE)) == nOldTool)
2073 TOOLTIPS_Show (infoPtr, FALSE);
2074 break;
2076 case ID_TIMERPOP:
2077 TOOLTIPS_Hide (infoPtr);
2078 break;
2080 case ID_TIMERLEAVE:
2081 nOldTool = infoPtr->nTool;
2082 infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, FALSE);
2083 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
2084 infoPtr->nTool, infoPtr->nCurrentTool);
2085 if (infoPtr->nTool != nOldTool) {
2086 if(infoPtr->nTool == -1) { /* Moved out of all tools */
2087 TOOLTIPS_Hide(infoPtr);
2088 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2089 } else if (nOldTool == -1) { /* Moved from outside */
2090 ERR("How did this happen?\n");
2091 } else { /* Moved from one to another */
2092 TOOLTIPS_Hide (infoPtr);
2093 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2094 if(infoPtr->bActive) {
2095 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2096 TRACE("timer 1 started!\n");
2100 break;
2102 default:
2103 ERR("Unknown timer id %d\n", iTimer);
2104 break;
2106 return 0;
2110 static LRESULT
2111 TOOLTIPS_WinIniChange (TOOLTIPS_INFO *infoPtr)
2113 TOOLTIPS_InitSystemSettings (infoPtr);
2115 return 0;
2119 static LRESULT CALLBACK
2120 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2122 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr ((HWND)dwRef);
2123 MSG msg;
2125 switch(uMsg) {
2126 case WM_MOUSEMOVE:
2127 case WM_LBUTTONDOWN:
2128 case WM_LBUTTONUP:
2129 case WM_MBUTTONDOWN:
2130 case WM_MBUTTONUP:
2131 case WM_RBUTTONDOWN:
2132 case WM_RBUTTONUP:
2133 msg.hwnd = hwnd;
2134 msg.message = uMsg;
2135 msg.wParam = wParam;
2136 msg.lParam = lParam;
2137 TOOLTIPS_RelayEvent(infoPtr, &msg);
2138 break;
2140 default:
2141 break;
2143 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
2147 static LRESULT CALLBACK
2148 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2150 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2152 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2153 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2154 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2155 switch (uMsg)
2157 case TTM_ACTIVATE:
2158 return TOOLTIPS_Activate (infoPtr, (BOOL)wParam);
2160 case TTM_ADDTOOLA:
2161 case TTM_ADDTOOLW:
2162 return TOOLTIPS_AddToolT (infoPtr, (LPTTTOOLINFOW)lParam, uMsg == TTM_ADDTOOLW);
2164 case TTM_DELTOOLA:
2165 case TTM_DELTOOLW:
2166 return TOOLTIPS_DelToolT (infoPtr, (LPTOOLINFOW)lParam,
2167 uMsg == TTM_DELTOOLW);
2168 case TTM_ENUMTOOLSA:
2169 case TTM_ENUMTOOLSW:
2170 return TOOLTIPS_EnumToolsT (infoPtr, (UINT)wParam, (LPTTTOOLINFOW)lParam,
2171 uMsg == TTM_ENUMTOOLSW);
2172 case TTM_GETBUBBLESIZE:
2173 return TOOLTIPS_GetBubbleSize (infoPtr, (LPTTTOOLINFOW)lParam);
2175 case TTM_GETCURRENTTOOLA:
2176 case TTM_GETCURRENTTOOLW:
2177 return TOOLTIPS_GetCurrentToolT (infoPtr, (LPTTTOOLINFOW)lParam,
2178 uMsg == TTM_GETCURRENTTOOLW);
2180 case TTM_GETDELAYTIME:
2181 return TOOLTIPS_GetDelayTime (infoPtr, (DWORD)wParam);
2183 case TTM_GETMARGIN:
2184 return TOOLTIPS_GetMargin (infoPtr, (LPRECT)lParam);
2186 case TTM_GETMAXTIPWIDTH:
2187 return TOOLTIPS_GetMaxTipWidth (infoPtr);
2189 case TTM_GETTEXTA:
2190 case TTM_GETTEXTW:
2191 return TOOLTIPS_GetTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2192 uMsg == TTM_GETTEXTW);
2194 case TTM_GETTIPBKCOLOR:
2195 return TOOLTIPS_GetTipBkColor (infoPtr);
2197 case TTM_GETTIPTEXTCOLOR:
2198 return TOOLTIPS_GetTipTextColor (infoPtr);
2200 case TTM_GETTOOLCOUNT:
2201 return TOOLTIPS_GetToolCount (infoPtr);
2203 case TTM_GETTOOLINFOA:
2204 case TTM_GETTOOLINFOW:
2205 return TOOLTIPS_GetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2206 uMsg == TTM_GETTOOLINFOW);
2208 case TTM_HITTESTA:
2209 case TTM_HITTESTW:
2210 return TOOLTIPS_HitTestT (infoPtr, (LPTTHITTESTINFOW)lParam,
2211 uMsg == TTM_HITTESTW);
2212 case TTM_NEWTOOLRECTA:
2213 case TTM_NEWTOOLRECTW:
2214 return TOOLTIPS_NewToolRectT (infoPtr, (LPTTTOOLINFOW)lParam);
2216 case TTM_POP:
2217 return TOOLTIPS_Pop (infoPtr);
2219 case TTM_RELAYEVENT:
2220 return TOOLTIPS_RelayEvent (infoPtr, (LPMSG)lParam);
2222 case TTM_SETDELAYTIME:
2223 return TOOLTIPS_SetDelayTime (infoPtr, (DWORD)wParam, (INT)LOWORD(lParam));
2225 case TTM_SETMARGIN:
2226 return TOOLTIPS_SetMargin (infoPtr, (LPRECT)lParam);
2228 case TTM_SETMAXTIPWIDTH:
2229 return TOOLTIPS_SetMaxTipWidth (infoPtr, (INT)lParam);
2231 case TTM_SETTIPBKCOLOR:
2232 return TOOLTIPS_SetTipBkColor (infoPtr, (COLORREF)wParam);
2234 case TTM_SETTIPTEXTCOLOR:
2235 return TOOLTIPS_SetTipTextColor (infoPtr, (COLORREF)wParam);
2237 case TTM_SETTITLEA:
2238 case TTM_SETTITLEW:
2239 return TOOLTIPS_SetTitleT (infoPtr, (UINT_PTR)wParam, (LPCWSTR)lParam,
2240 uMsg == TTM_SETTITLEW);
2242 case TTM_SETTOOLINFOA:
2243 case TTM_SETTOOLINFOW:
2244 return TOOLTIPS_SetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2245 uMsg == TTM_SETTOOLINFOW);
2247 case TTM_TRACKACTIVATE:
2248 return TOOLTIPS_TrackActivate (infoPtr, (BOOL)wParam, (LPTTTOOLINFOA)lParam);
2250 case TTM_TRACKPOSITION:
2251 return TOOLTIPS_TrackPosition (infoPtr, lParam);
2253 case TTM_UPDATE:
2254 return TOOLTIPS_Update (infoPtr);
2256 case TTM_UPDATETIPTEXTA:
2257 case TTM_UPDATETIPTEXTW:
2258 return TOOLTIPS_UpdateTipTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2259 uMsg == TTM_UPDATETIPTEXTW);
2261 case TTM_WINDOWFROMPOINT:
2262 return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2264 case WM_CREATE:
2265 return TOOLTIPS_Create (hwnd);
2267 case WM_DESTROY:
2268 return TOOLTIPS_Destroy (infoPtr);
2270 case WM_ERASEBKGND:
2271 /* we draw the background in WM_PAINT */
2272 return 0;
2274 case WM_GETFONT:
2275 return TOOLTIPS_GetFont (infoPtr);
2277 case WM_GETTEXT:
2278 return TOOLTIPS_OnWMGetText (infoPtr, wParam, (LPWSTR)lParam);
2280 case WM_GETTEXTLENGTH:
2281 return TOOLTIPS_GetTextLength (infoPtr);
2283 case WM_LBUTTONDOWN:
2284 case WM_LBUTTONUP:
2285 case WM_MBUTTONDOWN:
2286 case WM_MBUTTONUP:
2287 case WM_RBUTTONDOWN:
2288 case WM_RBUTTONUP:
2289 case WM_MOUSEMOVE:
2290 return TOOLTIPS_MouseMessage (infoPtr);
2292 case WM_NCCREATE:
2293 return TOOLTIPS_NCCreate (hwnd);
2295 case WM_NCHITTEST:
2296 return TOOLTIPS_NCHitTest (infoPtr, wParam, lParam);
2298 case WM_NOTIFYFORMAT:
2299 return TOOLTIPS_NotifyFormat (infoPtr, wParam, lParam);
2301 case WM_PRINTCLIENT:
2302 case WM_PAINT:
2303 return TOOLTIPS_Paint (infoPtr, (HDC)wParam);
2305 case WM_SETFONT:
2306 return TOOLTIPS_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
2308 case WM_SYSCOLORCHANGE:
2309 COMCTL32_RefreshSysColors();
2310 return 0;
2312 case WM_TIMER:
2313 return TOOLTIPS_Timer (infoPtr, (INT)wParam);
2315 case WM_WININICHANGE:
2316 return TOOLTIPS_WinIniChange (infoPtr);
2318 default:
2319 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2320 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2321 uMsg, wParam, lParam);
2322 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2327 VOID
2328 TOOLTIPS_Register (void)
2330 WNDCLASSW wndClass;
2332 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2333 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2334 wndClass.lpfnWndProc = TOOLTIPS_WindowProc;
2335 wndClass.cbClsExtra = 0;
2336 wndClass.cbWndExtra = sizeof(TOOLTIPS_INFO *);
2337 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2338 wndClass.hbrBackground = 0;
2339 wndClass.lpszClassName = TOOLTIPS_CLASSW;
2341 RegisterClassW (&wndClass);
2343 hTooltipIcons[TTI_NONE] = NULL;
2344 hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2345 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2346 hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2347 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2348 hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2349 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2353 VOID
2354 TOOLTIPS_Unregister (void)
2356 int i;
2357 for (i = TTI_INFO; i <= TTI_ERROR; i++)
2358 DestroyIcon(hTooltipIcons[i]);
2359 UnregisterClassW (TOOLTIPS_CLASSW, NULL);