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