1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/mac/classic/tooltip.cpp 
   3 // Purpose:     wxToolTip implementation 
   4 // Author:      Robert Roebling 
   6 // Copyright:   (c) 1998 Robert Roebling 
   7 // Licence:     wxWindows licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  10 #include "wx/wxprec.h" 
  18 #include "wx/tooltip.h" 
  22     #include "wx/window.h" 
  27 #include "wx/geometry.h" 
  28 #include "wx/mac/uma.h" 
  30 //----------------------------------------------------------------------------- 
  32 //----------------------------------------------------------------------------- 
  34 class wxMacToolTipTimer 
; 
  42         void            Setup( WindowRef window  
, const wxString
& text 
, wxPoint localPosition 
) ; 
  43         long            GetMark() { return m_mark 
; } 
  46         bool            IsShown() { return m_shown  
; } 
  53         PicHandle    m_backpict 
; 
  56         wxMacToolTipTimer
* m_timer 
; 
  58         wxMacCFStringHolder m_helpTextRef 
; 
  62 class wxMacToolTipTimer 
: public wxTimer
 
  65     wxMacToolTipTimer() {} ; 
  66     wxMacToolTipTimer(wxMacToolTip
* tip
, int iMilliseconds
) ; 
  67     virtual ~wxMacToolTipTimer() {} ; 
  70         if ( m_mark 
== m_tip
->GetMark() ) 
  78 //----------------------------------------------------------------------------- 
  80 //----------------------------------------------------------------------------- 
  81 static long s_ToolTipDelay 
= 500 ; 
  82 static bool s_ShowToolTips 
= true ; 
  83 static wxMacToolTip s_ToolTip 
; 
  84 static wxWindow
* s_LastWindowEntered 
= NULL 
; 
  85 static wxRect2DInt s_ToolTipArea 
; 
  86 static WindowRef s_ToolTipWindowRef 
= NULL 
; 
  88 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
) 
  90 wxToolTip::wxToolTip( const wxString 
&tip 
) 
  93     m_window 
= (wxWindow
*) NULL
; 
  96 wxToolTip::~wxToolTip() 
 100 void wxToolTip::SetTip( const wxString 
&tip 
) 
 107     // update it immediately 
 108     wxToolInfo ti(GetHwndOf(m_window)); 
 109     ti.lpszText = (wxChar *)m_text.c_str(); 
 111       (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti); 
 116 void wxToolTip::SetWindow( wxWindow 
*win 
) 
 121 void wxToolTip::Enable( bool flag 
) 
 123     if ( s_ShowToolTips 
!= flag 
) 
 125         s_ShowToolTips 
= flag 
; 
 126         if ( s_ShowToolTips 
) 
 136 void wxToolTip::SetDelay( long msecs 
) 
 138     s_ToolTipDelay 
= msecs 
; 
 141 void wxToolTip::RelayEvent( wxWindow 
*win 
, wxMouseEvent 
&event 
) 
 143     if ( s_ShowToolTips 
) 
 145         if ( event
.GetEventType() == wxEVT_LEAVE_WINDOW 
) 
 149         else if (event
.GetEventType() == wxEVT_ENTER_WINDOW 
|| event
.GetEventType() == wxEVT_MOTION 
) 
 151             wxPoint2DInt 
where( event
.m_x 
, event
.m_y 
) ; 
 152             if ( s_LastWindowEntered 
== win 
&& s_ToolTipArea
.Contains( where 
) ) 
 158                 s_ToolTipArea 
= wxRect2DInt( event
.m_x 
- 2 , event
.m_y 
- 2 , 4 , 4 ) ; 
 159                 s_LastWindowEntered 
= win 
; 
 161                 WindowRef window 
= MAC_WXHWND( win
->MacGetRootWindow() ) ; 
 164                 wxPoint 
local( x 
, y 
) ; 
 165                 win
->MacClientToRootWindow( &x
, &y 
) ; 
 166                 wxPoint 
windowlocal( x 
, y 
) ; 
 167                 s_ToolTip
.Setup( window 
, win
->MacGetToolTipString( local 
) , windowlocal 
) ; 
 173 void wxToolTip::RemoveToolTips() 
 179 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip 
*tip 
, int msec 
) 
 182     m_mark 
= tip
->GetMark() ; 
 186 wxMacToolTip::wxMacToolTip() 
 195 void wxMacToolTip::Setup( WindowRef win  
, const wxString
& text 
, wxPoint localPosition 
) 
 199     m_position 
= localPosition 
; 
 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
.empty() ) 
 227     if ( m_window 
== s_ToolTipWindowRef 
) 
 231         HMHelpContentRec tag 
; 
 232         tag
.version 
= kMacHelpVersion
; 
 233         SetRect( &tag
.absHotRect 
, m_position
.x 
- 2 , m_position
.y 
- 2 , m_position
.x 
+ 2 , m_position
.y 
+ 2 ) ; 
 236         SetPortWindowPort(m_window
) ; 
 237         LocalToGlobal( (Point 
*) &tag
.absHotRect
.top 
); 
 238         LocalToGlobal( (Point 
*) &tag
.absHotRect
.bottom 
); 
 240         m_helpTextRef
.Assign( m_label  
, wxFONTENCODING_DEFAULT 
) ; 
 241         tag
.content
[kHMMinimumContentIndex
].contentType 
= kHMCFStringContent 
; 
 242         tag
.content
[kHMMinimumContentIndex
].u
.tagCFString 
= m_helpTextRef 
; 
 243         tag
.content
[kHMMaximumContentIndex
].contentType 
= kHMCFStringContent 
; 
 244         tag
.content
[kHMMaximumContentIndex
].u
.tagCFString 
= m_helpTextRef 
; 
 245         tag
.tagSide 
= kHMDefaultSide
; 
 246         HMDisplayTag( &tag 
); 
 248         wxMacPortStateHelper 
help( (GrafPtr
) GetWindowPort( m_window 
) ); 
 249         FontFamilyID fontId 
; 
 253         GetThemeFont(kThemeSmallSystemFont 
, GetApplicationScript() , fontName 
, &fontSize 
, &fontStyle 
) ; 
 254         GetFNum( fontName
, &fontId 
); 
 257         TextSize( fontSize 
) ; 
 258         TextFace( fontStyle 
) ; 
 260         ::GetFontInfo(&fontInfo
); 
 261         short lineh 
= fontInfo
.ascent 
+ fontInfo
.descent 
+ fontInfo
.leading
; 
 265         int length 
= m_label
.length() ; 
 269         wxCharBuffer text 
= m_label
.mb_str( wxConvLocal
)  ; 
 273             if( text
[i
] == 13 || text
[i
] == 10) 
 275                 thiswidth 
= ::TextWidth( text 
, laststop 
, i 
- laststop 
) ; 
 276                 if ( thiswidth 
> width 
) 
 284         if ( i 
- laststop 
> 0 ) 
 286             thiswidth 
= ::TextWidth( text 
, laststop 
, i 
- laststop 
) ; 
 287             if ( thiswidth 
> width 
) 
 292         m_rect
.left 
= m_position
.x 
+ kTipOffset
; 
 293         m_rect
.top 
= m_position
.y 
+ kTipOffset
; 
 294         m_rect
.right 
= m_rect
.left 
+ width 
+ 2 * kTipBorder
; 
 296         m_rect
.bottom 
= m_rect
.top 
+ height 
+ 2 * kTipBorder
; 
 298         GetPortBounds( GetWindowPort( m_window 
) , &r 
) ; 
 299         if ( m_rect
.top 
< 0 ) 
 301             m_rect
.bottom 
+= -m_rect
.top 
; 
 304         if ( m_rect
.left 
< 0 ) 
 306             m_rect
.right 
+= -m_rect
.left 
; 
 309         if ( m_rect
.right 
> r
.right 
) 
 311             m_rect
.left 
-= (m_rect
.right 
- r
.right 
) ; 
 312             m_rect
.right 
= r
.right 
; 
 314         if ( m_rect
.bottom 
> r
.bottom 
) 
 316             m_rect
.top 
-= (m_rect
.bottom 
- r
.bottom
) ; 
 317             m_rect
.bottom 
= r
.bottom 
; 
 319         ClipRect( &m_rect 
) ; 
 320         BackColor( whiteColor 
) ; 
 321         ForeColor(blackColor 
) ; 
 323         NewGWorld( &port 
, wxDisplayDepth() , &m_rect 
, NULL 
, NULL 
, 0 ) ; 
 325         GDHandle    origDevice 
; 
 327         GetGWorld( &origPort 
, &origDevice 
) ; 
 328         SetGWorld( port 
, NULL 
) ; 
 330         m_backpict 
= OpenPicture(&m_rect
); 
 332         CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)), 
 333             GetPortBitMapForCopyBits(port
), 
 339         SetGWorld( origPort 
, origDevice 
) ; 
 340         DisposeGWorld( port 
) ; 
 343         RGBColor tooltipbackground 
= { 0xFFFF , 0xFFFF , 0xC000 } ; 
 344         BackColor( whiteColor 
) ; 
 345         RGBForeColor( &tooltipbackground 
) ; 
 347         PaintRect( &m_rect 
) ; 
 348         ForeColor(blackColor 
) ; 
 349         FrameRect( &m_rect 
) ; 
 350         SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ; 
 351         ::MoveTo( m_rect
.left 
+ kTipBorder 
, m_rect
.top 
+ fontInfo
.ascent 
+ kTipBorder
); 
 359             if( text
[i
] == 13 || text
[i
] == 10) 
 361                 ::DrawText( text 
, laststop 
, i 
- laststop 
) ; 
 363                 ::MoveTo( m_rect
.left 
+ kTipBorder 
, m_rect
.top 
+ fontInfo
.ascent 
+ kTipBorder 
+ height 
); 
 368         ::DrawText( text 
, laststop 
, i 
- laststop 
) ; 
 369         ::TextMode( srcOr 
) ; 
 374 void wxToolTip::NotifyWindowDelete( WXHWND win 
) 
 376     if ( win 
== s_ToolTipWindowRef 
) 
 378         s_ToolTipWindowRef 
= NULL 
; 
 382 void wxMacToolTip::Clear() 
 394     m_helpTextRef
.Release() ; 
 396     if ( m_window 
== s_ToolTipWindowRef 
&& m_backpict 
) 
 398         wxMacPortStateHelper 
help( (GrafPtr
) GetWindowPort(m_window
) ) ; 
 402         BackColor( whiteColor 
) ; 
 403         ForeColor(blackColor 
) ; 
 404         DrawPicture(m_backpict
, &m_rect
); 
 405         KillPicture(m_backpict
); 
 411 #endif // wxUSE_TOOLTIPS