]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tooltip.cpp
moved bookmarks to Contents panel
[wxWidgets.git] / src / msw / tooltip.cpp
CommitLineData
3a19e16d
VZ
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
cb1a1dc9
VZ
30#if wxUSE_TOOLTIPS
31
3a19e16d
VZ
32#include "wx/tooltip.h"
33#include "wx/msw/private.h"
34
c25a510b 35#if defined(__WIN95__) && (!defined(__GNUWIN32__) || defined(__MINGW32__))
f048e32f 36 #include <commctrl.h>
acbd13a3 37#endif
3a19e16d 38
f048e32f
VZ
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.
d9317fd4 46#define wxUSE_TTM_WINDOWFROMPOINT 1
f048e32f 47
086c94af
VZ
48// ----------------------------------------------------------------------------
49// global variables
50// ----------------------------------------------------------------------------
51
52// the tooltip parent window
f048e32f
VZ
53WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
54
55#if wxUSE_TTM_WINDOWFROMPOINT
56
57// the tooltip window proc
58static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
59
60#endif // wxUSE_TTM_WINDOWFROMPOINT
066f302c 61
3a19e16d
VZ
62// ----------------------------------------------------------------------------
63// private classes
64// ----------------------------------------------------------------------------
65
bb448552 66// a wrapper around TOOLINFO Win32 structure
26b83329 67#ifdef __VISUALC__
11c7d5b6 68 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
26b83329 69#endif
bb448552 70
3a19e16d
VZ
71class wxToolInfo : public TOOLINFO
72{
73public:
f048e32f 74 wxToolInfo(HWND hwnd)
3a19e16d
VZ
75 {
76 // initialize all members
77 ::ZeroMemory(this, sizeof(TOOLINFO));
78
bb448552
VZ
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
3a19e16d 89 cbSize = sizeof(TOOLINFO);
bb448552
VZ
90#endif // compile-time comctl32.dll version
91
3a19e16d 92 uFlags = TTF_IDISHWND;
f048e32f 93 uId = (UINT)hwnd;
3a19e16d
VZ
94 }
95};
bb448552 96
26b83329
VZ
97#ifdef __VISUALC__
98 #pragma warning( default : 4097 )
99#endif
3a19e16d
VZ
100
101// ----------------------------------------------------------------------------
102// private functions
103// ----------------------------------------------------------------------------
104
105// send a message to the tooltip control
106inline 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
16f6dfd8 115// send a message to all existing tooltip controls
066f302c 116static void SendTooltipMessageToAll(WXHWND hwnd,
18ba9da6
RD
117 UINT msg,
118 WPARAM wParam,
066f302c 119 LPARAM lParam)
16f6dfd8 120{
f048e32f 121 (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam);
16f6dfd8
VZ
122}
123
3a19e16d
VZ
124// ============================================================================
125// implementation
126// ============================================================================
127
f048e32f
VZ
128#if wxUSE_TTM_WINDOWFROMPOINT
129
3a19e16d 130// ----------------------------------------------------------------------------
f048e32f 131// window proc for our tooltip control
3a19e16d
VZ
132// ----------------------------------------------------------------------------
133
f048e32f
VZ
134LRESULT 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;
066f302c 160
f048e32f
VZ
161 return (LRESULT)hwnd;
162 }
163 }
164
8caa4ed1 165 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
f048e32f
VZ
166}
167
168#endif // wxUSE_TTM_WINDOWFROMPOINT
169
170// ----------------------------------------------------------------------------
171// static functions
172// ----------------------------------------------------------------------------
066f302c 173
16f6dfd8
VZ
174void wxToolTip::Enable(bool flag)
175{
f048e32f 176 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
16f6dfd8
VZ
177}
178
179void wxToolTip::SetDelay(long milliseconds)
180{
f048e32f
VZ
181 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
182 TTDT_INITIAL, milliseconds);
16f6dfd8
VZ
183}
184
185// ---------------------------------------------------------------------------
186// implementation helpers
187// ---------------------------------------------------------------------------
188
3a19e16d
VZ
189// create the tooltip ctrl for our parent frame if it doesn't exist yet
190WXHWND wxToolTip::GetToolTipCtrl()
191{
f048e32f 192 if ( !ms_hwndTT )
3a19e16d 193 {
f048e32f
VZ
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 )
086c94af 203 {
f048e32f
VZ
204 HWND hwnd = (HWND)ms_hwndTT;
205 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
086c94af 206 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
28195455 207
f048e32f
VZ
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 }
3a19e16d
VZ
214 }
215
f048e32f 216 return ms_hwndTT;
3a19e16d
VZ
217}
218
3a19e16d
VZ
219void wxToolTip::RelayEvent(WXMSG *msg)
220{
221 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
222}
223
3a19e16d
VZ
224// ----------------------------------------------------------------------------
225// ctor & dtor
226// ----------------------------------------------------------------------------
227
228wxToolTip::wxToolTip(const wxString &tip)
229 : m_text(tip)
230{
231 m_window = NULL;
232}
233
234wxToolTip::~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
244void wxToolTip::Remove()
245{
246 // remove this tool from the tooltip control
247 if ( m_window )
248 {
f048e32f 249 wxToolInfo ti(GetHwndOf(m_window));
3a19e16d
VZ
250 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, 0, &ti);
251 }
252}
253
f048e32f
VZ
254void 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 {
bb448552 269 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
f048e32f
VZ
270 }
271}
272
3a19e16d
VZ
273void wxToolTip::SetWindow(wxWindow *win)
274{
275 Remove();
276
277 m_window = win;
278
f048e32f 279 // add the window itself
3a19e16d
VZ
280 if ( m_window )
281 {
f048e32f
VZ
282 Add(m_window->GetHWND());
283 }
3a19e16d 284
d9317fd4 285#if 1 //!wxUSE_TTM_WINDOWFROMPOINT
f048e32f
VZ
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++ )
3a19e16d 292 {
f048e32f
VZ
293 wxWindowID id = control->GetSubcontrols()[n];
294 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
295
296 if ( hwnd )
297 {
298 Add((WXHWND)hwnd);
299 }
3a19e16d
VZ
300 }
301 }
d9317fd4 302#endif // !wxUSE_TTM_WINDOWFROMPOINT
3a19e16d
VZ
303}
304
305void wxToolTip::SetTip(const wxString& tip)
306{
307 m_text = tip;
308
309 if ( m_window )
310 {
311 // update it immediately
f048e32f 312 wxToolInfo ti(GetHwndOf(m_window));
837e5743 313 ti.lpszText = (wxChar *)m_text.c_str();
3a19e16d
VZ
314
315 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
316 }
317}
cb1a1dc9
VZ
318
319#endif // wxUSE_TOOLTIPS