]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tooltip.cpp
better native types for carbon
[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
7709915e
VZ
39#ifndef TTTOOLINFO_V1_SIZE
40 #define TTTOOLINFO_V1_SIZE 0x28
41#endif
42
3be7ca64
VZ
43#ifndef TTF_TRANSPARENT
44 #define TTF_TRANSPARENT 0x0100
45#endif
46
f048e32f
VZ
47// VZ: normally, the trick with subclassing the tooltip control and processing
48// TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
49// code here for now (but it's not compiled) in case we need it later.
50//
8614c467
VZ
51// For now I use an ugly workaround and process TTN_NEEDTEXT directly in
52// radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
53// because it would then work for all controls, not only radioboxes but for
54// now I don't understand what's wrong with it...
55#define wxUSE_TTM_WINDOWFROMPOINT 0
f048e32f 56
086c94af
VZ
57// ----------------------------------------------------------------------------
58// global variables
59// ----------------------------------------------------------------------------
60
61// the tooltip parent window
f048e32f
VZ
62WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
63
be8b4385
VZ
64// new tooltip maximum width, default value is set on first call to wxToolTip::Add()
65int wxToolTip::ms_maxWidth = 0;
66
f048e32f
VZ
67#if wxUSE_TTM_WINDOWFROMPOINT
68
69// the tooltip window proc
70static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
71
72#endif // wxUSE_TTM_WINDOWFROMPOINT
066f302c 73
3a19e16d
VZ
74// ----------------------------------------------------------------------------
75// private classes
76// ----------------------------------------------------------------------------
77
bb448552 78// a wrapper around TOOLINFO Win32 structure
26b83329 79#ifdef __VISUALC__
11c7d5b6 80 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
26b83329 81#endif
bb448552 82
3a19e16d
VZ
83class wxToolInfo : public TOOLINFO
84{
85public:
2b5f62a0 86 wxToolInfo(HWND hwndOwner)
3a19e16d
VZ
87 {
88 // initialize all members
89 ::ZeroMemory(this, sizeof(TOOLINFO));
90
bb448552 91 // the structure TOOLINFO has been extended with a 4 byte field in
7709915e
VZ
92 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
93 // use these extended fields so use the old struct size to ensure that
94 // the tooltips work on old (Windows 95) systems too
95 cbSize = TTTOOLINFO_V1_SIZE;
bb448552 96
2b5f62a0 97 hwnd = hwndOwner;
3a19e16d 98 uFlags = TTF_IDISHWND;
7709915e 99
613db756
VZ
100 // we use TTF_TRANSPARENT to fix a problem which arises at least with
101 // the text controls but may presumably happen with other controls
102 // which display the tooltip at mouse position: it can start flashing
103 // then as the control gets "focus lost" events and dismisses the
104 // tooltip which then reappears because mouse remains hovering over the
105 // control, see SF patch 1821229
106 if ( wxApp::GetComCtl32Version() >= 470 )
107 {
108 uFlags |= TTF_TRANSPARENT;
109 }
110
dca0f651 111 uId = (UINT_PTR)hwndOwner;
3a19e16d
VZ
112 }
113};
bb448552 114
26b83329
VZ
115#ifdef __VISUALC__
116 #pragma warning( default : 4097 )
117#endif
3a19e16d
VZ
118
119// ----------------------------------------------------------------------------
120// private functions
121// ----------------------------------------------------------------------------
122
0c5405b7
VZ
123// send a message to the tooltip control if it exists
124//
125// NB: wParam is always 0 for the TTM_XXX messages we use
126static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam)
3a19e16d 127{
0c5405b7 128 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
3a19e16d
VZ
129}
130
16f6dfd8 131// send a message to all existing tooltip controls
0c5405b7
VZ
132static inline void
133SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
16f6dfd8 134{
0c5405b7
VZ
135 if ( hwnd )
136 ::SendMessage((HWND)hwnd, msg, wParam, lParam);
16f6dfd8
VZ
137}
138
3a19e16d
VZ
139// ============================================================================
140// implementation
141// ============================================================================
142
f048e32f
VZ
143#if wxUSE_TTM_WINDOWFROMPOINT
144
3a19e16d 145// ----------------------------------------------------------------------------
f048e32f 146// window proc for our tooltip control
3a19e16d
VZ
147// ----------------------------------------------------------------------------
148
f048e32f
VZ
149LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
150 UINT msg,
151 WPARAM wParam,
152 LPARAM lParam)
153{
154 if ( msg == TTM_WINDOWFROMPOINT )
155 {
156 LPPOINT ppt = (LPPOINT)lParam;
8614c467 157
3103e8a9 158 // the window on which event occurred
f048e32f
VZ
159 HWND hwnd = ::WindowFromPoint(*ppt);
160
8614c467
VZ
161 OutputDebugString("TTM_WINDOWFROMPOINT: ");
162 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
163
77ffb593 164 // return a HWND corresponding to a wxWindow because only wxWidgets are
f048e32f 165 // associated with tooltips using TTM_ADDTOOL
8614c467 166 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
f048e32f 167
8614c467 168 if ( win )
f048e32f 169 {
8614c467
VZ
170 hwnd = GetHwndOf(win);
171 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
172
173#if 0
f048e32f
VZ
174 // modify the point too!
175 RECT rect;
176 GetWindowRect(hwnd, &rect);
177
8614c467
VZ
178 ppt->x = (rect.right - rect.left) / 2;
179 ppt->y = (rect.bottom - rect.top) / 2;
180#endif // 0
f048e32f
VZ
181 return (LRESULT)hwnd;
182 }
8614c467
VZ
183 else
184 {
185 OutputDebugString("no window\r\n");
186 }
f048e32f
VZ
187 }
188
8caa4ed1 189 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
f048e32f
VZ
190}
191
192#endif // wxUSE_TTM_WINDOWFROMPOINT
193
194// ----------------------------------------------------------------------------
195// static functions
196// ----------------------------------------------------------------------------
066f302c 197
16f6dfd8
VZ
198void wxToolTip::Enable(bool flag)
199{
f048e32f 200 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
16f6dfd8
VZ
201}
202
203void wxToolTip::SetDelay(long milliseconds)
204{
f048e32f
VZ
205 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
206 TTDT_INITIAL, milliseconds);
16f6dfd8
VZ
207}
208
becac1ef
VZ
209void wxToolTip::SetAutoPop(long milliseconds)
210{
211 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
212 TTDT_AUTOPOP, milliseconds);
213}
214
215void wxToolTip::SetReshow(long milliseconds)
216{
217 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
218 TTDT_RESHOW, milliseconds);
219}
220
be8b4385
VZ
221void wxToolTip::SetMaxWidth(int width)
222{
223 wxASSERT_MSG( width == -1 || width >= 0, _T("invalid width value") );
224
225 ms_maxWidth = width;
226}
227
16f6dfd8
VZ
228// ---------------------------------------------------------------------------
229// implementation helpers
230// ---------------------------------------------------------------------------
231
3a19e16d 232// create the tooltip ctrl for our parent frame if it doesn't exist yet
4453b38d 233/* static */
3a19e16d
VZ
234WXHWND wxToolTip::GetToolTipCtrl()
235{
f048e32f 236 if ( !ms_hwndTT )
3a19e16d 237 {
978af864
VZ
238 WXDWORD exflags = 0;
239 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
240 {
241 exflags |= WS_EX_LAYOUTRTL;
242 }
243
c5d7205c
VZ
244 // we want to show the tooltips always (even when the window is not
245 // active) and we don't want to strip "&"s from them
978af864
VZ
246 ms_hwndTT = (WXHWND)::CreateWindowEx(exflags,
247 TOOLTIPS_CLASS,
248 (LPCTSTR)NULL,
249 TTS_ALWAYSTIP | TTS_NOPREFIX,
250 CW_USEDEFAULT, CW_USEDEFAULT,
251 CW_USEDEFAULT, CW_USEDEFAULT,
252 NULL, (HMENU)NULL,
253 wxGetInstance(),
254 NULL);
f048e32f 255 if ( ms_hwndTT )
086c94af 256 {
f048e32f
VZ
257 HWND hwnd = (HWND)ms_hwndTT;
258 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
086c94af 259 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
28195455 260
f048e32f
VZ
261#if wxUSE_TTM_WINDOWFROMPOINT
262 // subclass the newly created control
975b6bcf 263 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
f048e32f
VZ
264#endif // wxUSE_TTM_WINDOWFROMPOINT
265 }
3a19e16d
VZ
266 }
267
f048e32f 268 return ms_hwndTT;
3a19e16d
VZ
269}
270
4453b38d 271/* static */
3a19e16d
VZ
272void wxToolTip::RelayEvent(WXMSG *msg)
273{
0c5405b7 274 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
3a19e16d
VZ
275}
276
3a19e16d
VZ
277// ----------------------------------------------------------------------------
278// ctor & dtor
279// ----------------------------------------------------------------------------
280
b342f8f0
RD
281IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
282
3a19e16d
VZ
283wxToolTip::wxToolTip(const wxString &tip)
284 : m_text(tip)
285{
286 m_window = NULL;
287}
288
289wxToolTip::~wxToolTip()
290{
d8909578 291 // the tooltip has to be removed before deleting. Otherwise, if it is visible
623d5f80
WS
292 // while being deleted, there will be a delay before it goes away.
293 Remove();
3a19e16d
VZ
294}
295
296// ----------------------------------------------------------------------------
297// others
298// ----------------------------------------------------------------------------
299
0c5405b7
VZ
300void wxToolTip::Remove(WXHWND hWnd)
301{
302 wxToolInfo ti((HWND)hWnd);
303 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
304}
305
3a19e16d
VZ
306void wxToolTip::Remove()
307{
308 // remove this tool from the tooltip control
309 if ( m_window )
310 {
0c5405b7 311 Remove(m_window->GetHWND());
3a19e16d
VZ
312 }
313}
314
f048e32f
VZ
315void wxToolTip::Add(WXHWND hWnd)
316{
317 HWND hwnd = (HWND)hWnd;
318
319 wxToolInfo ti(hwnd);
320
2b5f62a0
VZ
321 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
322 // store the tooltip text ourselves anyhow, and provide it in response to
323 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
324 // character tooltips as this is the size of the szText buffer in
325 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
326 // of any length
f048e32f 327 ti.hwnd = hwnd;
8c4209f7 328 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
f048e32f 329
0c5405b7 330 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
f048e32f 331 {
bb448552 332 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
8c4209f7
VZ
333
334 return;
f048e32f 335 }
8c4209f7 336
5b59df83 337#ifdef TTM_SETMAXTIPWIDTH
8c4209f7
VZ
338 if ( wxApp::GetComCtl32Version() >= 470 )
339 {
340 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
341 // extent of its first line as max value
342 HFONT hfont = (HFONT)
343 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0);
0c5405b7 344
8c4209f7
VZ
345 if ( !hfont )
346 {
347 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
be8b4385
VZ
348 if ( !hfont )
349 {
8c4209f7 350 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
be8b4385 351 }
8c4209f7 352 }
8614c467 353
8c4209f7
VZ
354 MemoryHDC hdc;
355 if ( !hdc )
356 {
357 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
358 }
8614c467 359
8c4209f7
VZ
360 if ( !SelectObject(hdc, hfont) )
361 {
362 wxLogLastError(wxT("SelectObject(hfont)"));
363 }
8614c467 364
8c4209f7
VZ
365 // find the width of the widest line
366 int maxWidth = 0;
367 wxStringTokenizer tokenizer(m_text, _T("\n"));
368 while ( tokenizer.HasMoreTokens() )
369 {
370 const wxString token = tokenizer.GetNextToken();
be8b4385 371
8c4209f7
VZ
372 SIZE sz;
373 if ( !::GetTextExtentPoint32(hdc, token.wx_str(),
374 token.length(), &sz) )
375 {
376 wxLogLastError(wxT("GetTextExtentPoint32"));
8614c467 377 }
be8b4385 378
8c4209f7
VZ
379 if ( sz.cx > maxWidth )
380 maxWidth = sz.cx;
381 }
382
383 // limit size to ms_maxWidth, if set
384 if ( ms_maxWidth == 0 )
385 {
386 // this is more or less arbitrary but seems to work well
387 static const int DEFAULT_MAX_WIDTH = 400;
8614c467 388
8c4209f7 389 ms_maxWidth = wxGetClientDisplayRect().width / 2;
be8b4385 390
8c4209f7
VZ
391 if ( ms_maxWidth > DEFAULT_MAX_WIDTH )
392 ms_maxWidth = DEFAULT_MAX_WIDTH;
393 }
be8b4385 394
8c4209f7
VZ
395 if ( ms_maxWidth != -1 && maxWidth > ms_maxWidth )
396 maxWidth = ms_maxWidth;
be8b4385 397
8c4209f7
VZ
398 // only set a new width if it is bigger than the current setting:
399 // otherwise adding a tooltip with shorter line(s) than a previous
400 // one would result in breaking the longer lines unnecessarily as
401 // all our tooltips share the same maximal width
402 if ( maxWidth > SendTooltipMessage(GetToolTipCtrl(),
403 TTM_GETMAXTIPWIDTH, 0) )
404 {
405 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
406 wxUIntToPtr(maxWidth));
be8b4385 407 }
8c4209f7
VZ
408 }
409 else
be8b4385 410#endif // TTM_SETMAXTIPWIDTH
8c4209f7
VZ
411 {
412 // replace the '\n's with spaces because otherwise they appear as
413 // unprintable characters in the tooltip string
414 m_text.Replace(_T("\n"), _T(" "));
415 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
be8b4385 416
8c4209f7
VZ
417 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
418 {
419 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
8614c467
VZ
420 }
421 }
f048e32f
VZ
422}
423
3a19e16d
VZ
424void wxToolTip::SetWindow(wxWindow *win)
425{
426 Remove();
427
428 m_window = win;
429
f048e32f 430 // add the window itself
3a19e16d
VZ
431 if ( m_window )
432 {
f048e32f
VZ
433 Add(m_window->GetHWND());
434 }
d4e5272b 435#if !defined(__WXUNIVERSAL__)
d6b9cc87 436 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
f048e32f
VZ
437 wxControl *control = wxDynamicCast(m_window, wxControl);
438 if ( control )
439 {
bf25c88b 440 const wxArrayLong& subcontrols = control->GetSubcontrols();
8614c467 441 size_t count = subcontrols.GetCount();
f048e32f 442 for ( size_t n = 0; n < count; n++ )
3a19e16d 443 {
8614c467 444 int id = subcontrols[n];
f048e32f 445 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
8614c467 446 if ( !hwnd )
f048e32f 447 {
8614c467
VZ
448 // may be it's a child of parent of the control, in fact?
449 // (radiobuttons are subcontrols, i.e. children of the radiobox
77ffb593 450 // for wxWidgets but are its siblings at Windows level)
8614c467 451 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
f048e32f 452 }
8614c467
VZ
453
454 // must have it by now!
455 wxASSERT_MSG( hwnd, _T("no hwnd for subcontrol?") );
456
457 Add((WXHWND)hwnd);
3a19e16d
VZ
458 }
459 }
d4e5272b 460#endif // !defined(__WXUNIVERSAL__)
3a19e16d
VZ
461}
462
463void wxToolTip::SetTip(const wxString& tip)
464{
465 m_text = tip;
466
467 if ( m_window )
468 {
2b5f62a0 469 // update the tip text shown by the control
f048e32f 470 wxToolInfo ti(GetHwndOf(m_window));
3a19e16d 471
4e916e61
VZ
472 // for some reason, changing the tooltip text directly results in
473 // repaint of the controls under it, see #10520 -- but this doesn't
474 // happen if we reset it first
8e1a01e4 475 ti.lpszText = const_cast<wxChar *>(_T(""));
4e916e61
VZ
476 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
477
478 ti.lpszText = const_cast<wxChar *>(m_text.wx_str());
0c5405b7 479 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
3a19e16d
VZ
480 }
481}
cb1a1dc9
VZ
482
483#endif // wxUSE_TOOLTIPS