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