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