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
80 wxToolInfo(HWND hwndOwner
)
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
99 uFlags
= TTF_IDISHWND
;
100 uId
= (UINT
)hwndOwner
;
105 #pragma warning( default : 4097 )
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 // send a message to the tooltip control
113 inline LRESULT
SendTooltipMessage(WXHWND hwnd
,
118 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, wParam
, (LPARAM
)lParam
)
122 // send a message to all existing tooltip controls
123 static void SendTooltipMessageToAll(WXHWND hwnd
,
128 (void)SendTooltipMessage((WXHWND
)hwnd
, msg
, wParam
, (void *)lParam
);
131 // ============================================================================
133 // ============================================================================
135 #if wxUSE_TTM_WINDOWFROMPOINT
137 // ----------------------------------------------------------------------------
138 // window proc for our tooltip control
139 // ----------------------------------------------------------------------------
141 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
146 if ( msg
== TTM_WINDOWFROMPOINT
)
148 LPPOINT ppt
= (LPPOINT
)lParam
;
150 // the window on which event occured
151 HWND hwnd
= ::WindowFromPoint(*ppt
);
153 OutputDebugString("TTM_WINDOWFROMPOINT: ");
154 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
156 // return a HWND corresponding to a wxWindow because only wxWindows are
157 // associated with tooltips using TTM_ADDTOOL
158 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
162 hwnd
= GetHwndOf(win
);
163 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
166 // modify the point too!
168 GetWindowRect(hwnd
, &rect
);
170 ppt
->x
= (rect
.right
- rect
.left
) / 2;
171 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
173 return (LRESULT
)hwnd
;
177 OutputDebugString("no window\r\n");
181 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
184 #endif // wxUSE_TTM_WINDOWFROMPOINT
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 void wxToolTip::Enable(bool flag
)
192 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
195 void wxToolTip::SetDelay(long milliseconds
)
197 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
198 TTDT_INITIAL
, milliseconds
);
201 // ---------------------------------------------------------------------------
202 // implementation helpers
203 // ---------------------------------------------------------------------------
205 // create the tooltip ctrl for our parent frame if it doesn't exist yet
206 WXHWND
wxToolTip::GetToolTipCtrl()
210 ms_hwndTT
= (WXHWND
)::CreateWindow(TOOLTIPS_CLASS
,
213 CW_USEDEFAULT
, CW_USEDEFAULT
,
214 CW_USEDEFAULT
, CW_USEDEFAULT
,
220 HWND hwnd
= (HWND
)ms_hwndTT
;
221 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
222 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
224 #if wxUSE_TTM_WINDOWFROMPOINT
225 // subclass the newly created control
226 gs_wndprocToolTip
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_WNDPROC
);
227 ::SetWindowLong(hwnd
, GWL_WNDPROC
, (long)wxToolTipWndProc
);
228 #endif // wxUSE_TTM_WINDOWFROMPOINT
235 void wxToolTip::RelayEvent(WXMSG
*msg
)
237 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, 0, msg
);
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
246 wxToolTip::wxToolTip(const wxString
&tip
)
252 wxToolTip::~wxToolTip()
254 // there is no need to Remove() this tool - it will be done automatically
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 void wxToolTip::Remove()
264 // remove this tool from the tooltip control
267 wxToolInfo
ti(GetHwndOf(m_window
));
268 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, 0, &ti
);
272 void wxToolTip::Add(WXHWND hWnd
)
274 HWND hwnd
= (HWND
)hWnd
;
278 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
279 // store the tooltip text ourselves anyhow, and provide it in response to
280 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
281 // character tooltips as this is the size of the szText buffer in
282 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
285 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
287 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, 0, &ti
) )
289 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
293 // check for multiline toopltip
294 int index
= m_text
.Find(_T('\n'));
296 if ( index
!= wxNOT_FOUND
)
298 #if _WIN32_IE >= 0x0300
299 if ( wxTheApp
->GetComCtl32Version() >= 470 )
301 // use TTM_SETMAXWIDTH to make tooltip multiline using the
302 // extent of its first line as max value
303 HFONT hfont
= (HFONT
)SendTooltipMessage(GetToolTipCtrl(),
308 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
311 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
318 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
321 if ( !SelectObject(hdc
, hfont
) )
323 wxLogLastError(wxT("SelectObject(hfont)"));
327 if ( !GetTextExtentPoint(hdc
, m_text
, index
, &sz
) )
329 wxLogLastError(wxT("GetTextExtentPoint"));
332 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
335 #endif // comctl32.dll >= 4.70
337 // replace the '\n's with spaces because otherwise they appear as
338 // unprintable characters in the tooltip string
339 m_text
.Replace(_T("\n"), _T(" "));
344 void wxToolTip::SetWindow(wxWindow
*win
)
350 // add the window itself
353 Add(m_window
->GetHWND());
355 #if !defined(__WXUNIVERSAL__)
356 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
357 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
360 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
361 size_t count
= subcontrols
.GetCount();
362 for ( size_t n
= 0; n
< count
; n
++ )
364 int id
= subcontrols
[n
];
365 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
368 // may be it's a child of parent of the control, in fact?
369 // (radiobuttons are subcontrols, i.e. children of the radiobox
370 // for wxWindows but are its siblings at Windows level)
371 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
374 // must have it by now!
375 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
381 // VZ: it's ugly to do it here, but I don't want any major changes right
382 // now, later we will probably want to have wxWindow::OnGotToolTip() or
383 // something like this where the derived class can do such things
384 // itself instead of wxToolTip "knowing" about them all
385 wxComboBox
*combo
= wxDynamicCast(control
, wxComboBox
);
388 WXHWND hwndComboEdit
= combo
->GetWindowStyle() & wxCB_READONLY
390 : combo
->GetEditHWND();
396 #endif // !defined(__WXUNIVERSAL__)
399 void wxToolTip::SetTip(const wxString
& tip
)
405 // update the tip text shown by the control
406 wxToolInfo
ti(GetHwndOf(m_window
));
407 ti
.lpszText
= (wxChar
*)m_text
.c_str();
409 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, 0, &ti
);
413 #endif // wxUSE_TOOLTIPS