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"
32 #include "wx/control.h"
33 #include "wx/combobox.h"
36 #include "wx/tokenzr.h"
37 #include "wx/msw/private.h"
39 // include <commctrl.h> "properly"
40 #include "wx/msw/wrapcctl.h"
42 // VZ: normally, the trick with subclassing the tooltip control and processing
43 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
44 // code here for now (but it's not compiled) in case we need it later.
46 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
47 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
48 // because it would then work for all controls, not only radioboxes but for
49 // now I don't understand what's wrong with it...
50 #define wxUSE_TTM_WINDOWFROMPOINT 0
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // the tooltip parent window
57 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
59 #if wxUSE_TTM_WINDOWFROMPOINT
61 // the tooltip window proc
62 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
64 #endif // wxUSE_TTM_WINDOWFROMPOINT
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // a wrapper around TOOLINFO Win32 structure
72 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
75 class wxToolInfo
: public TOOLINFO
78 wxToolInfo(HWND hwndOwner
)
80 // initialize all members
81 ::ZeroMemory(this, sizeof(TOOLINFO
));
83 // the structure TOOLINFO has been extended with a 4 byte field in
84 // version 4.70 of comctl32.dll and if we compile on a newer machine
85 // but run on one with the old version of comctl32, nothing will work
86 // because the library will detect that we rely on a more recent
87 // version of it. So we always use the old size - if we ever start
88 // using our lParam member, we'd have to check for comctl32 version
90 #if _WIN32_IE >= 0x0300
91 cbSize
= sizeof(TOOLINFO
) - sizeof(LPARAM
);
93 cbSize
= sizeof(TOOLINFO
);
94 #endif // compile-time comctl32.dll version
97 uFlags
= TTF_IDISHWND
;
98 uId
= (UINT
)hwndOwner
;
103 #pragma warning( default : 4097 )
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 // send a message to the tooltip control if it exists
112 // NB: wParam is always 0 for the TTM_XXX messages we use
113 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
115 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
118 // send a message to all existing tooltip controls
120 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
123 ::SendMessage((HWND
)hwnd
, msg
, wParam
, 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 occurred
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 wxWidgets 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
202 WXHWND
wxToolTip::GetToolTipCtrl()
207 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
209 exflags
|= WS_EX_LAYOUTRTL
;
212 // we want to show the tooltips always (even when the window is not
213 // active) and we don't want to strip "&"s from them
214 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
217 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
218 CW_USEDEFAULT
, CW_USEDEFAULT
,
219 CW_USEDEFAULT
, CW_USEDEFAULT
,
225 HWND hwnd
= (HWND
)ms_hwndTT
;
226 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
227 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
229 #if wxUSE_TTM_WINDOWFROMPOINT
230 // subclass the newly created control
231 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
232 #endif // wxUSE_TTM_WINDOWFROMPOINT
240 void wxToolTip::RelayEvent(WXMSG
*msg
)
242 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
251 wxToolTip::wxToolTip(const wxString
&tip
)
257 wxToolTip::~wxToolTip()
259 // the tooltip has to be removed before deleting. Otherwise, if it is visible
260 // while being deleted, there will be a delay before it goes away.
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 void wxToolTip::Remove(WXHWND hWnd
)
270 wxToolInfo
ti((HWND
)hWnd
);
271 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
274 void wxToolTip::Remove()
276 // remove this tool from the tooltip control
279 Remove(m_window
->GetHWND());
283 void wxToolTip::Add(WXHWND hWnd
)
285 HWND hwnd
= (HWND
)hWnd
;
289 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
290 // store the tooltip text ourselves anyhow, and provide it in response to
291 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
292 // character tooltips as this is the size of the szText buffer in
293 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
296 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
298 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
300 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
304 // check for multiline toopltip
305 int index
= m_text
.Find(_T('\n'));
307 if ( index
!= wxNOT_FOUND
)
309 #ifdef TTM_SETMAXTIPWIDTH
310 if ( wxApp::GetComCtl32Version() >= 470 )
312 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
313 // extent of its first line as max value
314 HFONT hfont
= (HFONT
)
315 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
319 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
322 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
329 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
332 if ( !SelectObject(hdc
, hfont
) )
334 wxLogLastError(wxT("SelectObject(hfont)"));
337 // find the width of the widest line
339 wxStringTokenizer
tokenizer(m_text
, _T("\n"));
340 wxString token
= tokenizer
.GetNextToken();
341 while (token
.length())
344 if ( !::GetTextExtentPoint32(hdc
, token
, token
.length(), &sz
) )
346 wxLogLastError(wxT("GetTextExtentPoint32"));
351 token
= tokenizer
.GetNextToken();
354 // only set a new width if it is bigger than the current setting
355 if (max
> SendTooltipMessage(GetToolTipCtrl(), TTM_GETMAXTIPWIDTH
, 0))
356 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
360 #endif // comctl32.dll >= 4.70
362 // replace the '\n's with spaces because otherwise they appear as
363 // unprintable characters in the tooltip string
364 m_text
.Replace(_T("\n"), _T(" "));
365 ti
.lpszText
= (wxChar
*)m_text
.c_str(); // const_cast
367 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
369 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text
.c_str());
376 void wxToolTip::SetWindow(wxWindow
*win
)
382 // add the window itself
385 Add(m_window
->GetHWND());
387 #if !defined(__WXUNIVERSAL__)
388 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
389 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
392 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
393 size_t count
= subcontrols
.GetCount();
394 for ( size_t n
= 0; n
< count
; n
++ )
396 int id
= subcontrols
[n
];
397 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
400 // may be it's a child of parent of the control, in fact?
401 // (radiobuttons are subcontrols, i.e. children of the radiobox
402 // for wxWidgets but are its siblings at Windows level)
403 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
406 // must have it by now!
407 wxASSERT_MSG( hwnd
, _T("no hwnd for subcontrol?") );
413 // VZ: it's ugly to do it here, but I don't want any major changes right
414 // now, later we will probably want to have wxWindow::OnGotToolTip() or
415 // something like this where the derived class can do such things
416 // itself instead of wxToolTip "knowing" about them all
417 wxComboBox
*combo
= wxDynamicCast(control
, wxComboBox
);
420 WXHWND hwndComboEdit
= combo
->GetWindowStyle() & wxCB_READONLY
422 : combo
->GetEditHWND();
428 #endif // !defined(__WXUNIVERSAL__)
431 void wxToolTip::SetTip(const wxString
& tip
)
437 // update the tip text shown by the control
438 wxToolInfo
ti(GetHwndOf(m_window
));
439 ti
.lpszText
= (wxChar
*)m_text
.c_str();
441 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
445 #endif // wxUSE_TOOLTIPS