1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolTip implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "tooltip.h"
20 #include "wx/window.h"
21 #include "wx/tooltip.h"
23 #include "wx/geometry.h"
24 #include "wx/mac/uma.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 class wxMacToolTipTimer
;
38 void Setup( WindowRef window
, wxString text
, wxPoint localPosition
) ;
39 long GetMark() { return m_mark
; }
42 bool IsShown() { return m_shown
; }
49 PicHandle m_backpict
;
52 wxMacToolTipTimer
* m_timer
;
54 CFStringRef m_helpTextRef
;
58 class wxMacToolTipTimer
: public wxTimer
61 wxMacToolTipTimer() {} ;
62 wxMacToolTipTimer(wxMacToolTip
* tip
, int iMilliseconds
) ;
63 virtual ~wxMacToolTipTimer() {} ;
66 if ( m_mark
== m_tip
->GetMark() )
74 //-----------------------------------------------------------------------------
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
;
84 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
86 wxToolTip::wxToolTip( const wxString
&tip
)
89 m_window
= (wxWindow
*) NULL
;
92 wxToolTip::~wxToolTip()
96 void wxToolTip::SetTip( const wxString
&tip
)
103 // update it immediately
104 wxToolInfo ti(GetHwndOf(m_window));
105 ti.lpszText = (wxChar *)m_text.c_str();
107 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
112 void wxToolTip::SetWindow( wxWindow
*win
)
117 void wxToolTip::Enable( bool flag
)
119 if ( s_ShowToolTips
!= flag
)
121 s_ShowToolTips
= flag
;
122 if ( s_ShowToolTips
)
132 void wxToolTip::SetDelay( long msecs
)
134 s_ToolTipDelay
= msecs
;
137 void wxToolTip::RelayEvent( wxWindow
*win
, wxMouseEvent
&event
)
139 if ( s_ShowToolTips
)
141 if ( event
.GetEventType() == wxEVT_LEAVE_WINDOW
)
145 else if (event
.GetEventType() == wxEVT_ENTER_WINDOW
|| event
.GetEventType() == wxEVT_MOTION
)
147 wxPoint2DInt
where( event
.m_x
, event
.m_y
) ;
148 if ( s_LastWindowEntered
== win
&& s_ToolTipArea
.Contains( where
) )
154 s_ToolTipArea
= wxRect2DInt( event
.m_x
- 2 , event
.m_y
- 2 , 4 , 4 ) ;
155 s_LastWindowEntered
= win
;
157 WindowRef window
= MAC_WXHWND( win
->MacGetRootWindow() ) ;
160 wxPoint
local( x
, y
) ;
161 win
->MacClientToRootWindow( &x
, &y
) ;
162 wxPoint
windowlocal( x
, y
) ;
163 s_ToolTip
.Setup( window
, win
->MacGetToolTipString( local
) , windowlocal
) ;
169 void wxToolTip::RemoveToolTips()
175 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip
*tip
, int msec
)
178 m_mark
= tip
->GetMark() ;
182 wxMacToolTip::wxMacToolTip()
189 m_helpTextRef
= NULL
;
192 void wxMacToolTip::Setup( WindowRef win
, wxString text
, wxPoint localPosition
)
196 m_position
= localPosition
;
197 if( wxApp::s_macDefaultEncodingIsPC
)
198 m_label
= wxMacMakeMacStringFromPC( text
) ;
202 s_ToolTipWindowRef
= m_window
;
206 m_timer
= new wxMacToolTipTimer( this , s_ToolTipDelay
) ;
209 wxMacToolTip::~wxMacToolTip()
219 const short kTipBorder
= 2 ;
220 const short kTipOffset
= 5 ;
222 void wxMacToolTip::Draw()
224 if ( m_label
.Length() == 0 )
227 if ( m_window
== s_ToolTipWindowRef
)
231 if ( HMDisplayTag
!= (void*) kUnresolvedCFragSymbolAddress
)
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 ) ;
238 SetPortWindowPort(m_window
) ;
239 LocalToGlobal( (Point
*) &tag
.absHotRect
.top
);
240 LocalToGlobal( (Point
*) &tag
.absHotRect
.bottom
);
244 CFRelease( m_helpTextRef
) ;
245 m_helpTextRef
= NULL
;
247 m_helpTextRef
= wxMacCreateCFString(m_label
) ;
248 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
249 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= m_helpTextRef
;
250 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
251 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= m_helpTextRef
;
252 tag
.tagSide
= kHMDefaultSide
;
253 HMDisplayTag( &tag
);
258 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( m_window
) );
260 bool useDrawThemeText
= ( DrawThemeTextBox
!= (void*) kUnresolvedCFragSymbolAddress
) ;
263 FontFamilyID fontId
;
267 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
268 GetFNum( fontName
, &fontId
);
271 TextSize( fontSize
) ;
272 TextFace( fontStyle
) ;
274 ::GetFontInfo(&fontInfo
);
275 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
277 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
280 int length
= m_label
.Length() ;
284 const char *text
= m_label
;
287 if( text
[i
] == 13 || text
[i
] == 10)
289 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
290 if ( thiswidth
> width
)
298 if ( i
- laststop
> 0 )
300 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
301 if ( thiswidth
> width
)
307 m_rect
.left
= m_position
.x
+ kTipOffset
;
308 m_rect
.top
= m_position
.y
+ kTipOffset
;
309 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
311 if ( useDrawThemeText
)
312 m_rect
.right
+= kTipBorder
;
314 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
316 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
317 if ( m_rect
.top
< 0 )
319 m_rect
.bottom
+= -m_rect
.top
;
322 if ( m_rect
.left
< 0 )
324 m_rect
.right
+= -m_rect
.left
;
327 if ( m_rect
.right
> r
.right
)
329 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
330 m_rect
.right
= r
.right
;
332 if ( m_rect
.bottom
> r
.bottom
)
334 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
335 m_rect
.bottom
= r
.bottom
;
337 ClipRect( &m_rect
) ;
338 BackColor( whiteColor
) ;
339 ForeColor(blackColor
) ;
341 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
343 GDHandle origDevice
;
345 GetGWorld( &origPort
, &origDevice
) ;
346 SetGWorld( port
, NULL
) ;
348 m_backpict
= OpenPicture(&m_rect
);
350 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
351 GetPortBitMapForCopyBits(port
),
357 SetGWorld( origPort
, origDevice
) ;
358 DisposeGWorld( port
) ;
361 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
362 BackColor( whiteColor
) ;
363 RGBForeColor( &tooltipbackground
) ;
365 PaintRect( &m_rect
) ;
366 ForeColor(blackColor
) ;
367 FrameRect( &m_rect
) ;
368 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
369 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
377 if( text
[i
] == 13 || text
[i
] == 10)
380 if ( useDrawThemeText
)
383 frame
.top
= m_rect
.top
+ kTipBorder
+ height
;
384 frame
.left
= m_rect
.left
+ kTipBorder
;
385 frame
.bottom
= frame
.top
+ 1000 ;
386 frame
.right
= frame
.left
+ 1000 ;
387 CFStringRef mString
= CFStringCreateWithBytes( NULL
, (UInt8
*) text
+ laststop
, i
- laststop
, CFStringGetSystemEncoding(), false ) ;
388 ::DrawThemeTextBox( mString
,
389 kThemeCurrentPortFont
,
395 CFRelease( mString
) ;
401 ::DrawText( text
, laststop
, i
- laststop
) ;
403 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
410 if ( useDrawThemeText
)
413 frame
.top
= m_rect
.top
+ kTipBorder
+ height
;
414 frame
.left
= m_rect
.left
+ kTipBorder
;
415 frame
.bottom
= frame
.top
+ 1000 ;
416 frame
.right
= frame
.left
+ 1000 ;
417 CFStringRef mString
= CFStringCreateWithCString( NULL
, text
+ laststop
, kCFStringEncodingMacRoman
) ;
418 ::DrawThemeTextBox( mString
,
419 kThemeCurrentPortFont
,
425 CFRelease( mString
) ;
430 ::DrawText( text
, laststop
, i
- laststop
) ;
432 ::TextMode( srcOr
) ;
437 void wxToolTip::NotifyWindowDelete( WXHWND win
)
439 if ( win
== s_ToolTipWindowRef
)
441 s_ToolTipWindowRef
= NULL
;
445 void wxMacToolTip::Clear()
459 CFRelease( m_helpTextRef
) ;
460 m_helpTextRef
= NULL
;
463 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
465 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(m_window
) ) ;
469 BackColor( whiteColor
) ;
470 ForeColor(blackColor
) ;
471 DrawPicture(m_backpict
, &m_rect
);
472 KillPicture(m_backpict
);