use a global hwndTT
[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(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
36 #include <commctrl.h>
37 #endif
38
39 HWND wxToolTip::hwndTT = NULL;
40
41 // ----------------------------------------------------------------------------
42 // private classes
43 // ----------------------------------------------------------------------------
44
45
46 // a simple wrapper around TOOLINFO Win32 structure
47 #pragma warning( disable : 4097 )
48 class wxToolInfo : public TOOLINFO
49 {
50 public:
51 wxToolInfo(wxWindow *win)
52 {
53 // initialize all members
54 #ifdef __GNUWIN32__ && !defined(wxUSE_NORLANDER_HEADERS)
55 memset(this, 0, sizeof(TOOLINFO));
56 #else
57 ::ZeroMemory(this, sizeof(TOOLINFO));
58 #endif
59
60 cbSize = sizeof(TOOLINFO);
61 uFlags = TTF_IDISHWND;
62 uId = (UINT)win->GetHWND();
63 }
64 };
65 #pragma warning( default : 4097 )
66
67 // ----------------------------------------------------------------------------
68 // private functions
69 // ----------------------------------------------------------------------------
70
71 // send a message to the tooltip control
72 inline LRESULT SendTooltipMessage(WXHWND hwnd,
73 UINT msg,
74 WPARAM wParam,
75 void *lParam)
76 {
77 return hwnd ? ::SendMessage((HWND)hwnd, msg, wParam, (LPARAM)lParam)
78 : 0;
79 }
80
81 // send a message to all existing tooltip controls
82 static void SendTooltipMessageToAll(WXHWND hwnd,
83 UINT msg,
84 WPARAM wParam,
85 LPARAM lParam)
86 {
87 if ( hwnd )
88 (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam);
89 }
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // static functions
97 // ----------------------------------------------------------------------------
98
99
100
101 void wxToolTip::Enable(bool flag)
102 {
103 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_ACTIVATE, flag, 0);
104 }
105
106 void wxToolTip::SetDelay(long milliseconds)
107 {
108 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_SETDELAYTIME, TTDT_INITIAL, milliseconds);
109 }
110
111 // ---------------------------------------------------------------------------
112 // implementation helpers
113 // ---------------------------------------------------------------------------
114
115 // create the tooltip ctrl for our parent frame if it doesn't exist yet
116 WXHWND wxToolTip::GetToolTipCtrl()
117 {
118 if ( !hwndTT )
119 {
120 hwndTT = ::CreateWindow(TOOLTIPS_CLASS,
121 (LPSTR)NULL,
122 TTS_ALWAYSTIP,
123 CW_USEDEFAULT, CW_USEDEFAULT,
124 CW_USEDEFAULT, CW_USEDEFAULT,
125 NULL, (HMENU)NULL,
126 wxGetInstance(), NULL);
127 }
128
129 return (WXHWND)hwndTT;
130 }
131
132 void wxToolTip::RelayEvent(WXMSG *msg)
133 {
134 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
135 }
136
137 // ----------------------------------------------------------------------------
138 // ctor & dtor
139 // ----------------------------------------------------------------------------
140
141 wxToolTip::wxToolTip(const wxString &tip)
142 : m_text(tip)
143 {
144 m_window = NULL;
145 }
146
147 wxToolTip::~wxToolTip()
148 {
149 // there is no need to Remove() this tool - it will be done automatically
150 // anyhow
151 }
152
153 // ----------------------------------------------------------------------------
154 // others
155 // ----------------------------------------------------------------------------
156
157 void wxToolTip::Remove()
158 {
159 // remove this tool from the tooltip control
160 if ( m_window )
161 {
162 wxToolInfo ti(m_window);
163 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, 0, &ti);
164 }
165 }
166
167 void wxToolTip::SetWindow(wxWindow *win)
168 {
169 Remove();
170
171 m_window = win;
172
173 if ( m_window )
174 {
175 wxToolInfo ti(m_window);
176
177 // as we store our text anyhow, it seems useless to waste system memory
178 // by asking the tooltip ctrl to remember it too - instead it will send
179 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
180 ti.hwnd = (HWND)m_window->GetHWND();
181 ti.lpszText = LPSTR_TEXTCALLBACK;
182 // instead of: ti.lpszText = (char *)m_text.c_str();
183
184 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, 0, &ti) )
185 {
186 wxLogSysError(_("Failed to create the tooltip '%s'"),
187 m_text.c_str());
188 }
189 }
190 }
191
192 void wxToolTip::SetTip(const wxString& tip)
193 {
194 m_text = tip;
195
196 if ( m_window )
197 {
198 // update it immediately
199 wxToolInfo ti(m_window);
200 ti.lpszText = (wxChar *)m_text.c_str();
201
202 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
203 }
204 }
205
206 #endif // wxUSE_TOOLTIPS