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