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