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