]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/tooltip.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tooltip.cpp
3 // Purpose: wxToolTip class implementation for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
32 #include "wx/tooltip.h"
33 #include "wx/msw/private.h"
35 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // a simple wrapper around TOOLINFO Win32 structure
44 #pragma warning( disable : 4097 )
45 class wxToolInfo
: public TOOLINFO
48 wxToolInfo(wxWindow
*win
)
50 // initialize all members
51 #if defined( __GNUWIN32__ ) && !defined(wxUSE_NORLANDER_HEADERS)
52 memset(this, 0, sizeof(TOOLINFO
));
54 ::ZeroMemory(this, sizeof(TOOLINFO
));
57 cbSize
= sizeof(TOOLINFO
);
58 uFlags
= TTF_IDISHWND
;
59 uId
= (UINT
)win
->GetHWND();
62 #pragma warning( default : 4097 )
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // send a message to the tooltip control
69 inline LRESULT
SendTooltipMessage(WXHWND hwnd
,
74 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, wParam
, (LPARAM
)lParam
)
78 // send a message to all existing tooltip controls
79 static void SendTooltipMessageToAll(UINT msg
, WPARAM wParam
, LPARAM lParam
)
81 // NB: it might be somewhat easier to maintain a list of all existing
82 // wxToolTip controls (put them there in ctor, delete from the list
83 // in dtor) - may be it's worth doing it this way? OTOH, typical
84 // application won't have many top level windows, so iterating over all
85 // of them shouldnt' take much time neither...
87 // iterate over all top level windows and send message to the tooltip
88 // control of each and every of them (or more precisely to all dialogs and
90 wxDialog
*dialog
= NULL
;
91 wxFrame
*frame
= NULL
;
93 wxNode
*node
= wxTopLevelWindows
.First();
96 wxWindow
*win
= (wxWindow
*)node
->Data();
100 if ( win
->IsKindOf(CLASSINFO(wxFrame
)) )
102 frame
= (wxFrame
*)win
;
105 else if ( win
->IsKindOf(CLASSINFO(wxDialog
)) )
107 dialog
= (wxDialog
*)win
;
112 // skip this strange top level window
116 wxASSERT_MSG( dialog
|| frame
, _T("logic error") );
118 WXHWND hwndTT
= frame
? frame
->GetToolTipCtrl()
119 : dialog
->GetToolTipCtrl();
122 (void)SendTooltipMessage(hwndTT
, msg
, wParam
, (void *)lParam
);
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 void wxToolTip::Enable(bool flag
)
137 SendTooltipMessageToAll(TTM_ACTIVATE
, flag
, 0);
140 void wxToolTip::SetDelay(long milliseconds
)
142 SendTooltipMessageToAll(TTM_SETDELAYTIME
, TTDT_INITIAL
, milliseconds
);
145 // ---------------------------------------------------------------------------
146 // implementation helpers
147 // ---------------------------------------------------------------------------
149 // create the tooltip ctrl for our parent frame if it doesn't exist yet
150 WXHWND
wxToolTip::GetToolTipCtrl()
152 // find either parent dialog or parent frame - tooltip controls are managed
153 // by these 2 classes only (it doesn't make sense to create one tooltip per
154 // each and every wxWindow)
155 wxFrame
*frame
= NULL
;
156 wxDialog
*dialog
= NULL
;
158 wxWindow
*parent
= m_window
;
161 if ( parent
->IsKindOf(CLASSINFO(wxFrame
)) )
163 frame
= (wxFrame
*)parent
;
167 else if ( parent
->IsKindOf(CLASSINFO(wxDialog
)) )
169 dialog
= (wxDialog
*)parent
;
174 parent
= parent
->GetParent();
177 wxCHECK_MSG( frame
|| dialog
, 0,
178 _T("can't create tooltip control outside a frame or a dialog") );
180 HWND hwndTT
= (HWND
)(frame
? frame
->GetToolTipCtrl()
181 : dialog
->GetToolTipCtrl());
184 hwndTT
= ::CreateWindow(TOOLTIPS_CLASS
,
187 CW_USEDEFAULT
, CW_USEDEFAULT
,
188 CW_USEDEFAULT
, CW_USEDEFAULT
,
189 (HWND
)parent
->GetHWND(), (HMENU
)NULL
,
190 wxGetInstance(), NULL
);
195 frame
->SetToolTipCtrl((WXHWND
)hwndTT
);
197 dialog
->SetToolTipCtrl((WXHWND
)hwndTT
);
201 wxLogSysError(_("Can not create tooltip control"));
205 return (WXHWND
)hwndTT
;
208 void wxToolTip::RelayEvent(WXMSG
*msg
)
210 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, 0, msg
);
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 wxToolTip::wxToolTip(const wxString
&tip
)
223 wxToolTip::~wxToolTip()
225 // there is no need to Remove() this tool - it will be done automatically
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 void wxToolTip::Remove()
235 // remove this tool from the tooltip control
238 wxToolInfo
ti(m_window
);
239 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, 0, &ti
);
243 void wxToolTip::SetWindow(wxWindow
*win
)
251 wxToolInfo
ti(m_window
);
253 // as we store our text anyhow, it seems useless to waste system memory
254 // by asking the tooltip ctrl to remember it too - instead it will send
255 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
256 ti
.hwnd
= (HWND
)m_window
->GetHWND();
257 ti
.lpszText
= LPSTR_TEXTCALLBACK
;
258 // instead of: ti.lpszText = (char *)m_text.c_str();
260 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, 0, &ti
) )
262 wxLogSysError(_("Failed to create the tooltip '%s'"),
268 void wxToolTip::SetTip(const wxString
& tip
)
274 // update it immediately
275 wxToolInfo
ti(m_window
);
276 ti
.lpszText
= (wxChar
*)m_text
.c_str();
278 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, 0, &ti
);
282 #endif // wxUSE_TOOLTIPS