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