dxgi/tests: Introduce an ARRAY_SIZE macro.
[wine.git] / dlls / comctl32 / tooltips.c
blob1c8cb664223751d3b096ea563ddaf3fbf7112d55
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;
1145 static void TOOLTIPS_ResetSubclass (const TTTOOL_INFO *toolPtr)
1147 /* Reset subclassing data. */
1148 if (toolPtr->uInternalFlags & TTF_SUBCLASS)
1149 SetWindowSubclass(toolPtr->uInternalFlags & TTF_IDISHWND ? (HWND)toolPtr->uId : toolPtr->hwnd,
1150 TOOLTIPS_SubclassProc, 1, 0);
1153 static LRESULT
1154 TOOLTIPS_DelToolT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1156 TTTOOL_INFO *toolPtr;
1157 INT nTool;
1159 if (!ti) return 0;
1160 if (isW && ti->cbSize > TTTOOLINFOW_V2_SIZE &&
1161 ti->cbSize != TTTOOLINFOW_V3_SIZE)
1162 return 0;
1163 if (infoPtr->uNumTools == 0)
1164 return 0;
1166 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1168 TRACE("tool %d\n", nTool);
1170 if (nTool == -1)
1171 return 0;
1173 /* make sure the tooltip has disappeared before deleting it */
1174 TOOLTIPS_Hide(infoPtr);
1176 /* delete text string */
1177 toolPtr = &infoPtr->tools[nTool];
1178 if (toolPtr->lpszText) {
1179 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1180 !IS_INTRESOURCE(toolPtr->lpszText) )
1181 Free (toolPtr->lpszText);
1184 TOOLTIPS_ResetSubclass (toolPtr);
1186 /* delete tool from tool list */
1187 if (infoPtr->uNumTools == 1) {
1188 Free (infoPtr->tools);
1189 infoPtr->tools = NULL;
1191 else {
1192 TTTOOL_INFO *oldTools = infoPtr->tools;
1193 infoPtr->tools =
1194 Alloc (sizeof(TTTOOL_INFO) * (infoPtr->uNumTools - 1));
1196 if (nTool > 0)
1197 memcpy (&infoPtr->tools[0], &oldTools[0],
1198 nTool * sizeof(TTTOOL_INFO));
1200 if (nTool < infoPtr->uNumTools - 1)
1201 memcpy (&infoPtr->tools[nTool], &oldTools[nTool + 1],
1202 (infoPtr->uNumTools - nTool - 1) * sizeof(TTTOOL_INFO));
1204 Free (oldTools);
1207 /* update any indices affected by delete */
1209 /* destroying tool that mouse was on on last relayed mouse move */
1210 if (infoPtr->nTool == nTool)
1211 /* -1 means no current tool (0 means first tool) */
1212 infoPtr->nTool = -1;
1213 else if (infoPtr->nTool > nTool)
1214 infoPtr->nTool--;
1216 if (infoPtr->nTrackTool == nTool)
1217 /* -1 means no current tool (0 means first tool) */
1218 infoPtr->nTrackTool = -1;
1219 else if (infoPtr->nTrackTool > nTool)
1220 infoPtr->nTrackTool--;
1222 if (infoPtr->nCurrentTool == nTool)
1223 /* -1 means no current tool (0 means first tool) */
1224 infoPtr->nCurrentTool = -1;
1225 else if (infoPtr->nCurrentTool > nTool)
1226 infoPtr->nCurrentTool--;
1228 infoPtr->uNumTools--;
1230 return 0;
1233 static LRESULT
1234 TOOLTIPS_EnumToolsT (const TOOLTIPS_INFO *infoPtr, UINT uIndex, TTTOOLINFOW *ti,
1235 BOOL isW)
1237 if (!ti || ti->cbSize < TTTOOLINFOW_V1_SIZE)
1238 return FALSE;
1239 if (uIndex >= infoPtr->uNumTools)
1240 return FALSE;
1242 TRACE("index=%u\n", uIndex);
1244 TOOLTIPS_CopyInfoT (infoPtr, uIndex, ti, isW);
1246 return TRUE;
1249 static LRESULT
1250 TOOLTIPS_GetBubbleSize (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
1252 INT nTool;
1253 SIZE size;
1255 if (lpToolInfo == NULL)
1256 return FALSE;
1257 if (lpToolInfo->cbSize < TTTOOLINFOW_V1_SIZE)
1258 return FALSE;
1260 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, lpToolInfo);
1261 if (nTool == -1) return 0;
1263 TRACE("tool %d\n", nTool);
1265 TOOLTIPS_CalcTipSize (infoPtr, &size);
1266 TRACE("size %d x %d\n", size.cx, size.cy);
1268 return MAKELRESULT(size.cx, size.cy);
1271 static LRESULT
1272 TOOLTIPS_GetCurrentToolT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1274 if (ti) {
1275 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1276 return FALSE;
1278 if (infoPtr->nCurrentTool != -1)
1279 TOOLTIPS_CopyInfoT (infoPtr, infoPtr->nCurrentTool, ti, isW);
1282 return infoPtr->nCurrentTool != -1;
1286 static LRESULT
1287 TOOLTIPS_GetDelayTime (const TOOLTIPS_INFO *infoPtr, DWORD duration)
1289 switch (duration) {
1290 case TTDT_RESHOW:
1291 return infoPtr->nReshowTime;
1293 case TTDT_AUTOPOP:
1294 return infoPtr->nAutoPopTime;
1296 case TTDT_INITIAL:
1297 case TTDT_AUTOMATIC: /* Apparently TTDT_AUTOMATIC returns TTDT_INITIAL */
1298 return infoPtr->nInitialTime;
1300 default:
1301 WARN("Invalid duration flag %x\n", duration);
1302 break;
1305 return -1;
1309 static LRESULT
1310 TOOLTIPS_GetMargin (const TOOLTIPS_INFO *infoPtr, RECT *rect)
1312 if (rect)
1313 *rect = infoPtr->rcMargin;
1315 return 0;
1319 static inline LRESULT
1320 TOOLTIPS_GetMaxTipWidth (const TOOLTIPS_INFO *infoPtr)
1322 return infoPtr->nMaxTipWidth;
1326 static LRESULT
1327 TOOLTIPS_GetTextT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1329 INT nTool;
1331 if (!ti) return 0;
1332 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1333 return 0;
1335 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1336 if (nTool == -1) return 0;
1338 if (infoPtr->tools[nTool].lpszText == NULL)
1339 return 0;
1341 if (isW) {
1342 ti->lpszText[0] = '\0';
1343 TOOLTIPS_GetTipText(infoPtr, nTool, ti->lpszText);
1345 else {
1346 WCHAR buffer[INFOTIPSIZE];
1348 /* NB this API is broken, there is no way for the app to determine
1349 what size buffer it requires nor a way to specify how long the
1350 one it supplies is. According to the test result, it's up to
1351 80 bytes by the ANSI version. */
1353 buffer[0] = '\0';
1354 TOOLTIPS_GetTipText(infoPtr, nTool, buffer);
1355 WideCharToMultiByte(CP_ACP, 0, buffer, -1, (LPSTR)ti->lpszText,
1356 MAX_TEXT_SIZE_A, NULL, NULL);
1359 return 0;
1363 static inline LRESULT
1364 TOOLTIPS_GetTipBkColor (const TOOLTIPS_INFO *infoPtr)
1366 return infoPtr->clrBk;
1370 static inline LRESULT
1371 TOOLTIPS_GetTipTextColor (const TOOLTIPS_INFO *infoPtr)
1373 return infoPtr->clrText;
1377 static inline LRESULT
1378 TOOLTIPS_GetToolCount (const TOOLTIPS_INFO *infoPtr)
1380 return infoPtr->uNumTools;
1384 static LRESULT
1385 TOOLTIPS_GetToolInfoT (const TOOLTIPS_INFO *infoPtr, TTTOOLINFOW *ti, BOOL isW)
1387 INT nTool;
1388 HWND hwnd;
1390 if (!ti) return FALSE;
1391 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1392 return FALSE;
1393 if (infoPtr->uNumTools == 0)
1394 return FALSE;
1396 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1397 if (nTool == -1)
1398 return FALSE;
1400 TRACE("tool %d\n", nTool);
1402 hwnd = ti->hwnd;
1403 TOOLTIPS_CopyInfoT (infoPtr, nTool, ti, isW);
1404 ti->hwnd = hwnd;
1406 return TRUE;
1410 static LRESULT
1411 TOOLTIPS_HitTestT (const TOOLTIPS_INFO *infoPtr, LPTTHITTESTINFOW lptthit,
1412 BOOL isW)
1414 INT nTool;
1416 if (lptthit == 0)
1417 return FALSE;
1419 nTool = TOOLTIPS_GetToolFromPoint (infoPtr, lptthit->hwnd, &lptthit->pt);
1420 if (nTool == -1)
1421 return FALSE;
1423 TRACE("tool %d\n", nTool);
1425 /* copy tool data */
1426 if (lptthit->ti.cbSize >= TTTOOLINFOW_V1_SIZE)
1427 TOOLTIPS_CopyInfoT (infoPtr, nTool, &lptthit->ti, isW);
1429 return TRUE;
1433 static LRESULT
1434 TOOLTIPS_NewToolRectT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti)
1436 INT nTool;
1438 if (!ti) return 0;
1439 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1440 return FALSE;
1442 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1444 TRACE("nTool = %d, rect = %s\n", nTool, wine_dbgstr_rect(&ti->rect));
1446 if (nTool == -1) return 0;
1448 infoPtr->tools[nTool].rect = ti->rect;
1450 return 0;
1454 static inline LRESULT
1455 TOOLTIPS_Pop (TOOLTIPS_INFO *infoPtr)
1457 TOOLTIPS_Hide (infoPtr);
1459 return 0;
1463 static LRESULT
1464 TOOLTIPS_RelayEvent (TOOLTIPS_INFO *infoPtr, LPMSG lpMsg)
1466 POINT pt;
1467 INT nOldTool;
1469 if (!lpMsg) {
1470 ERR("lpMsg == NULL\n");
1471 return 0;
1474 switch (lpMsg->message) {
1475 case WM_LBUTTONDOWN:
1476 case WM_LBUTTONUP:
1477 case WM_MBUTTONDOWN:
1478 case WM_MBUTTONUP:
1479 case WM_RBUTTONDOWN:
1480 case WM_RBUTTONUP:
1481 TOOLTIPS_Hide (infoPtr);
1482 break;
1484 case WM_MOUSEMOVE:
1485 pt.x = (short)LOWORD(lpMsg->lParam);
1486 pt.y = (short)HIWORD(lpMsg->lParam);
1487 nOldTool = infoPtr->nTool;
1488 infoPtr->nTool = TOOLTIPS_GetToolFromPoint(infoPtr, lpMsg->hwnd,
1489 &pt);
1490 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
1491 infoPtr->nTool, infoPtr->nCurrentTool);
1492 TRACE("WM_MOUSEMOVE (%p %s)\n", infoPtr->hwndSelf, wine_dbgstr_point(&pt));
1494 if (infoPtr->nTool != nOldTool) {
1495 if(infoPtr->nTool == -1) { /* Moved out of all tools */
1496 TOOLTIPS_Hide(infoPtr);
1497 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1498 } else if (nOldTool == -1) { /* Moved from outside */
1499 if(infoPtr->bActive) {
1500 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1501 TRACE("timer 1 started\n");
1503 } else { /* Moved from one to another */
1504 TOOLTIPS_Hide (infoPtr);
1505 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
1506 if(infoPtr->bActive) {
1507 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
1508 TRACE("timer 1 started\n");
1511 } else if(infoPtr->nCurrentTool != -1) { /* restart autopop */
1512 KillTimer(infoPtr->hwndSelf, ID_TIMERPOP);
1513 SetTimer(infoPtr->hwndSelf, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
1514 TRACE("timer 2 restarted\n");
1515 } else if(infoPtr->nTool != -1 && infoPtr->bActive) {
1516 /* previous show attempt didn't result in tooltip so try again */
1517 SetTimer(infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nInitialTime, 0);
1518 TRACE("timer 1 started\n");
1520 break;
1523 return 0;
1527 static LRESULT
1528 TOOLTIPS_SetDelayTime (TOOLTIPS_INFO *infoPtr, DWORD duration, INT nTime)
1530 switch (duration) {
1531 case TTDT_AUTOMATIC:
1532 if (nTime <= 0)
1533 nTime = GetDoubleClickTime();
1534 infoPtr->nReshowTime = nTime / 5;
1535 infoPtr->nAutoPopTime = nTime * 10;
1536 infoPtr->nInitialTime = nTime;
1537 break;
1539 case TTDT_RESHOW:
1540 if(nTime < 0)
1541 nTime = GetDoubleClickTime() / 5;
1542 infoPtr->nReshowTime = nTime;
1543 break;
1545 case TTDT_AUTOPOP:
1546 if(nTime < 0)
1547 nTime = GetDoubleClickTime() * 10;
1548 infoPtr->nAutoPopTime = nTime;
1549 break;
1551 case TTDT_INITIAL:
1552 if(nTime < 0)
1553 nTime = GetDoubleClickTime();
1554 infoPtr->nInitialTime = nTime;
1555 break;
1557 default:
1558 WARN("Invalid duration flag %x\n", duration);
1559 break;
1562 return 0;
1566 static LRESULT
1567 TOOLTIPS_SetMargin (TOOLTIPS_INFO *infoPtr, const RECT *rect)
1569 if (rect)
1570 infoPtr->rcMargin = *rect;
1572 return 0;
1576 static inline LRESULT
1577 TOOLTIPS_SetMaxTipWidth (TOOLTIPS_INFO *infoPtr, INT MaxWidth)
1579 INT nTemp = infoPtr->nMaxTipWidth;
1581 infoPtr->nMaxTipWidth = MaxWidth;
1583 return nTemp;
1587 static inline LRESULT
1588 TOOLTIPS_SetTipBkColor (TOOLTIPS_INFO *infoPtr, COLORREF clrBk)
1590 infoPtr->clrBk = clrBk;
1592 return 0;
1596 static inline LRESULT
1597 TOOLTIPS_SetTipTextColor (TOOLTIPS_INFO *infoPtr, COLORREF clrText)
1599 infoPtr->clrText = clrText;
1601 return 0;
1605 static LRESULT
1606 TOOLTIPS_SetTitleT (TOOLTIPS_INFO *infoPtr, UINT_PTR uTitleIcon, LPCWSTR pszTitle,
1607 BOOL isW)
1609 UINT size;
1611 TRACE("hwnd = %p, title = %s, icon = %p\n", infoPtr->hwndSelf, debugstr_w(pszTitle),
1612 (void*)uTitleIcon);
1614 Free(infoPtr->pszTitle);
1616 if (pszTitle)
1618 if (isW)
1620 size = (strlenW(pszTitle)+1)*sizeof(WCHAR);
1621 infoPtr->pszTitle = Alloc(size);
1622 if (!infoPtr->pszTitle)
1623 return FALSE;
1624 memcpy(infoPtr->pszTitle, pszTitle, size);
1626 else
1628 size = sizeof(WCHAR)*MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, NULL, 0);
1629 infoPtr->pszTitle = Alloc(size);
1630 if (!infoPtr->pszTitle)
1631 return FALSE;
1632 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszTitle, -1, infoPtr->pszTitle, size/sizeof(WCHAR));
1635 else
1636 infoPtr->pszTitle = NULL;
1638 if (uTitleIcon <= TTI_ERROR)
1639 infoPtr->hTitleIcon = hTooltipIcons[uTitleIcon];
1640 else
1641 infoPtr->hTitleIcon = CopyIcon((HICON)uTitleIcon);
1643 TRACE("icon = %p\n", infoPtr->hTitleIcon);
1645 return TRUE;
1649 static LRESULT
1650 TOOLTIPS_SetToolInfoT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1652 TTTOOL_INFO *toolPtr;
1653 INT nTool;
1655 if (!ti) return 0;
1656 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1657 return 0;
1659 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1660 if (nTool == -1) return 0;
1662 TRACE("tool %d\n", nTool);
1664 toolPtr = &infoPtr->tools[nTool];
1666 /* copy tool data */
1667 toolPtr->uFlags = ti->uFlags;
1668 toolPtr->hwnd = ti->hwnd;
1669 toolPtr->uId = ti->uId;
1670 toolPtr->rect = ti->rect;
1671 toolPtr->hinst = ti->hinst;
1673 if (IS_INTRESOURCE(ti->lpszText)) {
1674 TRACE("set string id %x\n", LOWORD(ti->lpszText));
1675 toolPtr->lpszText = ti->lpszText;
1677 else {
1678 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1679 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1680 else {
1681 if ( (toolPtr->lpszText) &&
1682 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1683 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1684 Free (toolPtr->lpszText);
1685 toolPtr->lpszText = NULL;
1687 if (ti->lpszText) {
1688 if (isW) {
1689 INT len = lstrlenW (ti->lpszText);
1690 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1691 strcpyW (toolPtr->lpszText, ti->lpszText);
1693 else {
1694 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1695 -1, NULL, 0);
1696 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1697 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1698 toolPtr->lpszText, len);
1704 if (ti->cbSize >= TTTOOLINFOW_V2_SIZE)
1705 toolPtr->lParam = ti->lParam;
1707 if (infoPtr->nCurrentTool == nTool)
1709 TOOLTIPS_GetTipText (infoPtr, infoPtr->nCurrentTool, infoPtr->szTipText);
1711 if (infoPtr->szTipText[0] == 0)
1712 TOOLTIPS_Hide(infoPtr);
1713 else
1714 TOOLTIPS_Show (infoPtr, FALSE);
1717 return 0;
1721 static LRESULT
1722 TOOLTIPS_TrackActivate (TOOLTIPS_INFO *infoPtr, BOOL track_activate, const TTTOOLINFOA *ti)
1724 if (track_activate) {
1726 if (!ti) return 0;
1727 if (ti->cbSize < TTTOOLINFOA_V1_SIZE)
1728 return FALSE;
1730 /* activate */
1731 infoPtr->nTrackTool = TOOLTIPS_GetToolFromInfoT (infoPtr, (const TTTOOLINFOW*)ti);
1732 if (infoPtr->nTrackTool != -1) {
1733 TRACE("activated\n");
1734 infoPtr->bTrackActive = TRUE;
1735 TOOLTIPS_TrackShow (infoPtr);
1738 else {
1739 /* deactivate */
1740 TOOLTIPS_TrackHide (infoPtr);
1742 infoPtr->bTrackActive = FALSE;
1743 infoPtr->nTrackTool = -1;
1745 TRACE("deactivated\n");
1748 return 0;
1752 static LRESULT
1753 TOOLTIPS_TrackPosition (TOOLTIPS_INFO *infoPtr, LPARAM coord)
1755 infoPtr->xTrackPos = (INT)LOWORD(coord);
1756 infoPtr->yTrackPos = (INT)HIWORD(coord);
1758 if (infoPtr->bTrackActive) {
1759 TRACE("[%d %d]\n",
1760 infoPtr->xTrackPos, infoPtr->yTrackPos);
1762 TOOLTIPS_TrackShow (infoPtr);
1765 return 0;
1769 static LRESULT
1770 TOOLTIPS_Update (TOOLTIPS_INFO *infoPtr)
1772 if (infoPtr->nCurrentTool != -1)
1773 UpdateWindow (infoPtr->hwndSelf);
1775 return 0;
1779 static LRESULT
1780 TOOLTIPS_UpdateTipTextT (TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *ti, BOOL isW)
1782 TTTOOL_INFO *toolPtr;
1783 INT nTool;
1785 if (!ti) return 0;
1786 if (ti->cbSize < TTTOOLINFOW_V1_SIZE)
1787 return FALSE;
1789 nTool = TOOLTIPS_GetToolFromInfoT (infoPtr, ti);
1790 if (nTool == -1)
1791 return 0;
1793 TRACE("tool %d\n", nTool);
1795 toolPtr = &infoPtr->tools[nTool];
1797 /* copy tool text */
1798 toolPtr->hinst = ti->hinst;
1800 if (IS_INTRESOURCE(ti->lpszText)){
1801 toolPtr->lpszText = ti->lpszText;
1803 else if (ti->lpszText) {
1804 if (TOOLTIPS_IsCallbackString(ti->lpszText, isW))
1805 toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
1806 else {
1807 if ( (toolPtr->lpszText) &&
1808 !IS_INTRESOURCE(toolPtr->lpszText) ) {
1809 if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
1810 Free (toolPtr->lpszText);
1811 toolPtr->lpszText = NULL;
1813 if (ti->lpszText) {
1814 if (isW) {
1815 INT len = lstrlenW (ti->lpszText);
1816 toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
1817 strcpyW (toolPtr->lpszText, ti->lpszText);
1819 else {
1820 INT len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText,
1821 -1, NULL, 0);
1822 toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
1823 MultiByteToWideChar(CP_ACP, 0, (LPSTR)ti->lpszText, -1,
1824 toolPtr->lpszText, len);
1830 if(infoPtr->nCurrentTool == -1) return 0;
1831 /* force repaint */
1832 if (infoPtr->bActive)
1833 TOOLTIPS_Show (infoPtr, FALSE);
1834 else if (infoPtr->bTrackActive)
1835 TOOLTIPS_Show (infoPtr, TRUE);
1837 return 0;
1841 static LRESULT
1842 TOOLTIPS_Create (HWND hwnd)
1844 TOOLTIPS_INFO *infoPtr;
1846 /* allocate memory for info structure */
1847 infoPtr = Alloc (sizeof(TOOLTIPS_INFO));
1848 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1850 /* initialize info structure */
1851 infoPtr->bActive = TRUE;
1852 infoPtr->bTrackActive = FALSE;
1854 infoPtr->nMaxTipWidth = -1;
1855 infoPtr->nTool = -1;
1856 infoPtr->nCurrentTool = -1;
1857 infoPtr->nTrackTool = -1;
1858 infoPtr->hwndSelf = hwnd;
1860 /* initialize colours and fonts */
1861 TOOLTIPS_InitSystemSettings(infoPtr);
1863 TOOLTIPS_SetDelayTime(infoPtr, TTDT_AUTOMATIC, 0);
1865 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
1867 return 0;
1871 static LRESULT
1872 TOOLTIPS_Destroy (TOOLTIPS_INFO *infoPtr)
1874 TTTOOL_INFO *toolPtr;
1875 UINT i;
1877 /* free tools */
1878 if (infoPtr->tools) {
1879 for (i = 0; i < infoPtr->uNumTools; i++) {
1880 toolPtr = &infoPtr->tools[i];
1881 if (toolPtr->lpszText) {
1882 if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
1883 !IS_INTRESOURCE(toolPtr->lpszText) )
1885 Free (toolPtr->lpszText);
1886 toolPtr->lpszText = NULL;
1890 TOOLTIPS_ResetSubclass (toolPtr);
1893 Free (infoPtr->tools);
1896 /* free title string */
1897 Free (infoPtr->pszTitle);
1898 /* free title icon if not a standard one */
1899 if (TOOLTIPS_GetTitleIconIndex(infoPtr->hTitleIcon) > TTI_ERROR)
1900 DeleteObject(infoPtr->hTitleIcon);
1902 /* delete fonts */
1903 DeleteObject (infoPtr->hFont);
1904 DeleteObject (infoPtr->hTitleFont);
1906 /* free tool tips info data */
1907 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1908 Free (infoPtr);
1910 return 0;
1914 static inline LRESULT
1915 TOOLTIPS_GetFont (const TOOLTIPS_INFO *infoPtr)
1917 return (LRESULT)infoPtr->hFont;
1921 static LRESULT
1922 TOOLTIPS_MouseMessage (TOOLTIPS_INFO *infoPtr)
1924 TOOLTIPS_Hide (infoPtr);
1926 return 0;
1930 static LRESULT
1931 TOOLTIPS_NCCreate (HWND hwnd)
1933 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1934 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
1936 dwStyle &= ~(WS_CHILD | /*WS_MAXIMIZE |*/ WS_BORDER | WS_DLGFRAME);
1937 dwStyle |= (WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS);
1939 /* WS_BORDER only draws a border round the window rect, not the
1940 * window region, therefore it is useless to us in balloon mode */
1941 if (dwStyle & TTS_BALLOON) dwStyle &= ~WS_BORDER;
1943 SetWindowLongW (hwnd, GWL_STYLE, dwStyle);
1945 dwExStyle |= WS_EX_TOOLWINDOW;
1946 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
1948 return TRUE;
1952 static LRESULT
1953 TOOLTIPS_NCHitTest (const TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1955 INT nTool = (infoPtr->bTrackActive) ? infoPtr->nTrackTool : infoPtr->nTool;
1957 TRACE(" nTool=%d\n", nTool);
1959 if ((nTool > -1) && (nTool < infoPtr->uNumTools)) {
1960 if (infoPtr->tools[nTool].uFlags & TTF_TRANSPARENT) {
1961 TRACE("-- in transparent mode\n");
1962 return HTTRANSPARENT;
1966 return DefWindowProcW (infoPtr->hwndSelf, WM_NCHITTEST, wParam, lParam);
1970 static LRESULT
1971 TOOLTIPS_NotifyFormat (TOOLTIPS_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1973 FIXME ("hwnd=%p wParam=%lx lParam=%lx\n", infoPtr->hwndSelf, wParam, lParam);
1975 return 0;
1979 static LRESULT
1980 TOOLTIPS_Paint (const TOOLTIPS_INFO *infoPtr, HDC hDC)
1982 HDC hdc;
1983 PAINTSTRUCT ps;
1985 hdc = (hDC == NULL) ? BeginPaint (infoPtr->hwndSelf, &ps) : hDC;
1986 TOOLTIPS_Refresh (infoPtr, hdc);
1987 if (!hDC)
1988 EndPaint (infoPtr->hwndSelf, &ps);
1989 return 0;
1993 static LRESULT
1994 TOOLTIPS_SetFont (TOOLTIPS_INFO *infoPtr, HFONT hFont, BOOL redraw)
1996 LOGFONTW lf;
1998 if(!GetObjectW(hFont, sizeof(lf), &lf))
1999 return 0;
2001 DeleteObject (infoPtr->hFont);
2002 infoPtr->hFont = CreateFontIndirectW(&lf);
2004 DeleteObject (infoPtr->hTitleFont);
2005 lf.lfWeight = FW_BOLD;
2006 infoPtr->hTitleFont = CreateFontIndirectW(&lf);
2008 if (redraw && infoPtr->nCurrentTool != -1)
2009 FIXME("full redraw needed\n");
2011 return 0;
2014 /******************************************************************
2015 * TOOLTIPS_GetTextLength
2017 * This function is called when the tooltip receive a
2018 * WM_GETTEXTLENGTH message.
2020 * returns the length, in characters, of the tip text
2022 static inline LRESULT
2023 TOOLTIPS_GetTextLength(const TOOLTIPS_INFO *infoPtr)
2025 return strlenW(infoPtr->szTipText);
2028 /******************************************************************
2029 * TOOLTIPS_OnWMGetText
2031 * This function is called when the tooltip receive a
2032 * WM_GETTEXT message.
2033 * wParam : specifies the maximum number of characters to be copied
2034 * lParam : is the pointer to the buffer that will receive
2035 * the tip text
2037 * returns the number of characters copied
2039 static LRESULT
2040 TOOLTIPS_OnWMGetText (const TOOLTIPS_INFO *infoPtr, WPARAM size, LPWSTR pszText)
2042 LRESULT res;
2044 if(!size)
2045 return 0;
2047 res = min(strlenW(infoPtr->szTipText)+1, size);
2048 memcpy(pszText, infoPtr->szTipText, res*sizeof(WCHAR));
2049 pszText[res-1] = '\0';
2050 return res-1;
2053 static LRESULT
2054 TOOLTIPS_Timer (TOOLTIPS_INFO *infoPtr, INT iTimer)
2056 INT nOldTool;
2058 TRACE("timer %d (%p) expired\n", iTimer, infoPtr->hwndSelf);
2060 switch (iTimer) {
2061 case ID_TIMERSHOW:
2062 KillTimer (infoPtr->hwndSelf, ID_TIMERSHOW);
2063 nOldTool = infoPtr->nTool;
2064 if ((infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, TRUE)) == nOldTool)
2065 TOOLTIPS_Show (infoPtr, FALSE);
2066 break;
2068 case ID_TIMERPOP:
2069 TOOLTIPS_Hide (infoPtr);
2070 break;
2072 case ID_TIMERLEAVE:
2073 nOldTool = infoPtr->nTool;
2074 infoPtr->nTool = TOOLTIPS_CheckTool (infoPtr, FALSE);
2075 TRACE("tool (%p) %d %d %d\n", infoPtr->hwndSelf, nOldTool,
2076 infoPtr->nTool, infoPtr->nCurrentTool);
2077 if (infoPtr->nTool != nOldTool) {
2078 if(infoPtr->nTool == -1) { /* Moved out of all tools */
2079 TOOLTIPS_Hide(infoPtr);
2080 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2081 } else if (nOldTool == -1) { /* Moved from outside */
2082 ERR("How did this happen?\n");
2083 } else { /* Moved from one to another */
2084 TOOLTIPS_Hide (infoPtr);
2085 KillTimer(infoPtr->hwndSelf, ID_TIMERLEAVE);
2086 if(infoPtr->bActive) {
2087 SetTimer (infoPtr->hwndSelf, ID_TIMERSHOW, infoPtr->nReshowTime, 0);
2088 TRACE("timer 1 started!\n");
2092 break;
2094 default:
2095 ERR("Unknown timer id %d\n", iTimer);
2096 break;
2098 return 0;
2102 static LRESULT
2103 TOOLTIPS_WinIniChange (TOOLTIPS_INFO *infoPtr)
2105 TOOLTIPS_InitSystemSettings (infoPtr);
2107 return 0;
2111 static LRESULT CALLBACK
2112 TOOLTIPS_SubclassProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
2114 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr ((HWND)dwRef);
2115 MSG msg;
2117 switch (message)
2119 case WM_MOUSEMOVE:
2120 case WM_LBUTTONDOWN:
2121 case WM_LBUTTONUP:
2122 case WM_MBUTTONDOWN:
2123 case WM_MBUTTONUP:
2124 case WM_RBUTTONDOWN:
2125 case WM_RBUTTONUP:
2126 if (infoPtr)
2128 msg.hwnd = hwnd;
2129 msg.message = message;
2130 msg.wParam = wParam;
2131 msg.lParam = lParam;
2132 TOOLTIPS_RelayEvent(infoPtr, &msg);
2134 break;
2135 case WM_NCDESTROY:
2136 RemoveWindowSubclass(hwnd, TOOLTIPS_SubclassProc, 1);
2137 break;
2138 default:
2139 break;
2142 return DefSubclassProc(hwnd, message, wParam, lParam);
2146 static LRESULT CALLBACK
2147 TOOLTIPS_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2149 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr (hwnd);
2151 TRACE("hwnd=%p msg=%x wparam=%lx lParam=%lx\n", hwnd, uMsg, wParam, lParam);
2152 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
2153 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2154 switch (uMsg)
2156 case TTM_ACTIVATE:
2157 return TOOLTIPS_Activate (infoPtr, (BOOL)wParam);
2159 case TTM_ADDTOOLA:
2160 case TTM_ADDTOOLW:
2161 return TOOLTIPS_AddToolT (infoPtr, (LPTTTOOLINFOW)lParam, uMsg == TTM_ADDTOOLW);
2163 case TTM_DELTOOLA:
2164 case TTM_DELTOOLW:
2165 return TOOLTIPS_DelToolT (infoPtr, (LPTOOLINFOW)lParam,
2166 uMsg == TTM_DELTOOLW);
2167 case TTM_ENUMTOOLSA:
2168 case TTM_ENUMTOOLSW:
2169 return TOOLTIPS_EnumToolsT (infoPtr, (UINT)wParam, (LPTTTOOLINFOW)lParam,
2170 uMsg == TTM_ENUMTOOLSW);
2171 case TTM_GETBUBBLESIZE:
2172 return TOOLTIPS_GetBubbleSize (infoPtr, (LPTTTOOLINFOW)lParam);
2174 case TTM_GETCURRENTTOOLA:
2175 case TTM_GETCURRENTTOOLW:
2176 return TOOLTIPS_GetCurrentToolT (infoPtr, (LPTTTOOLINFOW)lParam,
2177 uMsg == TTM_GETCURRENTTOOLW);
2179 case TTM_GETDELAYTIME:
2180 return TOOLTIPS_GetDelayTime (infoPtr, (DWORD)wParam);
2182 case TTM_GETMARGIN:
2183 return TOOLTIPS_GetMargin (infoPtr, (LPRECT)lParam);
2185 case TTM_GETMAXTIPWIDTH:
2186 return TOOLTIPS_GetMaxTipWidth (infoPtr);
2188 case TTM_GETTEXTA:
2189 case TTM_GETTEXTW:
2190 return TOOLTIPS_GetTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2191 uMsg == TTM_GETTEXTW);
2193 case TTM_GETTIPBKCOLOR:
2194 return TOOLTIPS_GetTipBkColor (infoPtr);
2196 case TTM_GETTIPTEXTCOLOR:
2197 return TOOLTIPS_GetTipTextColor (infoPtr);
2199 case TTM_GETTOOLCOUNT:
2200 return TOOLTIPS_GetToolCount (infoPtr);
2202 case TTM_GETTOOLINFOA:
2203 case TTM_GETTOOLINFOW:
2204 return TOOLTIPS_GetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2205 uMsg == TTM_GETTOOLINFOW);
2207 case TTM_HITTESTA:
2208 case TTM_HITTESTW:
2209 return TOOLTIPS_HitTestT (infoPtr, (LPTTHITTESTINFOW)lParam,
2210 uMsg == TTM_HITTESTW);
2211 case TTM_NEWTOOLRECTA:
2212 case TTM_NEWTOOLRECTW:
2213 return TOOLTIPS_NewToolRectT (infoPtr, (LPTTTOOLINFOW)lParam);
2215 case TTM_POP:
2216 return TOOLTIPS_Pop (infoPtr);
2218 case TTM_RELAYEVENT:
2219 return TOOLTIPS_RelayEvent (infoPtr, (LPMSG)lParam);
2221 case TTM_SETDELAYTIME:
2222 return TOOLTIPS_SetDelayTime (infoPtr, (DWORD)wParam, (INT)LOWORD(lParam));
2224 case TTM_SETMARGIN:
2225 return TOOLTIPS_SetMargin (infoPtr, (LPRECT)lParam);
2227 case TTM_SETMAXTIPWIDTH:
2228 return TOOLTIPS_SetMaxTipWidth (infoPtr, (INT)lParam);
2230 case TTM_SETTIPBKCOLOR:
2231 return TOOLTIPS_SetTipBkColor (infoPtr, (COLORREF)wParam);
2233 case TTM_SETTIPTEXTCOLOR:
2234 return TOOLTIPS_SetTipTextColor (infoPtr, (COLORREF)wParam);
2236 case TTM_SETTITLEA:
2237 case TTM_SETTITLEW:
2238 return TOOLTIPS_SetTitleT (infoPtr, (UINT_PTR)wParam, (LPCWSTR)lParam,
2239 uMsg == TTM_SETTITLEW);
2241 case TTM_SETTOOLINFOA:
2242 case TTM_SETTOOLINFOW:
2243 return TOOLTIPS_SetToolInfoT (infoPtr, (LPTTTOOLINFOW)lParam,
2244 uMsg == TTM_SETTOOLINFOW);
2246 case TTM_TRACKACTIVATE:
2247 return TOOLTIPS_TrackActivate (infoPtr, (BOOL)wParam, (LPTTTOOLINFOA)lParam);
2249 case TTM_TRACKPOSITION:
2250 return TOOLTIPS_TrackPosition (infoPtr, lParam);
2252 case TTM_UPDATE:
2253 return TOOLTIPS_Update (infoPtr);
2255 case TTM_UPDATETIPTEXTA:
2256 case TTM_UPDATETIPTEXTW:
2257 return TOOLTIPS_UpdateTipTextT (infoPtr, (LPTTTOOLINFOW)lParam,
2258 uMsg == TTM_UPDATETIPTEXTW);
2260 case TTM_WINDOWFROMPOINT:
2261 return (LRESULT)WindowFromPoint (*((LPPOINT)lParam));
2263 case WM_CREATE:
2264 return TOOLTIPS_Create (hwnd);
2266 case WM_DESTROY:
2267 return TOOLTIPS_Destroy (infoPtr);
2269 case WM_ERASEBKGND:
2270 /* we draw the background in WM_PAINT */
2271 return 0;
2273 case WM_GETFONT:
2274 return TOOLTIPS_GetFont (infoPtr);
2276 case WM_GETTEXT:
2277 return TOOLTIPS_OnWMGetText (infoPtr, wParam, (LPWSTR)lParam);
2279 case WM_GETTEXTLENGTH:
2280 return TOOLTIPS_GetTextLength (infoPtr);
2282 case WM_LBUTTONDOWN:
2283 case WM_LBUTTONUP:
2284 case WM_MBUTTONDOWN:
2285 case WM_MBUTTONUP:
2286 case WM_RBUTTONDOWN:
2287 case WM_RBUTTONUP:
2288 case WM_MOUSEMOVE:
2289 return TOOLTIPS_MouseMessage (infoPtr);
2291 case WM_NCCREATE:
2292 return TOOLTIPS_NCCreate (hwnd);
2294 case WM_NCHITTEST:
2295 return TOOLTIPS_NCHitTest (infoPtr, wParam, lParam);
2297 case WM_NOTIFYFORMAT:
2298 return TOOLTIPS_NotifyFormat (infoPtr, wParam, lParam);
2300 case WM_PRINTCLIENT:
2301 case WM_PAINT:
2302 return TOOLTIPS_Paint (infoPtr, (HDC)wParam);
2304 case WM_SETFONT:
2305 return TOOLTIPS_SetFont (infoPtr, (HFONT)wParam, LOWORD(lParam));
2307 case WM_SYSCOLORCHANGE:
2308 COMCTL32_RefreshSysColors();
2309 return 0;
2311 case WM_TIMER:
2312 return TOOLTIPS_Timer (infoPtr, (INT)wParam);
2314 case WM_WININICHANGE:
2315 return TOOLTIPS_WinIniChange (infoPtr);
2317 default:
2318 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2319 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
2320 uMsg, wParam, lParam);
2321 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2326 VOID
2327 TOOLTIPS_Register (void)
2329 WNDCLASSW wndClass;
2331 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2332 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
2333 wndClass.lpfnWndProc = TOOLTIPS_WindowProc;
2334 wndClass.cbClsExtra = 0;
2335 wndClass.cbWndExtra = sizeof(TOOLTIPS_INFO *);
2336 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
2337 wndClass.hbrBackground = 0;
2338 wndClass.lpszClassName = TOOLTIPS_CLASSW;
2340 RegisterClassW (&wndClass);
2342 hTooltipIcons[TTI_NONE] = NULL;
2343 hTooltipIcons[TTI_INFO] = LoadImageW(COMCTL32_hModule,
2344 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_INFO_SM), IMAGE_ICON, 0, 0, 0);
2345 hTooltipIcons[TTI_WARNING] = LoadImageW(COMCTL32_hModule,
2346 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_WARN_SM), IMAGE_ICON, 0, 0, 0);
2347 hTooltipIcons[TTI_ERROR] = LoadImageW(COMCTL32_hModule,
2348 (LPCWSTR)MAKEINTRESOURCE(IDI_TT_ERROR_SM), IMAGE_ICON, 0, 0, 0);
2352 VOID
2353 TOOLTIPS_Unregister (void)
2355 int i;
2356 for (i = TTI_INFO; i <= TTI_ERROR; i++)
2357 DestroyIcon(hTooltipIcons[i]);
2358 UnregisterClassW (TOOLTIPS_CLASSW, NULL);