1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tooltip.cpp
3 // Purpose: wxToolTip class implementation for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/tooltip.h"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/control.h"
36 #include "wx/tokenzr.h"
37 #include "wx/msw/private.h"
39 #ifndef TTTOOLINFO_V1_SIZE
40 #define TTTOOLINFO_V1_SIZE 0x28
43 #ifndef TTF_TRANSPARENT
44 #define TTF_TRANSPARENT 0x0100
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.
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
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // the tooltip parent window
62 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
64 // new tooltip maximum width, default value is set on first call to wxToolTip::Add()
65 int wxToolTip::ms_maxWidth
= 0;
67 #if wxUSE_TTM_WINDOWFROMPOINT
69 // the tooltip window proc
70 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
72 #endif // wxUSE_TTM_WINDOWFROMPOINT
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // a wrapper around TOOLINFO Win32 structure
80 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
83 class wxToolInfo
: public TOOLINFO
86 wxToolInfo(HWND hwndOwner
, unsigned int id
, const wxRect
& rc
)
88 // initialize all members
89 ::ZeroMemory(this, sizeof(TOOLINFO
));
91 // the structure TOOLINFO has been extended with a 4 byte field in
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
;
101 uFlags
= TTF_IDISHWND
;
102 uId
= (UINT_PTR
)hwndOwner
;
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();
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
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 )
128 uFlags
|= TTF_TRANSPARENT
;
134 #pragma warning( default : 4097 )
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 // send a message to the tooltip control if it exists
143 // NB: wParam is always 0 for the TTM_XXX messages we use
144 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
146 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
149 // send a message to all existing tooltip controls
151 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
154 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
157 // ============================================================================
159 // ============================================================================
161 #if wxUSE_TTM_WINDOWFROMPOINT
163 // ----------------------------------------------------------------------------
164 // window proc for our tooltip control
165 // ----------------------------------------------------------------------------
167 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
172 if ( msg
== TTM_WINDOWFROMPOINT
)
174 LPPOINT ppt
= (LPPOINT
)lParam
;
176 // the window on which event occurred
177 HWND hwnd
= ::WindowFromPoint(*ppt
);
179 OutputDebugString("TTM_WINDOWFROMPOINT: ");
180 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
182 // return a HWND corresponding to a wxWindow because only wxWidgets are
183 // associated with tooltips using TTM_ADDTOOL
184 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
188 hwnd
= GetHwndOf(win
);
189 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
192 // modify the point too!
194 GetWindowRect(hwnd
, &rect
);
196 ppt
->x
= (rect
.right
- rect
.left
) / 2;
197 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
199 return (LRESULT
)hwnd
;
203 OutputDebugString("no window\r\n");
207 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
210 #endif // wxUSE_TTM_WINDOWFROMPOINT
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 void wxToolTip::Enable(bool flag
)
218 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
221 void wxToolTip::SetDelay(long milliseconds
)
223 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
224 TTDT_INITIAL
, milliseconds
);
227 void wxToolTip::SetAutoPop(long milliseconds
)
229 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
230 TTDT_AUTOPOP
, milliseconds
);
233 void wxToolTip::SetReshow(long milliseconds
)
235 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
236 TTDT_RESHOW
, milliseconds
);
239 void wxToolTip::SetMaxWidth(int width
)
241 wxASSERT_MSG( width
== -1 || width
>= 0, _T("invalid width value") );
246 // ---------------------------------------------------------------------------
247 // implementation helpers
248 // ---------------------------------------------------------------------------
250 // create the tooltip ctrl for our parent frame if it doesn't exist yet
252 WXHWND
wxToolTip::GetToolTipCtrl()
257 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
259 exflags
|= WS_EX_LAYOUTRTL
;
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
264 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
267 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
268 CW_USEDEFAULT
, CW_USEDEFAULT
,
269 CW_USEDEFAULT
, CW_USEDEFAULT
,
275 HWND hwnd
= (HWND
)ms_hwndTT
;
276 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
277 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
279 #if wxUSE_TTM_WINDOWFROMPOINT
280 // subclass the newly created control
281 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
282 #endif // wxUSE_TTM_WINDOWFROMPOINT
290 void wxToolTip::RelayEvent(WXMSG
*msg
)
292 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
301 wxToolTip::wxToolTip(const wxString
&tip
)
306 // make sure m_rect.IsEmpty() == true
310 // since m_rect is not valid, m_id is ignored by wxToolInfo ctor...
314 wxToolTip::wxToolTip(wxWindow
* win
, unsigned int id
, const wxString
&tip
, const wxRect
& rc
)
315 : m_text(tip
), m_rect(rc
), m_id(id
)
322 wxToolTip::~wxToolTip()
324 // the tooltip has to be removed before deleting. Otherwise, if it is visible
325 // while being deleted, there will be a delay before it goes away.
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
334 void wxToolTip::Remove(WXHWND hWnd
, unsigned int id
, const wxRect
& rc
)
336 wxToolInfo
ti((HWND
)hWnd
, id
, rc
);
338 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
341 void wxToolTip::Remove()
343 // remove this tool from the tooltip control
346 Remove(m_window
->GetHWND(), m_id
, m_rect
);
350 void wxToolTip::Add(WXHWND hWnd
)
352 HWND hwnd
= (HWND
)hWnd
;
354 wxToolInfo
ti(hwnd
, m_id
, m_rect
);
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
363 ti
.lpszText
= const_cast<wxChar
*>(m_text
.wx_str());
365 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
367 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
372 #ifdef TTM_SETMAXTIPWIDTH
373 if ( wxApp::GetComCtl32Version() >= 470 )
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);
382 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
385 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
392 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
395 if ( !SelectObject(hdc
, hfont
) )
397 wxLogLastError(wxT("SelectObject(hfont)"));
400 // find the width of the widest line
402 wxStringTokenizer
tokenizer(m_text
, _T("\n"));
403 while ( tokenizer
.HasMoreTokens() )
405 const wxString token
= tokenizer
.GetNextToken();
408 if ( !::GetTextExtentPoint32(hdc
, token
.wx_str(),
409 token
.length(), &sz
) )
411 wxLogLastError(wxT("GetTextExtentPoint32"));
414 if ( sz
.cx
> maxWidth
)
418 // limit size to ms_maxWidth, if set
419 if ( ms_maxWidth
== 0 )
421 // this is more or less arbitrary but seems to work well
422 static const int DEFAULT_MAX_WIDTH
= 400;
424 ms_maxWidth
= wxGetClientDisplayRect().width
/ 2;
426 if ( ms_maxWidth
> DEFAULT_MAX_WIDTH
)
427 ms_maxWidth
= DEFAULT_MAX_WIDTH
;
430 if ( ms_maxWidth
!= -1 && maxWidth
> ms_maxWidth
)
431 maxWidth
= ms_maxWidth
;
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) )
440 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
441 wxUIntToPtr(maxWidth
));
445 #endif // TTM_SETMAXTIPWIDTH
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());
452 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
454 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
459 void wxToolTip::SetWindow(wxWindow
*win
)
465 // add the window itself
468 Add(m_window
->GetHWND());
470 #if !defined(__WXUNIVERSAL__)
471 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
472 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
475 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
476 size_t count
= subcontrols
.GetCount();
477 for ( size_t n
= 0; n
< count
; n
++ )
479 int id
= subcontrols
[n
];
480 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
483 // may be it's a child of parent of the control, in fact?
484 // (radiobuttons are subcontrols, i.e. children of the radiobox
485 // for wxWidgets but are its siblings at Windows level)
486 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
489 // must have it by now!
490 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
495 #endif // !defined(__WXUNIVERSAL__)
498 void wxToolTip::SetRect(const wxRect
& rc
)
504 wxToolInfo
ti(GetHwndOf(m_window
), m_id
, m_rect
);
505 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_NEWTOOLRECT
, &ti
);
509 void wxToolTip::SetTip(const wxString
& tip
)
515 // update the tip text shown by the control
516 wxToolInfo
ti(GetHwndOf(m_window
), m_id
, m_rect
);
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
521 ti
.lpszText
= const_cast<wxChar
*>(_T(""));
522 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
524 ti
.lpszText
= const_cast<wxChar
*>(m_text
.wx_str());
525 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
529 #endif // wxUSE_TOOLTIPS