]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tooltip.cpp
Initial revision
[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_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
36 #include <commctrl.h>
37 #endif
38
39 #ifndef _WIN32_IE
40 // minimal set of features by default
41 #define _WIN32_IE 0x0200
42 #endif
43
44 // VZ: normally, the trick with subclassing the tooltip control and processing
45 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
46 // code here for now (but it's not compiled) in case we need it later.
47 //
48 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
49 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
50 // because it would then work for all controls, not only radioboxes but for
51 // now I don't understand what's wrong with it...
52 #define wxUSE_TTM_WINDOWFROMPOINT 0
53
54 // ----------------------------------------------------------------------------
55 // global variables
56 // ----------------------------------------------------------------------------
57
58 // the tooltip parent window
59 WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
60
61 #if wxUSE_TTM_WINDOWFROMPOINT
62
63 // the tooltip window proc
64 static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
65
66 #endif // wxUSE_TTM_WINDOWFROMPOINT
67
68 // ----------------------------------------------------------------------------
69 // private classes
70 // ----------------------------------------------------------------------------
71
72 // a wrapper around TOOLINFO Win32 structure
73 #ifdef __VISUALC__
74 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
75 #endif
76
77 class wxToolInfo : public TOOLINFO
78 {
79 public:
80 wxToolInfo(HWND hwnd)
81 {
82 // initialize all members
83 ::ZeroMemory(this, sizeof(TOOLINFO));
84
85 // the structure TOOLINFO has been extended with a 4 byte field in
86 // version 4.70 of comctl32.dll and if we compile on a newer machine
87 // but run on one with the old version of comctl32, nothing will work
88 // because the library will detect that we rely on a more recent
89 // version of it. So we always use the old size - if we ever start
90 // using our lParam member, we'd have to check for comctl32 version
91 // during run-time
92 #if _WIN32_IE >= 0x0300
93 cbSize = sizeof(TOOLINFO) - sizeof(LPARAM);
94 #else // old headers
95 cbSize = sizeof(TOOLINFO);
96 #endif // compile-time comctl32.dll version
97
98 uFlags = TTF_IDISHWND;
99 uId = (UINT)hwnd;
100 }
101 };
102
103 #ifdef __VISUALC__
104 #pragma warning( default : 4097 )
105 #endif
106
107 // ----------------------------------------------------------------------------
108 // private functions
109 // ----------------------------------------------------------------------------
110
111 // send a message to the tooltip control
112 inline LRESULT SendTooltipMessage(WXHWND hwnd,
113 UINT msg,
114 WPARAM wParam,
115 void *lParam)
116 {
117 return hwnd ? ::SendMessage((HWND)hwnd, msg, wParam, (LPARAM)lParam)
118 : 0;
119 }
120
121 // send a message to all existing tooltip controls
122 static void SendTooltipMessageToAll(WXHWND hwnd,
123 UINT msg,
124 WPARAM wParam,
125 LPARAM lParam)
126 {
127 (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam);
128 }
129
130 // ============================================================================
131 // implementation
132 // ============================================================================
133
134 #if wxUSE_TTM_WINDOWFROMPOINT
135
136 // ----------------------------------------------------------------------------
137 // window proc for our tooltip control
138 // ----------------------------------------------------------------------------
139
140 LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
141 UINT msg,
142 WPARAM wParam,
143 LPARAM lParam)
144 {
145 if ( msg == TTM_WINDOWFROMPOINT )
146 {
147 LPPOINT ppt = (LPPOINT)lParam;
148
149 // the window on which event occured
150 HWND hwnd = ::WindowFromPoint(*ppt);
151
152 OutputDebugString("TTM_WINDOWFROMPOINT: ");
153 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
154
155 // return a HWND corresponding to a wxWindow because only wxWindows are
156 // associated with tooltips using TTM_ADDTOOL
157 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
158
159 if ( win )
160 {
161 hwnd = GetHwndOf(win);
162 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
163
164 #if 0
165 // modify the point too!
166 RECT rect;
167 GetWindowRect(hwnd, &rect);
168
169 ppt->x = (rect.right - rect.left) / 2;
170 ppt->y = (rect.bottom - rect.top) / 2;
171 #endif // 0
172 return (LRESULT)hwnd;
173 }
174 else
175 {
176 OutputDebugString("no window\r\n");
177 }
178 }
179
180 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
181 }
182
183 #endif // wxUSE_TTM_WINDOWFROMPOINT
184
185 // ----------------------------------------------------------------------------
186 // static functions
187 // ----------------------------------------------------------------------------
188
189 void wxToolTip::Enable(bool flag)
190 {
191 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
192 }
193
194 void wxToolTip::SetDelay(long milliseconds)
195 {
196 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
197 TTDT_INITIAL, milliseconds);
198 }
199
200 // ---------------------------------------------------------------------------
201 // implementation helpers
202 // ---------------------------------------------------------------------------
203
204 // create the tooltip ctrl for our parent frame if it doesn't exist yet
205 WXHWND wxToolTip::GetToolTipCtrl()
206 {
207 if ( !ms_hwndTT )
208 {
209 ms_hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS,
210 (LPCTSTR)NULL,
211 TTS_ALWAYSTIP,
212 CW_USEDEFAULT, CW_USEDEFAULT,
213 CW_USEDEFAULT, CW_USEDEFAULT,
214 NULL, (HMENU)NULL,
215 wxGetInstance(),
216 NULL);
217 if ( ms_hwndTT )
218 {
219 HWND hwnd = (HWND)ms_hwndTT;
220 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
221 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
222
223 #if wxUSE_TTM_WINDOWFROMPOINT
224 // subclass the newly created control
225 gs_wndprocToolTip = (WNDPROC)::GetWindowLong(hwnd, GWL_WNDPROC);
226 ::SetWindowLong(hwnd, GWL_WNDPROC, (long)wxToolTipWndProc);
227 #endif // wxUSE_TTM_WINDOWFROMPOINT
228 }
229 }
230
231 return ms_hwndTT;
232 }
233
234 void wxToolTip::RelayEvent(WXMSG *msg)
235 {
236 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
237 }
238
239 // ----------------------------------------------------------------------------
240 // ctor & dtor
241 // ----------------------------------------------------------------------------
242
243 wxToolTip::wxToolTip(const wxString &tip)
244 : m_text(tip)
245 {
246 m_window = NULL;
247 }
248
249 wxToolTip::~wxToolTip()
250 {
251 // there is no need to Remove() this tool - it will be done automatically
252 // anyhow
253 }
254
255 // ----------------------------------------------------------------------------
256 // others
257 // ----------------------------------------------------------------------------
258
259 void wxToolTip::Remove()
260 {
261 // remove this tool from the tooltip control
262 if ( m_window )
263 {
264 wxToolInfo ti(GetHwndOf(m_window));
265 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, 0, &ti);
266 }
267 }
268
269 void wxToolTip::Add(WXHWND hWnd)
270 {
271 HWND hwnd = (HWND)hWnd;
272
273 wxToolInfo ti(hwnd);
274
275 // as we store our text anyhow, it seems useless to waste system memory
276 // by asking the tooltip ctrl to remember it too - instead it will send
277 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
278 ti.hwnd = hwnd;
279 ti.lpszText = LPSTR_TEXTCALLBACK;
280 // instead of: ti.lpszText = (char *)m_text.c_str();
281
282 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, 0, &ti) )
283 {
284 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
285 }
286 else
287 {
288 // check for multiline toopltip
289 int index = m_text.Find(_T('\n'));
290
291 if ( index != wxNOT_FOUND )
292 {
293 #if _WIN32_IE >= 0x0300
294 if ( wxTheApp->GetComCtl32Version() >= 470 )
295 {
296 // use TTM_SETMAXWIDTH to make tooltip multiline using the
297 // extent of its first line as max value
298 HFONT hfont = (HFONT)SendTooltipMessage(GetToolTipCtrl(),
299 WM_GETFONT,
300 0, 0);
301 if ( !hfont )
302 {
303 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
304 if ( !hfont )
305 {
306 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
307 }
308 }
309
310 HDC hdc = CreateCompatibleDC(NULL);
311 if ( !hdc )
312 {
313 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
314 }
315
316 if ( !SelectObject(hdc, hfont) )
317 {
318 wxLogLastError(wxT("SelectObject(hfont)"));
319 }
320
321 SIZE sz;
322 if ( !GetTextExtentPoint(hdc, m_text, index, &sz) )
323 {
324 wxLogLastError(wxT("GetTextExtentPoint"));
325 }
326
327 DeleteDC(hdc);
328
329 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
330 0, (void *)sz.cx);
331 }
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 }
338 }
339 }
340
341 void wxToolTip::SetWindow(wxWindow *win)
342 {
343 Remove();
344
345 m_window = win;
346
347 // add the window itself
348 if ( m_window )
349 {
350 Add(m_window->GetHWND());
351 }
352
353 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
354 wxControl *control = wxDynamicCast(m_window, wxControl);
355 if ( control )
356 {
357 const wxArrayLong& subcontrols = control->GetSubcontrols();
358 size_t count = subcontrols.GetCount();
359 for ( size_t n = 0; n < count; n++ )
360 {
361 int id = subcontrols[n];
362 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
363 if ( !hwnd )
364 {
365 // may be it's a child of parent of the control, in fact?
366 // (radiobuttons are subcontrols, i.e. children of the radiobox
367 // for wxWindows but are its siblings at Windows level)
368 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
369 }
370
371 // must have it by now!
372 wxASSERT_MSG( hwnd, _T("no hwnd for subcontrol?") );
373
374 Add((WXHWND)hwnd);
375 }
376 }
377
378 // VZ: it's ugly to do it here, but I don't want any major changes right
379 // now, later we will probably want to have wxWindow::OnGotToolTip() or
380 // something like this where the derived class can do such things
381 // itself instead of wxToolTip "knowing" about them all
382 wxComboBox *combo = wxDynamicCast(control, wxComboBox);
383 if ( combo )
384 {
385 WXHWND hwndComboEdit = combo->GetWindowStyle() & wxCB_READONLY
386 ? combo->GetHWND()
387 : combo->GetEditHWND();
388 if ( hwndComboEdit )
389 {
390 Add(hwndComboEdit);
391 }
392 }
393 }
394
395 void wxToolTip::SetTip(const wxString& tip)
396 {
397 m_text = tip;
398
399 if ( m_window )
400 {
401 // update it immediately
402 wxToolInfo ti(GetHwndOf(m_window));
403 ti.lpszText = (wxChar *)m_text.c_str();
404
405 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
406 }
407 }
408
409 #endif // wxUSE_TOOLTIPS