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 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
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
108 inline LRESULT
SendTooltipMessage(WXHWND hwnd
,
113 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, wParam
, (LPARAM
)lParam
)
117 // send a message to all existing tooltip controls
118 static void SendTooltipMessageToAll(WXHWND hwnd
,
123 (void)SendTooltipMessage((WXHWND
)hwnd
, msg
, wParam
, (void *)lParam
);
126 // ============================================================================
128 // ============================================================================
130 #if wxUSE_TTM_WINDOWFROMPOINT
132 // ----------------------------------------------------------------------------
133 // window proc for our tooltip control
134 // ----------------------------------------------------------------------------
136 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
141 if ( msg
== TTM_WINDOWFROMPOINT
)
143 LPPOINT ppt
= (LPPOINT
)lParam
;
145 // the window on which event occured
146 HWND hwnd
= ::WindowFromPoint(*ppt
);
148 OutputDebugString("TTM_WINDOWFROMPOINT: ");
149 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
151 // return a HWND corresponding to a wxWindow because only wxWindows are
152 // associated with tooltips using TTM_ADDTOOL
153 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
157 hwnd
= GetHwndOf(win
);
158 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
161 // modify the point too!
163 GetWindowRect(hwnd
, &rect
);
165 ppt
->x
= (rect
.right
- rect
.left
) / 2;
166 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
168 return (LRESULT
)hwnd
;
172 OutputDebugString("no window\r\n");
176 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
179 #endif // wxUSE_TTM_WINDOWFROMPOINT
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 void wxToolTip::Enable(bool flag
)
187 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
190 void wxToolTip::SetDelay(long milliseconds
)
192 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
193 TTDT_INITIAL
, milliseconds
);
196 // ---------------------------------------------------------------------------
197 // implementation helpers
198 // ---------------------------------------------------------------------------
200 // create the tooltip ctrl for our parent frame if it doesn't exist yet
201 WXHWND
wxToolTip::GetToolTipCtrl()
205 ms_hwndTT
= (WXHWND
)::CreateWindow(TOOLTIPS_CLASS
,
208 CW_USEDEFAULT
, CW_USEDEFAULT
,
209 CW_USEDEFAULT
, CW_USEDEFAULT
,
215 HWND hwnd
= (HWND
)ms_hwndTT
;
216 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
217 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
219 #if wxUSE_TTM_WINDOWFROMPOINT
220 // subclass the newly created control
221 gs_wndprocToolTip
= (WNDPROC
)::GetWindowLong(hwnd
, GWL_WNDPROC
);
222 ::SetWindowLong(hwnd
, GWL_WNDPROC
, (long)wxToolTipWndProc
);
223 #endif // wxUSE_TTM_WINDOWFROMPOINT
230 void wxToolTip::RelayEvent(WXMSG
*msg
)
232 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, 0, msg
);
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
241 wxToolTip::wxToolTip(const wxString
&tip
)
247 wxToolTip::~wxToolTip()
249 // there is no need to Remove() this tool - it will be done automatically
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 void wxToolTip::Remove()
259 // remove this tool from the tooltip control
262 wxToolInfo
ti(GetHwndOf(m_window
));
263 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, 0, &ti
);
267 void wxToolTip::Add(WXHWND hWnd
)
269 HWND hwnd
= (HWND
)hWnd
;
273 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
274 // store the tooltip text ourselves anyhow, and provide it in response to
275 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
276 // character tooltips as this is the size of the szText buffer in
277 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
280 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
282 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, 0, &ti
) )
284 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
288 // check for multiline toopltip
289 int index
= m_text
.Find(_T('\n'));
291 if ( index
!= wxNOT_FOUND
)
293 #ifdef TTM_SETMAXTIPWIDTH
294 if ( wxTheApp
->GetComCtl32Version() >= 470 )
296 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
297 // extent of its first line as max value
298 HFONT hfont
= (HFONT
)SendTooltipMessage(GetToolTipCtrl(),
303 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
306 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
313 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
316 if ( !SelectObject(hdc
, hfont
) )
318 wxLogLastError(wxT("SelectObject(hfont)"));
322 if ( !GetTextExtentPoint(hdc
, m_text
, index
, &sz
) )
324 wxLogLastError(wxT("GetTextExtentPoint"));
327 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
330 #endif // comctl32.dll >= 4.70
332 // replace the '\n's with spaces because otherwise they appear as
333 // unprintable characters in the tooltip string
334 m_text
.Replace(_T("\n"), _T(" "));
339 void wxToolTip::SetWindow(wxWindow
*win
)
345 // add the window itself
348 Add(m_window
->GetHWND());
350 #if !defined(__WXUNIVERSAL__)
351 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
352 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
355 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
356 size_t count
= subcontrols
.GetCount();
357 for ( size_t n
= 0; n
< count
; n
++ )
359 int id
= subcontrols
[n
];
360 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
363 // may be it's a child of parent of the control, in fact?
364 // (radiobuttons are subcontrols, i.e. children of the radiobox
365 // for wxWindows but are its siblings at Windows level)
366 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
369 // must have it by now!
370 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
376 // VZ: it's ugly to do it here, but I don't want any major changes right
377 // now, later we will probably want to have wxWindow::OnGotToolTip() or
378 // something like this where the derived class can do such things
379 // itself instead of wxToolTip "knowing" about them all
380 wxComboBox
*combo
= wxDynamicCast(control
, wxComboBox
);
383 WXHWND hwndComboEdit
= combo
->GetWindowStyle() & wxCB_READONLY
385 : combo
->GetEditHWND();
391 #endif // !defined(__WXUNIVERSAL__)
394 void wxToolTip::SetTip(const wxString
& tip
)
400 // update the tip text shown by the control
401 wxToolInfo
ti(GetHwndOf(m_window
));
402 ti
.lpszText
= (wxChar
*)m_text
.c_str();
404 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, 0, &ti
);
408 #endif // wxUSE_TOOLTIPS