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