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 #if wxUSE_TTM_WINDOWFROMPOINT
66 // the tooltip window proc
67 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
69 #endif // wxUSE_TTM_WINDOWFROMPOINT
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // a wrapper around TOOLINFO Win32 structure
77 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
80 class wxToolInfo
: public TOOLINFO
83 wxToolInfo(HWND hwndOwner
)
85 // initialize all members
86 ::ZeroMemory(this, sizeof(TOOLINFO
));
88 // the structure TOOLINFO has been extended with a 4 byte field in
89 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
90 // use these extended fields so use the old struct size to ensure that
91 // the tooltips work on old (Windows 95) systems too
92 cbSize
= TTTOOLINFO_V1_SIZE
;
95 uFlags
= TTF_IDISHWND
;
97 // we use TTF_TRANSPARENT to fix a problem which arises at least with
98 // the text controls but may presumably happen with other controls
99 // which display the tooltip at mouse position: it can start flashing
100 // then as the control gets "focus lost" events and dismisses the
101 // tooltip which then reappears because mouse remains hovering over the
102 // control, see SF patch 1821229
103 if ( wxApp::GetComCtl32Version() >= 470 )
105 uFlags
|= TTF_TRANSPARENT
;
108 uId
= (UINT_PTR
)hwndOwner
;
113 #pragma warning( default : 4097 )
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // send a message to the tooltip control if it exists
122 // NB: wParam is always 0 for the TTM_XXX messages we use
123 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
125 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
128 // send a message to all existing tooltip controls
130 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
133 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
136 // ============================================================================
138 // ============================================================================
140 #if wxUSE_TTM_WINDOWFROMPOINT
142 // ----------------------------------------------------------------------------
143 // window proc for our tooltip control
144 // ----------------------------------------------------------------------------
146 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
151 if ( msg
== TTM_WINDOWFROMPOINT
)
153 LPPOINT ppt
= (LPPOINT
)lParam
;
155 // the window on which event occurred
156 HWND hwnd
= ::WindowFromPoint(*ppt
);
158 OutputDebugString("TTM_WINDOWFROMPOINT: ");
159 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
161 // return a HWND corresponding to a wxWindow because only wxWidgets are
162 // associated with tooltips using TTM_ADDTOOL
163 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
167 hwnd
= GetHwndOf(win
);
168 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
171 // modify the point too!
173 GetWindowRect(hwnd
, &rect
);
175 ppt
->x
= (rect
.right
- rect
.left
) / 2;
176 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
178 return (LRESULT
)hwnd
;
182 OutputDebugString("no window\r\n");
186 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
189 #endif // wxUSE_TTM_WINDOWFROMPOINT
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void wxToolTip::Enable(bool flag
)
197 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
200 void wxToolTip::SetDelay(long milliseconds
)
202 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
203 TTDT_INITIAL
, milliseconds
);
206 void wxToolTip::SetAutoPop(long milliseconds
)
208 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
209 TTDT_AUTOPOP
, milliseconds
);
212 void wxToolTip::SetReshow(long milliseconds
)
214 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
215 TTDT_RESHOW
, milliseconds
);
218 // ---------------------------------------------------------------------------
219 // implementation helpers
220 // ---------------------------------------------------------------------------
222 // create the tooltip ctrl for our parent frame if it doesn't exist yet
224 WXHWND
wxToolTip::GetToolTipCtrl()
229 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
231 exflags
|= WS_EX_LAYOUTRTL
;
234 // we want to show the tooltips always (even when the window is not
235 // active) and we don't want to strip "&"s from them
236 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
239 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
240 CW_USEDEFAULT
, CW_USEDEFAULT
,
241 CW_USEDEFAULT
, CW_USEDEFAULT
,
247 HWND hwnd
= (HWND
)ms_hwndTT
;
248 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
249 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
251 #if wxUSE_TTM_WINDOWFROMPOINT
252 // subclass the newly created control
253 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
254 #endif // wxUSE_TTM_WINDOWFROMPOINT
262 void wxToolTip::RelayEvent(WXMSG
*msg
)
264 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
273 wxToolTip::wxToolTip(const wxString
&tip
)
279 wxToolTip::~wxToolTip()
281 // the tooltip has to be removed before deleting. Otherwise, if it is visible
282 // while being deleted, there will be a delay before it goes away.
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 void wxToolTip::Remove(WXHWND hWnd
)
292 wxToolInfo
ti((HWND
)hWnd
);
293 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
296 void wxToolTip::Remove()
298 // remove this tool from the tooltip control
301 Remove(m_window
->GetHWND());
305 void wxToolTip::Add(WXHWND hWnd
)
307 HWND hwnd
= (HWND
)hWnd
;
311 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
312 // store the tooltip text ourselves anyhow, and provide it in response to
313 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
314 // character tooltips as this is the size of the szText buffer in
315 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
318 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
320 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
322 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
326 // check for multiline toopltip
327 int index
= m_text
.Find(_T('\n'));
329 if ( index
!= wxNOT_FOUND
)
331 #ifdef TTM_SETMAXTIPWIDTH
332 if ( wxApp::GetComCtl32Version() >= 470 )
334 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
335 // extent of its first line as max value
336 HFONT hfont
= (HFONT
)
337 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
341 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
344 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
351 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
354 if ( !SelectObject(hdc
, hfont
) )
356 wxLogLastError(wxT("SelectObject(hfont)"));
359 // find the width of the widest line
361 wxStringTokenizer
tokenizer(m_text
, _T("\n"));
362 wxString token
= tokenizer
.GetNextToken();
363 while (token
.length())
366 if ( !::GetTextExtentPoint32(hdc
, token
.wx_str(), token
.length(), &sz
) )
368 wxLogLastError(wxT("GetTextExtentPoint32"));
373 token
= tokenizer
.GetNextToken();
376 // only set a new width if it is bigger than the current setting
377 if ( max
> SendTooltipMessage(GetToolTipCtrl(),
378 TTM_GETMAXTIPWIDTH
, 0) )
380 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
385 #endif // comctl32.dll >= 4.70
387 // replace the '\n's with spaces because otherwise they appear as
388 // unprintable characters in the tooltip string
389 m_text
.Replace(_T("\n"), _T(" "));
390 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
392 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
394 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
401 void wxToolTip::SetWindow(wxWindow
*win
)
407 // add the window itself
410 Add(m_window
->GetHWND());
412 #if !defined(__WXUNIVERSAL__)
413 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
414 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
417 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
418 size_t count
= subcontrols
.GetCount();
419 for ( size_t n
= 0; n
< count
; n
++ )
421 int id
= subcontrols
[n
];
422 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
425 // may be it's a child of parent of the control, in fact?
426 // (radiobuttons are subcontrols, i.e. children of the radiobox
427 // for wxWidgets but are its siblings at Windows level)
428 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
431 // must have it by now!
432 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
437 #endif // !defined(__WXUNIVERSAL__)
440 void wxToolTip::SetTip(const wxString
& tip
)
446 // update the tip text shown by the control
447 wxToolInfo
ti(GetHwndOf(m_window
));
448 ti
.lpszText
= (wxChar
*)m_text
.wx_str();
450 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
454 #endif // wxUSE_TOOLTIPS