added Remove(HWND); removed unused wParam parameter from SendTooltipMessage
[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 licence
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 // include <commctrl.h> "properly"
36 #include "wx/msw/wrapcctl.h"
37
38 // VZ: normally, the trick with subclassing the tooltip control and processing
39 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
40 // code here for now (but it's not compiled) in case we need it later.
41 //
42 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
43 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
44 // because it would then work for all controls, not only radioboxes but for
45 // now I don't understand what's wrong with it...
46 #define wxUSE_TTM_WINDOWFROMPOINT 0
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 hwndOwner)
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 _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 hwnd = hwndOwner;
93 uFlags = TTF_IDISHWND;
94 uId = (UINT)hwndOwner;
95 }
96 };
97
98 #ifdef __VISUALC__
99 #pragma warning( default : 4097 )
100 #endif
101
102 // ----------------------------------------------------------------------------
103 // private functions
104 // ----------------------------------------------------------------------------
105
106 // send a message to the tooltip control if it exists
107 //
108 // NB: wParam is always 0 for the TTM_XXX messages we use
109 static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam)
110 {
111 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
112 }
113
114 // send a message to all existing tooltip controls
115 static inline void
116 SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
117 {
118 if ( hwnd )
119 ::SendMessage((HWND)hwnd, msg, wParam, lParam);
120 }
121
122 // ============================================================================
123 // implementation
124 // ============================================================================
125
126 #if wxUSE_TTM_WINDOWFROMPOINT
127
128 // ----------------------------------------------------------------------------
129 // window proc for our tooltip control
130 // ----------------------------------------------------------------------------
131
132 LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
133 UINT msg,
134 WPARAM wParam,
135 LPARAM lParam)
136 {
137 if ( msg == TTM_WINDOWFROMPOINT )
138 {
139 LPPOINT ppt = (LPPOINT)lParam;
140
141 // the window on which event occurred
142 HWND hwnd = ::WindowFromPoint(*ppt);
143
144 OutputDebugString("TTM_WINDOWFROMPOINT: ");
145 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
146
147 // return a HWND corresponding to a wxWindow because only wxWidgets are
148 // associated with tooltips using TTM_ADDTOOL
149 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
150
151 if ( win )
152 {
153 hwnd = GetHwndOf(win);
154 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
155
156 #if 0
157 // modify the point too!
158 RECT rect;
159 GetWindowRect(hwnd, &rect);
160
161 ppt->x = (rect.right - rect.left) / 2;
162 ppt->y = (rect.bottom - rect.top) / 2;
163 #endif // 0
164 return (LRESULT)hwnd;
165 }
166 else
167 {
168 OutputDebugString("no window\r\n");
169 }
170 }
171
172 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
173 }
174
175 #endif // wxUSE_TTM_WINDOWFROMPOINT
176
177 // ----------------------------------------------------------------------------
178 // static functions
179 // ----------------------------------------------------------------------------
180
181 void wxToolTip::Enable(bool flag)
182 {
183 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
184 }
185
186 void wxToolTip::SetDelay(long milliseconds)
187 {
188 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
189 TTDT_INITIAL, milliseconds);
190 }
191
192 // ---------------------------------------------------------------------------
193 // implementation helpers
194 // ---------------------------------------------------------------------------
195
196 // create the tooltip ctrl for our parent frame if it doesn't exist yet
197 WXHWND wxToolTip::GetToolTipCtrl()
198 {
199 if ( !ms_hwndTT )
200 {
201 ms_hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS,
202 (LPCTSTR)NULL,
203 TTS_ALWAYSTIP,
204 CW_USEDEFAULT, CW_USEDEFAULT,
205 CW_USEDEFAULT, CW_USEDEFAULT,
206 NULL, (HMENU)NULL,
207 wxGetInstance(),
208 NULL);
209 if ( ms_hwndTT )
210 {
211 HWND hwnd = (HWND)ms_hwndTT;
212 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
213 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
214
215 #if wxUSE_TTM_WINDOWFROMPOINT
216 // subclass the newly created control
217 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
218 #endif // wxUSE_TTM_WINDOWFROMPOINT
219 }
220 }
221
222 return ms_hwndTT;
223 }
224
225 void wxToolTip::RelayEvent(WXMSG *msg)
226 {
227 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
228 }
229
230 // ----------------------------------------------------------------------------
231 // ctor & dtor
232 // ----------------------------------------------------------------------------
233
234 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
235
236 wxToolTip::wxToolTip(const wxString &tip)
237 : m_text(tip)
238 {
239 m_window = NULL;
240 }
241
242 wxToolTip::~wxToolTip()
243 {
244 // the tooltip has to be removed before deleting. Otherwise, if it is visible
245 // while being deleted, there will be a delay before it goes away.
246 Remove();
247 }
248
249 // ----------------------------------------------------------------------------
250 // others
251 // ----------------------------------------------------------------------------
252
253 void wxToolTip::Remove(WXHWND hWnd)
254 {
255 wxToolInfo ti((HWND)hWnd);
256 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
257 }
258
259 void wxToolTip::Remove()
260 {
261 // remove this tool from the tooltip control
262 if ( m_window )
263 {
264 Remove(m_window->GetHWND());
265 }
266 }
267
268 void wxToolTip::Add(WXHWND hWnd)
269 {
270 HWND hwnd = (HWND)hWnd;
271
272 wxToolInfo ti(hwnd);
273
274 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
275 // store the tooltip text ourselves anyhow, and provide it in response to
276 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
277 // character tooltips as this is the size of the szText buffer in
278 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
279 // of any length
280 ti.hwnd = hwnd;
281 ti.lpszText = (wxChar *)m_text.c_str(); // const_cast
282
283 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
284 {
285 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
286 }
287 else
288 {
289 // check for multiline toopltip
290 int index = m_text.Find(_T('\n'));
291
292 if ( index != wxNOT_FOUND )
293 {
294 #ifdef TTM_SETMAXTIPWIDTH
295 if ( wxApp::GetComCtl32Version() >= 470 )
296 {
297 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
298 // extent of its first line as max value
299 HFONT hfont = (HFONT)
300 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0);
301
302 if ( !hfont )
303 {
304 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
305 if ( !hfont )
306 {
307 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
308 }
309 }
310
311 MemoryHDC hdc;
312 if ( !hdc )
313 {
314 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
315 }
316
317 if ( !SelectObject(hdc, hfont) )
318 {
319 wxLogLastError(wxT("SelectObject(hfont)"));
320 }
321
322 SIZE sz;
323 if ( !::GetTextExtentPoint32(hdc, m_text, index, &sz) )
324 {
325 wxLogLastError(wxT("GetTextExtentPoint32"));
326 }
327
328 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
329 (void *)sz.cx);
330 }
331 else
332 #endif // comctl32.dll >= 4.70
333 {
334 // replace the '\n's with spaces because otherwise they appear as
335 // unprintable characters in the tooltip string
336 m_text.Replace(_T("\n"), _T(" "));
337 ti.lpszText = (wxChar *)m_text.c_str(); // const_cast
338
339 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
340 {
341 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
342 }
343 }
344 }
345 }
346 }
347
348 void wxToolTip::SetWindow(wxWindow *win)
349 {
350 Remove();
351
352 m_window = win;
353
354 // add the window itself
355 if ( m_window )
356 {
357 Add(m_window->GetHWND());
358 }
359 #if !defined(__WXUNIVERSAL__)
360 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
361 wxControl *control = wxDynamicCast(m_window, wxControl);
362 if ( control )
363 {
364 const wxArrayLong& subcontrols = control->GetSubcontrols();
365 size_t count = subcontrols.GetCount();
366 for ( size_t n = 0; n < count; n++ )
367 {
368 int id = subcontrols[n];
369 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
370 if ( !hwnd )
371 {
372 // may be it's a child of parent of the control, in fact?
373 // (radiobuttons are subcontrols, i.e. children of the radiobox
374 // for wxWidgets but are its siblings at Windows level)
375 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
376 }
377
378 // must have it by now!
379 wxASSERT_MSG( hwnd, _T("no hwnd for subcontrol?") );
380
381 Add((WXHWND)hwnd);
382 }
383 }
384
385 // VZ: it's ugly to do it here, but I don't want any major changes right
386 // now, later we will probably want to have wxWindow::OnGotToolTip() or
387 // something like this where the derived class can do such things
388 // itself instead of wxToolTip "knowing" about them all
389 wxComboBox *combo = wxDynamicCast(control, wxComboBox);
390 if ( combo )
391 {
392 WXHWND hwndComboEdit = combo->GetWindowStyle() & wxCB_READONLY
393 ? combo->GetHWND()
394 : combo->GetEditHWND();
395 if ( hwndComboEdit )
396 {
397 Add(hwndComboEdit);
398 }
399 }
400 #endif // !defined(__WXUNIVERSAL__)
401 }
402
403 void wxToolTip::SetTip(const wxString& tip)
404 {
405 m_text = tip;
406
407 if ( m_window )
408 {
409 // update the tip text shown by the control
410 wxToolInfo ti(GetHwndOf(m_window));
411 ti.lpszText = (wxChar *)m_text.c_str();
412
413 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
414 }
415 }
416
417 #endif // wxUSE_TOOLTIPS