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