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