]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/tooltip.cpp
don't call TB_GETITEMRECT with invalid tool index
[wxWidgets.git] / src / mac / carbon / tooltip.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/tooltip.cpp
3 // Purpose: wxToolTip implementation
4 // Author: Stefan Csomor
5 // Id: $Id$
6 // Copyright: (c) Stefan Csomor
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #if wxUSE_TOOLTIPS
13
14 #include "wx/tooltip.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/window.h"
19 #include "wx/dc.h"
20 #include "wx/timer.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/geometry.h"
24 #include "wx/mac/uma.h"
25
26 //-----------------------------------------------------------------------------
27 // global data
28 //-----------------------------------------------------------------------------
29
30 #if wxUSE_TIMER
31 class wxMacToolTipTimer : public wxTimer
32 {
33 public:
34 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
35 wxMacToolTipTimer() {} ;
36 virtual ~wxMacToolTipTimer() {} ;
37
38 void Notify()
39 {
40 if ( m_mark == m_tip->GetMark() )
41 m_tip->Draw() ;
42 }
43
44 protected:
45 wxMacToolTip* m_tip;
46 long m_mark ;
47 };
48 #endif // wxUSE_TIMER
49
50 //-----------------------------------------------------------------------------
51 // wxToolTip
52 //-----------------------------------------------------------------------------
53 static long s_ToolTipDelay = 500 ;
54 static bool s_ShowToolTips = true ;
55 static wxMacToolTip s_ToolTip ;
56 static wxWindow* s_LastWindowEntered = NULL ;
57 static wxRect2DInt s_ToolTipArea ;
58 static WindowRef s_ToolTipWindowRef = NULL ;
59
60 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
61
62
63 wxToolTip::wxToolTip( const wxString &tip )
64 {
65 m_text = tip;
66 m_window = (wxWindow*) NULL;
67 }
68
69 wxToolTip::~wxToolTip()
70 {
71 }
72
73 void wxToolTip::SetTip( const wxString &tip )
74 {
75 m_text = tip;
76
77 if ( m_window )
78 {
79 #if 0
80 // update it immediately
81 wxToolInfo ti(GetHwndOf(m_window));
82 ti.lpszText = (wxChar *)m_text.c_str();
83
84 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
85 #endif
86 }
87 }
88
89 void wxToolTip::SetWindow( wxWindow *win )
90 {
91 m_window = win ;
92 }
93
94 void wxToolTip::Enable( bool flag )
95 {
96 if ( s_ShowToolTips != flag )
97 {
98 s_ShowToolTips = flag ;
99
100 if ( s_ShowToolTips )
101 {
102 }
103 else
104 {
105 s_ToolTip.Clear() ;
106 }
107 }
108 }
109
110 void wxToolTip::SetDelay( long msecs )
111 {
112 s_ToolTipDelay = msecs ;
113 }
114
115 void wxToolTip::SetAutoPop( long WXUNUSED(msecs) )
116 {
117 }
118
119 void wxToolTip::SetReshow( long WXUNUSED(msecs) )
120 {
121 }
122
123 void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
124 {
125 if ( s_ShowToolTips )
126 {
127 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
128 {
129 s_ToolTip.Clear() ;
130 }
131 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
132 {
133 wxPoint2DInt where( event.m_x , event.m_y ) ;
134 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
135 {
136 }
137 else
138 {
139 s_ToolTip.Clear() ;
140 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
141 s_LastWindowEntered = win ;
142
143 WindowRef window = MAC_WXHWND( win->MacGetTopLevelWindowRef() ) ;
144 int x = event.m_x ;
145 int y = event.m_y ;
146 wxPoint local( x , y ) ;
147 win->MacClientToRootWindow( &x, &y ) ;
148 wxPoint windowlocal( x , y ) ;
149 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
150 }
151 }
152 }
153 }
154
155 void wxToolTip::RemoveToolTips()
156 {
157 s_ToolTip.Clear() ;
158 }
159
160 // --- mac specific
161 #if wxUSE_TIMER
162 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
163 {
164 m_tip = tip;
165 m_mark = tip->GetMark() ;
166 Start(msec, true);
167 }
168 #endif // wxUSE_TIMER
169
170 wxMacToolTip::wxMacToolTip()
171 {
172 m_window = NULL ;
173 m_backpict = NULL ;
174 #if wxUSE_TIMER
175 m_timer = NULL ;
176 #endif
177 m_mark = 0 ;
178 m_shown = false ;
179 }
180
181 void wxMacToolTip::Setup( WindowRef win , const wxString& text , const wxPoint& localPosition )
182 {
183 m_mark++ ;
184
185 Clear() ;
186 m_position = localPosition ;
187 m_label = text ;
188 m_window =win;
189 s_ToolTipWindowRef = m_window ;
190 m_backpict = NULL ;
191 #if wxUSE_TIMER
192 if ( m_timer )
193 delete m_timer ;
194
195 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
196 #endif // wxUSE_TIMER
197 }
198
199 wxMacToolTip::~wxMacToolTip()
200 {
201 #if wxUSE_TIMER
202 if ( m_timer )
203 {
204 delete m_timer ;
205 m_timer = NULL;
206 }
207 #endif // wxUSE_TIMER
208 if ( m_backpict )
209 Clear() ;
210 }
211
212 const short kTipBorder = 2 ;
213 const short kTipOffset = 5 ;
214
215 void wxMacToolTip::Draw()
216 {
217 if ( m_label.empty() )
218 return ;
219
220 if ( m_window == s_ToolTipWindowRef )
221 {
222 m_shown = true ;
223
224 HMHelpContentRec tag ;
225 tag.version = kMacHelpVersion;
226
227 Point p = { m_position.y , m_position.x };
228 wxMacLocalToGlobal( m_window , &p ) ;
229 SetRect( &tag.absHotRect , p.h - 2 , p.v - 2 , p.h + 2 , p.v + 2 );
230
231 m_helpTextRef = wxCFStringRef( m_label , wxFONTENCODING_DEFAULT ) ;
232 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
233 tag.content[kHMMinimumContentIndex].u.tagCFString = m_helpTextRef ;
234 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
235 tag.content[kHMMaximumContentIndex].u.tagCFString = m_helpTextRef ;
236 tag.tagSide = kHMDefaultSide;
237 HMDisplayTag( &tag );
238 }
239 }
240
241 void wxToolTip::NotifyWindowDelete( WXHWND win )
242 {
243 if ( win == s_ToolTipWindowRef )
244 s_ToolTipWindowRef = NULL ;
245 }
246
247 void wxMacToolTip::Clear()
248 {
249 m_mark++ ;
250 #if wxUSE_TIMER
251 if ( m_timer )
252 {
253 delete m_timer ;
254 m_timer = NULL ;
255 }
256 #endif // wxUSE_TIMER
257 if ( !m_shown )
258 return ;
259
260 HMHideTag() ;
261 }
262
263 #endif // wxUSE_TOOLTIPS