]> git.saurik.com Git - wxWidgets.git/blob - src/mac/tooltip.cpp
crash upon hide fix
[wxWidgets.git] / src / mac / tooltip.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tooltip.cpp
3 // Purpose: wxToolTip implementation
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "tooltip.h"
12 #endif
13
14 #include "wx/defs.h"
15
16 #if wxUSE_TOOLTIPS
17
18 #include "wx/app.h"
19 #include "wx/dc.h"
20 #include "wx/window.h"
21 #include "wx/tooltip.h"
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 , wxString text , wxPoint localPosition ) ;
39 long GetMark() { return m_mark ; }
40 void Draw() ;
41 void Clear() ;
42 bool IsShown() { return m_shown ; }
43 private :
44
45 wxString m_label ;
46 wxPoint m_position ;
47 Rect m_rect ;
48 WindowRef m_window ;
49 PicHandle m_backpict ;
50 bool m_shown ;
51 long m_mark ;
52 wxMacToolTipTimer* m_timer ;
53 #ifdef TARGET_CARBON
54 CFStringRef m_helpTextRef ;
55 #endif
56 } ;
57
58 class wxMacToolTipTimer : public wxTimer
59 {
60 public:
61 wxMacToolTipTimer() {} ;
62 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
63 virtual ~wxMacToolTipTimer() {} ;
64 void Notify()
65 {
66 if ( m_mark == m_tip->GetMark() )
67 m_tip->Draw() ;
68 }
69 protected:
70 wxMacToolTip* m_tip;
71 long m_mark ;
72 };
73
74 //-----------------------------------------------------------------------------
75 // wxToolTip
76 //-----------------------------------------------------------------------------
77 static long s_ToolTipDelay = 500 ;
78 static bool s_ShowToolTips = true ;
79 static wxMacToolTip s_ToolTip ;
80 static wxWindow* s_LastWindowEntered = NULL ;
81 static wxRect2DInt s_ToolTipArea ;
82 static WindowRef s_ToolTipWindowRef = NULL ;
83
84 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
85
86 wxToolTip::wxToolTip( const wxString &tip )
87 {
88 m_text = tip;
89 m_window = (wxWindow*) NULL;
90 }
91
92 wxToolTip::~wxToolTip()
93 {
94 }
95
96 void wxToolTip::SetTip( const wxString &tip )
97 {
98 m_text = tip;
99
100 if ( m_window )
101 {
102 /*
103 // update it immediately
104 wxToolInfo ti(GetHwndOf(m_window));
105 ti.lpszText = (wxChar *)m_text.c_str();
106
107 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
108 */
109 }
110 }
111
112 void wxToolTip::SetWindow( wxWindow *win )
113 {
114 m_window = win ;
115 }
116
117 void wxToolTip::Enable( bool flag )
118 {
119 if ( s_ShowToolTips != flag )
120 {
121 s_ShowToolTips = flag ;
122 if ( s_ShowToolTips )
123 {
124 }
125 else
126 {
127 s_ToolTip.Clear() ;
128 }
129 }
130 }
131
132 void wxToolTip::SetDelay( long msecs )
133 {
134 s_ToolTipDelay = msecs ;
135 }
136
137 void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
138 {
139 if ( s_ShowToolTips )
140 {
141 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
142 {
143 s_ToolTip.Clear() ;
144 }
145 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
146 {
147 wxPoint2DInt where( event.m_x , event.m_y ) ;
148 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
149 {
150 }
151 else
152 {
153 s_ToolTip.Clear() ;
154 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
155 s_LastWindowEntered = win ;
156
157 WindowRef window = MAC_WXHWND( win->MacGetRootWindow() ) ;
158 int x = event.m_x ;
159 int y = event.m_y ;
160 wxPoint local( x , y ) ;
161 win->MacClientToRootWindow( &x, &y ) ;
162 wxPoint windowlocal( x , y ) ;
163 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
164 }
165 }
166 }
167 }
168
169 void wxToolTip::RemoveToolTips()
170 {
171 s_ToolTip.Clear() ;
172 }
173 // --- mac specific
174
175 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
176 {
177 m_tip = tip;
178 m_mark = tip->GetMark() ;
179 Start(msec, true);
180 }
181
182 wxMacToolTip::wxMacToolTip()
183 {
184 m_window = NULL ;
185 m_backpict = NULL ;
186 m_mark = 0 ;
187 m_shown = false ;
188 m_timer = NULL ;
189 m_helpTextRef = NULL ;
190 }
191
192 void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition )
193 {
194 m_mark++ ;
195 Clear() ;
196 m_position = localPosition ;
197 if( wxApp::s_macDefaultEncodingIsPC )
198 m_label = wxMacMakeMacStringFromPC( text ) ;
199 else
200 m_label = text ;
201 m_window =win;
202 s_ToolTipWindowRef = m_window ;
203 m_backpict = NULL ;
204 if ( m_timer )
205 delete m_timer ;
206 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
207 }
208
209 wxMacToolTip::~wxMacToolTip()
210 {
211 if ( m_timer ) {
212 delete m_timer ;
213 m_timer = NULL;
214 }
215 if ( m_backpict )
216 Clear() ;
217 }
218
219 const short kTipBorder = 2 ;
220 const short kTipOffset = 5 ;
221
222 void wxMacToolTip::Draw()
223 {
224 if ( m_label.Length() == 0 )
225 return ;
226
227 if ( m_window == s_ToolTipWindowRef )
228 {
229 m_shown = true ;
230 #if TARGET_CARBON
231 if ( HMDisplayTag != (void*) kUnresolvedCFragSymbolAddress )
232 {
233 HMHelpContentRec tag ;
234 tag.version = kMacHelpVersion;
235 SetRect( &tag.absHotRect , m_position.x - 2 , m_position.y - 2 , m_position.x + 2 , m_position.y + 2 ) ;
236 GrafPtr port ;
237 GetPort( &port ) ;
238 SetPortWindowPort(m_window) ;
239 LocalToGlobal( (Point *) &tag.absHotRect.top );
240 LocalToGlobal( (Point *) &tag.absHotRect.bottom );
241 SetPort( port );
242 if( m_helpTextRef )
243 CFRelease( m_helpTextRef )
244 m_helpTextRef = wxMacCreateCFString(m_label) ;
245 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
246 tag.content[kHMMinimumContentIndex].u.tagCFString = text ;
247 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
248 tag.content[kHMMaximumContentIndex].u.tagCFString = text ;
249 tag.tagSide = kHMDefaultSide;
250 HMDisplayTag( &tag );
251 }
252 else
253 #endif
254 {
255 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
256 #if TARGET_CARBON
257 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
258 #endif
259
260 FontFamilyID fontId ;
261 Str255 fontName ;
262 SInt16 fontSize ;
263 Style fontStyle ;
264 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
265 GetFNum( fontName, &fontId );
266
267 TextFont( fontId ) ;
268 TextSize( fontSize ) ;
269 TextFace( fontStyle ) ;
270 FontInfo fontInfo;
271 ::GetFontInfo(&fontInfo);
272 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
273 short height = 0 ;
274 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
275
276 int i = 0 ;
277 int length = m_label.Length() ;
278 int width = 0 ;
279 int thiswidth = 0 ;
280 int laststop = 0 ;
281 const char *text = m_label ;
282 while( i < length )
283 {
284 if( text[i] == 13 || text[i] == 10)
285 {
286 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
287 if ( thiswidth > width )
288 width = thiswidth ;
289
290 height += lineh ;
291 laststop = i+1 ;
292 }
293 i++ ;
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
304 m_rect.left = m_position.x + kTipOffset;
305 m_rect.top = m_position.y + kTipOffset;
306 m_rect.right = m_rect.left + width + 2 * kTipBorder;
307 #if TARGET_CARBON
308 if ( useDrawThemeText )
309 m_rect.right += kTipBorder ;
310 #endif
311 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
312 Rect r ;
313 GetPortBounds( GetWindowPort( m_window ) , &r ) ;
314 if ( m_rect.top < 0 )
315 {
316 m_rect.bottom += -m_rect.top ;
317 m_rect.top = 0 ;
318 }
319 if ( m_rect.left < 0 )
320 {
321 m_rect.right += -m_rect.left ;
322 m_rect.left = 0 ;
323 }
324 if ( m_rect.right > r.right )
325 {
326 m_rect.left -= (m_rect.right - r.right ) ;
327 m_rect.right = r.right ;
328 }
329 if ( m_rect.bottom > r.bottom )
330 {
331 m_rect.top -= (m_rect.bottom - r.bottom) ;
332 m_rect.bottom = r.bottom ;
333 }
334 ClipRect( &m_rect ) ;
335 BackColor( whiteColor ) ;
336 ForeColor(blackColor ) ;
337 GWorldPtr port ;
338 NewGWorld( &port , wxDisplayDepth() , &m_rect , NULL , NULL , 0 ) ;
339 CGrafPtr origPort ;
340 GDHandle origDevice ;
341
342 GetGWorld( &origPort , &origDevice ) ;
343 SetGWorld( port , NULL ) ;
344
345 m_backpict = OpenPicture(&m_rect);
346
347 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
348 GetPortBitMapForCopyBits(port),
349 &m_rect,
350 &m_rect,
351 srcCopy,
352 NULL);
353 ClosePicture();
354 SetGWorld( origPort , origDevice ) ;
355 DisposeGWorld( port ) ;
356 PenNormal() ;
357
358 RGBColor tooltipbackground = { 0xFFFF , 0xFFFF , 0xC000 } ;
359 BackColor( whiteColor ) ;
360 RGBForeColor( &tooltipbackground ) ;
361
362 PaintRect( &m_rect ) ;
363 ForeColor(blackColor ) ;
364 FrameRect( &m_rect ) ;
365 SetThemeTextColor(kThemeTextColorNotification,wxDisplayDepth(),true) ;
366 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
367
368 i = 0 ;
369 laststop = 0 ;
370 height = 0 ;
371
372 while( i < length )
373 {
374 if( text[i] == 13 || text[i] == 10)
375 {
376 #if TARGET_CARBON
377 if ( useDrawThemeText )
378 {
379 Rect frame ;
380 frame.top = m_rect.top + kTipBorder + height ;
381 frame.left = m_rect.left + kTipBorder ;
382 frame.bottom = frame.top + 1000 ;
383 frame.right = frame.left + 1000 ;
384 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
385 ::DrawThemeTextBox( mString,
386 kThemeCurrentPortFont,
387 kThemeStateActive,
388 true,
389 &frame,
390 teJustLeft,
391 nil );
392 CFRelease( mString ) ;
393 height += lineh ;
394 }
395 else
396 #endif
397 {
398 ::DrawText( text , laststop , i - laststop ) ;
399 height += lineh ;
400 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
401 }
402 laststop = i+1 ;
403 }
404 i++ ;
405 }
406 #if TARGET_CARBON
407 if ( useDrawThemeText )
408 {
409 Rect frame ;
410 frame.top = m_rect.top + kTipBorder + height ;
411 frame.left = m_rect.left + kTipBorder ;
412 frame.bottom = frame.top + 1000 ;
413 frame.right = frame.left + 1000 ;
414 CFStringRef mString = CFStringCreateWithCString( NULL , text + laststop , kCFStringEncodingMacRoman ) ;
415 ::DrawThemeTextBox( mString,
416 kThemeCurrentPortFont,
417 kThemeStateActive,
418 true,
419 &frame,
420 teJustLeft,
421 nil );
422 CFRelease( mString ) ;
423 }
424 else
425 #endif
426 {
427 ::DrawText( text , laststop , i - laststop ) ;
428 }
429 ::TextMode( srcOr ) ;
430 }
431 }
432 }
433
434 void wxToolTip::NotifyWindowDelete( WXHWND win )
435 {
436 if ( win == s_ToolTipWindowRef )
437 {
438 s_ToolTipWindowRef = NULL ;
439 }
440 }
441
442 void wxMacToolTip::Clear()
443 {
444 m_mark++ ;
445 if ( m_timer )
446 {
447 delete m_timer ;
448 m_timer = NULL ;
449 }
450 if ( !m_shown )
451 return ;
452 #if TARGET_CARBON
453 HMHideTag() ;
454 if( m_helpTextRef )
455 CFRelease( m_helpTextRef )
456 #else
457 if ( m_window == s_ToolTipWindowRef && m_backpict )
458 {
459 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
460
461 m_shown = false ;
462
463 BackColor( whiteColor ) ;
464 ForeColor(blackColor ) ;
465 DrawPicture(m_backpict, &m_rect);
466 KillPicture(m_backpict);
467 m_backpict = NULL ;
468 }
469 #endif
470 }
471
472 #endif
473