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