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