1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tooltip.cpp
3 // Purpose: wxToolTip class implementation for MSW
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999 Vadim Zeitlin
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
27 #include "wx/tooltip.h"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/control.h"
35 #include "wx/tokenzr.h"
36 #include "wx/vector.h"
37 #include "wx/msw/private.h"
39 #ifndef TTTOOLINFO_V1_SIZE
40 #define TTTOOLINFO_V1_SIZE 0x28
43 #ifndef TTF_TRANSPARENT
44 #define TTF_TRANSPARENT 0x0100
47 // VZ: normally, the trick with subclassing the tooltip control and processing
48 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
49 // code here for now (but it's not compiled) in case we need it later.
51 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
52 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
53 // because it would then work for all controls, not only radioboxes but for
54 // now I don't understand what's wrong with it...
55 #define wxUSE_TTM_WINDOWFROMPOINT 0
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // the tooltip parent window
62 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
64 // new tooltip maximum width, default value is set on first call to wxToolTip::Add()
65 int wxToolTip::ms_maxWidth
= 0;
67 #if wxUSE_TTM_WINDOWFROMPOINT
69 // the tooltip window proc
70 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
72 #endif // wxUSE_TTM_WINDOWFROMPOINT
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // This is simply a wrapper for vector<HWND> but defined as a class to hide the
79 // details from the public header.
80 class wxToolTipOtherWindows
: public wxVector
<WXHWND
>
84 // a wrapper around TOOLINFO Win32 structure
86 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
89 class wxToolInfo
: public TOOLINFO
92 wxToolInfo(HWND hwndOwner
, unsigned int id
, const wxRect
& rc
)
94 // initialize all members
95 ::ZeroMemory(this, sizeof(TOOLINFO
));
97 // the structure TOOLINFO has been extended with a 4 byte field in
98 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
99 // use these extended fields so use the old struct size to ensure that
100 // the tooltips work on old (Windows 95) systems too
101 cbSize
= TTTOOLINFO_V1_SIZE
;
107 uFlags
= TTF_IDISHWND
;
108 uId
= (UINT_PTR
)hwndOwner
;
112 // this tooltip must be shown only if the mouse hovers a specific rect
113 // of the hwnd parameter!
114 rect
.left
= rc
.GetLeft();
115 rect
.right
= rc
.GetRight();
116 rect
.top
= rc
.GetTop();
117 rect
.bottom
= rc
.GetBottom();
119 // note that not setting TTF_IDISHWND from the uFlags member means that the
120 // ti.uId field should not contain the HWND but rather as MSDN says an
121 // "Application-defined identifier of the tool"; this is used internally by
122 // Windows to distinguish the different tooltips attached to the same window
126 // we use TTF_TRANSPARENT to fix a problem which arises at least with
127 // the text controls but may presumably happen with other controls
128 // which display the tooltip at mouse position: it can start flashing
129 // then as the control gets "focus lost" events and dismisses the
130 // tooltip which then reappears because mouse remains hovering over the
131 // control, see SF patch 1821229
132 if ( wxApp::GetComCtl32Version() >= 470 )
134 uFlags
|= TTF_TRANSPARENT
;
140 #pragma warning( default : 4097 )
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 // send a message to the tooltip control if it exists
149 // NB: wParam is always 0 for the TTM_XXX messages we use
150 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
152 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
155 // send a message to all existing tooltip controls
157 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
160 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
163 // ============================================================================
165 // ============================================================================
167 #if wxUSE_TTM_WINDOWFROMPOINT
169 // ----------------------------------------------------------------------------
170 // window proc for our tooltip control
171 // ----------------------------------------------------------------------------
173 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
178 if ( msg
== TTM_WINDOWFROMPOINT
)
180 LPPOINT ppt
= (LPPOINT
)lParam
;
182 // the window on which event occurred
183 HWND hwnd
= ::WindowFromPoint(*ppt
);
185 OutputDebugString("TTM_WINDOWFROMPOINT: ");
186 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
188 // return a HWND corresponding to a wxWindow because only wxWidgets are
189 // associated with tooltips using TTM_ADDTOOL
190 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
194 hwnd
= GetHwndOf(win
);
195 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
198 // modify the point too!
200 GetWindowRect(hwnd
, &rect
);
202 ppt
->x
= (rect
.right
- rect
.left
) / 2;
203 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
205 return (LRESULT
)hwnd
;
209 OutputDebugString("no window\r\n");
213 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
216 #endif // wxUSE_TTM_WINDOWFROMPOINT
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 void wxToolTip::Enable(bool flag
)
224 // Make sure the tooltip has been created
225 (void) GetToolTipCtrl();
227 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
230 void wxToolTip::SetDelay(long milliseconds
)
232 // Make sure the tooltip has been created
233 (void) GetToolTipCtrl();
235 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
236 TTDT_INITIAL
, milliseconds
);
239 void wxToolTip::SetAutoPop(long milliseconds
)
241 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
242 TTDT_AUTOPOP
, milliseconds
);
245 void wxToolTip::SetReshow(long milliseconds
)
247 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
248 TTDT_RESHOW
, milliseconds
);
251 void wxToolTip::SetMaxWidth(int width
)
253 wxASSERT_MSG( width
== -1 || width
>= 0, wxT("invalid width value") );
258 // ---------------------------------------------------------------------------
259 // implementation helpers
260 // ---------------------------------------------------------------------------
262 // create the tooltip ctrl for our parent frame if it doesn't exist yet
264 WXHWND
wxToolTip::GetToolTipCtrl()
269 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
271 exflags
|= WS_EX_LAYOUTRTL
;
274 // we want to show the tooltips always (even when the window is not
275 // active) and we don't want to strip "&"s from them
276 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
279 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
280 CW_USEDEFAULT
, CW_USEDEFAULT
,
281 CW_USEDEFAULT
, CW_USEDEFAULT
,
287 HWND hwnd
= (HWND
)ms_hwndTT
;
288 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
289 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
291 #if wxUSE_TTM_WINDOWFROMPOINT
292 // subclass the newly created control
293 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
294 #endif // wxUSE_TTM_WINDOWFROMPOINT
302 void wxToolTip::RelayEvent(WXMSG
*msg
)
304 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
313 wxToolTip::wxToolTip(const wxString
&tip
)
319 // make sure m_rect.IsEmpty() == true
323 // since m_rect is not valid, m_id is ignored by wxToolInfo ctor...
327 wxToolTip::wxToolTip(wxWindow
* win
, unsigned int id
, const wxString
&tip
, const wxRect
& rc
)
328 : m_text(tip
), m_rect(rc
), m_id(id
)
336 wxToolTip::~wxToolTip()
338 // the tooltip has to be removed before deleting. Otherwise, if it is visible
339 // while being deleted, there will be a delay before it goes away.
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
350 void wxToolTip::Remove(WXHWND hWnd
, unsigned int id
, const wxRect
& rc
)
352 wxToolInfo
ti((HWND
)hWnd
, id
, rc
);
354 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
357 void wxToolTip::DoRemove(WXHWND hWnd
)
359 if ( m_window
&& hWnd
== m_window
->GetHWND() )
361 // Remove the tooltip from the main window.
362 Remove(hWnd
, m_id
, m_rect
);
366 // Not really sure what to pass to remove in this case...
367 Remove(hWnd
, 0, wxRect());
371 void wxToolTip::Remove()
373 DoForAllWindows(&wxToolTip::DoRemove
);
376 void wxToolTip::AddOtherWindow(WXHWND hWnd
)
379 m_others
= new wxToolTipOtherWindows
;
381 m_others
->push_back(hWnd
);
386 void wxToolTip::DoAddHWND(WXHWND hWnd
)
388 HWND hwnd
= (HWND
)hWnd
;
390 wxToolInfo
ti(hwnd
, m_id
, m_rect
);
392 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
393 // store the tooltip text ourselves anyhow, and provide it in response to
394 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
395 // character tooltips as this is the size of the szText buffer in
396 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
399 ti
.lpszText
= wxMSW_CONV_LPTSTR(m_text
);
401 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
403 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text
.c_str());
408 #ifdef TTM_SETMAXTIPWIDTH
409 if ( wxApp::GetComCtl32Version() >= 470 )
411 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
412 // extent of its first line as max value
413 HFONT hfont
= (HFONT
)
414 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
418 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
421 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
428 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
431 if ( !SelectObject(hdc
, hfont
) )
433 wxLogLastError(wxT("SelectObject(hfont)"));
436 // find the width of the widest line
438 wxStringTokenizer
tokenizer(m_text
, wxT("\n"));
439 while ( tokenizer
.HasMoreTokens() )
441 const wxString token
= tokenizer
.GetNextToken();
444 if ( !::GetTextExtentPoint32(hdc
, token
.t_str(),
445 token
.length(), &sz
) )
447 wxLogLastError(wxT("GetTextExtentPoint32"));
450 if ( sz
.cx
> maxWidth
)
454 // limit size to ms_maxWidth, if set
455 if ( ms_maxWidth
== 0 )
457 // this is more or less arbitrary but seems to work well
458 static const int DEFAULT_MAX_WIDTH
= 400;
460 ms_maxWidth
= wxGetClientDisplayRect().width
/ 2;
462 if ( ms_maxWidth
> DEFAULT_MAX_WIDTH
)
463 ms_maxWidth
= DEFAULT_MAX_WIDTH
;
466 if ( ms_maxWidth
!= -1 && maxWidth
> ms_maxWidth
)
467 maxWidth
= ms_maxWidth
;
469 // only set a new width if it is bigger than the current setting:
470 // otherwise adding a tooltip with shorter line(s) than a previous
471 // one would result in breaking the longer lines unnecessarily as
472 // all our tooltips share the same maximal width
473 if ( maxWidth
> SendTooltipMessage(GetToolTipCtrl(),
474 TTM_GETMAXTIPWIDTH
, 0) )
476 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
477 wxUIntToPtr(maxWidth
));
481 #endif // TTM_SETMAXTIPWIDTH
483 // replace the '\n's with spaces because otherwise they appear as
484 // unprintable characters in the tooltip string
485 m_text
.Replace(wxT("\n"), wxT(" "));
486 ti
.lpszText
= wxMSW_CONV_LPTSTR(m_text
);
488 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
490 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text
.c_str());
495 void wxToolTip::SetWindow(wxWindow
*win
)
501 // add the window itself
504 DoAddHWND(m_window
->GetHWND());
506 #if !defined(__WXUNIVERSAL__)
507 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
508 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
511 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
512 size_t count
= subcontrols
.GetCount();
513 for ( size_t n
= 0; n
< count
; n
++ )
515 int id
= subcontrols
[n
];
516 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
519 // maybe it's a child of parent of the control, in fact?
520 // (radiobuttons are subcontrols, i.e. children of the radiobox
521 // for wxWidgets but are its siblings at Windows level)
522 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
525 // must have it by now!
526 wxASSERT_MSG( hwnd
, wxT("no hwnd for subcontrol?") );
528 AddOtherWindow((WXHWND
)hwnd
);
531 #endif // !defined(__WXUNIVERSAL__)
534 void wxToolTip::SetRect(const wxRect
& rc
)
540 wxToolInfo
ti(GetHwndOf(m_window
), m_id
, m_rect
);
541 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_NEWTOOLRECT
, &ti
);
545 void wxToolTip::SetTip(const wxString
& tip
)
549 DoForAllWindows(&wxToolTip::DoSetTip
);
552 void wxToolTip::DoSetTip(WXHWND hWnd
)
554 // update the tip text shown by the control
555 wxToolInfo
ti((HWND
)hWnd
, m_id
, m_rect
);
557 // for some reason, changing the tooltip text directly results in
558 // repaint of the controls under it, see #10520 -- but this doesn't
559 // happen if we reset it first
560 ti
.lpszText
= const_cast<wxChar
*>(wxT(""));
561 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
563 ti
.lpszText
= wxMSW_CONV_LPTSTR(m_text
);
564 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
567 void wxToolTip::DoForAllWindows(void (wxToolTip::*func
)(WXHWND
))
571 (this->*func
)(m_window
->GetHWND());
576 for ( wxToolTipOtherWindows::const_iterator it
= m_others
->begin();
577 it
!= m_others
->end();
585 #endif // wxUSE_TOOLTIPS