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 // VZ: normally, the trick with subclassing the tooltip control and processing
40 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
41 // code here for now (but it's not compiled) in case we need it later.
43 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
44 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
45 // because it would then work for all controls, not only radioboxes but for
46 // now I don't understand what's wrong with it...
47 #define wxUSE_TTM_WINDOWFROMPOINT 0
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // the tooltip parent window
54 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
56 #if wxUSE_TTM_WINDOWFROMPOINT
58 // the tooltip window proc
59 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
61 #endif // wxUSE_TTM_WINDOWFROMPOINT
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // a wrapper around TOOLINFO Win32 structure
69 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
72 class wxToolInfo
: public TOOLINFO
75 wxToolInfo(HWND hwndOwner
)
77 // initialize all members
78 ::ZeroMemory(this, sizeof(TOOLINFO
));
80 // the structure TOOLINFO has been extended with a 4 byte field in
81 // version 4.70 of comctl32.dll and if we compile on a newer machine
82 // but run on one with the old version of comctl32, nothing will work
83 // because the library will detect that we rely on a more recent
84 // version of it. So we always use the old size - if we ever start
85 // using our lParam member, we'd have to check for comctl32 version
87 #if _WIN32_IE >= 0x0300
88 cbSize
= sizeof(TOOLINFO
) - sizeof(LPARAM
);
90 cbSize
= sizeof(TOOLINFO
);
91 #endif // compile-time comctl32.dll version
94 uFlags
= TTF_IDISHWND
;
95 uId
= (UINT
)hwndOwner
;
100 #pragma warning( default : 4097 )
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // send a message to the tooltip control if it exists
109 // NB: wParam is always 0 for the TTM_XXX messages we use
110 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
112 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
115 // send a message to all existing tooltip controls
117 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
120 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
123 // ============================================================================
125 // ============================================================================
127 #if wxUSE_TTM_WINDOWFROMPOINT
129 // ----------------------------------------------------------------------------
130 // window proc for our tooltip control
131 // ----------------------------------------------------------------------------
133 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
138 if ( msg
== TTM_WINDOWFROMPOINT
)
140 LPPOINT ppt
= (LPPOINT
)lParam
;
142 // the window on which event occurred
143 HWND hwnd
= ::WindowFromPoint(*ppt
);
145 OutputDebugString("TTM_WINDOWFROMPOINT: ");
146 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
148 // return a HWND corresponding to a wxWindow because only wxWidgets are
149 // associated with tooltips using TTM_ADDTOOL
150 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
154 hwnd
= GetHwndOf(win
);
155 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
158 // modify the point too!
160 GetWindowRect(hwnd
, &rect
);
162 ppt
->x
= (rect
.right
- rect
.left
) / 2;
163 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
165 return (LRESULT
)hwnd
;
169 OutputDebugString("no window\r\n");
173 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
176 #endif // wxUSE_TTM_WINDOWFROMPOINT
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 void wxToolTip::Enable(bool flag
)
184 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
187 void wxToolTip::SetDelay(long milliseconds
)
189 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
190 TTDT_INITIAL
, milliseconds
);
193 void wxToolTip::SetAutoPop(long milliseconds
)
195 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
196 TTDT_AUTOPOP
, milliseconds
);
199 void wxToolTip::SetReshow(long milliseconds
)
201 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
202 TTDT_RESHOW
, milliseconds
);
205 // ---------------------------------------------------------------------------
206 // implementation helpers
207 // ---------------------------------------------------------------------------
209 // create the tooltip ctrl for our parent frame if it doesn't exist yet
211 WXHWND
wxToolTip::GetToolTipCtrl()
216 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
218 exflags
|= WS_EX_LAYOUTRTL
;
221 // we want to show the tooltips always (even when the window is not
222 // active) and we don't want to strip "&"s from them
223 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
226 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
227 CW_USEDEFAULT
, CW_USEDEFAULT
,
228 CW_USEDEFAULT
, CW_USEDEFAULT
,
234 HWND hwnd
= (HWND
)ms_hwndTT
;
235 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
236 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
238 #if wxUSE_TTM_WINDOWFROMPOINT
239 // subclass the newly created control
240 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
241 #endif // wxUSE_TTM_WINDOWFROMPOINT
249 void wxToolTip::RelayEvent(WXMSG
*msg
)
251 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
260 wxToolTip::wxToolTip(const wxString
&tip
)
266 wxToolTip::~wxToolTip()
268 // the tooltip has to be removed before deleting. Otherwise, if it is visible
269 // while being deleted, there will be a delay before it goes away.
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 void wxToolTip::Remove(WXHWND hWnd
)
279 wxToolInfo
ti((HWND
)hWnd
);
280 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
283 void wxToolTip::Remove()
285 // remove this tool from the tooltip control
288 Remove(m_window
->GetHWND());
292 void wxToolTip::Add(WXHWND hWnd
)
294 HWND hwnd
= (HWND
)hWnd
;
298 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
299 // store the tooltip text ourselves anyhow, and provide it in response to
300 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
301 // character tooltips as this is the size of the szText buffer in
302 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
305 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
307 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
309 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
313 // check for multiline toopltip
314 int index
= m_text
.Find(_T('\n'));
316 if ( index
!= wxNOT_FOUND
)
318 #ifdef TTM_SETMAXTIPWIDTH
319 if ( wxApp::GetComCtl32Version() >= 470 )
321 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
322 // extent of its first line as max value
323 HFONT hfont
= (HFONT
)
324 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
328 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
331 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
338 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
341 if ( !SelectObject(hdc
, hfont
) )
343 wxLogLastError(wxT("SelectObject(hfont)"));
346 // find the width of the widest line
348 wxStringTokenizer
tokenizer(m_text
, _T("\n"));
349 wxString token
= tokenizer
.GetNextToken();
350 while (token
.length())
353 if ( !::GetTextExtentPoint32(hdc
, token
.wx_str(), token
.length(), &sz
) )
355 wxLogLastError(wxT("GetTextExtentPoint32"));
360 token
= tokenizer
.GetNextToken();
363 // only set a new width if it is bigger than the current setting
364 if (max
> SendTooltipMessage(GetToolTipCtrl(), TTM_GETMAXTIPWIDTH
, 0))
365 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
369 #endif // comctl32.dll >= 4.70
371 // replace the '\n's with spaces because otherwise they appear as
372 // unprintable characters in the tooltip string
373 m_text
.Replace(_T("\n"), _T(" "));
374 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
376 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
378 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
385 void wxToolTip::SetWindow(wxWindow
*win
)
391 // add the window itself
394 Add(m_window
->GetHWND());
396 #if !defined(__WXUNIVERSAL__)
397 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
398 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
401 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
402 size_t count
= subcontrols
.GetCount();
403 for ( size_t n
= 0; n
< count
; n
++ )
405 int id
= subcontrols
[n
];
406 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
409 // may be it's a child of parent of the control, in fact?
410 // (radiobuttons are subcontrols, i.e. children of the radiobox
411 // for wxWidgets but are its siblings at Windows level)
412 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
415 // must have it by now!
416 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
421 #endif // !defined(__WXUNIVERSAL__)
424 void wxToolTip::SetTip(const wxString
& tip
)
430 // update the tip text shown by the control
431 wxToolInfo
ti(GetHwndOf(m_window
));
432 ti
.lpszText
= (wxChar
*)m_text
.wx_str();
434 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
438 #endif // wxUSE_TOOLTIPS