Define _CRT_NONSTDC_NO_WARNINGS for zlib compilation with MSVC.
[wxWidgets.git] / src / osx / carbon / tooltip.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/tooltip.cpp
3 // Purpose: wxToolTip implementation
4 // Author: Stefan Csomor
5 // Copyright: (c) Stefan Csomor
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #include "wx/wxprec.h"
10
11 #if wxUSE_TOOLTIPS
12
13 #include "wx/tooltip.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/window.h"
18 #include "wx/dc.h"
19 #include "wx/timer.h"
20 #include "wx/nonownedwnd.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/geometry.h"
24 #include "wx/osx/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 = 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 delete m_timer ;
193
194 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
195 #endif // wxUSE_TIMER
196 }
197
198 wxMacToolTip::~wxMacToolTip()
199 {
200 #if wxUSE_TIMER
201 wxDELETE(m_timer);
202 #endif // wxUSE_TIMER
203 if ( m_backpict )
204 Clear() ;
205 }
206
207 const short kTipBorder = 2 ;
208 const short kTipOffset = 5 ;
209
210 void wxMacToolTip::Draw()
211 {
212 if ( m_label.empty() )
213 return ;
214
215 if ( m_window == s_ToolTipWindowRef )
216 {
217 m_shown = true ;
218
219 HMHelpContentRec tag ;
220 tag.version = kMacHelpVersion;
221
222 int x = m_position.x;
223 int y = m_position.y;
224 wxNonOwnedWindow* tlw = wxNonOwnedWindow::GetFromWXWindow((WXWindow) m_window);
225 if ( tlw )
226 tlw->GetNonOwnedPeer()->WindowToScreen( &x, &y );
227 SetRect( &tag.absHotRect , x - 2 , y - 2 , x + 2 , y + 2 );
228
229 m_helpTextRef = wxCFStringRef( m_label , wxFONTENCODING_DEFAULT ) ;
230 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
231 tag.content[kHMMinimumContentIndex].u.tagCFString = m_helpTextRef ;
232 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
233 tag.content[kHMMaximumContentIndex].u.tagCFString = m_helpTextRef ;
234 tag.tagSide = kHMDefaultSide;
235 HMDisplayTag( &tag );
236 }
237 }
238
239 void wxToolTip::NotifyWindowDelete( WXHWND win )
240 {
241 if ( win == s_ToolTipWindowRef )
242 s_ToolTipWindowRef = NULL ;
243 }
244
245 void wxMacToolTip::Clear()
246 {
247 m_mark++ ;
248 #if wxUSE_TIMER
249 wxDELETE(m_timer);
250 #endif // wxUSE_TIMER
251 if ( !m_shown )
252 return ;
253
254 HMHideTag() ;
255 }
256
257 #endif // wxUSE_TOOLTIPS