]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tooltip.cpp
Halfway reasonable implementation of wxFont for wxCocoa.
[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
57bd4c60 31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
623d5f80
WS
32 #include "wx/app.h"
33 #include "wx/control.h"
623d5f80
WS
34#endif
35
543a9921 36#include "wx/tokenzr.h"
3a19e16d
VZ
37#include "wx/msw/private.h"
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
0c5405b7
VZ
107// send a message to the tooltip control if it exists
108//
109// NB: wParam is always 0 for the TTM_XXX messages we use
110static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam)
3a19e16d 111{
0c5405b7 112 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
3a19e16d
VZ
113}
114
16f6dfd8 115// send a message to all existing tooltip controls
0c5405b7
VZ
116static inline void
117SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
16f6dfd8 118{
0c5405b7
VZ
119 if ( hwnd )
120 ::SendMessage((HWND)hwnd, msg, wParam, lParam);
16f6dfd8
VZ
121}
122
3a19e16d
VZ
123// ============================================================================
124// implementation
125// ============================================================================
126
f048e32f
VZ
127#if wxUSE_TTM_WINDOWFROMPOINT
128
3a19e16d 129// ----------------------------------------------------------------------------
f048e32f 130// window proc for our tooltip control
3a19e16d
VZ
131// ----------------------------------------------------------------------------
132
f048e32f
VZ
133LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
134 UINT msg,
135 WPARAM wParam,
136 LPARAM lParam)
137{
138 if ( msg == TTM_WINDOWFROMPOINT )
139 {
140 LPPOINT ppt = (LPPOINT)lParam;
8614c467 141
3103e8a9 142 // the window on which event occurred
f048e32f
VZ
143 HWND hwnd = ::WindowFromPoint(*ppt);
144
8614c467
VZ
145 OutputDebugString("TTM_WINDOWFROMPOINT: ");
146 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
147
77ffb593 148 // return a HWND corresponding to a wxWindow because only wxWidgets are
f048e32f 149 // associated with tooltips using TTM_ADDTOOL
8614c467 150 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
f048e32f 151
8614c467 152 if ( win )
f048e32f 153 {
8614c467
VZ
154 hwnd = GetHwndOf(win);
155 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
156
157#if 0
f048e32f
VZ
158 // modify the point too!
159 RECT rect;
160 GetWindowRect(hwnd, &rect);
161
8614c467
VZ
162 ppt->x = (rect.right - rect.left) / 2;
163 ppt->y = (rect.bottom - rect.top) / 2;
164#endif // 0
f048e32f
VZ
165 return (LRESULT)hwnd;
166 }
8614c467
VZ
167 else
168 {
169 OutputDebugString("no window\r\n");
170 }
f048e32f
VZ
171 }
172
8caa4ed1 173 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
f048e32f
VZ
174}
175
176#endif // wxUSE_TTM_WINDOWFROMPOINT
177
178// ----------------------------------------------------------------------------
179// static functions
180// ----------------------------------------------------------------------------
066f302c 181
16f6dfd8
VZ
182void wxToolTip::Enable(bool flag)
183{
f048e32f 184 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
16f6dfd8
VZ
185}
186
187void wxToolTip::SetDelay(long milliseconds)
188{
f048e32f
VZ
189 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
190 TTDT_INITIAL, milliseconds);
16f6dfd8
VZ
191}
192
becac1ef
VZ
193void wxToolTip::SetAutoPop(long milliseconds)
194{
195 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
196 TTDT_AUTOPOP, milliseconds);
197}
198
199void wxToolTip::SetReshow(long milliseconds)
200{
201 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
202 TTDT_RESHOW, milliseconds);
203}
204
16f6dfd8
VZ
205// ---------------------------------------------------------------------------
206// implementation helpers
207// ---------------------------------------------------------------------------
208
3a19e16d 209// create the tooltip ctrl for our parent frame if it doesn't exist yet
4453b38d 210/* static */
3a19e16d
VZ
211WXHWND wxToolTip::GetToolTipCtrl()
212{
f048e32f 213 if ( !ms_hwndTT )
3a19e16d 214 {
978af864
VZ
215 WXDWORD exflags = 0;
216 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
217 {
218 exflags |= WS_EX_LAYOUTRTL;
219 }
220
c5d7205c
VZ
221 // we want to show the tooltips always (even when the window is not
222 // active) and we don't want to strip "&"s from them
978af864
VZ
223 ms_hwndTT = (WXHWND)::CreateWindowEx(exflags,
224 TOOLTIPS_CLASS,
225 (LPCTSTR)NULL,
226 TTS_ALWAYSTIP | TTS_NOPREFIX,
227 CW_USEDEFAULT, CW_USEDEFAULT,
228 CW_USEDEFAULT, CW_USEDEFAULT,
229 NULL, (HMENU)NULL,
230 wxGetInstance(),
231 NULL);
f048e32f 232 if ( ms_hwndTT )
086c94af 233 {
f048e32f
VZ
234 HWND hwnd = (HWND)ms_hwndTT;
235 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
086c94af 236 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
28195455 237
f048e32f
VZ
238#if wxUSE_TTM_WINDOWFROMPOINT
239 // subclass the newly created control
975b6bcf 240 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
f048e32f
VZ
241#endif // wxUSE_TTM_WINDOWFROMPOINT
242 }
3a19e16d
VZ
243 }
244
f048e32f 245 return ms_hwndTT;
3a19e16d
VZ
246}
247
4453b38d 248/* static */
3a19e16d
VZ
249void wxToolTip::RelayEvent(WXMSG *msg)
250{
0c5405b7 251 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
3a19e16d
VZ
252}
253
3a19e16d
VZ
254// ----------------------------------------------------------------------------
255// ctor & dtor
256// ----------------------------------------------------------------------------
257
b342f8f0
RD
258IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
259
3a19e16d
VZ
260wxToolTip::wxToolTip(const wxString &tip)
261 : m_text(tip)
262{
263 m_window = NULL;
264}
265
266wxToolTip::~wxToolTip()
267{
d8909578 268 // the tooltip has to be removed before deleting. Otherwise, if it is visible
623d5f80
WS
269 // while being deleted, there will be a delay before it goes away.
270 Remove();
3a19e16d
VZ
271}
272
273// ----------------------------------------------------------------------------
274// others
275// ----------------------------------------------------------------------------
276
0c5405b7
VZ
277void wxToolTip::Remove(WXHWND hWnd)
278{
279 wxToolInfo ti((HWND)hWnd);
280 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
281}
282
3a19e16d
VZ
283void wxToolTip::Remove()
284{
285 // remove this tool from the tooltip control
286 if ( m_window )
287 {
0c5405b7 288 Remove(m_window->GetHWND());
3a19e16d
VZ
289 }
290}
291
f048e32f
VZ
292void wxToolTip::Add(WXHWND hWnd)
293{
294 HWND hwnd = (HWND)hWnd;
295
296 wxToolInfo ti(hwnd);
297
2b5f62a0
VZ
298 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
299 // store the tooltip text ourselves anyhow, and provide it in response to
300 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
301 // character tooltips as this is the size of the szText buffer in
302 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
303 // of any length
f048e32f 304 ti.hwnd = hwnd;
c9f78968 305 ti.lpszText = (wxChar *)m_text.wx_str(); // const_cast
f048e32f 306
0c5405b7 307 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
f048e32f 308 {
bb448552 309 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
f048e32f 310 }
8614c467
VZ
311 else
312 {
313 // check for multiline toopltip
314 int index = m_text.Find(_T('\n'));
315
316 if ( index != wxNOT_FOUND )
317 {
5b59df83 318#ifdef TTM_SETMAXTIPWIDTH
2a1f999f 319 if ( wxApp::GetComCtl32Version() >= 470 )
8614c467 320 {
5b59df83 321 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
8614c467 322 // extent of its first line as max value
0c5405b7
VZ
323 HFONT hfont = (HFONT)
324 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0);
325
8614c467
VZ
326 if ( !hfont )
327 {
328 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
329 if ( !hfont )
330 {
f6bcfd97 331 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
8614c467
VZ
332 }
333 }
334
2b5f62a0 335 MemoryHDC hdc;
8614c467
VZ
336 if ( !hdc )
337 {
f6bcfd97 338 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
8614c467
VZ
339 }
340
341 if ( !SelectObject(hdc, hfont) )
342 {
f6bcfd97 343 wxLogLastError(wxT("SelectObject(hfont)"));
8614c467
VZ
344 }
345
543a9921
RD
346 // find the width of the widest line
347 int max = 0;
348 wxStringTokenizer tokenizer(m_text, _T("\n"));
349 wxString token = tokenizer.GetNextToken();
350 while (token.length())
8614c467 351 {
543a9921 352 SIZE sz;
e0a050e3 353 if ( !::GetTextExtentPoint32(hdc, token.wx_str(), token.length(), &sz) )
543a9921
RD
354 {
355 wxLogLastError(wxT("GetTextExtentPoint32"));
356 }
357 if ( sz.cx > max )
358 max = sz.cx;
57bd4c60 359
543a9921 360 token = tokenizer.GetNextToken();
8614c467
VZ
361 }
362
543a9921
RD
363 // only set a new width if it is bigger than the current setting
364 if (max > SendTooltipMessage(GetToolTipCtrl(), TTM_GETMAXTIPWIDTH, 0))
365 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
366 (void *)max);
8614c467 367 }
95130bea 368 else
8614c467 369#endif // comctl32.dll >= 4.70
95130bea
WS
370 {
371 // replace the '\n's with spaces because otherwise they appear as
372 // unprintable characters in the tooltip string
373 m_text.Replace(_T("\n"), _T(" "));
c9f78968 374 ti.lpszText = (wxChar *)m_text.wx_str(); // const_cast
8614c467 375
0c5405b7 376 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
95130bea
WS
377 {
378 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
379 }
380 }
8614c467
VZ
381 }
382 }
f048e32f
VZ
383}
384
3a19e16d
VZ
385void wxToolTip::SetWindow(wxWindow *win)
386{
387 Remove();
388
389 m_window = win;
390
f048e32f 391 // add the window itself
3a19e16d
VZ
392 if ( m_window )
393 {
f048e32f
VZ
394 Add(m_window->GetHWND());
395 }
d4e5272b 396#if !defined(__WXUNIVERSAL__)
d6b9cc87 397 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
f048e32f
VZ
398 wxControl *control = wxDynamicCast(m_window, wxControl);
399 if ( control )
400 {
bf25c88b 401 const wxArrayLong& subcontrols = control->GetSubcontrols();
8614c467 402 size_t count = subcontrols.GetCount();
f048e32f 403 for ( size_t n = 0; n < count; n++ )
3a19e16d 404 {
8614c467 405 int id = subcontrols[n];
f048e32f 406 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
8614c467 407 if ( !hwnd )
f048e32f 408 {
8614c467
VZ
409 // may be it's a child of parent of the control, in fact?
410 // (radiobuttons are subcontrols, i.e. children of the radiobox
77ffb593 411 // for wxWidgets but are its siblings at Windows level)
8614c467 412 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
f048e32f 413 }
8614c467
VZ
414
415 // must have it by now!
416 wxASSERT_MSG( hwnd, _T("no hwnd for subcontrol?") );
417
418 Add((WXHWND)hwnd);
3a19e16d
VZ
419 }
420 }
d4e5272b 421#endif // !defined(__WXUNIVERSAL__)
3a19e16d
VZ
422}
423
424void wxToolTip::SetTip(const wxString& tip)
425{
426 m_text = tip;
427
428 if ( m_window )
429 {
2b5f62a0 430 // update the tip text shown by the control
f048e32f 431 wxToolInfo ti(GetHwndOf(m_window));
c9f78968 432 ti.lpszText = (wxChar *)m_text.wx_str();
3a19e16d 433
0c5405b7 434 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
3a19e16d
VZ
435 }
436}
cb1a1dc9
VZ
437
438#endif // wxUSE_TOOLTIPS