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