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