]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/tooltip.cpp
Merge new item attributes if any are already existing
[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 class wxMacToolTipTimer ;
31
32 class wxMacToolTip
33 {
34 public :
35 wxMacToolTip() ;
36 ~wxMacToolTip() ;
37
38 void Setup( WindowRef window , const wxString& text , const wxPoint& localPosition ) ;
39 void Draw() ;
40 void Clear() ;
41
42 long GetMark()
43 { return m_mark ; }
44
45 bool IsShown()
46 { return m_shown ; }
47
48 private :
49 wxString m_label ;
50 wxPoint m_position ;
51 Rect m_rect ;
52 WindowRef m_window ;
53 PicHandle m_backpict ;
54 bool m_shown ;
55 long m_mark ;
56 wxMacToolTipTimer* m_timer ;
57
58 #if TARGET_CARBON
59 wxMacCFStringHolder m_helpTextRef ;
60 #endif
61 } ;
62
63 class wxMacToolTipTimer : public wxTimer
64 {
65 public:
66 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
67 wxMacToolTipTimer() {} ;
68 virtual ~wxMacToolTipTimer() {} ;
69
70 void Notify()
71 {
72 if ( m_mark == m_tip->GetMark() )
73 m_tip->Draw() ;
74 }
75
76 protected:
77 wxMacToolTip* m_tip;
78 long m_mark ;
79 };
80
81 //-----------------------------------------------------------------------------
82 // wxToolTip
83 //-----------------------------------------------------------------------------
84 static long s_ToolTipDelay = 500 ;
85 static bool s_ShowToolTips = true ;
86 static wxMacToolTip s_ToolTip ;
87 static wxWindow* s_LastWindowEntered = NULL ;
88 static wxRect2DInt s_ToolTipArea ;
89 static WindowRef s_ToolTipWindowRef = NULL ;
90
91 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
92
93
94 wxToolTip::wxToolTip( const wxString &tip )
95 {
96 m_text = tip;
97 m_window = (wxWindow*) NULL;
98 }
99
100 wxToolTip::~wxToolTip()
101 {
102 }
103
104 void wxToolTip::SetTip( const wxString &tip )
105 {
106 m_text = tip;
107
108 if ( m_window )
109 {
110 #if 0
111 // update it immediately
112 wxToolInfo ti(GetHwndOf(m_window));
113 ti.lpszText = (wxChar *)m_text.c_str();
114
115 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
116 #endif
117 }
118 }
119
120 void wxToolTip::SetWindow( wxWindow *win )
121 {
122 m_window = win ;
123 }
124
125 void wxToolTip::Enable( bool flag )
126 {
127 if ( s_ShowToolTips != flag )
128 {
129 s_ShowToolTips = flag ;
130
131 if ( s_ShowToolTips )
132 {
133 }
134 else
135 {
136 s_ToolTip.Clear() ;
137 }
138 }
139 }
140
141 void wxToolTip::SetDelay( long msecs )
142 {
143 s_ToolTipDelay = msecs ;
144 }
145
146 void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
147 {
148 if ( s_ShowToolTips )
149 {
150 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
151 {
152 s_ToolTip.Clear() ;
153 }
154 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
155 {
156 wxPoint2DInt where( event.m_x , event.m_y ) ;
157 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
158 {
159 }
160 else
161 {
162 s_ToolTip.Clear() ;
163 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
164 s_LastWindowEntered = win ;
165
166 WindowRef window = MAC_WXHWND( win->MacGetTopLevelWindowRef() ) ;
167 int x = event.m_x ;
168 int y = event.m_y ;
169 wxPoint local( x , y ) ;
170 win->MacClientToRootWindow( &x, &y ) ;
171 wxPoint windowlocal( x , y ) ;
172 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
173 }
174 }
175 }
176 }
177
178 void wxToolTip::RemoveToolTips()
179 {
180 s_ToolTip.Clear() ;
181 }
182
183 // --- mac specific
184
185 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
186 {
187 m_tip = tip;
188 m_mark = tip->GetMark() ;
189 Start(msec, true);
190 }
191
192 wxMacToolTip::wxMacToolTip()
193 {
194 m_window = NULL ;
195 m_backpict = NULL ;
196 m_timer = NULL ;
197 m_mark = 0 ;
198 m_shown = false ;
199 }
200
201 void wxMacToolTip::Setup( WindowRef win , const wxString& text , const wxPoint& localPosition )
202 {
203 m_mark++ ;
204
205 Clear() ;
206 m_position = localPosition ;
207 m_label = text ;
208 m_window =win;
209 s_ToolTipWindowRef = m_window ;
210 m_backpict = NULL ;
211
212 if ( m_timer )
213 delete m_timer ;
214
215 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
216 }
217
218 wxMacToolTip::~wxMacToolTip()
219 {
220 if ( m_timer )
221 {
222 delete m_timer ;
223 m_timer = NULL;
224 }
225
226 if ( m_backpict )
227 Clear() ;
228 }
229
230 const short kTipBorder = 2 ;
231 const short kTipOffset = 5 ;
232
233 void wxMacToolTip::Draw()
234 {
235 if ( m_label.empty() )
236 return ;
237
238 if ( m_window == s_ToolTipWindowRef )
239 {
240 m_shown = true ;
241
242 HMHelpContentRec tag ;
243 tag.version = kMacHelpVersion;
244
245 Point p = { m_position.y , m_position.x };
246 wxMacLocalToGlobal( m_window , &p ) ;
247 SetRect( &tag.absHotRect , p.h - 2 , p.v - 2 , p.h + 2 , p.v + 2 );
248
249 m_helpTextRef.Assign( m_label , wxFONTENCODING_DEFAULT ) ;
250 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
251 tag.content[kHMMinimumContentIndex].u.tagCFString = m_helpTextRef ;
252 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
253 tag.content[kHMMaximumContentIndex].u.tagCFString = m_helpTextRef ;
254 tag.tagSide = kHMDefaultSide;
255 HMDisplayTag( &tag );
256 }
257 }
258
259 void wxToolTip::NotifyWindowDelete( WXHWND win )
260 {
261 if ( win == s_ToolTipWindowRef )
262 s_ToolTipWindowRef = NULL ;
263 }
264
265 void wxMacToolTip::Clear()
266 {
267 m_mark++ ;
268
269 if ( m_timer )
270 {
271 delete m_timer ;
272 m_timer = NULL ;
273 }
274
275 if ( !m_shown )
276 return ;
277
278 HMHideTag() ;
279 m_helpTextRef.Release() ;
280 }
281
282 #endif // wxUSE_TOOLTIPS