]>
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 | #endif // WX_PRECOMP | |
21 | ||
22 | #include "wx/timer.h" | |
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 | #if TARGET_CARBON | |
243 | HMHelpContentRec tag ; | |
244 | tag.version = kMacHelpVersion; | |
245 | SetRect( &tag.absHotRect , m_position.x - 2 , m_position.y - 2 , m_position.x + 2 , m_position.y + 2 ) ; | |
246 | ||
247 | QDLocalToGlobalRect( GetWindowPort( m_window ) , &tag.absHotRect ) ; | |
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 | #else | |
257 | wxMacPortStateHelper help( (GrafPtr)GetWindowPort( m_window ) ); | |
258 | FontFamilyID fontId ; | |
259 | Str255 fontName ; | |
260 | SInt16 fontSize ; | |
261 | Style fontStyle ; | |
262 | GetThemeFont( kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ; | |
263 | GetFNum( fontName, &fontId ); | |
264 | ||
265 | TextFont( fontId ) ; | |
266 | TextSize( fontSize ) ; | |
267 | TextFace( fontStyle ) ; | |
268 | FontInfo fontInfo; | |
269 | ::GetFontInfo(&fontInfo); | |
270 | short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading; | |
271 | short height = 0 ; | |
272 | ||
273 | int i = 0 ; | |
274 | int length = m_label.length() ; | |
275 | int width = 0 ; | |
276 | int thiswidth = 0 ; | |
277 | int laststop = 0 ; | |
278 | wxCharBuffer text = m_label.mb_str( wxConvLocal ) ; | |
279 | ||
280 | while ( i < length ) | |
281 | { | |
282 | if ( text[i] == 13 || text[i] == 10) | |
283 | { | |
284 | thiswidth = ::TextWidth( text , laststop , i - laststop ) ; | |
285 | if ( thiswidth > width ) | |
286 | width = thiswidth ; | |
287 | ||
288 | height += lineh ; | |
289 | laststop = i + 1 ; | |
290 | } | |
291 | ||
292 | i++ ; | |
293 | } | |
294 | ||
295 | if ( i - laststop > 0 ) | |
296 | { | |
297 | thiswidth = ::TextWidth( text , laststop , i - laststop ) ; | |
298 | if ( thiswidth > width ) | |
299 | width = thiswidth ; | |
300 | height += lineh ; | |
301 | } | |
302 | ||
303 | m_rect.left = m_position.x + kTipOffset; | |
304 | m_rect.top = m_position.y + kTipOffset; | |
305 | m_rect.right = m_rect.left + width + 2 * kTipBorder; | |
306 | ||
307 | m_rect.bottom = m_rect.top + height + 2 * kTipBorder; | |
308 | Rect r ; | |
309 | ||
310 | GetPortBounds( GetWindowPort( m_window ) , &r ) ; | |
311 | if ( m_rect.top < 0 ) | |
312 | { | |
313 | m_rect.bottom += -m_rect.top ; | |
314 | m_rect.top = 0 ; | |
315 | } | |
316 | if ( m_rect.left < 0 ) | |
317 | { | |
318 | m_rect.right += -m_rect.left ; | |
319 | m_rect.left = 0 ; | |
320 | } | |
321 | if ( m_rect.right > r.right ) | |
322 | { | |
323 | m_rect.left -= (m_rect.right - r.right ) ; | |
324 | m_rect.right = r.right ; | |
325 | } | |
326 | if ( m_rect.bottom > r.bottom ) | |
327 | { | |
328 | m_rect.top -= (m_rect.bottom - r.bottom) ; | |
329 | m_rect.bottom = r.bottom ; | |
330 | } | |
331 | ||
332 | GWorldPtr port ; | |
333 | CGrafPtr origPort ; | |
334 | GDHandle origDevice ; | |
335 | ||
336 | ClipRect( &m_rect ) ; | |
337 | BackColor( whiteColor ) ; | |
338 | ForeColor(blackColor ) ; | |
339 | NewGWorld( &port , wxDisplayDepth() , &m_rect , NULL , NULL , 0 ) ; | |
340 | ||
341 | GetGWorld( &origPort , &origDevice ) ; | |
342 | SetGWorld( port , NULL ) ; | |
343 | ||
344 | m_backpict = OpenPicture(&m_rect); | |
345 | ||
346 | CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)), | |
347 | GetPortBitMapForCopyBits(port), | |
348 | &m_rect, | |
349 | &m_rect, | |
350 | srcCopy, | |
351 | NULL); | |
352 | ClosePicture(); | |
353 | SetGWorld( origPort , origDevice ) ; | |
354 | DisposeGWorld( port ) ; | |
355 | PenNormal() ; | |
356 | ||
357 | RGBColor tooltipbackground = { 0xFFFF , 0xFFFF , 0xC000 } ; | |
358 | BackColor( whiteColor ) ; | |
359 | RGBForeColor( &tooltipbackground ) ; | |
360 | ||
361 | PaintRect( &m_rect ) ; | |
362 | ForeColor(blackColor ) ; | |
363 | FrameRect( &m_rect ) ; | |
364 | SetThemeTextColor(kThemeTextColorNotification,wxDisplayDepth(),true) ; | |
365 | ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder); | |
366 | ||
367 | i = 0 ; | |
368 | laststop = 0 ; | |
369 | height = 0 ; | |
370 | ||
371 | while ( i < length ) | |
372 | { | |
373 | if ( text[i] == 13 || text[i] == 10) | |
374 | { | |
375 | ::DrawText( text , laststop , i - laststop ) ; | |
376 | height += lineh ; | |
377 | ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height ); | |
378 | laststop = i+1 ; | |
379 | } | |
380 | ||
381 | i++ ; | |
382 | } | |
383 | ||
384 | ::DrawText( text , laststop , i - laststop ) ; | |
385 | ::TextMode( srcOr ) ; | |
386 | #endif | |
387 | } | |
388 | } | |
389 | ||
390 | void wxToolTip::NotifyWindowDelete( WXHWND win ) | |
391 | { | |
392 | if ( win == s_ToolTipWindowRef ) | |
393 | s_ToolTipWindowRef = NULL ; | |
394 | } | |
395 | ||
396 | void wxMacToolTip::Clear() | |
397 | { | |
398 | m_mark++ ; | |
399 | ||
400 | if ( m_timer ) | |
401 | { | |
402 | delete m_timer ; | |
403 | m_timer = NULL ; | |
404 | } | |
405 | ||
406 | if ( !m_shown ) | |
407 | return ; | |
408 | ||
409 | #if TARGET_CARBON | |
410 | HMHideTag() ; | |
411 | m_helpTextRef.Release() ; | |
412 | #else | |
413 | if ( m_window == s_ToolTipWindowRef && m_backpict ) | |
414 | { | |
415 | wxMacPortStateHelper help( (GrafPtr)GetWindowPort( m_window ) ) ; | |
416 | ||
417 | m_shown = false ; | |
418 | ||
419 | BackColor( whiteColor ) ; | |
420 | ForeColor( blackColor ) ; | |
421 | DrawPicture( m_backpict, &m_rect ); | |
422 | KillPicture( m_backpict ); | |
423 | m_backpict = NULL ; | |
424 | } | |
425 | #endif | |
426 | } | |
427 | ||
428 | #endif |