1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
32 #include "wx/tooltip.h"
33 #include "wx/msw/private.h"
35 // include <commctrl.h> "properly"
36 #include "wx/msw/wrapcctl.h"
38 // VZ: normally, the trick with subclassing the tooltip control and processing
39 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
40 // code here for now (but it's not compiled) in case we need it later.
42 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
43 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
44 // because it would then work for all controls, not only radioboxes but for
45 // now I don't understand what's wrong with it...
46 #define wxUSE_TTM_WINDOWFROMPOINT 0
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the tooltip parent window
53 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
55 #if wxUSE_TTM_WINDOWFROMPOINT
57 // the tooltip window proc
58 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
60 #endif // wxUSE_TTM_WINDOWFROMPOINT
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // a wrapper around TOOLINFO Win32 structure
68 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
71 class wxToolInfo
: public TOOLINFO
74 wxToolInfo(HWND hwndOwner
)
76 // initialize all members
77 ::ZeroMemory(this, sizeof(TOOLINFO
));
79 // the structure TOOLINFO has been extended with a 4 byte field in
80 // version 4.70 of comctl32.dll and if we compile on a newer machine
81 // but run on one with the old version of comctl32, nothing will work
82 // because the library will detect that we rely on a more recent
83 // version of it. So we always use the old size - if we ever start
84 // using our lParam member, we'd have to check for comctl32 version
86 #if _WIN32_IE >= 0x0300
87 cbSize
= sizeof(TOOLINFO
) - sizeof(LPARAM
);
89 cbSize
= sizeof(TOOLINFO
);
90 #endif // compile-time comctl32.dll version
93 uFlags
= TTF_IDISHWND
;
94 uId
= (UINT
)hwndOwner
;
99 #pragma warning( default : 4097 )
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // send a message to the tooltip control if it exists
108 // NB: wParam is always 0 for the TTM_XXX messages we use
109 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
111 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
114 // send a message to all existing tooltip controls
116 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
119 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
122 // ============================================================================
124 // ============================================================================
126 #if wxUSE_TTM_WINDOWFROMPOINT
128 // ----------------------------------------------------------------------------
129 // window proc for our tooltip control
130 // ----------------------------------------------------------------------------
132 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
137 if ( msg
== TTM_WINDOWFROMPOINT
)
139 LPPOINT ppt
= (LPPOINT
)lParam
;
141 // the window on which event occurred
142 HWND hwnd
= ::WindowFromPoint(*ppt
);
144 OutputDebugString("TTM_WINDOWFROMPOINT: ");
145 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
147 // return a HWND corresponding to a wxWindow because only wxWidgets are
148 // associated with tooltips using TTM_ADDTOOL
149 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
153 hwnd
= GetHwndOf(win
);
154 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
157 // modify the point too!
159 GetWindowRect(hwnd
, &rect
);
161 ppt
->x
= (rect
.right
- rect
.left
) / 2;
162 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
164 return (LRESULT
)hwnd
;
168 OutputDebugString("no window\r\n");
172 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
175 #endif // wxUSE_TTM_WINDOWFROMPOINT
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 void wxToolTip::Enable(bool flag
)
183 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
186 void wxToolTip::SetDelay(long milliseconds
)
188 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
189 TTDT_INITIAL
, milliseconds
);
192 // ---------------------------------------------------------------------------
193 // implementation helpers
194 // ---------------------------------------------------------------------------
196 // create the tooltip ctrl for our parent frame if it doesn't exist yet
198 WXHWND
wxToolTip::GetToolTipCtrl()
202 ms_hwndTT
= (WXHWND
)::CreateWindow(TOOLTIPS_CLASS
,
205 CW_USEDEFAULT
, CW_USEDEFAULT
,
206 CW_USEDEFAULT
, CW_USEDEFAULT
,
212 HWND hwnd
= (HWND
)ms_hwndTT
;
213 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
214 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
216 #if wxUSE_TTM_WINDOWFROMPOINT
217 // subclass the newly created control
218 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
219 #endif // wxUSE_TTM_WINDOWFROMPOINT
227 void wxToolTip::RelayEvent(WXMSG
*msg
)
229 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
238 wxToolTip::wxToolTip(const wxString
&tip
)
244 wxToolTip::~wxToolTip()
246 // the tooltip has to be removed before deleting. Otherwise, if it is visible
247 // while being deleted, there will be a delay before it goes away.
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 void wxToolTip::Remove(WXHWND hWnd
)
257 wxToolInfo
ti((HWND
)hWnd
);
258 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
261 void wxToolTip::Remove()
263 // remove this tool from the tooltip control
266 Remove(m_window
->GetHWND());
270 void wxToolTip::Add(WXHWND hWnd
)
272 HWND hwnd
= (HWND
)hWnd
;
276 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
277 // store the tooltip text ourselves anyhow, and provide it in response to
278 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
279 // character tooltips as this is the size of the szText buffer in
280 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
283 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
285 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
287 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
291 // check for multiline toopltip
292 int index
= m_text
.Find(_T('\n'));
294 if ( index
!= wxNOT_FOUND
)
296 #ifdef TTM_SETMAXTIPWIDTH
297 if ( wxApp::GetComCtl32Version() >= 470 )
299 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
300 // extent of its first line as max value
301 HFONT hfont
= (HFONT
)
302 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
306 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
309 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
316 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
319 if ( !SelectObject(hdc
, hfont
) )
321 wxLogLastError(wxT("SelectObject(hfont)"));
325 if ( !::GetTextExtentPoint32(hdc
, m_text
, index
, &sz
) )
327 wxLogLastError(wxT("GetTextExtentPoint32"));
330 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
334 #endif // comctl32.dll >= 4.70
336 // replace the '\n's with spaces because otherwise they appear as
337 // unprintable characters in the tooltip string
338 m_text
.Replace(_T("\n"), _T(" "));
339 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
341 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
343 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
350 void wxToolTip::SetWindow(wxWindow
*win
)
356 // add the window itself
359 Add(m_window
->GetHWND());
361 #if !defined(__WXUNIVERSAL__)
362 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
363 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
366 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
367 size_t count
= subcontrols
.GetCount();
368 for ( size_t n
= 0; n
< count
; n
++ )
370 int id
= subcontrols
[n
];
371 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
374 // may be it's a child of parent of the control, in fact?
375 // (radiobuttons are subcontrols, i.e. children of the radiobox
376 // for wxWidgets but are its siblings at Windows level)
377 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
380 // must have it by now!
381 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
387 // VZ: it's ugly to do it here, but I don't want any major changes right
388 // now, later we will probably want to have wxWindow::OnGotToolTip() or
389 // something like this where the derived class can do such things
390 // itself instead of wxToolTip "knowing" about them all
391 wxComboBox
*combo
= wxDynamicCast(control
, wxComboBox
);
394 WXHWND hwndComboEdit
= combo
->GetWindowStyle() & wxCB_READONLY
396 : combo
->GetEditHWND();
402 #endif // !defined(__WXUNIVERSAL__)
405 void wxToolTip::SetTip(const wxString
& tip
)
411 // update the tip text shown by the control
412 wxToolInfo
ti(GetHwndOf(m_window
));
413 ti
.lpszText
= (wxChar
*)m_text
.c_str();
415 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
419 #endif // wxUSE_TOOLTIPS