]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/tooltip.cpp
Updated list of subprojects.
[wxWidgets.git] / src / mac / carbon / tooltip.cpp
... / ...
CommitLineData
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
30class wxMacToolTipTimer ;
31
32class wxMacToolTip
33{
34public :
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
48private :
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
63class wxMacToolTipTimer : public wxTimer
64{
65public:
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
76protected:
77 wxMacToolTip* m_tip;
78 long m_mark ;
79};
80
81//-----------------------------------------------------------------------------
82// wxToolTip
83//-----------------------------------------------------------------------------
84static long s_ToolTipDelay = 500 ;
85static bool s_ShowToolTips = true ;
86static wxMacToolTip s_ToolTip ;
87static wxWindow* s_LastWindowEntered = NULL ;
88static wxRect2DInt s_ToolTipArea ;
89static WindowRef s_ToolTipWindowRef = NULL ;
90
91IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
92
93
94wxToolTip::wxToolTip( const wxString &tip )
95{
96 m_text = tip;
97 m_window = (wxWindow*) NULL;
98}
99
100wxToolTip::~wxToolTip()
101{
102}
103
104void 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
120void wxToolTip::SetWindow( wxWindow *win )
121{
122 m_window = win ;
123}
124
125void 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
141void wxToolTip::SetDelay( long msecs )
142{
143 s_ToolTipDelay = msecs ;
144}
145
146void 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
178void wxToolTip::RemoveToolTips()
179{
180 s_ToolTip.Clear() ;
181}
182
183// --- mac specific
184
185wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
186{
187 m_tip = tip;
188 m_mark = tip->GetMark() ;
189 Start(msec, true);
190}
191
192wxMacToolTip::wxMacToolTip()
193{
194 m_window = NULL ;
195 m_backpict = NULL ;
196 m_timer = NULL ;
197 m_mark = 0 ;
198 m_shown = false ;
199}
200
201void 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
218wxMacToolTip::~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
230const short kTipBorder = 2 ;
231const short kTipOffset = 5 ;
232
233void 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
259void wxToolTip::NotifyWindowDelete( WXHWND win )
260{
261 if ( win == s_ToolTipWindowRef )
262 s_ToolTipWindowRef = NULL ;
263}
264
265void 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