]>
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__)
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // a simple wrapper around TOOLINFO Win32 structure
44 class wxToolInfo
: public TOOLINFO
47 wxToolInfo(wxWindow
*win
)
49 // initialize all members
51 memset(this, 0, sizeof(TOOLINFO
));
53 ::ZeroMemory(this, sizeof(TOOLINFO
));
56 cbSize
= sizeof(TOOLINFO
);
57 uFlags
= TTF_IDISHWND
;
58 uId
= (UINT
)win
->GetHWND();
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // send a message to the tooltip control
67 inline LRESULT
SendTooltipMessage(WXHWND hwnd
,
72 return hwnd
? ::SendMessage((HWND
)hwnd
, msg
, wParam
, (LPARAM
)lParam
)
76 // send a message to all existing tooltip controls
77 static void SendTooltipMessageToAll(UINT msg
, WPARAM wParam
, LPARAM lParam
)
79 // NB: it might be somewhat easier to maintain a list of all existing
80 // wxToolTip controls (put them there in ctor, delete from the list
81 // in dtor) - may be it's worth doing it this way? OTOH, typical
82 // application won't have many top level windows, so iterating over all
83 // of them shouldnt' take much time neither...
85 // iterate over all top level windows and send message to the tooltip
86 // control of each and every of them (or more precisely to all dialogs and
88 wxDialog
*dialog
= NULL
;
89 wxFrame
*frame
= NULL
;
91 wxNode
*node
= wxTopLevelWindows
.First();
94 wxWindow
*win
= (wxWindow
*)node
->Data();
98 if ( win
->IsKindOf(CLASSINFO(wxFrame
)) )
100 frame
= (wxFrame
*)win
;
103 else if ( win
->IsKindOf(CLASSINFO(wxDialog
)) )
105 dialog
= (wxDialog
*)win
;
110 // skip this strange top level window
114 wxASSERT_MSG( dialog
|| frame
, "logic error" );
116 WXHWND hwndTT
= frame
? frame
->GetToolTipCtrl()
117 : dialog
->GetToolTipCtrl();
120 (void)SendTooltipMessage(hwndTT
, msg
, wParam
, (void *)lParam
);
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 void wxToolTip::Enable(bool flag
)
135 SendTooltipMessageToAll(TTM_ACTIVATE
, flag
, 0);
138 void wxToolTip::SetDelay(long milliseconds
)
140 SendTooltipMessageToAll(TTM_SETDELAYTIME
, TTDT_INITIAL
, milliseconds
);
143 // ---------------------------------------------------------------------------
144 // implementation helpers
145 // ---------------------------------------------------------------------------
147 // create the tooltip ctrl for our parent frame if it doesn't exist yet
148 WXHWND
wxToolTip::GetToolTipCtrl()
150 // find either parent dialog or parent frame - tooltip controls are managed
151 // by these 2 classes only (it doesn't make sense to create one tooltip per
152 // each and every wxWindow)
153 wxFrame
*frame
= NULL
;
154 wxDialog
*dialog
= NULL
;
156 wxWindow
*parent
= m_window
;
159 if ( parent
->IsKindOf(CLASSINFO(wxFrame
)) )
161 frame
= (wxFrame
*)parent
;
165 else if ( parent
->IsKindOf(CLASSINFO(wxDialog
)) )
167 dialog
= (wxDialog
*)parent
;
172 parent
= parent
->GetParent();
175 wxCHECK_MSG( frame
|| dialog
, 0,
176 "can't create tooltip control outside a frame or a dialog" );
178 HWND hwndTT
= (HWND
)(frame
? frame
->GetToolTipCtrl()
179 : dialog
->GetToolTipCtrl());
182 hwndTT
= ::CreateWindow(TOOLTIPS_CLASS
,
185 CW_USEDEFAULT
, CW_USEDEFAULT
,
186 CW_USEDEFAULT
, CW_USEDEFAULT
,
187 (HWND
)parent
->GetHWND(), (HMENU
)NULL
,
188 wxGetInstance(), NULL
);
193 frame
->SetToolTipCtrl((WXHWND
)hwndTT
);
195 dialog
->SetToolTipCtrl((WXHWND
)hwndTT
);
199 wxLogSysError(_("Can not create tooltip control"));
203 return (WXHWND
)hwndTT
;
206 void wxToolTip::RelayEvent(WXMSG
*msg
)
208 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT
, 0, msg
);
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
215 wxToolTip::wxToolTip(const wxString
&tip
)
221 wxToolTip::~wxToolTip()
223 // there is no need to Remove() this tool - it will be done automatically
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 void wxToolTip::Remove()
233 // remove this tool from the tooltip control
236 wxToolInfo
ti(m_window
);
237 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL
, 0, &ti
);
241 void wxToolTip::SetWindow(wxWindow
*win
)
249 wxToolInfo
ti(m_window
);
251 // as we store our text anyhow, it seems useless to waste system memory
252 // by asking the tooltip ctrl to remember it too - instead it will send
253 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
254 ti
.hwnd
= (HWND
)m_window
->GetHWND();
255 ti
.lpszText
= LPSTR_TEXTCALLBACK
;
256 // instead of: ti.lpszText = (char *)m_text.c_str();
258 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL
, 0, &ti
) )
260 wxLogSysError(_("Failed to create the tooltip '%s'"),
266 void wxToolTip::SetTip(const wxString
& tip
)
272 // update it immediately
273 wxToolInfo
ti(m_window
);
274 ti
.lpszText
= (char *)m_text
.c_str();
276 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, 0, &ti
);
280 #endif // wxUSE_TOOLTIPS