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