]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tooltip.cpp
adding more infos, links
[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 #ifndef TTF_TRANSPARENT
44 #define TTF_TRANSPARENT 0x0100
45 #endif
46
47 // VZ: normally, the trick with subclassing the tooltip control and processing
48 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
49 // code here for now (but it's not compiled) in case we need it later.
50 //
51 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
52 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
53 // because it would then work for all controls, not only radioboxes but for
54 // now I don't understand what's wrong with it...
55 #define wxUSE_TTM_WINDOWFROMPOINT 0
56
57 // ----------------------------------------------------------------------------
58 // global variables
59 // ----------------------------------------------------------------------------
60
61 // the tooltip parent window
62 WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
63
64 // new tooltip maximum width, default value is set on first call to wxToolTip::Add()
65 int wxToolTip::ms_maxWidth = 0;
66
67 #if wxUSE_TTM_WINDOWFROMPOINT
68
69 // the tooltip window proc
70 static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
71
72 #endif // wxUSE_TTM_WINDOWFROMPOINT
73
74 // ----------------------------------------------------------------------------
75 // private classes
76 // ----------------------------------------------------------------------------
77
78 // a wrapper around TOOLINFO Win32 structure
79 #ifdef __VISUALC__
80 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
81 #endif
82
83 class wxToolInfo : public TOOLINFO
84 {
85 public:
86 wxToolInfo(HWND hwndOwner, unsigned int id, const wxRect& rc)
87 {
88 // initialize all members
89 ::ZeroMemory(this, sizeof(TOOLINFO));
90
91 // the structure TOOLINFO has been extended with a 4 byte field in
92 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
93 // use these extended fields so use the old struct size to ensure that
94 // the tooltips work on old (Windows 95) systems too
95 cbSize = TTTOOLINFO_V1_SIZE;
96
97 hwnd = hwndOwner;
98
99 if (rc.IsEmpty())
100 {
101 uFlags = TTF_IDISHWND;
102 uId = (UINT_PTR)hwndOwner;
103 }
104 else
105 {
106 // this tooltip must be shown only if the mouse hovers a specific rect
107 // of the hwnd parameter!
108 rect.left = rc.GetLeft();
109 rect.right = rc.GetRight();
110 rect.top = rc.GetTop();
111 rect.bottom = rc.GetBottom();
112
113 // note that not setting TTF_IDISHWND from the uFlags member means that the
114 // ti.uId field should not contain the HWND but rather as MSDN says an
115 // "Application-defined identifier of the tool"; this is used internally by
116 // Windows to distinguish the different tooltips attached to the same window
117 uId = id;
118 }
119
120 // we use TTF_TRANSPARENT to fix a problem which arises at least with
121 // the text controls but may presumably happen with other controls
122 // which display the tooltip at mouse position: it can start flashing
123 // then as the control gets "focus lost" events and dismisses the
124 // tooltip which then reappears because mouse remains hovering over the
125 // control, see SF patch 1821229
126 if ( wxApp::GetComCtl32Version() >= 470 )
127 {
128 uFlags |= TTF_TRANSPARENT;
129 }
130 }
131 };
132
133 #ifdef __VISUALC__
134 #pragma warning( default : 4097 )
135 #endif
136
137 // ----------------------------------------------------------------------------
138 // private functions
139 // ----------------------------------------------------------------------------
140
141 // send a message to the tooltip control if it exists
142 //
143 // NB: wParam is always 0 for the TTM_XXX messages we use
144 static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam)
145 {
146 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
147 }
148
149 // send a message to all existing tooltip controls
150 static inline void
151 SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
152 {
153 if ( hwnd )
154 ::SendMessage((HWND)hwnd, msg, wParam, lParam);
155 }
156
157 // ============================================================================
158 // implementation
159 // ============================================================================
160
161 #if wxUSE_TTM_WINDOWFROMPOINT
162
163 // ----------------------------------------------------------------------------
164 // window proc for our tooltip control
165 // ----------------------------------------------------------------------------
166
167 LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
168 UINT msg,
169 WPARAM wParam,
170 LPARAM lParam)
171 {
172 if ( msg == TTM_WINDOWFROMPOINT )
173 {
174 LPPOINT ppt = (LPPOINT)lParam;
175
176 // the window on which event occurred
177 HWND hwnd = ::WindowFromPoint(*ppt);
178
179 OutputDebugString("TTM_WINDOWFROMPOINT: ");
180 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
181
182 // return a HWND corresponding to a wxWindow because only wxWidgets are
183 // associated with tooltips using TTM_ADDTOOL
184 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
185
186 if ( win )
187 {
188 hwnd = GetHwndOf(win);
189 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
190
191 #if 0
192 // modify the point too!
193 RECT rect;
194 GetWindowRect(hwnd, &rect);
195
196 ppt->x = (rect.right - rect.left) / 2;
197 ppt->y = (rect.bottom - rect.top) / 2;
198 #endif // 0
199 return (LRESULT)hwnd;
200 }
201 else
202 {
203 OutputDebugString("no window\r\n");
204 }
205 }
206
207 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
208 }
209
210 #endif // wxUSE_TTM_WINDOWFROMPOINT
211
212 // ----------------------------------------------------------------------------
213 // static functions
214 // ----------------------------------------------------------------------------
215
216 void wxToolTip::Enable(bool flag)
217 {
218 // Make sure the tooltip has been created
219 (void) GetToolTipCtrl();
220
221 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
222 }
223
224 void wxToolTip::SetDelay(long milliseconds)
225 {
226 // Make sure the tooltip has been created
227 (void) GetToolTipCtrl();
228
229 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
230 TTDT_INITIAL, milliseconds);
231 }
232
233 void wxToolTip::SetAutoPop(long milliseconds)
234 {
235 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
236 TTDT_AUTOPOP, milliseconds);
237 }
238
239 void wxToolTip::SetReshow(long milliseconds)
240 {
241 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
242 TTDT_RESHOW, milliseconds);
243 }
244
245 void wxToolTip::SetMaxWidth(int width)
246 {
247 wxASSERT_MSG( width == -1 || width >= 0, wxT("invalid width value") );
248
249 ms_maxWidth = width;
250 }
251
252 // ---------------------------------------------------------------------------
253 // implementation helpers
254 // ---------------------------------------------------------------------------
255
256 // create the tooltip ctrl for our parent frame if it doesn't exist yet
257 /* static */
258 WXHWND wxToolTip::GetToolTipCtrl()
259 {
260 if ( !ms_hwndTT )
261 {
262 WXDWORD exflags = 0;
263 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
264 {
265 exflags |= WS_EX_LAYOUTRTL;
266 }
267
268 // we want to show the tooltips always (even when the window is not
269 // active) and we don't want to strip "&"s from them
270 ms_hwndTT = (WXHWND)::CreateWindowEx(exflags,
271 TOOLTIPS_CLASS,
272 (LPCTSTR)NULL,
273 TTS_ALWAYSTIP | TTS_NOPREFIX,
274 CW_USEDEFAULT, CW_USEDEFAULT,
275 CW_USEDEFAULT, CW_USEDEFAULT,
276 NULL, (HMENU)NULL,
277 wxGetInstance(),
278 NULL);
279 if ( ms_hwndTT )
280 {
281 HWND hwnd = (HWND)ms_hwndTT;
282 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
283 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
284
285 #if wxUSE_TTM_WINDOWFROMPOINT
286 // subclass the newly created control
287 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
288 #endif // wxUSE_TTM_WINDOWFROMPOINT
289 }
290 }
291
292 return ms_hwndTT;
293 }
294
295 /* static */
296 void wxToolTip::RelayEvent(WXMSG *msg)
297 {
298 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
299 }
300
301 // ----------------------------------------------------------------------------
302 // ctor & dtor
303 // ----------------------------------------------------------------------------
304
305 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
306
307 wxToolTip::wxToolTip(const wxString &tip)
308 : m_text(tip)
309 {
310 m_window = NULL;
311
312 // make sure m_rect.IsEmpty() == true
313 m_rect.SetWidth(0);
314 m_rect.SetHeight(0);
315
316 // since m_rect is not valid, m_id is ignored by wxToolInfo ctor...
317 m_id = 0;
318 }
319
320 wxToolTip::wxToolTip(wxWindow* win, unsigned int id, const wxString &tip, const wxRect& rc)
321 : m_text(tip), m_rect(rc), m_id(id)
322 {
323 m_window = NULL;
324
325 SetWindow(win);
326 }
327
328 wxToolTip::~wxToolTip()
329 {
330 // the tooltip has to be removed before deleting. Otherwise, if it is visible
331 // while being deleted, there will be a delay before it goes away.
332 Remove();
333 }
334
335 // ----------------------------------------------------------------------------
336 // others
337 // ----------------------------------------------------------------------------
338
339 /* static */
340 void wxToolTip::Remove(WXHWND hWnd, unsigned int id, const wxRect& rc)
341 {
342 wxToolInfo ti((HWND)hWnd, id, rc);
343
344 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
345 }
346
347 void wxToolTip::Remove()
348 {
349 // remove this tool from the tooltip control
350 if ( m_window )
351 {
352 Remove(m_window->GetHWND(), m_id, m_rect);
353 }
354 }
355
356 void wxToolTip::Add(WXHWND hWnd)
357 {
358 HWND hwnd = (HWND)hWnd;
359
360 wxToolInfo ti(hwnd, m_id, m_rect);
361
362 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
363 // store the tooltip text ourselves anyhow, and provide it in response to
364 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
365 // character tooltips as this is the size of the szText buffer in
366 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
367 // of any length
368 ti.hwnd = hwnd;
369 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
370
371 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
372 {
373 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text.c_str());
374
375 return;
376 }
377
378 #ifdef TTM_SETMAXTIPWIDTH
379 if ( wxApp::GetComCtl32Version() >= 470 )
380 {
381 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
382 // extent of its first line as max value
383 HFONT hfont = (HFONT)
384 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0);
385
386 if ( !hfont )
387 {
388 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
389 if ( !hfont )
390 {
391 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
392 }
393 }
394
395 MemoryHDC hdc;
396 if ( !hdc )
397 {
398 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
399 }
400
401 if ( !SelectObject(hdc, hfont) )
402 {
403 wxLogLastError(wxT("SelectObject(hfont)"));
404 }
405
406 // find the width of the widest line
407 int maxWidth = 0;
408 wxStringTokenizer tokenizer(m_text, wxT("\n"));
409 while ( tokenizer.HasMoreTokens() )
410 {
411 const wxString token = tokenizer.GetNextToken();
412
413 SIZE sz;
414 if ( !::GetTextExtentPoint32(hdc, token.wx_str(),
415 token.length(), &sz) )
416 {
417 wxLogLastError(wxT("GetTextExtentPoint32"));
418 }
419
420 if ( sz.cx > maxWidth )
421 maxWidth = sz.cx;
422 }
423
424 // limit size to ms_maxWidth, if set
425 if ( ms_maxWidth == 0 )
426 {
427 // this is more or less arbitrary but seems to work well
428 static const int DEFAULT_MAX_WIDTH = 400;
429
430 ms_maxWidth = wxGetClientDisplayRect().width / 2;
431
432 if ( ms_maxWidth > DEFAULT_MAX_WIDTH )
433 ms_maxWidth = DEFAULT_MAX_WIDTH;
434 }
435
436 if ( ms_maxWidth != -1 && maxWidth > ms_maxWidth )
437 maxWidth = ms_maxWidth;
438
439 // only set a new width if it is bigger than the current setting:
440 // otherwise adding a tooltip with shorter line(s) than a previous
441 // one would result in breaking the longer lines unnecessarily as
442 // all our tooltips share the same maximal width
443 if ( maxWidth > SendTooltipMessage(GetToolTipCtrl(),
444 TTM_GETMAXTIPWIDTH, 0) )
445 {
446 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
447 wxUIntToPtr(maxWidth));
448 }
449 }
450 else
451 #endif // TTM_SETMAXTIPWIDTH
452 {
453 // replace the '\n's with spaces because otherwise they appear as
454 // unprintable characters in the tooltip string
455 m_text.Replace(wxT("\n"), wxT(" "));
456 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
457
458 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
459 {
460 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text.c_str());
461 }
462 }
463 }
464
465 void wxToolTip::SetWindow(wxWindow *win)
466 {
467 Remove();
468
469 m_window = win;
470
471 // add the window itself
472 if ( m_window )
473 {
474 Add(m_window->GetHWND());
475 }
476 #if !defined(__WXUNIVERSAL__)
477 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
478 wxControl *control = wxDynamicCast(m_window, wxControl);
479 if ( control )
480 {
481 const wxArrayLong& subcontrols = control->GetSubcontrols();
482 size_t count = subcontrols.GetCount();
483 for ( size_t n = 0; n < count; n++ )
484 {
485 int id = subcontrols[n];
486 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
487 if ( !hwnd )
488 {
489 // may be it's a child of parent of the control, in fact?
490 // (radiobuttons are subcontrols, i.e. children of the radiobox
491 // for wxWidgets but are its siblings at Windows level)
492 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
493 }
494
495 // must have it by now!
496 wxASSERT_MSG( hwnd, wxT("no hwnd for subcontrol?") );
497
498 Add((WXHWND)hwnd);
499 }
500 }
501 #endif // !defined(__WXUNIVERSAL__)
502 }
503
504 void wxToolTip::SetRect(const wxRect& rc)
505 {
506 m_rect = rc;
507
508 if ( m_window )
509 {
510 wxToolInfo ti(GetHwndOf(m_window), m_id, m_rect);
511 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_NEWTOOLRECT, &ti);
512 }
513 }
514
515 void wxToolTip::SetTip(const wxString& tip)
516 {
517 m_text = tip;
518
519 if ( m_window )
520 {
521 // update the tip text shown by the control
522 wxToolInfo ti(GetHwndOf(m_window), m_id, m_rect);
523
524 // for some reason, changing the tooltip text directly results in
525 // repaint of the controls under it, see #10520 -- but this doesn't
526 // happen if we reset it first
527 ti.lpszText = const_cast<wxChar *>(wxT(""));
528 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
529
530 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
531 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
532 }
533 }
534
535 #endif // wxUSE_TOOLTIPS