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 // VZ: normally, the trick with subclassing the tooltip control and processing
44 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
45 // code here for now (but it's not compiled) in case we need it later.
47 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
48 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
49 // because it would then work for all controls, not only radioboxes but for
50 // now I don't understand what's wrong with it...
51 #define wxUSE_TTM_WINDOWFROMPOINT 0
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // the tooltip parent window
58 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
60 #if wxUSE_TTM_WINDOWFROMPOINT
62 // the tooltip window proc
63 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
65 #endif // wxUSE_TTM_WINDOWFROMPOINT
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // a wrapper around TOOLINFO Win32 structure
73 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
76 class wxToolInfo
: public TOOLINFO
79 wxToolInfo(HWND hwndOwner
)
81 // initialize all members
82 ::ZeroMemory(this, sizeof(TOOLINFO
));
84 // the structure TOOLINFO has been extended with a 4 byte field in
85 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
86 // use these extended fields so use the old struct size to ensure that
87 // the tooltips work on old (Windows 95) systems too
88 cbSize
= TTTOOLINFO_V1_SIZE
;
91 uFlags
= TTF_IDISHWND
;
93 // we use TTF_TRANSPARENT to fix a problem which arises at least with
94 // the text controls but may presumably happen with other controls
95 // which display the tooltip at mouse position: it can start flashing
96 // then as the control gets "focus lost" events and dismisses the
97 // tooltip which then reappears because mouse remains hovering over the
98 // control, see SF patch 1821229
99 if ( wxApp::GetComCtl32Version() >= 470 )
101 uFlags
|= TTF_TRANSPARENT
;
104 uId
= (UINT
)hwndOwner
;
109 #pragma warning( default : 4097 )
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 // send a message to the tooltip control if it exists
118 // NB: wParam is always 0 for the TTM_XXX messages we use
119 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
121 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
124 // send a message to all existing tooltip controls
126 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
129 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
132 // ============================================================================
134 // ============================================================================
136 #if wxUSE_TTM_WINDOWFROMPOINT
138 // ----------------------------------------------------------------------------
139 // window proc for our tooltip control
140 // ----------------------------------------------------------------------------
142 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
147 if ( msg
== TTM_WINDOWFROMPOINT
)
149 LPPOINT ppt
= (LPPOINT
)lParam
;
151 // the window on which event occurred
152 HWND hwnd
= ::WindowFromPoint(*ppt
);
154 OutputDebugString("TTM_WINDOWFROMPOINT: ");
155 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
157 // return a HWND corresponding to a wxWindow because only wxWidgets are
158 // associated with tooltips using TTM_ADDTOOL
159 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
163 hwnd
= GetHwndOf(win
);
164 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
167 // modify the point too!
169 GetWindowRect(hwnd
, &rect
);
171 ppt
->x
= (rect
.right
- rect
.left
) / 2;
172 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
174 return (LRESULT
)hwnd
;
178 OutputDebugString("no window\r\n");
182 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
185 #endif // wxUSE_TTM_WINDOWFROMPOINT
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 void wxToolTip::Enable(bool flag
)
193 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
196 void wxToolTip::SetDelay(long milliseconds
)
198 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
199 TTDT_INITIAL
, milliseconds
);
202 void wxToolTip::SetAutoPop(long milliseconds
)
204 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
205 TTDT_AUTOPOP
, milliseconds
);
208 void wxToolTip::SetReshow(long milliseconds
)
210 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
211 TTDT_RESHOW
, milliseconds
);
214 // ---------------------------------------------------------------------------
215 // implementation helpers
216 // ---------------------------------------------------------------------------
218 // create the tooltip ctrl for our parent frame if it doesn't exist yet
220 WXHWND
wxToolTip::GetToolTipCtrl()
225 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
227 exflags
|= WS_EX_LAYOUTRTL
;
230 // we want to show the tooltips always (even when the window is not
231 // active) and we don't want to strip "&"s from them
232 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
235 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
236 CW_USEDEFAULT
, CW_USEDEFAULT
,
237 CW_USEDEFAULT
, CW_USEDEFAULT
,
243 HWND hwnd
= (HWND
)ms_hwndTT
;
244 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
245 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
247 #if wxUSE_TTM_WINDOWFROMPOINT
248 // subclass the newly created control
249 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
250 #endif // wxUSE_TTM_WINDOWFROMPOINT
258 void wxToolTip::RelayEvent(WXMSG
*msg
)
260 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
269 wxToolTip::wxToolTip(const wxString
&tip
)
275 wxToolTip::~wxToolTip()
277 // the tooltip has to be removed before deleting. Otherwise, if it is visible
278 // while being deleted, there will be a delay before it goes away.
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 void wxToolTip::Remove(WXHWND hWnd
)
288 wxToolInfo
ti((HWND
)hWnd
);
289 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
292 void wxToolTip::Remove()
294 // remove this tool from the tooltip control
297 Remove(m_window
->GetHWND());
301 void wxToolTip::Add(WXHWND hWnd
)
303 HWND hwnd
= (HWND
)hWnd
;
307 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
308 // store the tooltip text ourselves anyhow, and provide it in response to
309 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
310 // character tooltips as this is the size of the szText buffer in
311 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
314 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
316 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
318 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
322 // check for multiline toopltip
323 int index
= m_text
.Find(_T('\n'));
325 if ( index
!= wxNOT_FOUND
)
327 #ifdef TTM_SETMAXTIPWIDTH
328 if ( wxApp::GetComCtl32Version() >= 470 )
330 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
331 // extent of its first line as max value
332 HFONT hfont
= (HFONT
)
333 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
337 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
340 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
347 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
350 if ( !SelectObject(hdc
, hfont
) )
352 wxLogLastError(wxT("SelectObject(hfont)"));
355 // find the width of the widest line
357 wxStringTokenizer
tokenizer(m_text
, _T("\n"));
358 wxString token
= tokenizer
.GetNextToken();
359 while (token
.length())
362 if ( !::GetTextExtentPoint32(hdc
, token
.wx_str(), token
.length(), &sz
) )
364 wxLogLastError(wxT("GetTextExtentPoint32"));
369 token
= tokenizer
.GetNextToken();
372 // only set a new width if it is bigger than the current setting
373 if (max
> SendTooltipMessage(GetToolTipCtrl(), TTM_GETMAXTIPWIDTH
, 0))
374 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
378 #endif // comctl32.dll >= 4.70
380 // replace the '\n's with spaces because otherwise they appear as
381 // unprintable characters in the tooltip string
382 m_text
.Replace(_T("\n"), _T(" "));
383 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
385 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
387 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
394 void wxToolTip::SetWindow(wxWindow
*win
)
400 // add the window itself
403 Add(m_window
->GetHWND());
405 #if !defined(__WXUNIVERSAL__)
406 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
407 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
410 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
411 size_t count
= subcontrols
.GetCount();
412 for ( size_t n
= 0; n
< count
; n
++ )
414 int id
= subcontrols
[n
];
415 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
418 // may be it's a child of parent of the control, in fact?
419 // (radiobuttons are subcontrols, i.e. children of the radiobox
420 // for wxWidgets but are its siblings at Windows level)
421 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
424 // must have it by now!
425 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
430 #endif // !defined(__WXUNIVERSAL__)
433 void wxToolTip::SetTip(const wxString
& tip
)
439 // update the tip text shown by the control
440 wxToolInfo
ti(GetHwndOf(m_window
));
441 ti
.lpszText
= (wxChar
*)m_text
.wx_str();
443 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
447 #endif // wxUSE_TOOLTIPS