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"
34 #include "wx/combobox.h"
37 #include "wx/tokenzr.h"
38 #include "wx/msw/private.h"
40 // VZ: normally, the trick with subclassing the tooltip control and processing
41 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
42 // code here for now (but it's not compiled) in case we need it later.
44 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
45 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
46 // because it would then work for all controls, not only radioboxes but for
47 // now I don't understand what's wrong with it...
48 #define wxUSE_TTM_WINDOWFROMPOINT 0
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // the tooltip parent window
55 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
57 #if wxUSE_TTM_WINDOWFROMPOINT
59 // the tooltip window proc
60 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
62 #endif // wxUSE_TTM_WINDOWFROMPOINT
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // a wrapper around TOOLINFO Win32 structure
70 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
73 class wxToolInfo
: public TOOLINFO
76 wxToolInfo(HWND hwndOwner
)
78 // initialize all members
79 ::ZeroMemory(this, sizeof(TOOLINFO
));
81 // the structure TOOLINFO has been extended with a 4 byte field in
82 // version 4.70 of comctl32.dll and if we compile on a newer machine
83 // but run on one with the old version of comctl32, nothing will work
84 // because the library will detect that we rely on a more recent
85 // version of it. So we always use the old size - if we ever start
86 // using our lParam member, we'd have to check for comctl32 version
88 #if _WIN32_IE >= 0x0300
89 cbSize
= sizeof(TOOLINFO
) - sizeof(LPARAM
);
91 cbSize
= sizeof(TOOLINFO
);
92 #endif // compile-time comctl32.dll version
95 uFlags
= TTF_IDISHWND
;
96 uId
= (UINT
)hwndOwner
;
101 #pragma warning( default : 4097 )
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // send a message to the tooltip control if it exists
110 // NB: wParam is always 0 for the TTM_XXX messages we use
111 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
113 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
116 // send a message to all existing tooltip controls
118 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
121 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
124 // ============================================================================
126 // ============================================================================
128 #if wxUSE_TTM_WINDOWFROMPOINT
130 // ----------------------------------------------------------------------------
131 // window proc for our tooltip control
132 // ----------------------------------------------------------------------------
134 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
139 if ( msg
== TTM_WINDOWFROMPOINT
)
141 LPPOINT ppt
= (LPPOINT
)lParam
;
143 // the window on which event occurred
144 HWND hwnd
= ::WindowFromPoint(*ppt
);
146 OutputDebugString("TTM_WINDOWFROMPOINT: ");
147 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
149 // return a HWND corresponding to a wxWindow because only wxWidgets are
150 // associated with tooltips using TTM_ADDTOOL
151 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
155 hwnd
= GetHwndOf(win
);
156 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
159 // modify the point too!
161 GetWindowRect(hwnd
, &rect
);
163 ppt
->x
= (rect
.right
- rect
.left
) / 2;
164 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
166 return (LRESULT
)hwnd
;
170 OutputDebugString("no window\r\n");
174 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
177 #endif // wxUSE_TTM_WINDOWFROMPOINT
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 void wxToolTip::Enable(bool flag
)
185 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
188 void wxToolTip::SetDelay(long milliseconds
)
190 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
191 TTDT_INITIAL
, milliseconds
);
194 void wxToolTip::SetAutoPop(long milliseconds
)
196 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
197 TTDT_AUTOPOP
, milliseconds
);
200 void wxToolTip::SetReshow(long milliseconds
)
202 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
203 TTDT_RESHOW
, milliseconds
);
206 // ---------------------------------------------------------------------------
207 // implementation helpers
208 // ---------------------------------------------------------------------------
210 // create the tooltip ctrl for our parent frame if it doesn't exist yet
212 WXHWND
wxToolTip::GetToolTipCtrl()
217 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
219 exflags
|= WS_EX_LAYOUTRTL
;
222 // we want to show the tooltips always (even when the window is not
223 // active) and we don't want to strip "&"s from them
224 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
227 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
228 CW_USEDEFAULT
, CW_USEDEFAULT
,
229 CW_USEDEFAULT
, CW_USEDEFAULT
,
235 HWND hwnd
= (HWND
)ms_hwndTT
;
236 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
237 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
239 #if wxUSE_TTM_WINDOWFROMPOINT
240 // subclass the newly created control
241 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
242 #endif // wxUSE_TTM_WINDOWFROMPOINT
250 void wxToolTip::RelayEvent(WXMSG
*msg
)
252 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
261 wxToolTip::wxToolTip(const wxString
&tip
)
267 wxToolTip::~wxToolTip()
269 // the tooltip has to be removed before deleting. Otherwise, if it is visible
270 // while being deleted, there will be a delay before it goes away.
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 void wxToolTip::Remove(WXHWND hWnd
)
280 wxToolInfo
ti((HWND
)hWnd
);
281 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
284 void wxToolTip::Remove()
286 // remove this tool from the tooltip control
289 Remove(m_window
->GetHWND());
293 void wxToolTip::Add(WXHWND hWnd
)
295 HWND hwnd
= (HWND
)hWnd
;
299 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
300 // store the tooltip text ourselves anyhow, and provide it in response to
301 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
302 // character tooltips as this is the size of the szText buffer in
303 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
306 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
308 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
310 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
314 // check for multiline toopltip
315 int index
= m_text
.Find(_T('\n'));
317 if ( index
!= wxNOT_FOUND
)
319 #ifdef TTM_SETMAXTIPWIDTH
320 if ( wxApp::GetComCtl32Version() >= 470 )
322 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
323 // extent of its first line as max value
324 HFONT hfont
= (HFONT
)
325 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
329 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
332 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
339 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
342 if ( !SelectObject(hdc
, hfont
) )
344 wxLogLastError(wxT("SelectObject(hfont)"));
347 // find the width of the widest line
349 wxStringTokenizer
tokenizer(m_text
, _T("\n"));
350 wxString token
= tokenizer
.GetNextToken();
351 while (token
.length())
354 if ( !::GetTextExtentPoint32(hdc
, token
.wx_str(), token
.length(), &sz
) )
356 wxLogLastError(wxT("GetTextExtentPoint32"));
361 token
= tokenizer
.GetNextToken();
364 // only set a new width if it is bigger than the current setting
365 if (max
> SendTooltipMessage(GetToolTipCtrl(), TTM_GETMAXTIPWIDTH
, 0))
366 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
370 #endif // comctl32.dll >= 4.70
372 // replace the '\n's with spaces because otherwise they appear as
373 // unprintable characters in the tooltip string
374 m_text
.Replace(_T("\n"), _T(" "));
375 ti
.lpszText
= (wxChar
*)m_text
.wx_str(); // const_cast
377 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
379 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
386 void wxToolTip::SetWindow(wxWindow
*win
)
392 // add the window itself
395 Add(m_window
->GetHWND());
397 #if !defined(__WXUNIVERSAL__)
398 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
399 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
402 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
403 size_t count
= subcontrols
.GetCount();
404 for ( size_t n
= 0; n
< count
; n
++ )
406 int id
= subcontrols
[n
];
407 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
410 // may be it's a child of parent of the control, in fact?
411 // (radiobuttons are subcontrols, i.e. children of the radiobox
412 // for wxWidgets but are its siblings at Windows level)
413 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
416 // must have it by now!
417 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
423 // VZ: it's ugly to do it here, but I don't want any major changes right
424 // now, later we will probably want to have wxWindow::OnGotToolTip() or
425 // something like this where the derived class can do such things
426 // itself instead of wxToolTip "knowing" about them all
427 wxComboBox
*combo
= wxDynamicCast(control
, wxComboBox
);
430 WXHWND hwndComboEdit
= combo
->GetWindowStyle() & wxCB_READONLY
432 : combo
->GetEditHWND();
438 #endif // !defined(__WXUNIVERSAL__)
441 void wxToolTip::SetTip(const wxString
& tip
)
447 // update the tip text shown by the control
448 wxToolInfo
ti(GetHwndOf(m_window
));
449 ti
.lpszText
= (wxChar
*)m_text
.wx_str();
451 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
455 #endif // wxUSE_TOOLTIPS