]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tooltip.cpp
don't reset the selection after event was vetoed if there is no old selection
[wxWidgets.git] / src / msw / tooltip.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #if wxUSE_TOOLTIPS
27
28 #include "wx/tooltip.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/control.h"
33 #include "wx/combobox.h"
34 #endif
35
36 #include "wx/msw/private.h"
37
38 // include <commctrl.h> "properly"
39 #include "wx/msw/wrapcctl.h"
40
41 // VZ: normally, the trick with subclassing the tooltip control and processing
42 // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the
43 // code here for now (but it's not compiled) in case we need it later.
44 //
45 // For now I use an ugly workaround and process TTN_NEEDTEXT directly in
46 // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice
47 // because it would then work for all controls, not only radioboxes but for
48 // now I don't understand what's wrong with it...
49 #define wxUSE_TTM_WINDOWFROMPOINT 0
50
51 // ----------------------------------------------------------------------------
52 // global variables
53 // ----------------------------------------------------------------------------
54
55 // the tooltip parent window
56 WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
57
58 #if wxUSE_TTM_WINDOWFROMPOINT
59
60 // the tooltip window proc
61 static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
62
63 #endif // wxUSE_TTM_WINDOWFROMPOINT
64
65 // ----------------------------------------------------------------------------
66 // private classes
67 // ----------------------------------------------------------------------------
68
69 // a wrapper around TOOLINFO Win32 structure
70 #ifdef __VISUALC__
71 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
72 #endif
73
74 class wxToolInfo : public TOOLINFO
75 {
76 public:
77 wxToolInfo(HWND hwndOwner)
78 {
79 // initialize all members
80 ::ZeroMemory(this, sizeof(TOOLINFO));
81
82 // the structure TOOLINFO has been extended with a 4 byte field in
83 // version 4.70 of comctl32.dll and if we compile on a newer machine
84 // but run on one with the old version of comctl32, nothing will work
85 // because the library will detect that we rely on a more recent
86 // version of it. So we always use the old size - if we ever start
87 // using our lParam member, we'd have to check for comctl32 version
88 // during run-time
89 #if _WIN32_IE >= 0x0300
90 cbSize = sizeof(TOOLINFO) - sizeof(LPARAM);
91 #else // old headers
92 cbSize = sizeof(TOOLINFO);
93 #endif // compile-time comctl32.dll version
94
95 hwnd = hwndOwner;
96 uFlags = TTF_IDISHWND;
97 uId = (UINT)hwndOwner;
98 }
99 };
100
101 #ifdef __VISUALC__
102 #pragma warning( default : 4097 )
103 #endif
104
105 // ----------------------------------------------------------------------------
106 // private functions
107 // ----------------------------------------------------------------------------
108
109 // send a message to the tooltip control if it exists
110 //
111 // NB: wParam is always 0 for the TTM_XXX messages we use
112 static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam)
113 {
114 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
115 }
116
117 // send a message to all existing tooltip controls
118 static inline void
119 SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
120 {
121 if ( hwnd )
122 ::SendMessage((HWND)hwnd, msg, wParam, 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 /* static */
201 WXHWND wxToolTip::GetToolTipCtrl()
202 {
203 if ( !ms_hwndTT )
204 {
205 // we want to show the tooltips always (even when the window is not
206 // active) and we don't want to strip "&"s from them
207 ms_hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS,
208 (LPCTSTR)NULL,
209 TTS_ALWAYSTIP | TTS_NOPREFIX,
210 CW_USEDEFAULT, CW_USEDEFAULT,
211 CW_USEDEFAULT, CW_USEDEFAULT,
212 NULL, (HMENU)NULL,
213 wxGetInstance(),
214 NULL);
215 if ( ms_hwndTT )
216 {
217 HWND hwnd = (HWND)ms_hwndTT;
218 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
219 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
220
221 #if wxUSE_TTM_WINDOWFROMPOINT
222 // subclass the newly created control
223 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
224 #endif // wxUSE_TTM_WINDOWFROMPOINT
225 }
226 }
227
228 return ms_hwndTT;
229 }
230
231 /* static */
232 void wxToolTip::RelayEvent(WXMSG *msg)
233 {
234 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
235 }
236
237 // ----------------------------------------------------------------------------
238 // ctor & dtor
239 // ----------------------------------------------------------------------------
240
241 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
242
243 wxToolTip::wxToolTip(const wxString &tip)
244 : m_text(tip)
245 {
246 m_window = NULL;
247 }
248
249 wxToolTip::~wxToolTip()
250 {
251 // the tooltip has to be removed before deleting. Otherwise, if it is visible
252 // while being deleted, there will be a delay before it goes away.
253 Remove();
254 }
255
256 // ----------------------------------------------------------------------------
257 // others
258 // ----------------------------------------------------------------------------
259
260 void wxToolTip::Remove(WXHWND hWnd)
261 {
262 wxToolInfo ti((HWND)hWnd);
263 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
264 }
265
266 void wxToolTip::Remove()
267 {
268 // remove this tool from the tooltip control
269 if ( m_window )
270 {
271 Remove(m_window->GetHWND());
272 }
273 }
274
275 void wxToolTip::Add(WXHWND hWnd)
276 {
277 HWND hwnd = (HWND)hWnd;
278
279 wxToolInfo ti(hwnd);
280
281 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
282 // store the tooltip text ourselves anyhow, and provide it in response to
283 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
284 // character tooltips as this is the size of the szText buffer in
285 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
286 // of any length
287 ti.hwnd = hwnd;
288 ti.lpszText = (wxChar *)m_text.c_str(); // const_cast
289
290 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
291 {
292 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
293 }
294 else
295 {
296 // check for multiline toopltip
297 int index = m_text.Find(_T('\n'));
298
299 if ( index != wxNOT_FOUND )
300 {
301 #ifdef TTM_SETMAXTIPWIDTH
302 if ( wxApp::GetComCtl32Version() >= 470 )
303 {
304 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
305 // extent of its first line as max value
306 HFONT hfont = (HFONT)
307 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0);
308
309 if ( !hfont )
310 {
311 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
312 if ( !hfont )
313 {
314 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
315 }
316 }
317
318 MemoryHDC hdc;
319 if ( !hdc )
320 {
321 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
322 }
323
324 if ( !SelectObject(hdc, hfont) )
325 {
326 wxLogLastError(wxT("SelectObject(hfont)"));
327 }
328
329 SIZE sz;
330 if ( !::GetTextExtentPoint32(hdc, m_text, index, &sz) )
331 {
332 wxLogLastError(wxT("GetTextExtentPoint32"));
333 }
334
335 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
336 (void *)sz.cx);
337 }
338 else
339 #endif // comctl32.dll >= 4.70
340 {
341 // replace the '\n's with spaces because otherwise they appear as
342 // unprintable characters in the tooltip string
343 m_text.Replace(_T("\n"), _T(" "));
344 ti.lpszText = (wxChar *)m_text.c_str(); // const_cast
345
346 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
347 {
348 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
349 }
350 }
351 }
352 }
353 }
354
355 void wxToolTip::SetWindow(wxWindow *win)
356 {
357 Remove();
358
359 m_window = win;
360
361 // add the window itself
362 if ( m_window )
363 {
364 Add(m_window->GetHWND());
365 }
366 #if !defined(__WXUNIVERSAL__)
367 // and all of its subcontrols (e.g. radiobuttons in a radiobox) as well
368 wxControl *control = wxDynamicCast(m_window, wxControl);
369 if ( control )
370 {
371 const wxArrayLong& subcontrols = control->GetSubcontrols();
372 size_t count = subcontrols.GetCount();
373 for ( size_t n = 0; n < count; n++ )
374 {
375 int id = subcontrols[n];
376 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
377 if ( !hwnd )
378 {
379 // may be it's a child of parent of the control, in fact?
380 // (radiobuttons are subcontrols, i.e. children of the radiobox
381 // for wxWidgets but are its siblings at Windows level)
382 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
383 }
384
385 // must have it by now!
386 wxASSERT_MSG( hwnd, _T("no hwnd for subcontrol?") );
387
388 Add((WXHWND)hwnd);
389 }
390 }
391
392 // VZ: it's ugly to do it here, but I don't want any major changes right
393 // now, later we will probably want to have wxWindow::OnGotToolTip() or
394 // something like this where the derived class can do such things
395 // itself instead of wxToolTip "knowing" about them all
396 wxComboBox *combo = wxDynamicCast(control, wxComboBox);
397 if ( combo )
398 {
399 WXHWND hwndComboEdit = combo->GetWindowStyle() & wxCB_READONLY
400 ? combo->GetHWND()
401 : combo->GetEditHWND();
402 if ( hwndComboEdit )
403 {
404 Add(hwndComboEdit);
405 }
406 }
407 #endif // !defined(__WXUNIVERSAL__)
408 }
409
410 void wxToolTip::SetTip(const wxString& tip)
411 {
412 m_text = tip;
413
414 if ( m_window )
415 {
416 // update the tip text shown by the control
417 wxToolInfo ti(GetHwndOf(m_window));
418 ti.lpszText = (wxChar *)m_text.c_str();
419
420 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
421 }
422 }
423
424 #endif // wxUSE_TOOLTIPS