1. added wxEnhMetaFileXXX classes
[wxWidgets.git] / src / msw / tooltip.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tooltip.cpp
3 // Purpose: wxToolTip class implementation for MSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.01.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #if wxUSE_TOOLTIPS
31
32 #include "wx/tooltip.h"
33 #include "wx/msw/private.h"
34
35 #if defined(__WIN95__) && (!defined(__GNUWIN32__) || defined(__MINGW32__))
36 #include <commctrl.h>
37 #endif
38
39 // VZ: normally, the trick with subclassing the tooltip control and processing
40 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
41 // code here for now (but it's not compiled) in case we need it later.
42 //
43 // For now, instead of this, we just add all radiobox buttons to the
44 // tooltip control as well (see SetWindow) - this is probably less
45 // efficient, but it works.
46 #define wxUSE_TTM_WINDOWFROMPOINT 1
47
48 // ----------------------------------------------------------------------------
49 // global variables
50 // ----------------------------------------------------------------------------
51
52 // the tooltip parent window
53 WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
54
55 #if wxUSE_TTM_WINDOWFROMPOINT
56
57 // the tooltip window proc
58 static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
59
60 #endif // wxUSE_TTM_WINDOWFROMPOINT
61
62 // ----------------------------------------------------------------------------
63 // private classes
64 // ----------------------------------------------------------------------------
65
66 // a wrapper around TOOLINFO Win32 structure
67 #ifdef __VISUALC__
68 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
69 #endif
70
71 class wxToolInfo : public TOOLINFO
72 {
73 public:
74 wxToolInfo(HWND hwnd)
75 {
76 // initialize all members
77 ::ZeroMemory(this, sizeof(TOOLINFO));
78
79 // the structure TOOLINFO has been extended with a 4 byte field in
80 // version 4.70 of comctl32.dll and if we compile on a newer machine
81 // but run on one with the old version of comctl32, nothing will work
82 // because the library will detect that we rely on a more recent
83 // version of it. So we always use the old size - if we ever start
84 // using our lParam member, we'd have to check for comctl32 version
85 // during run-time
86 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x0300)
87 cbSize = sizeof(TOOLINFO) - sizeof(LPARAM);
88 #else // old headers
89 cbSize = sizeof(TOOLINFO);
90 #endif // compile-time comctl32.dll version
91
92 uFlags = TTF_IDISHWND;
93 uId = (UINT)hwnd;
94 }
95 };
96
97 #ifdef __VISUALC__
98 #pragma warning( default : 4097 )
99 #endif
100
101 // ----------------------------------------------------------------------------
102 // private functions
103 // ----------------------------------------------------------------------------
104
105 // send a message to the tooltip control
106 inline LRESULT SendTooltipMessage(WXHWND hwnd,
107 UINT msg,
108 WPARAM wParam,
109 void *lParam)
110 {
111 return hwnd ? ::SendMessage((HWND)hwnd, msg, wParam, (LPARAM)lParam)
112 : 0;
113 }
114
115 // send a message to all existing tooltip controls
116 static void SendTooltipMessageToAll(WXHWND hwnd,
117 UINT msg,
118 WPARAM wParam,
119 LPARAM lParam)
120 {
121 (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam);
122 }
123
124 // ============================================================================
125 // implementation
126 // ============================================================================
127
128 #if wxUSE_TTM_WINDOWFROMPOINT
129
130 // ----------------------------------------------------------------------------
131 // window proc for our tooltip control
132 // ----------------------------------------------------------------------------
133
134 LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
135 UINT msg,
136 WPARAM wParam,
137 LPARAM lParam)
138 {
139 if ( msg == TTM_WINDOWFROMPOINT )
140 {
141 LPPOINT ppt = (LPPOINT)lParam;
142 // is the window under control a wxWindow?
143 HWND hwnd = ::WindowFromPoint(*ppt);
144
145 // return a HWND correspondign to wxWindow because only wxWindows are
146 // associated with tooltips using TTM_ADDTOOL
147 while ( hwnd && !wxFindWinFromHandle((WXHWND)hwnd) )
148 {
149 hwnd = ::GetParent(hwnd);
150 }
151
152 if ( hwnd )
153 {
154 // modify the point too!
155 RECT rect;
156 GetWindowRect(hwnd, &rect);
157
158 ppt->x = rect.left;
159 ppt->y = rect.top;
160
161 return (LRESULT)hwnd;
162 }
163 }
164
165 return ::CallWindowProc(gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
166 }
167
168 #endif // wxUSE_TTM_WINDOWFROMPOINT
169
170 // ----------------------------------------------------------------------------
171 // static functions
172 // ----------------------------------------------------------------------------
173
174 void wxToolTip::Enable(bool flag)
175 {
176 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
177 }
178
179 void wxToolTip::SetDelay(long milliseconds)
180 {
181 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
182 TTDT_INITIAL, milliseconds);
183 }
184
185 // ---------------------------------------------------------------------------
186 // implementation helpers
187 // ---------------------------------------------------------------------------
188
189 // create the tooltip ctrl for our parent frame if it doesn't exist yet
190 WXHWND wxToolTip::GetToolTipCtrl()
191 {
192 if ( !ms_hwndTT )
193 {
194 ms_hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS,
195 (LPSTR)NULL,
196 TTS_ALWAYSTIP,
197 CW_USEDEFAULT, CW_USEDEFAULT,
198 CW_USEDEFAULT, CW_USEDEFAULT,
199 NULL, (HMENU)NULL,
200 wxGetInstance(),
201 NULL);
202 if ( ms_hwndTT )
203 {
204 HWND hwnd = (HWND)ms_hwndTT;
205 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
206 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
207
208 #if wxUSE_TTM_WINDOWFROMPOINT
209 // subclass the newly created control
210 gs_wndprocToolTip = (WNDPROC)::GetWindowLong(hwnd, GWL_WNDPROC);
211 ::SetWindowLong(hwnd, GWL_WNDPROC, (long)wxToolTipWndProc);
212 #endif // wxUSE_TTM_WINDOWFROMPOINT
213 }
214 }
215
216 return ms_hwndTT;
217 }
218
219 void wxToolTip::RelayEvent(WXMSG *msg)
220 {
221 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
222 }
223
224 // ----------------------------------------------------------------------------
225 // ctor & dtor
226 // ----------------------------------------------------------------------------
227
228 wxToolTip::wxToolTip(const wxString &tip)
229 : m_text(tip)
230 {
231 m_window = NULL;
232 }
233
234 wxToolTip::~wxToolTip()
235 {
236 // there is no need to Remove() this tool - it will be done automatically
237 // anyhow
238 }
239
240 // ----------------------------------------------------------------------------
241 // others
242 // ----------------------------------------------------------------------------
243
244 void wxToolTip::Remove()
245 {
246 // remove this tool from the tooltip control
247 if ( m_window )
248 {
249 wxToolInfo ti(GetHwndOf(m_window));
250 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, 0, &ti);
251 }
252 }
253
254 void wxToolTip::Add(WXHWND hWnd)
255 {
256 HWND hwnd = (HWND)hWnd;
257
258 wxToolInfo ti(hwnd);
259
260 // as we store our text anyhow, it seems useless to waste system memory
261 // by asking the tooltip ctrl to remember it too - instead it will send
262 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
263 ti.hwnd = hwnd;
264 ti.lpszText = LPSTR_TEXTCALLBACK;
265 // instead of: ti.lpszText = (char *)m_text.c_str();
266
267 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, 0, &ti) )
268 {
269 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
270 }
271 }
272
273 void wxToolTip::SetWindow(wxWindow *win)
274 {
275 Remove();
276
277 m_window = win;
278
279 // add the window itself
280 if ( m_window )
281 {
282 Add(m_window->GetHWND());
283 }
284
285 #if 1 //!wxUSE_TTM_WINDOWFROMPOINT
286 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
287 wxControl *control = wxDynamicCast(m_window, wxControl);
288 if ( control )
289 {
290 size_t count = control->GetSubcontrols().GetCount();
291 for ( size_t n = 0; n < count; n++ )
292 {
293 wxWindowID id = control->GetSubcontrols()[n];
294 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
295
296 if ( hwnd )
297 {
298 Add((WXHWND)hwnd);
299 }
300 }
301 }
302 #endif // !wxUSE_TTM_WINDOWFROMPOINT
303 }
304
305 void wxToolTip::SetTip(const wxString& tip)
306 {
307 m_text = tip;
308
309 if ( m_window )
310 {
311 // update it immediately
312 wxToolInfo ti(GetHwndOf(m_window));
313 ti.lpszText = (wxChar *)m_text.c_str();
314
315 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
316 }
317 }
318
319 #endif // wxUSE_TOOLTIPS