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