]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tooltip.cpp
avoid leaking allocated tooltips
[wxWidgets.git] / src / msw / tooltip.cpp
CommitLineData
3a19e16d 1///////////////////////////////////////////////////////////////////////////////
623d5f80 2// Name: src/msw/tooltip.cpp
3a19e16d
VZ
3// Purpose: wxToolTip class implementation for MSW
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 31.01.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin
65571936 9// Licence: wxWindows licence
3a19e16d
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
cb1a1dc9
VZ
26#if wxUSE_TOOLTIPS
27
3a19e16d 28#include "wx/tooltip.h"
623d5f80
WS
29
30#ifndef WX_PRECOMP
57bd4c60 31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
623d5f80
WS
32 #include "wx/app.h"
33 #include "wx/control.h"
623d5f80
WS
34#endif
35
543a9921 36#include "wx/tokenzr.h"
3a19e16d
VZ
37#include "wx/msw/private.h"
38
7709915e
VZ
39#ifndef TTTOOLINFO_V1_SIZE
40 #define TTTOOLINFO_V1_SIZE 0x28
41#endif
42
3be7ca64
VZ
43#ifndef TTF_TRANSPARENT
44 #define TTF_TRANSPARENT 0x0100
45#endif
46
f048e32f
VZ
47// VZ: normally, the trick with subclassing the tooltip control and processing
48// TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
49// code here for now (but it's not compiled) in case we need it later.
50//
8614c467
VZ
51// For now I use an ugly workaround and process TTN_NEEDTEXT directly in
52// radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
53// because it would then work for all controls, not only radioboxes but for
54// now I don't understand what's wrong with it...
55#define wxUSE_TTM_WINDOWFROMPOINT 0
f048e32f 56
086c94af
VZ
57// ----------------------------------------------------------------------------
58// global variables
59// ----------------------------------------------------------------------------
60
61// the tooltip parent window
f048e32f
VZ
62WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
63
be8b4385
VZ
64// new tooltip maximum width, default value is set on first call to wxToolTip::Add()
65int wxToolTip::ms_maxWidth = 0;
66
f048e32f
VZ
67#if wxUSE_TTM_WINDOWFROMPOINT
68
69// the tooltip window proc
70static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
71
72#endif // wxUSE_TTM_WINDOWFROMPOINT
066f302c 73
3a19e16d
VZ
74// ----------------------------------------------------------------------------
75// private classes
76// ----------------------------------------------------------------------------
77
bb448552 78// a wrapper around TOOLINFO Win32 structure
26b83329 79#ifdef __VISUALC__
11c7d5b6 80 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
26b83329 81#endif
bb448552 82
3a19e16d
VZ
83class wxToolInfo : public TOOLINFO
84{
85public:
6418ad5e 86 wxToolInfo(HWND hwndOwner, unsigned int id, const wxRect& rc)
3a19e16d
VZ
87 {
88 // initialize all members
89 ::ZeroMemory(this, sizeof(TOOLINFO));
90
bb448552 91 // the structure TOOLINFO has been extended with a 4 byte field in
7709915e
VZ
92 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
93 // use these extended fields so use the old struct size to ensure that
94 // the tooltips work on old (Windows 95) systems too
95 cbSize = TTTOOLINFO_V1_SIZE;
bb448552 96
2b5f62a0 97 hwnd = hwndOwner;
6418ad5e
FM
98
99 if (rc.IsEmpty())
100 {
101 uFlags = TTF_IDISHWND;
102 uId = (UINT_PTR)hwndOwner;
103 }
104 else
105 {
106 // this tooltip must be shown only if the mouse hovers a specific rect
107 // of the hwnd parameter!
108 rect.left = rc.GetLeft();
109 rect.right = rc.GetRight();
110 rect.top = rc.GetTop();
111 rect.bottom = rc.GetBottom();
112
113 // note that not setting TTF_IDISHWND from the uFlags member means that the
114 // ti.uId field should not contain the HWND but rather as MSDN says an
115 // "Application-defined identifier of the tool"; this is used internally by
116 // Windows to distinguish the different tooltips attached to the same window
117 uId = id;
118 }
7709915e 119
613db756
VZ
120 // we use TTF_TRANSPARENT to fix a problem which arises at least with
121 // the text controls but may presumably happen with other controls
122 // which display the tooltip at mouse position: it can start flashing
123 // then as the control gets "focus lost" events and dismisses the
124 // tooltip which then reappears because mouse remains hovering over the
125 // control, see SF patch 1821229
126 if ( wxApp::GetComCtl32Version() >= 470 )
127 {
128 uFlags |= TTF_TRANSPARENT;
129 }
3a19e16d
VZ
130 }
131};
bb448552 132
26b83329
VZ
133#ifdef __VISUALC__
134 #pragma warning( default : 4097 )
135#endif
3a19e16d
VZ
136
137// ----------------------------------------------------------------------------
138// private functions
139// ----------------------------------------------------------------------------
140
0c5405b7
VZ
141// send a message to the tooltip control if it exists
142//
143// NB: wParam is always 0 for the TTM_XXX messages we use
144static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam)
3a19e16d 145{
0c5405b7 146 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
3a19e16d
VZ
147}
148
16f6dfd8 149// send a message to all existing tooltip controls
0c5405b7
VZ
150static inline void
151SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
16f6dfd8 152{
0c5405b7
VZ
153 if ( hwnd )
154 ::SendMessage((HWND)hwnd, msg, wParam, lParam);
16f6dfd8
VZ
155}
156
3a19e16d
VZ
157// ============================================================================
158// implementation
159// ============================================================================
160
f048e32f
VZ
161#if wxUSE_TTM_WINDOWFROMPOINT
162
3a19e16d 163// ----------------------------------------------------------------------------
f048e32f 164// window proc for our tooltip control
3a19e16d
VZ
165// ----------------------------------------------------------------------------
166
f048e32f
VZ
167LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
168 UINT msg,
169 WPARAM wParam,
170 LPARAM lParam)
171{
172 if ( msg == TTM_WINDOWFROMPOINT )
173 {
174 LPPOINT ppt = (LPPOINT)lParam;
8614c467 175
3103e8a9 176 // the window on which event occurred
f048e32f
VZ
177 HWND hwnd = ::WindowFromPoint(*ppt);
178
8614c467
VZ
179 OutputDebugString("TTM_WINDOWFROMPOINT: ");
180 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
181
77ffb593 182 // return a HWND corresponding to a wxWindow because only wxWidgets are
f048e32f 183 // associated with tooltips using TTM_ADDTOOL
8614c467 184 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
f048e32f 185
8614c467 186 if ( win )
f048e32f 187 {
8614c467
VZ
188 hwnd = GetHwndOf(win);
189 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
190
191#if 0
f048e32f
VZ
192 // modify the point too!
193 RECT rect;
194 GetWindowRect(hwnd, &rect);
195
8614c467
VZ
196 ppt->x = (rect.right - rect.left) / 2;
197 ppt->y = (rect.bottom - rect.top) / 2;
198#endif // 0
f048e32f
VZ
199 return (LRESULT)hwnd;
200 }
8614c467
VZ
201 else
202 {
203 OutputDebugString("no window\r\n");
204 }
f048e32f
VZ
205 }
206
8caa4ed1 207 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
f048e32f
VZ
208}
209
210#endif // wxUSE_TTM_WINDOWFROMPOINT
211
212// ----------------------------------------------------------------------------
213// static functions
214// ----------------------------------------------------------------------------
066f302c 215
16f6dfd8
VZ
216void wxToolTip::Enable(bool flag)
217{
f048e32f 218 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
16f6dfd8
VZ
219}
220
221void wxToolTip::SetDelay(long milliseconds)
222{
f048e32f
VZ
223 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
224 TTDT_INITIAL, milliseconds);
16f6dfd8
VZ
225}
226
becac1ef
VZ
227void wxToolTip::SetAutoPop(long milliseconds)
228{
229 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
230 TTDT_AUTOPOP, milliseconds);
231}
232
233void wxToolTip::SetReshow(long milliseconds)
234{
235 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
236 TTDT_RESHOW, milliseconds);
237}
238
be8b4385
VZ
239void wxToolTip::SetMaxWidth(int width)
240{
241 wxASSERT_MSG( width == -1 || width >= 0, _T("invalid width value") );
242
243 ms_maxWidth = width;
244}
245
16f6dfd8
VZ
246// ---------------------------------------------------------------------------
247// implementation helpers
248// ---------------------------------------------------------------------------
249
3a19e16d 250// create the tooltip ctrl for our parent frame if it doesn't exist yet
4453b38d 251/* static */
3a19e16d
VZ
252WXHWND wxToolTip::GetToolTipCtrl()
253{
f048e32f 254 if ( !ms_hwndTT )
3a19e16d 255 {
978af864
VZ
256 WXDWORD exflags = 0;
257 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
258 {
259 exflags |= WS_EX_LAYOUTRTL;
260 }
261
c5d7205c
VZ
262 // we want to show the tooltips always (even when the window is not
263 // active) and we don't want to strip "&"s from them
978af864
VZ
264 ms_hwndTT = (WXHWND)::CreateWindowEx(exflags,
265 TOOLTIPS_CLASS,
266 (LPCTSTR)NULL,
267 TTS_ALWAYSTIP | TTS_NOPREFIX,
268 CW_USEDEFAULT, CW_USEDEFAULT,
269 CW_USEDEFAULT, CW_USEDEFAULT,
270 NULL, (HMENU)NULL,
271 wxGetInstance(),
272 NULL);
f048e32f 273 if ( ms_hwndTT )
086c94af 274 {
f048e32f
VZ
275 HWND hwnd = (HWND)ms_hwndTT;
276 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
086c94af 277 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
28195455 278
f048e32f
VZ
279#if wxUSE_TTM_WINDOWFROMPOINT
280 // subclass the newly created control
975b6bcf 281 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
f048e32f
VZ
282#endif // wxUSE_TTM_WINDOWFROMPOINT
283 }
3a19e16d
VZ
284 }
285
f048e32f 286 return ms_hwndTT;
3a19e16d
VZ
287}
288
4453b38d 289/* static */
3a19e16d
VZ
290void wxToolTip::RelayEvent(WXMSG *msg)
291{
0c5405b7 292 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
3a19e16d
VZ
293}
294
3a19e16d
VZ
295// ----------------------------------------------------------------------------
296// ctor & dtor
297// ----------------------------------------------------------------------------
298
b342f8f0
RD
299IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
300
3a19e16d
VZ
301wxToolTip::wxToolTip(const wxString &tip)
302 : m_text(tip)
303{
304 m_window = NULL;
6418ad5e
FM
305
306 // make sure m_rect.IsEmpty() == true
307 m_rect.SetWidth(0);
308 m_rect.SetHeight(0);
309
310 // since m_rect is not valid, m_id is ignored by wxToolInfo ctor...
311 m_id = 0;
312}
313
314wxToolTip::wxToolTip(wxWindow* win, unsigned int id, const wxString &tip, const wxRect& rc)
315 : m_text(tip), m_rect(rc), m_id(id)
316{
317 m_window = NULL;
318
319 SetWindow(win);
3a19e16d
VZ
320}
321
322wxToolTip::~wxToolTip()
323{
d8909578 324 // the tooltip has to be removed before deleting. Otherwise, if it is visible
623d5f80
WS
325 // while being deleted, there will be a delay before it goes away.
326 Remove();
3a19e16d
VZ
327}
328
329// ----------------------------------------------------------------------------
330// others
331// ----------------------------------------------------------------------------
332
6418ad5e
FM
333/* static */
334void wxToolTip::Remove(WXHWND hWnd, unsigned int id, const wxRect& rc)
0c5405b7 335{
6418ad5e
FM
336 wxToolInfo ti((HWND)hWnd, id, rc);
337
0c5405b7
VZ
338 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
339}
340
3a19e16d
VZ
341void wxToolTip::Remove()
342{
343 // remove this tool from the tooltip control
344 if ( m_window )
345 {
6418ad5e 346 Remove(m_window->GetHWND(), m_id, m_rect);
3a19e16d
VZ
347 }
348}
349
f048e32f
VZ
350void wxToolTip::Add(WXHWND hWnd)
351{
352 HWND hwnd = (HWND)hWnd;
353
6418ad5e 354 wxToolInfo ti(hwnd, m_id, m_rect);
f048e32f 355
2b5f62a0
VZ
356 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
357 // store the tooltip text ourselves anyhow, and provide it in response to
358 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
359 // character tooltips as this is the size of the szText buffer in
360 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
361 // of any length
f048e32f 362 ti.hwnd = hwnd;
8c4209f7 363 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
f048e32f 364
0c5405b7 365 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
f048e32f 366 {
bb448552 367 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
8c4209f7
VZ
368
369 return;
f048e32f 370 }
8c4209f7 371
5b59df83 372#ifdef TTM_SETMAXTIPWIDTH
8c4209f7
VZ
373 if ( wxApp::GetComCtl32Version() >= 470 )
374 {
375 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
376 // extent of its first line as max value
377 HFONT hfont = (HFONT)
378 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0);
0c5405b7 379
8c4209f7
VZ
380 if ( !hfont )
381 {
382 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
be8b4385
VZ
383 if ( !hfont )
384 {
8c4209f7 385 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
be8b4385 386 }
8c4209f7 387 }
8614c467 388
8c4209f7
VZ
389 MemoryHDC hdc;
390 if ( !hdc )
391 {
392 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
393 }
8614c467 394
8c4209f7
VZ
395 if ( !SelectObject(hdc, hfont) )
396 {
397 wxLogLastError(wxT("SelectObject(hfont)"));
398 }
8614c467 399
8c4209f7
VZ
400 // find the width of the widest line
401 int maxWidth = 0;
402 wxStringTokenizer tokenizer(m_text, _T("\n"));
403 while ( tokenizer.HasMoreTokens() )
404 {
405 const wxString token = tokenizer.GetNextToken();
be8b4385 406
8c4209f7
VZ
407 SIZE sz;
408 if ( !::GetTextExtentPoint32(hdc, token.wx_str(),
409 token.length(), &sz) )
410 {
411 wxLogLastError(wxT("GetTextExtentPoint32"));
8614c467 412 }
be8b4385 413
8c4209f7
VZ
414 if ( sz.cx > maxWidth )
415 maxWidth = sz.cx;
416 }
417
418 // limit size to ms_maxWidth, if set
419 if ( ms_maxWidth == 0 )
420 {
421 // this is more or less arbitrary but seems to work well
422 static const int DEFAULT_MAX_WIDTH = 400;
8614c467 423
8c4209f7 424 ms_maxWidth = wxGetClientDisplayRect().width / 2;
be8b4385 425
8c4209f7
VZ
426 if ( ms_maxWidth > DEFAULT_MAX_WIDTH )
427 ms_maxWidth = DEFAULT_MAX_WIDTH;
428 }
be8b4385 429
8c4209f7
VZ
430 if ( ms_maxWidth != -1 && maxWidth > ms_maxWidth )
431 maxWidth = ms_maxWidth;
be8b4385 432
8c4209f7
VZ
433 // only set a new width if it is bigger than the current setting:
434 // otherwise adding a tooltip with shorter line(s) than a previous
435 // one would result in breaking the longer lines unnecessarily as
436 // all our tooltips share the same maximal width
437 if ( maxWidth > SendTooltipMessage(GetToolTipCtrl(),
438 TTM_GETMAXTIPWIDTH, 0) )
439 {
440 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
441 wxUIntToPtr(maxWidth));
be8b4385 442 }
8c4209f7
VZ
443 }
444 else
be8b4385 445#endif // TTM_SETMAXTIPWIDTH
8c4209f7
VZ
446 {
447 // replace the '\n's with spaces because otherwise they appear as
448 // unprintable characters in the tooltip string
449 m_text.Replace(_T("\n"), _T(" "));
450 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
be8b4385 451
8c4209f7
VZ
452 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
453 {
454 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
8614c467
VZ
455 }
456 }
f048e32f
VZ
457}
458
3a19e16d
VZ
459void wxToolTip::SetWindow(wxWindow *win)
460{
461 Remove();
462
463 m_window = win;
464
f048e32f 465 // add the window itself
3a19e16d
VZ
466 if ( m_window )
467 {
f048e32f
VZ
468 Add(m_window->GetHWND());
469 }
d4e5272b 470#if !defined(__WXUNIVERSAL__)
d6b9cc87 471 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
f048e32f
VZ
472 wxControl *control = wxDynamicCast(m_window, wxControl);
473 if ( control )
474 {
bf25c88b 475 const wxArrayLong& subcontrols = control->GetSubcontrols();
8614c467 476 size_t count = subcontrols.GetCount();
f048e32f 477 for ( size_t n = 0; n < count; n++ )
3a19e16d 478 {
8614c467 479 int id = subcontrols[n];
f048e32f 480 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
8614c467 481 if ( !hwnd )
f048e32f 482 {
8614c467
VZ
483 // may be it's a child of parent of the control, in fact?
484 // (radiobuttons are subcontrols, i.e. children of the radiobox
77ffb593 485 // for wxWidgets but are its siblings at Windows level)
8614c467 486 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
f048e32f 487 }
8614c467
VZ
488
489 // must have it by now!
490 wxASSERT_MSG( hwnd, _T("no hwnd for subcontrol?") );
491
492 Add((WXHWND)hwnd);
3a19e16d
VZ
493 }
494 }
d4e5272b 495#endif // !defined(__WXUNIVERSAL__)
3a19e16d
VZ
496}
497
6418ad5e
FM
498void wxToolTip::SetRect(const wxRect& rc)
499{
500 m_rect = rc;
501
502 if ( m_window )
503 {
504 wxToolInfo ti(GetHwndOf(m_window), m_id, m_rect);
505 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_NEWTOOLRECT, &ti);
506 }
507}
508
3a19e16d
VZ
509void wxToolTip::SetTip(const wxString& tip)
510{
511 m_text = tip;
512
513 if ( m_window )
514 {
2b5f62a0 515 // update the tip text shown by the control
6418ad5e 516 wxToolInfo ti(GetHwndOf(m_window), m_id, m_rect);
3a19e16d 517
4e916e61
VZ
518 // for some reason, changing the tooltip text directly results in
519 // repaint of the controls under it, see #10520 -- but this doesn't
520 // happen if we reset it first
8e1a01e4 521 ti.lpszText = const_cast<wxChar *>(_T(""));
4e916e61
VZ
522 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
523
524 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
0c5405b7 525 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
3a19e16d
VZ
526 }
527}
cb1a1dc9
VZ
528
529#endif // wxUSE_TOOLTIPS