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"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/control.h"
36 #include "wx/tokenzr.h"
37 #include "wx/msw/private.h"
39 #ifndef TTTOOLINFO_V1_SIZE
40 #define TTTOOLINFO_V1_SIZE 0x28
43 // VZ: normally, the trick with subclassing the tooltip control and processing
44 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
45 // code here for now (but it's not compiled) in case we need it later.
47 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
48 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
49 // because it would then work for all controls, not only radioboxes but for
50 // now I don't understand what's wrong with it...
51 #define wxUSE_TTM_WINDOWFROMPOINT 0
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // the tooltip parent window
58 WXHWND wxToolTip
::ms_hwndTT
= (WXHWND
)NULL
;
60 #if wxUSE_TTM_WINDOWFROMPOINT
62 // the tooltip window proc
63 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
65 #endif // wxUSE_TTM_WINDOWFROMPOINT
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // a wrapper around TOOLINFO Win32 structure
73 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
76 class wxToolInfo
: public TOOLINFO
79 wxToolInfo(HWND hwndOwner
)
81 // initialize all members
82 ::ZeroMemory(this, sizeof(TOOLINFO
));
84 // the structure TOOLINFO has been extended with a 4 byte field in
85 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
86 // use these extended fields so use the old struct size to ensure that
87 // the tooltips work on old (Windows 95) systems too
88 cbSize
= TTTOOLINFO_V1_SIZE
;
91 uFlags
= TTF_IDISHWND
;
93 uId
= (UINT
)hwndOwner
;
98 #pragma warning( default : 4097 )
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // send a message to the tooltip control if it exists
107 // NB: wParam is always 0 for the TTM_XXX messages we use
108 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
110 return hwnd ?
::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
113 // send a message to all existing tooltip controls
115 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
118 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
121 // ============================================================================
123 // ============================================================================
125 #if wxUSE_TTM_WINDOWFROMPOINT
127 // ----------------------------------------------------------------------------
128 // window proc for our tooltip control
129 // ----------------------------------------------------------------------------
131 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
136 if ( msg
== TTM_WINDOWFROMPOINT
)
138 LPPOINT ppt
= (LPPOINT
)lParam
;
140 // the window on which event occurred
141 HWND hwnd
= ::WindowFromPoint(*ppt
);
143 OutputDebugString("TTM_WINDOWFROMPOINT: ");
144 OutputDebugString(wxString
::Format("0x%08x => ", hwnd
));
146 // return a HWND corresponding to a wxWindow because only wxWidgets are
147 // associated with tooltips using TTM_ADDTOOL
148 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
152 hwnd
= GetHwndOf(win
);
153 OutputDebugString(wxString
::Format("0x%08x\r\n", hwnd
));
156 // modify the point too!
158 GetWindowRect(hwnd
, &rect
);
160 ppt
->x
= (rect
.right
- rect
.left
) / 2;
161 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
163 return (LRESULT
)hwnd
;
167 OutputDebugString("no window\r\n");
171 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
174 #endif // wxUSE_TTM_WINDOWFROMPOINT
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 void wxToolTip
::Enable(bool flag
)
182 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
185 void wxToolTip
::SetDelay(long milliseconds
)
187 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
188 TTDT_INITIAL
, milliseconds
);
191 void wxToolTip
::SetAutoPop(long milliseconds
)
193 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
194 TTDT_AUTOPOP
, milliseconds
);
197 void wxToolTip
::SetReshow(long milliseconds
)
199 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
200 TTDT_RESHOW
, milliseconds
);
203 // ---------------------------------------------------------------------------
204 // implementation helpers
205 // ---------------------------------------------------------------------------
207 // create the tooltip ctrl for our parent frame if it doesn't exist yet
209 WXHWND wxToolTip
::GetToolTipCtrl()
214 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
216 exflags
|= WS_EX_LAYOUTRTL
;
219 // we want to show the tooltips always (even when the window is not
220 // active) and we don't want to strip "&"s from them
221 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
224 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
225 CW_USEDEFAULT
, CW_USEDEFAULT
,
226 CW_USEDEFAULT
, CW_USEDEFAULT
,
232 HWND hwnd
= (HWND
)ms_hwndTT
;
233 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
234 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
236 #if wxUSE_TTM_WINDOWFROMPOINT
237 // subclass the newly created control
238 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
239 #endif // wxUSE_TTM_WINDOWFROMPOINT
247 void wxToolTip
::RelayEvent(WXMSG
*msg
)
249 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
258 wxToolTip
::wxToolTip(const wxString
&tip
)
264 wxToolTip
::~wxToolTip()
266 // the tooltip has to be removed before deleting. Otherwise, if it is visible
267 // while being deleted, there will be a delay before it goes away.
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 void wxToolTip
::Remove(WXHWND hWnd
)
277 wxToolInfo
ti((HWND
)hWnd
);
278 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
281 void wxToolTip
::Remove()
283 // remove this tool from the tooltip control
286 Remove(m_window
->GetHWND());
290 void wxToolTip
::Add(WXHWND hWnd
)
292 HWND hwnd
= (HWND
)hWnd
;
296 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
297 // store the tooltip text ourselves anyhow, and provide it in response to
298 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
299 // character tooltips as this is the size of the szText buffer in
300 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
303 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
305 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
307 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
311 // check for multiline toopltip
312 int index
= m_text
.Find(_T('\n'));
314 if ( index
!= wxNOT_FOUND
)
316 #ifdef TTM_SETMAXTIPWIDTH
317 if ( wxApp
::GetComCtl32Version() >= 470 )
319 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
320 // extent of its first line as max value
321 HFONT hfont
= (HFONT
)
322 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
326 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
329 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
336 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
339 if ( !SelectObject(hdc
, hfont
) )
341 wxLogLastError(wxT("SelectObject(hfont)"));
344 // find the width of the widest line
346 wxStringTokenizer
tokenizer(m_text
, _T("\n"));
347 wxString token
= tokenizer
.GetNextToken();
348 while (token
.length())
351 if ( !::GetTextExtentPoint32(hdc
, token
.wx_str(), token
.length(), &sz
) )
353 wxLogLastError(wxT("GetTextExtentPoint32"));
358 token
= tokenizer
.GetNextToken();
361 // only set a new width if it is bigger than the current setting
362 if (max
> SendTooltipMessage(GetToolTipCtrl(), TTM_GETMAXTIPWIDTH
, 0))
363 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
367 #endif // comctl32.dll >= 4.70
369 // replace the '\n's with spaces because otherwise they appear as
370 // unprintable characters in the tooltip string
371 m_text
.Replace(_T("\n"), _T(" "));
372 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
374 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
376 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
383 void wxToolTip
::SetWindow(wxWindow
*win
)
389 // add the window itself
392 Add(m_window
->GetHWND());
394 #if !defined(__WXUNIVERSAL__)
395 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
396 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
399 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
400 size_t count
= subcontrols
.GetCount();
401 for ( size_t n
= 0; n
< count
; n
++ )
403 int id
= subcontrols
[n
];
404 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
407 // may be it's a child of parent of the control, in fact?
408 // (radiobuttons are subcontrols, i.e. children of the radiobox
409 // for wxWidgets but are its siblings at Windows level)
410 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
413 // must have it by now!
414 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
419 #endif // !defined(__WXUNIVERSAL__)
422 void wxToolTip
::SetTip(const wxString
& tip
)
428 // update the tip text shown by the control
429 wxToolInfo
ti(GetHwndOf(m_window
));
430 ti
.lpszText
= (wxChar
*)m_text
.wx_str();
432 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
436 #endif // wxUSE_TOOLTIPS