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/vector.h"
38 #include "wx/msw/private.h"
40 #ifndef TTTOOLINFO_V1_SIZE
41 #define TTTOOLINFO_V1_SIZE 0x28
44 #ifndef TTF_TRANSPARENT
45 #define TTF_TRANSPARENT 0x0100
48 // VZ: normally, the trick with subclassing the tooltip control and processing
49 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
50 // code here for now (but it's not compiled) in case we need it later.
52 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
53 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
54 // because it would then work for all controls, not only radioboxes but for
55 // now I don't understand what's wrong with it...
56 #define wxUSE_TTM_WINDOWFROMPOINT 0
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // the tooltip parent window
63 WXHWND
wxToolTip::ms_hwndTT
= (WXHWND
)NULL
;
65 // new tooltip maximum width, default value is set on first call to wxToolTip::Add()
66 int wxToolTip::ms_maxWidth
= 0;
68 #if wxUSE_TTM_WINDOWFROMPOINT
70 // the tooltip window proc
71 static WNDPROC gs_wndprocToolTip
= (WNDPROC
)NULL
;
73 #endif // wxUSE_TTM_WINDOWFROMPOINT
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // This is simply a wrapper for vector<HWND> but defined as a class to hide the
80 // details from the public header.
81 class wxToolTipOtherWindows
: public wxVector
<WXHWND
>
85 // a wrapper around TOOLINFO Win32 structure
87 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
90 class wxToolInfo
: public TOOLINFO
93 wxToolInfo(HWND hwndOwner
, unsigned int id
, const wxRect
& rc
)
95 // initialize all members
96 ::ZeroMemory(this, sizeof(TOOLINFO
));
98 // the structure TOOLINFO has been extended with a 4 byte field in
99 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
100 // use these extended fields so use the old struct size to ensure that
101 // the tooltips work on old (Windows 95) systems too
102 cbSize
= TTTOOLINFO_V1_SIZE
;
108 uFlags
= TTF_IDISHWND
;
109 uId
= (UINT_PTR
)hwndOwner
;
113 // this tooltip must be shown only if the mouse hovers a specific rect
114 // of the hwnd parameter!
115 rect
.left
= rc
.GetLeft();
116 rect
.right
= rc
.GetRight();
117 rect
.top
= rc
.GetTop();
118 rect
.bottom
= rc
.GetBottom();
120 // note that not setting TTF_IDISHWND from the uFlags member means that the
121 // ti.uId field should not contain the HWND but rather as MSDN says an
122 // "Application-defined identifier of the tool"; this is used internally by
123 // Windows to distinguish the different tooltips attached to the same window
127 // we use TTF_TRANSPARENT to fix a problem which arises at least with
128 // the text controls but may presumably happen with other controls
129 // which display the tooltip at mouse position: it can start flashing
130 // then as the control gets "focus lost" events and dismisses the
131 // tooltip which then reappears because mouse remains hovering over the
132 // control, see SF patch 1821229
133 if ( wxApp::GetComCtl32Version() >= 470 )
135 uFlags
|= TTF_TRANSPARENT
;
141 #pragma warning( default : 4097 )
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 // send a message to the tooltip control if it exists
150 // NB: wParam is always 0 for the TTM_XXX messages we use
151 static inline LRESULT
SendTooltipMessage(WXHWND hwnd
, UINT msg
, void *lParam
)
153 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, 0, (LPARAM
)lParam
) : 0;
156 // send a message to all existing tooltip controls
158 SendTooltipMessageToAll(WXHWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
161 ::SendMessage((HWND
)hwnd
, msg
, wParam
, lParam
);
164 // ============================================================================
166 // ============================================================================
168 #if wxUSE_TTM_WINDOWFROMPOINT
170 // ----------------------------------------------------------------------------
171 // window proc for our tooltip control
172 // ----------------------------------------------------------------------------
174 LRESULT APIENTRY
wxToolTipWndProc(HWND hwndTT
,
179 if ( msg
== TTM_WINDOWFROMPOINT
)
181 LPPOINT ppt
= (LPPOINT
)lParam
;
183 // the window on which event occurred
184 HWND hwnd
= ::WindowFromPoint(*ppt
);
186 OutputDebugString("TTM_WINDOWFROMPOINT: ");
187 OutputDebugString(wxString::Format("0x%08x => ", hwnd
));
189 // return a HWND corresponding to a wxWindow because only wxWidgets are
190 // associated with tooltips using TTM_ADDTOOL
191 wxWindow
*win
= wxGetWindowFromHWND((WXHWND
)hwnd
);
195 hwnd
= GetHwndOf(win
);
196 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd
));
199 // modify the point too!
201 GetWindowRect(hwnd
, &rect
);
203 ppt
->x
= (rect
.right
- rect
.left
) / 2;
204 ppt
->y
= (rect
.bottom
- rect
.top
) / 2;
206 return (LRESULT
)hwnd
;
210 OutputDebugString("no window\r\n");
214 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip
, hwndTT
, msg
, wParam
, lParam
);
217 #endif // wxUSE_TTM_WINDOWFROMPOINT
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 void wxToolTip::Enable(bool flag
)
225 // Make sure the tooltip has been created
226 (void) GetToolTipCtrl();
228 SendTooltipMessageToAll(ms_hwndTT
, TTM_ACTIVATE
, flag
, 0);
231 void wxToolTip::SetDelay(long milliseconds
)
233 // Make sure the tooltip has been created
234 (void) GetToolTipCtrl();
236 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
237 TTDT_INITIAL
, milliseconds
);
240 void wxToolTip::SetAutoPop(long milliseconds
)
242 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
243 TTDT_AUTOPOP
, milliseconds
);
246 void wxToolTip::SetReshow(long milliseconds
)
248 SendTooltipMessageToAll(ms_hwndTT
, TTM_SETDELAYTIME
,
249 TTDT_RESHOW
, milliseconds
);
252 void wxToolTip::SetMaxWidth(int width
)
254 wxASSERT_MSG( width
== -1 || width
>= 0, wxT("invalid width value") );
259 // ---------------------------------------------------------------------------
260 // implementation helpers
261 // ---------------------------------------------------------------------------
263 // create the tooltip ctrl for our parent frame if it doesn't exist yet
265 WXHWND
wxToolTip::GetToolTipCtrl()
270 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
272 exflags
|= WS_EX_LAYOUTRTL
;
275 // we want to show the tooltips always (even when the window is not
276 // active) and we don't want to strip "&"s from them
277 ms_hwndTT
= (WXHWND
)::CreateWindowEx(exflags
,
280 TTS_ALWAYSTIP
| TTS_NOPREFIX
,
281 CW_USEDEFAULT
, CW_USEDEFAULT
,
282 CW_USEDEFAULT
, CW_USEDEFAULT
,
288 HWND hwnd
= (HWND
)ms_hwndTT
;
289 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
290 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
292 #if wxUSE_TTM_WINDOWFROMPOINT
293 // subclass the newly created control
294 gs_wndprocToolTip
= wxSetWindowProc(hwnd
, wxToolTipWndProc
);
295 #endif // wxUSE_TTM_WINDOWFROMPOINT
303 void wxToolTip::RelayEvent(WXMSG
*msg
)
305 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, msg
);
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
314 wxToolTip::wxToolTip(const wxString
&tip
)
320 // make sure m_rect.IsEmpty() == true
324 // since m_rect is not valid, m_id is ignored by wxToolInfo ctor...
328 wxToolTip::wxToolTip(wxWindow
* win
, unsigned int id
, const wxString
&tip
, const wxRect
& rc
)
329 : m_text(tip
), m_rect(rc
), m_id(id
)
337 wxToolTip::~wxToolTip()
339 // the tooltip has to be removed before deleting. Otherwise, if it is visible
340 // while being deleted, there will be a delay before it goes away.
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
351 void wxToolTip::Remove(WXHWND hWnd
, unsigned int id
, const wxRect
& rc
)
353 wxToolInfo
ti((HWND
)hWnd
, id
, rc
);
355 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, &ti
);
358 void wxToolTip::DoRemove(WXHWND hWnd
)
360 if ( m_window
&& hWnd
== m_window
->GetHWND() )
362 // Remove the tooltip from the main window.
363 Remove(hWnd
, m_id
, m_rect
);
367 // Not really sure what to pass to remove in this case...
368 Remove(hWnd
, 0, wxRect());
372 void wxToolTip::Remove()
374 DoForAllWindows(&wxToolTip::DoRemove
);
377 void wxToolTip::AddOtherWindow(WXHWND hWnd
)
380 m_others
= new wxToolTipOtherWindows
;
382 m_others
->push_back(hWnd
);
387 void wxToolTip::DoAddHWND(WXHWND hWnd
)
389 HWND hwnd
= (HWND
)hWnd
;
391 wxToolInfo
ti(hwnd
, m_id
, m_rect
);
393 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
394 // store the tooltip text ourselves anyhow, and provide it in response to
395 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
396 // character tooltips as this is the size of the szText buffer in
397 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
400 ti
.lpszText
= const_cast<wxChar
*>(m_text
.wx_str());
402 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
404 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text
.c_str());
409 #ifdef TTM_SETMAXTIPWIDTH
410 if ( wxApp::GetComCtl32Version() >= 470 )
412 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
413 // extent of its first line as max value
414 HFONT hfont
= (HFONT
)
415 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT
, 0);
419 hfont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
422 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
429 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
432 if ( !SelectObject(hdc
, hfont
) )
434 wxLogLastError(wxT("SelectObject(hfont)"));
437 // find the width of the widest line
439 wxStringTokenizer
tokenizer(m_text
, wxT("\n"));
440 while ( tokenizer
.HasMoreTokens() )
442 const wxString token
= tokenizer
.GetNextToken();
445 if ( !::GetTextExtentPoint32(hdc
, token
.wx_str(),
446 token
.length(), &sz
) )
448 wxLogLastError(wxT("GetTextExtentPoint32"));
451 if ( sz
.cx
> maxWidth
)
455 // limit size to ms_maxWidth, if set
456 if ( ms_maxWidth
== 0 )
458 // this is more or less arbitrary but seems to work well
459 static const int DEFAULT_MAX_WIDTH
= 400;
461 ms_maxWidth
= wxGetClientDisplayRect().width
/ 2;
463 if ( ms_maxWidth
> DEFAULT_MAX_WIDTH
)
464 ms_maxWidth
= DEFAULT_MAX_WIDTH
;
467 if ( ms_maxWidth
!= -1 && maxWidth
> ms_maxWidth
)
468 maxWidth
= ms_maxWidth
;
470 // only set a new width if it is bigger than the current setting:
471 // otherwise adding a tooltip with shorter line(s) than a previous
472 // one would result in breaking the longer lines unnecessarily as
473 // all our tooltips share the same maximal width
474 if ( maxWidth
> SendTooltipMessage(GetToolTipCtrl(),
475 TTM_GETMAXTIPWIDTH
, 0) )
477 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH
,
478 wxUIntToPtr(maxWidth
));
482 #endif // TTM_SETMAXTIPWIDTH
484 // replace the '\n's with spaces because otherwise they appear as
485 // unprintable characters in the tooltip string
486 m_text
.Replace(wxT("\n"), wxT(" "));
487 ti
.lpszText
= const_cast<wxChar
*>(m_text
.wx_str());
489 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, &ti
) )
491 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text
.c_str());
496 void wxToolTip::SetWindow(wxWindow
*win
)
502 // add the window itself
505 AddOtherWindow(m_window
->GetHWND());
507 #if !defined(__WXUNIVERSAL__)
508 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
509 wxControl
*control
= wxDynamicCast(m_window
, wxControl
);
512 const wxArrayLong
& subcontrols
= control
->GetSubcontrols();
513 size_t count
= subcontrols
.GetCount();
514 for ( size_t n
= 0; n
< count
; n
++ )
516 int id
= subcontrols
[n
];
517 HWND hwnd
= GetDlgItem(GetHwndOf(m_window
), id
);
520 // maybe it's a child of parent of the control, in fact?
521 // (radiobuttons are subcontrols, i.e. children of the radiobox
522 // for wxWidgets but are its siblings at Windows level)
523 hwnd
= GetDlgItem(GetHwndOf(m_window
->GetParent()), id
);
526 // must have it by now!
527 wxASSERT_MSG( hwnd
, wxT("no hwnd for subcontrol?") );
529 AddOtherWindow((WXHWND
)hwnd
);
532 #endif // !defined(__WXUNIVERSAL__)
535 void wxToolTip::SetRect(const wxRect
& rc
)
541 wxToolInfo
ti(GetHwndOf(m_window
), m_id
, m_rect
);
542 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_NEWTOOLRECT
, &ti
);
546 void wxToolTip::SetTip(const wxString
& tip
)
550 DoForAllWindows(&wxToolTip::DoSetTip
);
553 void wxToolTip::DoSetTip(WXHWND hWnd
)
555 // update the tip text shown by the control
556 wxToolInfo
ti((HWND
)hWnd
, m_id
, m_rect
);
558 // for some reason, changing the tooltip text directly results in
559 // repaint of the controls under it, see #10520 -- but this doesn't
560 // happen if we reset it first
561 ti
.lpszText
= const_cast<wxChar
*>(wxT(""));
562 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
564 ti
.lpszText
= const_cast<wxChar
*>(m_text
.wx_str());
565 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, &ti
);
568 void wxToolTip::DoForAllWindows(void (wxToolTip::*func
)(WXHWND
))
572 (this->*func
)(m_window
->GetHWND());
577 for ( wxToolTipOtherWindows::const_iterator it
= m_others
->begin();
578 it
!= m_others
->end();
586 #endif // wxUSE_TOOLTIPS