1 /*
2 * Tool tip control
3 *
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 2004 Robert Shearman
6 *
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.
11 *
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.
16 *
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
20 *
21 * NOTES
22 *
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.
25 *
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.
29 *
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
43 *
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.
49 *
50 * Timer logic.
51 *
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.
60 *
61 *
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.
70 *
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.
80 *
81 *
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.
84 *
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.
89 *
90 */
91
92
93
94 #include <stdarg.h>
95 #include <string.h>
96
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"
106
107 WINE_DEFAULT_DEBUG_CHANNEL(tooltips);
108
109 static HICON hTooltipIcons[TTI_ERROR+1];
110
111 typedef struct
112 {
113 UINT uFlags;
114 HWND hwnd;
115 BOOL bNotifyUnicode;
116 UINT_PTR uId;
117 RECT rect;
118 HINSTANCE hinst;
119 LPWSTR lpszText;
120 LPARAM lParam;
121 } TTTOOL_INFO;
122
123
124 typedef struct
125 {
126 WCHAR szTipText[INFOTIPSIZE];
127 BOOL bActive;
128 BOOL bTrackActive;
129 UINT uNumTools;
130 COLORREF clrBk;
131 COLORREF clrText;
132 HFONT hFont;
133 HFONT hTitleFont;
134 INT xTrackPos;
135 INT yTrackPos;
136 INT nMaxTipWidth;
137 INT nTool; /* tool that mouse was on on last relayed mouse move */
138 INT nCurrentTool;
139 INT nTrackTool;
140 INT nReshowTime;
141 INT nAutoPopTime;
142 INT nInitialTime;
143 RECT rcMargin;
144 BOOL bToolBelow;
145 LPWSTR pszTitle;
146 HICON hTitleIcon;
147
148 TTTOOL_INFO *tools;
149 } TOOLTIPS_INFO;
150
151 #define ID_TIMERSHOW 1 /* show delay timer */
152 #define ID_TIMERPOP 2 /* auto pop timer */
153 #define ID_TIMERLEAVE 3 /* tool leave timer */
154
155
156 #define TOOLTIPS_GetInfoPtr(hWindow) ((TOOLTIPS_INFO *)GetWindowLongPtrW (hWindow, 0))
157
158 /* offsets from window edge to start of text */
159 #define NORMAL_TEXT_MARGIN 2
160 #define BALLOON_TEXT_MARGIN (NORMAL_TEXT_MARGIN+8)
161 /* value used for CreateRoundRectRgn that specifies how much
162 * each corner is curved */
163 #define BALLOON_ROUNDEDNESS 20
164 #define BALLOON_STEMHEIGHT 13
165 #define BALLOON_STEMWIDTH 10
166 #define BALLOON_STEMINDENT 20
167
168 #define BALLOON_ICON_TITLE_SPACING 8 /* horizontal spacing between icon and title */
169 #define BALLOON_TITLE_TEXT_SPACING 8 /* vertical spacing between icon/title and main text */
170 #define ICON_HEIGHT 16
171 #define ICON_WIDTH 16
172
173 static LRESULT CALLBACK
174 TOOLTIPS_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uId, DWORD_PTR dwRef);
175
176
177 static inline UINT_PTR
178 TOOLTIPS_GetTitleIconIndex(HICON hIcon)
179 {
180 UINT i;
181 for (i = 0; i <= TTI_ERROR; i++)
182 if (hTooltipIcons[i] == hIcon)
183 return i;
184 return (UINT_PTR)hIcon;
185 }
186
187 static void
188 TOOLTIPS_InitSystemSettings (TOOLTIPS_INFO *infoPtr)
189 {
190 NONCLIENTMETRICSW nclm;
191
192 infoPtr->clrBk = GetSysColor (COLOR_INFOBK);
193 infoPtr->clrText = GetSysColor (COLOR_INFOTEXT);
194
195 DeleteObject (infoPtr->hFont);
196 nclm.cbSize = sizeof(nclm);
197 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(nclm), &nclm, 0);
198 infoPtr->hFont = CreateFontIndirectW (&nclm.lfStatusFont);
199
200 DeleteObject (infoPtr->hTitleFont);
201 nclm.lfStatusFont.lfWeight = FW_BOLD;
202 infoPtr->hTitleFont = CreateFontIndirectW (&nclm.lfStatusFont);
203 }
204
205 /* Custom draw routines */
206 static void
207 TOOLTIPS_customdraw_fill(NMTTCUSTOMDRAW *lpnmttcd,
208 const HWND hwnd,
209 HDC hdc, const RECT *rcBounds, UINT uFlags)
210 {
211 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr(hwnd);
212
213 ZeroMemory(lpnmttcd, sizeof(NMTTCUSTOMDRAW));
214 lpnmttcd->uDrawFlags = uFlags;
215 lpnmttcd->nmcd.hdr.hwndFrom = hwnd;
216 lpnmttcd->nmcd.hdr.code = NM_CUSTOMDRAW;
217 if (infoPtr->nCurrentTool != -1) {
218 TTTOOL_INFO *toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
219 lpnmttcd->nmcd.hdr.idFrom = toolPtr->uId;
220 }
221 lpnmttcd->nmcd.hdc = hdc;
222 lpnmttcd->nmcd.rc = *rcBounds;
223 /* FIXME - dwItemSpec, uItemState, lItemlParam */
224 }
225
226 static inline DWORD
227 TOOLTIPS_notify_customdraw (DWORD dwDrawStage, NMTTCUSTOMDRAW *lpnmttcd)
228 {
229 LRESULT result = CDRF_DODEFAULT;
230 lpnmttcd->nmcd.dwDrawStage = dwDrawStage;
231
232 TRACE("Notifying stage %d, flags %x, id %x\n", lpnmttcd->nmcd.dwDrawStage,
233 lpnmttcd->uDrawFlags, lpnmttcd->nmcd.hdr.code);
234
235 result = SendMessageW(GetParent(lpnmttcd->nmcd.hdr.hwndFrom), WM_NOTIFY,
236 0, (LPARAM)lpnmttcd);
237
238 TRACE("Notify result %x\n", (unsigned int)result);
239
240 return result;
241 }
242
243 static void
244 TOOLTIPS_Refresh (HWND hwnd, HDC hdc)
245 {
246 TOOLTIPS_INFO *infoPtr = TOOLTIPS_GetInfoPtr(hwnd);
247 RECT rc;
248 INT oldBkMode;
249 HFONT hOldFont;
250 HBRUSH hBrush;
251 UINT uFlags = DT_EXTERNALLEADING;
252 HRGN hRgn = NULL;
253 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
254 NMTTCUSTOMDRAW nmttcd;
255 DWORD cdmode;
256
257 if (infoPtr->nMaxTipWidth > -1)
258 uFlags |= DT_WORDBREAK;
259 if (GetWindowLongW (hwnd, GWL_STYLE) & TTS_NOPREFIX)
260 uFlags |= DT_NOPREFIX;
261 GetClientRect (hwnd, &rc);
262
263 hBrush = CreateSolidBrush(infoPtr->clrBk);
264
265 oldBkMode = SetBkMode (hdc, TRANSPARENT);
266 SetTextColor (hdc, infoPtr->clrText);
267 hOldFont = SelectObject (hdc, infoPtr->hFont);
268
269 /* Custom draw - Call PrePaint once initial properties set up */
270 /* Note: Contrary to MSDN, CDRF_SKIPDEFAULT still draws a tooltip */
271 TOOLTIPS_customdraw_fill(&nmttcd, hwnd, hdc, &rc, uFlags);
272 cdmode = TOOLTIPS_notify_customdraw(CDDS_PREPAINT, &nmttcd);
273 uFlags = nmttcd.uDrawFlags;
274
275 if (dwStyle & TTS_BALLOON)
276 {
277 /* create a region to store result into */
278 hRgn = CreateRectRgn(0, 0, 0, 0);
279
280 GetWindowRgn(hwnd, hRgn);
281
282 /* fill the background */
283 FillRgn(hdc, hRgn, hBrush);
284 DeleteObject(hBrush);
285 hBrush = NULL;
286 }
287 else
288 {
289 /* fill the background */
290 FillRect(hdc, &rc, hBrush);
291 DeleteObject(hBrush);
292 hBrush = NULL;
293 }
294
295 if ((dwStyle & TTS_BALLOON) || infoPtr->pszTitle)
296 {
297 /* calculate text rectangle */
298 rc.left += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.left);
299 rc.top += (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.top);
300 rc.right -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.right);
301 rc.bottom -= (BALLOON_TEXT_MARGIN + infoPtr->rcMargin.bottom);
302 if(infoPtr->bToolBelow) rc.top += BALLOON_STEMHEIGHT;
303
304 if (infoPtr->pszTitle)
305 {
306 RECT rcTitle = {rc.left, rc.top, rc.right, rc.bottom};
307 int height;
308 BOOL icon_present;
309 HFONT prevFont;
310
311 /* draw icon */
312 icon_present = infoPtr->hTitleIcon &&
313 DrawIconEx(hdc, rc.left, rc.top, infoPtr->hTitleIcon,
314 ICON_WIDTH, ICON_HEIGHT, 0, NULL, DI_NORMAL);
315 if (icon_present)
316 rcTitle.left += ICON_WIDTH + BALLOON_ICON_TITLE_SPACING;
317
318 rcTitle.bottom = rc.top + ICON_HEIGHT;
319
320 /* draw title text */
321 prevFont = SelectObject (hdc, infoPtr->hTitleFont);
322 height = DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_BOTTOM | DT_SINGLELINE | DT_NOPREFIX);
323 SelectObject (hdc, prevFont);
324 rc.top += height + BALLOON_TITLE_TEXT_SPACING;
325 }
326 }
327 else
328 {
329 /* calculate text rectangle */
330 rc.left += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.left);
331 rc.top += (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.top);
332 rc.right -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.right);
333 rc.bottom -= (NORMAL_TEXT_MARGIN + infoPtr->rcMargin.bottom);
334 }
335
336 /* draw text */
337 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
338
339 /* Custom draw - Call PostPaint after drawing */
340 if (cdmode & CDRF_NOTIFYPOSTPAINT) {
341 TOOLTIPS_notify_customdraw(CDDS_POSTPAINT, &nmttcd);
342 }
343
344 /* be polite and reset the things we changed in the dc */
345 SelectObject (hdc, hOldFont);
346 SetBkMode (hdc, oldBkMode);
347
348 if (dwStyle & TTS_BALLOON)
349 {
350 /* frame region because default window proc doesn't do it */
351 INT width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
352 INT height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
353
354 hBrush = GetSysColorBrush(COLOR_WINDOWFRAME);
355 FrameRgn(hdc, hRgn, hBrush, width, height);
356 }
357
358 if (hRgn)
359 DeleteObject(hRgn);
360 }
361
362 static void TOOLTIPS_GetDispInfoA(HWND hwnd, TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr)
363 {
364 NMTTDISPINFOA ttnmdi;
365
366 /* fill NMHDR struct */
367 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOA));
368 ttnmdi.hdr.hwndFrom = hwnd;
369 ttnmdi.hdr.idFrom = toolPtr->uId;
370 ttnmdi.hdr.code = TTN_GETDISPINFOA; /* == TTN_NEEDTEXTA */
371 ttnmdi.lpszText = (LPSTR)ttnmdi.szText;
372 ttnmdi.uFlags = toolPtr->uFlags;
373 ttnmdi.lParam = toolPtr->lParam;
374
375 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
376 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
377
378 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
379 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
380 infoPtr->szTipText, INFOTIPSIZE);
381 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
382 toolPtr->hinst = ttnmdi.hinst;
383 toolPtr->lpszText = (LPWSTR)ttnmdi.lpszText;
384 }
385 }
386 else if (ttnmdi.lpszText == 0) {
387 infoPtr->szTipText[0] = '\0';
388 }
389 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
390 Str_GetPtrAtoW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
391 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
392 toolPtr->hinst = 0;
393 toolPtr->lpszText = NULL;
394 Str_SetPtrW(&toolPtr->lpszText, infoPtr->szTipText);
395 }
396 }
397 else {
398 ERR("recursive text callback!\n");
399 infoPtr->szTipText[0] = '\0';
400 }
401
402 /* no text available - try calling parent instead as per native */
403 /* FIXME: Unsure if SETITEM should save the value or not */
404 if (infoPtr->szTipText[0] == 0x00) {
405
406 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
407
408 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
409 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
410 infoPtr->szTipText, INFOTIPSIZE);
411 } else if (ttnmdi.lpszText &&
412 ttnmdi.lpszText != LPSTR_TEXTCALLBACKA) {
413 Str_GetPtrAtoW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
414 }
415 }
416 }
417
418 static void TOOLTIPS_GetDispInfoW(HWND hwnd, TOOLTIPS_INFO *infoPtr, TTTOOL_INFO *toolPtr)
419 {
420 NMTTDISPINFOW ttnmdi;
421
422 /* fill NMHDR struct */
423 ZeroMemory (&ttnmdi, sizeof(NMTTDISPINFOW));
424 ttnmdi.hdr.hwndFrom = hwnd;
425 ttnmdi.hdr.idFrom = toolPtr->uId;
426 ttnmdi.hdr.code = TTN_GETDISPINFOW; /* == TTN_NEEDTEXTW */
427 ttnmdi.lpszText = (LPWSTR)ttnmdi.szText;
428 ttnmdi.uFlags = toolPtr->uFlags;
429 ttnmdi.lParam = toolPtr->lParam;
430
431 TRACE("hdr.idFrom = %lx\n", ttnmdi.hdr.idFrom);
432 SendMessageW(toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
433
434 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
435 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
436 infoPtr->szTipText, INFOTIPSIZE);
437 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
438 toolPtr->hinst = ttnmdi.hinst;
439 toolPtr->lpszText = ttnmdi.lpszText;
440 }
441 }
442 else if (ttnmdi.lpszText == 0) {
443 infoPtr->szTipText[0] = '\0';
444 }
445 else if (ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
446 Str_GetPtrW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
447 if (ttnmdi.uFlags & TTF_DI_SETITEM) {
448 toolPtr->hinst = 0;
449 toolPtr->lpszText = NULL;
450 Str_SetPtrW(&toolPtr->lpszText, infoPtr->szTipText);
451 }
452 }
453 else {
454 ERR("recursive text callback!\n");
455 infoPtr->szTipText[0] = '\0';
456 }
457
458 /* no text available - try calling parent instead as per native */
459 /* FIXME: Unsure if SETITEM should save the value or not */
460 if (infoPtr->szTipText[0] == 0x00) {
461
462 SendMessageW(GetParent(toolPtr->hwnd), WM_NOTIFY, toolPtr->uId, (LPARAM)&ttnmdi);
463
464 if (IS_INTRESOURCE(ttnmdi.lpszText)) {
465 LoadStringW(ttnmdi.hinst, LOWORD(ttnmdi.lpszText),
466 infoPtr->szTipText, INFOTIPSIZE);
467 } else if (ttnmdi.lpszText &&
468 ttnmdi.lpszText != LPSTR_TEXTCALLBACKW) {
469 Str_GetPtrW(ttnmdi.lpszText, infoPtr->szTipText, INFOTIPSIZE);
470 }
471 }
472
473 }
474
475 static void
476 TOOLTIPS_GetTipText (HWND hwnd, TOOLTIPS_INFO *infoPtr, INT nTool)
477 {
478 TTTOOL_INFO *toolPtr = &infoPtr->tools[nTool];
479
480 if (IS_INTRESOURCE(toolPtr->lpszText) && toolPtr->hinst) {
481 /* load a resource */
482 TRACE("load res string %p %x\n",
483 toolPtr->hinst, LOWORD(toolPtr->lpszText));
484 LoadStringW (toolPtr->hinst, LOWORD(toolPtr->lpszText),
485 infoPtr->szTipText, INFOTIPSIZE);
486 }
487 else if (toolPtr->lpszText) {
488 if (toolPtr->lpszText == LPSTR_TEXTCALLBACKW) {
489 if (toolPtr->bNotifyUnicode)
490 TOOLTIPS_GetDispInfoW(hwnd, infoPtr, toolPtr);
491 else
492 TOOLTIPS_GetDispInfoA(hwnd, infoPtr, toolPtr);
493 }
494 else {
495 /* the item is a usual (unicode) text */
496 lstrcpynW (infoPtr->szTipText, toolPtr->lpszText, INFOTIPSIZE);
497 }
498 }
499 else {
500 /* no text available */
501 infoPtr->szTipText[0] = '\0';
502 }
503
504 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
505 }
506
507
508 static void
509 TOOLTIPS_CalcTipSize (HWND hwnd, const TOOLTIPS_INFO *infoPtr, LPSIZE lpSize)
510 {
511 HDC hdc;
512 HFONT hOldFont;
513 DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
514 UINT uFlags = DT_EXTERNALLEADING | DT_CALCRECT;
515 RECT rc = {0, 0, 0, 0};
516 SIZE title = {0, 0};
517
518 if (infoPtr->nMaxTipWidth > -1) {
519 rc.right = infoPtr->nMaxTipWidth;
520 uFlags |= DT_WORDBREAK;
521 }
522 if (style & TTS_NOPREFIX)
523 uFlags |= DT_NOPREFIX;
524 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
525
526 hdc = GetDC (hwnd);
527 if (infoPtr->pszTitle)
528 {
529 RECT rcTitle = {0, 0, 0, 0};
530 TRACE("title %s\n", debugstr_w(infoPtr->pszTitle));
531 if (infoPtr->hTitleIcon)
532 {
533 title.cx = ICON_WIDTH;
534 title.cy = ICON_HEIGHT;
535 }
536 if (title.cx != 0) title.cx += BALLOON_ICON_TITLE_SPACING;
537 hOldFont = SelectObject (hdc, infoPtr->hTitleFont);
538 DrawTextW(hdc, infoPtr->pszTitle, -1, &rcTitle, DT_SINGLELINE | DT_NOPREFIX | DT_CALCRECT);
539 SelectObject (hdc, hOldFont);
540 title.cy = max(title.cy, rcTitle.bottom - rcTitle.top) + BALLOON_TITLE_TEXT_SPACING;
541 title.cx += (rcTitle.right - rcTitle.left);
542 }
543 hOldFont = SelectObject (hdc, infoPtr->hFont);
544 DrawTextW (hdc, infoPtr->szTipText, -1, &rc, uFlags);
545 SelectObject (hdc, hOldFont);
546 ReleaseDC (hwnd, hdc);
547
548 if ((style & TTS_BALLOON) || infoPtr->pszTitle)
549 {
550 lpSize->cx = max(rc.right - rc.left, title.cx) + 2*BALLOON_TEXT_MARGIN +
551 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
552 lpSize->cy = title.cy + rc.bottom - rc.top + 2*BALLOON_TEXT_MARGIN +
553 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top +
554 BALLOON_STEMHEIGHT;
555 }
556 else
557 {
558 lpSize->cx = rc.right - rc.left + 2*NORMAL_TEXT_MARGIN +
559 infoPtr->rcMargin.left + infoPtr->rcMargin.right;
560 lpSize->cy = rc.bottom - rc.top + 2*NORMAL_TEXT_MARGIN +
561 infoPtr->rcMargin.bottom + infoPtr->rcMargin.top;
562 }
563 }
564
565
566 static void
567 TOOLTIPS_Show (HWND hwnd, TOOLTIPS_INFO *infoPtr, BOOL track_activate)
568 {
569 TTTOOL_INFO *toolPtr;
570 HMONITOR monitor;
571 MONITORINFO mon_info;
572 RECT rect;
573 SIZE size;
574 NMHDR hdr;
575 int ptfx = 0;
576 DWORD style = GetWindowLongW(hwnd, GWL_STYLE);
577 INT nTool;
578
579 if (track_activate)
580 {
581 if (infoPtr->nTrackTool == -1)
582 {
583 TRACE("invalid tracking tool (-1)!\n");
584 return;
585 }
586 nTool = infoPtr->nTrackTool;
587 }
588 else
589 {
590 if (infoPtr->nTool == -1)
591 {
592 TRACE("invalid tool (-1)!\n");
593 return;
594 }
595 nTool = infoPtr->nTool;
596 }
597
598 TRACE("Show tooltip pre %d! (%p)\n", nTool, hwnd);
599
600 TOOLTIPS_GetTipText (hwnd, infoPtr, nTool);
601
602 if (infoPtr->szTipText[0] == '\0')
603 return;
604
605 toolPtr = &infoPtr->tools[nTool];
606
607 if (!track_activate)
608 infoPtr->nCurrentTool = infoPtr->nTool;
609
610 TRACE("Show tooltip %d!\n", nTool);
611
612 hdr.hwndFrom = hwnd;
613 hdr.idFrom = toolPtr->uId;
614 hdr.code = TTN_SHOW;
615 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
616
617 TRACE("%s\n", debugstr_w(infoPtr->szTipText));
618
619 TOOLTIPS_CalcTipSize (hwnd, infoPtr, &size);
620 TRACE("size %d x %d\n", size.cx, size.cy);
621
622 if (track_activate)
623 {
624 rect.left = infoPtr->xTrackPos;
625 rect.top = infoPtr->yTrackPos;
626 ptfx = rect.left;
627
628 if (toolPtr->uFlags & TTF_CENTERTIP)
629 {
630 rect.left -= (size.cx / 2);
631 if (!(style & TTS_BALLOON))
632 rect.top -= (size.cy / 2);
633 }
634 infoPtr->bToolBelow = TRUE;
635
636 if (!(toolPtr->uFlags & TTF_ABSOLUTE))
637 {
638 if (style & TTS_BALLOON)
639 rect.left -= BALLOON_STEMINDENT;
640 else
641 {
642 RECT rcTool;
643
644 if (toolPtr->uFlags & TTF_IDISHWND)
645 GetWindowRect ((HWND)toolPtr->uId, &rcTool);
646 else
647 {
648 rcTool = toolPtr->rect;
649 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rcTool, 2);
650 }
651
652 /* smart placement */
653 if ((rect.left + size.cx > rcTool.left) && (rect.left < rcTool.right) &&
654 (rect.top + size.cy > rcTool.top) && (rect.top < rcTool.bottom))
655 rect.left = rcTool.right;
656 }
657 }
658 }
659 else
660 {
661 if (toolPtr->uFlags & TTF_CENTERTIP)
662 {
663 RECT rc;
664
665 if (toolPtr->uFlags & TTF_IDISHWND)
666 GetWindowRect ((HWND)toolPtr->uId, &rc);
667 else {
668 rc = toolPtr->rect;
669 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
670 }
671 rect.left = (rc.left + rc.right - size.cx) / 2;
672 if (style & TTS_BALLOON)
673 {
674 ptfx = rc.left + ((rc.right - rc.left) / 2);
675
676 /* CENTERTIP ballon tooltips default to below the field
677 * if they fit on the screen */
678 if (rc.bottom + size.cy > GetSystemMetrics(SM_CYSCREEN))
679 {
680 rect.top = rc.top - size.cy;
681 infoPtr->bToolBelow = FALSE;
682 }
683 else
684 {
685 infoPtr->bToolBelow = TRUE;
686 rect.top = rc.bottom;
687 }
688 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
689 }
690 else
691 {
692 rect.top = rc.bottom + 2;
693 infoPtr->bToolBelow = TRUE;
694 }
695 }
696 else
697 {
698 GetCursorPos ((LPPOINT)&rect);
699 if (style & TTS_BALLOON)
700 {
701 ptfx = rect.left;
702 if(rect.top - size.cy >= 0)
703 {
704 rect.top -= size.cy;
705 infoPtr->bToolBelow = FALSE;
706 }
707 else
708 {
709 infoPtr->bToolBelow = TRUE;
710 rect.top += 20;
711 }
712 rect.left = max(0, rect.left - BALLOON_STEMINDENT);
713 }
714 else
715 {
716 rect.top += 20;
717 infoPtr->bToolBelow = TRUE;
718 }
719 }
720 }
721
722 TRACE("pos %d - %d\n", rect.left, rect.top);
723
724 rect.right = rect.left + size.cx;
725 rect.bottom = rect.top + size.cy;
726
727 /* check position */
728
729 monitor = MonitorFromRect( &rect, MONITOR_DEFAULTTOPRIMARY );
730 mon_info.cbSize = sizeof(mon_info);
731 GetMonitorInfoW( monitor, &mon_info );
732
733 if( rect.right > mon_info.rcWork.right ) {
734 rect.left -= rect.right - mon_info.rcWork.right + 2;
735 rect.right = mon_info.rcWork.right - 2;
736 }
737 if (rect.left < mon_info.rcWork.left) rect.left = mon_info.rcWork.left;
738
739 if( rect.bottom > mon_info.rcWork.bottom ) {
740 RECT rc;
741
742 if (toolPtr->uFlags & TTF_IDISHWND)
743 GetWindowRect ((HWND)toolPtr->uId, &rc);
744 else {
745 rc = toolPtr->rect;
746 MapWindowPoints (toolPtr->hwnd, NULL, (LPPOINT)&rc, 2);
747 }
748 rect.bottom = rc.top - 2;
749 rect.top = rect.bottom - size.cy;
750 }
751
752 AdjustWindowRectEx (&rect, GetWindowLongW (hwnd, GWL_STYLE),
753 FALSE, GetWindowLongW (hwnd, GWL_EXSTYLE));
754
755 if (style & TTS_BALLOON)
756 {
757 HRGN hRgn;
758 HRGN hrStem;
759 POINT pts[3];
760
761 ptfx -= rect.left;
762
763 if(infoPtr->bToolBelow)
764 {
765 pts[0].x = ptfx;
766 pts[0].y = 0;
767 pts[1].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
768 pts[1].y = BALLOON_STEMHEIGHT;
769 pts[2].x = pts[1].x + BALLOON_STEMWIDTH;
770 pts[2].y = pts[1].y;
771 if(pts[2].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
772 {
773 pts[2].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
774 pts[1].x = pts[2].x - BALLOON_STEMWIDTH;
775 }
776 }
777 else
778 {
779 pts[0].x = max(BALLOON_STEMINDENT, ptfx - (BALLOON_STEMWIDTH / 2));
780 pts[0].y = (rect.bottom - rect.top) - BALLOON_STEMHEIGHT;
781 pts[1].x = pts[0].x + BALLOON_STEMWIDTH;
782 pts[1].y = pts[0].y;
783 pts[2].x = ptfx;
784 pts[2].y = (rect.bottom - rect.top);
785 if(pts[1].x > (rect.right - rect.left) - BALLOON_STEMINDENT)
786 {
787 pts[1].x = (rect.right - rect.left) - BALLOON_STEMINDENT;
788 pts[0].x = pts[1].x - BALLOON_STEMWIDTH;
789 }
790 }
791
792 hrStem = CreatePolygonRgn(pts, sizeof(pts) / sizeof(pts[0]), ALTERNATE);
793
794 hRgn = CreateRoundRectRgn(0,
795 (infoPtr->bToolBelow ? BALLOON_STEMHEIGHT : 0),
796 rect.right - rect.left,
797 (infoPtr->bToolBelow ? rect.bottom - rect.top : rect.bottom - rect.top - BALLOON_STEMHEIGHT),
798 BALLOON_ROUNDEDNESS, BALLOON_ROUNDEDNESS);
799
800 CombineRgn(hRgn, hRgn, hrStem, RGN_OR);
801 DeleteObject(hrStem);
802
803 SetWindowRgn(hwnd, hRgn, FALSE);
804 /* we don't free the region handle as the system deletes it when
805 * it is no longer needed */
806 }
807
808 SetWindowPos (hwnd, HWND_TOPMOST, rect.left, rect.top,
809 rect.right - rect.left, rect.bottom - rect.top,
810 SWP_SHOWWINDOW | SWP_NOACTIVATE);
811
812 /* repaint the tooltip */
813 InvalidateRect(hwnd, NULL, TRUE);
814 UpdateWindow(hwnd);
815
816 if (!track_activate)
817 {
818 SetTimer (hwnd, ID_TIMERPOP, infoPtr->nAutoPopTime, 0);
819 TRACE("timer 2 started!\n");
820 SetTimer (hwnd, ID_TIMERLEAVE, infoPtr->nReshowTime, 0);
821 TRACE("timer 3 started!\n");
822 }
823 }
824
825
826 static void
827 TOOLTIPS_Hide (HWND hwnd, TOOLTIPS_INFO *infoPtr)
828 {
829 TTTOOL_INFO *toolPtr;
830 NMHDR hdr;
831
832 TRACE("Hide tooltip %d! (%p)\n", infoPtr->nCurrentTool, hwnd);
833
834 if (infoPtr->nCurrentTool == -1)
835 return;
836
837 toolPtr = &infoPtr->tools[infoPtr->nCurrentTool];
838 KillTimer (hwnd, ID_TIMERPOP);
839
840 hdr.hwndFrom = hwnd;
841 hdr.idFrom = toolPtr->uId;
842 hdr.code = TTN_POP;
843 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
844
845 infoPtr->nCurrentTool = -1;
846
847 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
848 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
849 }
850
851
852 static void
853 TOOLTIPS_TrackShow (HWND hwnd, TOOLTIPS_INFO *infoPtr)
854 {
855 TOOLTIPS_Show(hwnd, infoPtr, TRUE);
856 }
857
858
859 static void
860 TOOLTIPS_TrackHide (HWND hwnd, const TOOLTIPS_INFO *infoPtr)
861 {
862 TTTOOL_INFO *toolPtr;
863 NMHDR hdr;
864
865 TRACE("hide tracking tooltip %d\n", infoPtr->nTrackTool);
866
867 if (infoPtr->nTrackTool == -1)
868 return;
869
870 toolPtr = &infoPtr->tools[infoPtr->nTrackTool];
871
872 hdr.hwndFrom = hwnd;
873 hdr.idFrom = toolPtr->uId;
874 hdr.code = TTN_POP;
875 SendMessageW (toolPtr->hwnd, WM_NOTIFY, toolPtr->uId, (LPARAM)&hdr);
876
877 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
878 SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE);
879 }
880
881
882 static INT
883 TOOLTIPS_GetToolFromInfoA (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOA *lpToolInfo)
884 {
885 TTTOOL_INFO *toolPtr;
886 UINT nTool;
887
888 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
889 toolPtr = &infoPtr->tools[nTool];
890
891 if (!(toolPtr->uFlags & TTF_IDISHWND) &&
892 (lpToolInfo->hwnd == toolPtr->hwnd) &&
893 (lpToolInfo->uId == toolPtr->uId))
894 return nTool;
895 }
896
897 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
898 toolPtr = &infoPtr->tools[nTool];
899
900 if ((toolPtr->uFlags & TTF_IDISHWND) &&
901 (lpToolInfo->uId == toolPtr->uId))
902 return nTool;
903 }
904
905 return -1;
906 }
907
908
909 static INT
910 TOOLTIPS_GetToolFromInfoW (const TOOLTIPS_INFO *infoPtr, const TTTOOLINFOW *lpToolInfo)
911 {
912 TTTOOL_INFO *toolPtr;
913 UINT nTool;
914
915 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
916 toolPtr = &infoPtr->tools[nTool];
917
918 if (!(toolPtr->uFlags & TTF_IDISHWND) &&
919 (lpToolInfo->hwnd == toolPtr->hwnd) &&
920 (lpToolInfo->uId == toolPtr->uId))
921 return nTool;
922 }
923
924 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
925 toolPtr = &infoPtr->tools[nTool];
926
927 if ((toolPtr->uFlags & TTF_IDISHWND) &&
928 (lpToolInfo->uId == toolPtr->uId))
929 return nTool;
930 }
931
932 return -1;
933 }
934
935
936 static INT
937 TOOLTIPS_GetToolFromPoint (const TOOLTIPS_INFO *infoPtr, HWND hwnd, const POINT *lpPt)
938 {
939 TTTOOL_INFO *toolPtr;
940 UINT nTool;
941
942 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
943 toolPtr = &infoPtr->tools[nTool];
944
945 if (!(toolPtr->uFlags & TTF_IDISHWND)) {
946 if (hwnd != toolPtr->hwnd)
947 continue;
948 if (!PtInRect (&toolPtr->rect, *lpPt))
949 continue;
950 return nTool;
951 }
952 }
953
954 for (nTool = 0; nTool < infoPtr->uNumTools; nTool++) {
955 toolPtr = &infoPtr->tools[nTool];
956
957 if (toolPtr->uFlags & TTF_IDISHWND) {
958 if ((HWND)toolPtr->uId == hwnd)