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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
32 #include "wx/tooltip.h"
33 #include "wx/msw/private.h"
35 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
40 // minimal set of features by default
41 #define _WIN32_IE 0x0200
44 // VZ: normally, the trick with subclassing the tooltip control and processing
45 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
46 // code here for now (but it's not compiled) in case we need it later.
48 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
49 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
50 // because it would then work for all controls, not only radioboxes but for
51 // now I don't understand what's wrong with it...
52 #define wxUSE_TTM_WINDOWFROMPOINT 0
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // the tooltip parent window
59 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
61 #if wxUSE_TTM_WINDOWFROMPOINT
63 // the tooltip window proc
64 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
66 #endif // wxUSE_TTM_WINDOWFROMPOINT
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // a wrapper around TOOLINFO Win32 structure
74 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
77 class wxToolInfo
: public TOOLINFO
82 // initialize all members
83 ::ZeroMemory(this, sizeof(TOOLINFO
));
85 // the structure TOOLINFO has been extended with a 4 byte field in
86 // version 4.70 of comctl32.dll and if we compile on a newer machine
87 // but run on one with the old version of comctl32, nothing will work
88 // because the library will detect that we rely on a more recent
89 // version of it. So we always use the old size - if we ever start
90 // using our lParam member, we'd have to check for comctl32 version
92 #if _WIN32_IE >= 0x0300
93 cbSize
= sizeof(TOOLINFO
) - sizeof(LPARAM
);
95 cbSize
= sizeof(TOOLINFO
);
96 #endif // compile-time comctl32.dll version
98 uFlags
= TTF_IDISHWND
;
104 #pragma warning( default : 4097 )
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 // send a message to the tooltip control
112 inline LRESULT
SendTooltipMessage(WXHWND hwnd
,
117 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, wParam
, (LPARAM
)lParam
)
121 // send a message to all existing tooltip controls
122 static void SendTooltipMessageToAll(WXHWND hwnd
,
127 (void)SendTooltipMessage((WXHWND
)hwnd
, msg
, wParam
, (void *)lParam
);
130 // ============================================================================
132 // ============================================================================
134 #if wxUSE_TTM_WINDOWFROMPOINT
136 // ----------------------------------------------------------------------------
137 // window proc for our tooltip control
138 // ----------------------------------------------------------------------------
140 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
145 if ( msg
== TTM_WINDOWFROMPOINT
)
147 LPPOINT ppt
= (LPPOINT
)lParam
;
149 // the window on which event occured
150 HWND hwnd
= ::WindowFromPoint(*ppt
);
152 OutputDebugString("TTM_WINDOWFROMPOINT: ");
153 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
155 // return a HWND corresponding to a wxWindow because only wxWindows are
156 // associated with tooltips using TTM_ADDTOOL
157 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
161 hwnd
= GetHwndOf(win
);
162 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
165 // modify the point too!
167 GetWindowRect(hwnd
, &rect
);
169 ppt
->x
= (rect
.right
- rect
.left
) / 2;
170 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
172 return (LRESULT
)hwnd
;
176 OutputDebugString("no window\r\n");
180 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
183 #endif // wxUSE_TTM_WINDOWFROMPOINT
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 void wxToolTip::Enable(bool flag
)
191 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
194 void wxToolTip::SetDelay(long milliseconds
)
196 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
197 TTDT_INITIAL
, milliseconds
);
200 // ---------------------------------------------------------------------------
201 // implementation helpers
202 // ---------------------------------------------------------------------------
204 // create the tooltip ctrl for our parent frame if it doesn't exist yet
205 WXHWND
wxToolTip::GetToolTipCtrl()
209 ms_hwndTT
= (WXHWND
)::CreateWindow(TOOLTIPS_CLASS
,
212 CW_USEDEFAULT
, CW_USEDEFAULT
,
213 CW_USEDEFAULT
, CW_USEDEFAULT
,
219 HWND hwnd
= (HWND
)ms_hwndTT
;
220 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
221 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
223 #if wxUSE_TTM_WINDOWFROMPOINT
224 // subclass the newly created control
225 gs_wndprocToolTip
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_WNDPROC
);
226 ::SetWindowLong(hwnd
, GWL_WNDPROC
, (long)wxToolTipWndProc
);
227 #endif // wxUSE_TTM_WINDOWFROMPOINT
234 void wxToolTip::RelayEvent(WXMSG
*msg
)
236 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, 0, msg
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
245 wxToolTip::wxToolTip(const wxString
&tip
)
251 wxToolTip::~wxToolTip()
253 // there is no need to Remove() this tool - it will be done automatically
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
261 void wxToolTip::Remove()
263 // remove this tool from the tooltip control
266 wxToolInfo
ti(GetHwndOf(m_window
));
267 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, 0, &ti
);
271 void wxToolTip::Add(WXHWND hWnd
)
273 HWND hwnd
= (HWND
)hWnd
;
277 // as we store our text anyhow, it seems useless to waste system memory
278 // by asking the tooltip ctrl to remember it too - instead it will send
279 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
281 ti
.lpszText
= LPSTR_TEXTCALLBACK
;
282 // instead of: ti.lpszText = (char *)m_text.c_str();
284 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, 0, &ti
) )
286 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
290 // check for multiline toopltip
291 int index
= m_text
.Find(_T('\n'));
293 if ( index
!= wxNOT_FOUND
)
295 #if _WIN32_IE >= 0x0300
296 if ( wxTheApp
->GetComCtl32Version() >= 470 )
298 // use TTM_SETMAXWIDTH to make tooltip multiline using the
299 // extent of its first line as max value
300 HFONT hfont
= (HFONT
)SendTooltipMessage(GetToolTipCtrl(),
305 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
308 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
312 HDC hdc
= CreateCompatibleDC(NULL
);
315 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
318 if ( !SelectObject(hdc
, hfont
) )
320 wxLogLastError(wxT("SelectObject(hfont)"));
324 if ( !GetTextExtentPoint(hdc
, m_text
, index
, &sz
) )
326 wxLogLastError(wxT("GetTextExtentPoint"));
331 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(" "));
343 void wxToolTip::SetWindow(wxWindow
*win
)
349 // add the window itself
352 Add(m_window
->GetHWND());
355 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
356 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
359 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
360 size_t count
= subcontrols
.GetCount();
361 for ( size_t n
= 0; n
< count
; n
++ )
363 int id
= subcontrols
[n
];
364 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
367 // may be it's a child of parent of the control, in fact?
368 // (radiobuttons are subcontrols, i.e. children of the radiobox
369 // for wxWindows but are its siblings at Windows level)
370 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
373 // must have it by now!
374 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
380 // VZ: it's ugly to do it here, but I don't want any major changes right
381 // now, later we will probably want to have wxWindow::OnGotToolTip() or
382 // something like this where the derived class can do such things
383 // itself instead of wxToolTip "knowing" about them all
384 wxComboBox
*combo
= wxDynamicCast(control
, wxComboBox
);
387 WXHWND hwndComboEdit
= combo
->GetWindowStyle() & wxCB_READONLY
389 : combo
->GetEditHWND();
397 void wxToolTip::SetTip(const wxString
& tip
)
403 // update it immediately
404 wxToolInfo
ti(GetHwndOf(m_window
));
405 ti
.lpszText
= (wxChar
*)m_text
.c_str();
407 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, 0, &ti
);
411 #endif // wxUSE_TOOLTIPS