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