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"
32 #include "wx/control.h"
33 #include "wx/combobox.h"
36 #include "wx/msw/private.h"
38 // include <commctrl.h> "properly"
39 #include "wx/msw/wrapcctl.h"
41 // VZ: normally, the trick with subclassing the tooltip control and processing
42 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
43 // code here for now (but it's not compiled) in case we need it later.
45 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
46 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
47 // because it would then work for all controls, not only radioboxes but for
48 // now I don't understand what's wrong with it...
49 #define wxUSE_TTM_WINDOWFROMPOINT 0
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // the tooltip parent window
56 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
58 #if wxUSE_TTM_WINDOWFROMPOINT
60 // the tooltip window proc
61 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
63 #endif // wxUSE_TTM_WINDOWFROMPOINT
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // a wrapper around TOOLINFO Win32 structure
71 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
74 class wxToolInfo
: public TOOLINFO
77 wxToolInfo(HWND hwndOwner
)
79 // initialize all members
80 ::ZeroMemory(this, sizeof(TOOLINFO
));
82 // the structure TOOLINFO has been extended with a 4 byte field in
83 // version 4.70 of comctl32.dll and if we compile on a newer machine
84 // but run on one with the old version of comctl32, nothing will work
85 // because the library will detect that we rely on a more recent
86 // version of it. So we always use the old size - if we ever start
87 // using our lParam member, we'd have to check for comctl32 version
89 #if _WIN32_IE >= 0x0300
90 cbSize
= sizeof(TOOLINFO
) - sizeof(LPARAM
);
92 cbSize
= sizeof(TOOLINFO
);
93 #endif // compile-time comctl32.dll version
96 uFlags
= TTF_IDISHWND
;
97 uId
= (UINT
)hwndOwner
;
102 #pragma warning( default : 4097 )
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // send a message to the tooltip control if it exists
111 // NB: wParam is always 0 for the TTM_XXX messages we use
112 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
114 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
117 // send a message to all existing tooltip controls
119 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
122 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
125 // ============================================================================
127 // ============================================================================
129 #if wxUSE_TTM_WINDOWFROMPOINT
131 // ----------------------------------------------------------------------------
132 // window proc for our tooltip control
133 // ----------------------------------------------------------------------------
135 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
140 if ( msg
== TTM_WINDOWFROMPOINT
)
142 LPPOINT ppt
= (LPPOINT
)lParam
;
144 // the window on which event occurred
145 HWND hwnd
= ::WindowFromPoint(*ppt
);
147 OutputDebugString("TTM_WINDOWFROMPOINT: ");
148 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
150 // return a HWND corresponding to a wxWindow because only wxWidgets are
151 // associated with tooltips using TTM_ADDTOOL
152 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
156 hwnd
= GetHwndOf(win
);
157 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
160 // modify the point too!
162 GetWindowRect(hwnd
, &rect
);
164 ppt
->x
= (rect
.right
- rect
.left
) / 2;
165 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
167 return (LRESULT
)hwnd
;
171 OutputDebugString("no window\r\n");
175 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
178 #endif // wxUSE_TTM_WINDOWFROMPOINT
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 void wxToolTip::Enable(bool flag
)
186 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
189 void wxToolTip::SetDelay(long milliseconds
)
191 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
192 TTDT_INITIAL
, milliseconds
);
195 // ---------------------------------------------------------------------------
196 // implementation helpers
197 // ---------------------------------------------------------------------------
199 // create the tooltip ctrl for our parent frame if it doesn't exist yet
201 WXHWND
wxToolTip::GetToolTipCtrl()
205 // we want to show the tooltips always (even when the window is not
206 // active) and we don't want to strip "&"s from them
207 ms_hwndTT
= (WXHWND
)::CreateWindow(TOOLTIPS_CLASS
,
209 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
210 CW_USEDEFAULT
, CW_USEDEFAULT
,
211 CW_USEDEFAULT
, CW_USEDEFAULT
,
217 HWND hwnd
= (HWND
)ms_hwndTT
;
218 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
219 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
221 #if wxUSE_TTM_WINDOWFROMPOINT
222 // subclass the newly created control
223 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
224 #endif // wxUSE_TTM_WINDOWFROMPOINT
232 void wxToolTip::RelayEvent(WXMSG
*msg
)
234 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
243 wxToolTip::wxToolTip(const wxString
&tip
)
249 wxToolTip::~wxToolTip()
251 // the tooltip has to be removed before deleting. Otherwise, if it is visible
252 // while being deleted, there will be a delay before it goes away.
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 void wxToolTip::Remove(WXHWND hWnd
)
262 wxToolInfo
ti((HWND
)hWnd
);
263 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
266 void wxToolTip::Remove()
268 // remove this tool from the tooltip control
271 Remove(m_window
->GetHWND());
275 void wxToolTip::Add(WXHWND hWnd
)
277 HWND hwnd
= (HWND
)hWnd
;
281 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
282 // store the tooltip text ourselves anyhow, and provide it in response to
283 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
284 // character tooltips as this is the size of the szText buffer in
285 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
288 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
290 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
292 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
296 // check for multiline toopltip
297 int index
= m_text
.Find(_T('\n'));
299 if ( index
!= wxNOT_FOUND
)
301 #ifdef TTM_SETMAXTIPWIDTH
302 if ( wxApp::GetComCtl32Version() >= 470 )
304 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
305 // extent of its first line as max value
306 HFONT hfont
= (HFONT
)
307 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
311 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
314 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
321 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
324 if ( !SelectObject(hdc
, hfont
) )
326 wxLogLastError(wxT("SelectObject(hfont)"));
330 if ( !::GetTextExtentPoint32(hdc
, m_text
, index
, &sz
) )
332 wxLogLastError(wxT("GetTextExtentPoint32"));
335 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
339 #endif // comctl32.dll >= 4.70
341 // replace the '\n's with spaces because otherwise they appear as
342 // unprintable characters in the tooltip string
343 m_text
.Replace(_T("\n"), _T(" "));
344 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
346 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
348 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
355 void wxToolTip::SetWindow(wxWindow
*win
)
361 // add the window itself
364 Add(m_window
->GetHWND());
366 #if !defined(__WXUNIVERSAL__)
367 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
368 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
371 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
372 size_t count
= subcontrols
.GetCount();
373 for ( size_t n
= 0; n
< count
; n
++ )
375 int id
= subcontrols
[n
];
376 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
379 // may be it's a child of parent of the control, in fact?
380 // (radiobuttons are subcontrols, i.e. children of the radiobox
381 // for wxWidgets but are its siblings at Windows level)
382 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
385 // must have it by now!
386 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
392 // VZ: it's ugly to do it here, but I don't want any major changes right
393 // now, later we will probably want to have wxWindow::OnGotToolTip() or
394 // something like this where the derived class can do such things
395 // itself instead of wxToolTip "knowing" about them all
396 wxComboBox
*combo
= wxDynamicCast(control
, wxComboBox
);
399 WXHWND hwndComboEdit
= combo
->GetWindowStyle() & wxCB_READONLY
401 : combo
->GetEditHWND();
407 #endif // !defined(__WXUNIVERSAL__)
410 void wxToolTip::SetTip(const wxString
& tip
)
416 // update the tip text shown by the control
417 wxToolInfo
ti(GetHwndOf(m_window
));
418 ti
.lpszText
= (wxChar
*)m_text
.c_str();
420 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
424 #endif // wxUSE_TOOLTIPS