1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolTip implementation
4 // Author: Stefan Csomor
6 // Copyright: (c) Stefan Csomor
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #include "wx/window.h"
17 #include "wx/tooltip.h"
19 #include "wx/geometry.h"
20 #include "wx/mac/uma.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 class wxMacToolTipTimer
;
34 void Setup( WindowRef window
, const wxString
& text
, const wxPoint
& localPosition
) ;
49 PicHandle m_backpict
;
52 wxMacToolTipTimer
* m_timer
;
55 wxMacCFStringHolder m_helpTextRef
;
59 class wxMacToolTipTimer
: public wxTimer
62 wxMacToolTipTimer(wxMacToolTip
* tip
, int iMilliseconds
) ;
63 wxMacToolTipTimer() {} ;
64 virtual ~wxMacToolTipTimer() {} ;
68 if ( m_mark
== m_tip
->GetMark() )
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 static long s_ToolTipDelay
= 500 ;
81 static bool s_ShowToolTips
= true ;
82 static wxMacToolTip s_ToolTip
;
83 static wxWindow
* s_LastWindowEntered
= NULL
;
84 static wxRect2DInt s_ToolTipArea
;
85 static WindowRef s_ToolTipWindowRef
= NULL
;
87 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
;
127 if ( s_ShowToolTips
)
137 void wxToolTip::SetDelay( long msecs
)
139 s_ToolTipDelay
= msecs
;
142 void wxToolTip::RelayEvent( wxWindow
*win
, wxMouseEvent
&event
)
144 if ( s_ShowToolTips
)
146 if ( event
.GetEventType() == wxEVT_LEAVE_WINDOW
)
150 else if (event
.GetEventType() == wxEVT_ENTER_WINDOW
|| event
.GetEventType() == wxEVT_MOTION
)
152 wxPoint2DInt
where( event
.m_x
, event
.m_y
) ;
153 if ( s_LastWindowEntered
== win
&& s_ToolTipArea
.Contains( where
) )
159 s_ToolTipArea
= wxRect2DInt( event
.m_x
- 2 , event
.m_y
- 2 , 4 , 4 ) ;
160 s_LastWindowEntered
= win
;
162 WindowRef window
= MAC_WXHWND( win
->MacGetTopLevelWindowRef() ) ;
165 wxPoint
local( x
, y
) ;
166 win
->MacClientToRootWindow( &x
, &y
) ;
167 wxPoint
windowlocal( x
, y
) ;
168 s_ToolTip
.Setup( window
, win
->MacGetToolTipString( local
) , windowlocal
) ;
174 void wxToolTip::RemoveToolTips()
181 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip
*tip
, int msec
)
184 m_mark
= tip
->GetMark() ;
188 wxMacToolTip::wxMacToolTip()
197 void wxMacToolTip::Setup( WindowRef win
, const wxString
& text
, const wxPoint
& localPosition
)
202 m_position
= localPosition
;
205 s_ToolTipWindowRef
= m_window
;
211 m_timer
= new wxMacToolTipTimer( this , s_ToolTipDelay
) ;
214 wxMacToolTip::~wxMacToolTip()
226 const short kTipBorder
= 2 ;
227 const short kTipOffset
= 5 ;
229 void wxMacToolTip::Draw()
231 if ( m_label
.Length() == 0 )
234 if ( m_window
== s_ToolTipWindowRef
)
239 HMHelpContentRec tag
;
240 tag
.version
= kMacHelpVersion
;
241 SetRect( &tag
.absHotRect
, m_position
.x
- 2 , m_position
.y
- 2 , m_position
.x
+ 2 , m_position
.y
+ 2 ) ;
243 QDLocalToGlobalRect( GetWindowPort( m_window
) , &tag
.absHotRect
) ;
245 m_helpTextRef
.Assign( m_label
, wxFONTENCODING_DEFAULT
) ;
246 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
247 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= m_helpTextRef
;
248 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
249 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= m_helpTextRef
;
250 tag
.tagSide
= kHMDefaultSide
;
251 HMDisplayTag( &tag
);
253 wxMacPortStateHelper
help( (GrafPtr
)GetWindowPort( m_window
) );
254 FontFamilyID fontId
;
258 GetThemeFont( kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
259 GetFNum( fontName
, &fontId
);
262 TextSize( fontSize
) ;
263 TextFace( fontStyle
) ;
265 ::GetFontInfo(&fontInfo
);
266 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
270 int length
= m_label
.Length() ;
274 wxCharBuffer text
= m_label
.mb_str( wxConvLocal
) ;
278 if ( text
[i
] == 13 || text
[i
] == 10)
280 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
281 if ( thiswidth
> width
)
291 if ( i
- laststop
> 0 )
293 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
294 if ( thiswidth
> width
)
299 m_rect
.left
= m_position
.x
+ kTipOffset
;
300 m_rect
.top
= m_position
.y
+ kTipOffset
;
301 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
303 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
306 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
307 if ( m_rect
.top
< 0 )
309 m_rect
.bottom
+= -m_rect
.top
;
312 if ( m_rect
.left
< 0 )
314 m_rect
.right
+= -m_rect
.left
;
317 if ( m_rect
.right
> r
.right
)
319 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
320 m_rect
.right
= r
.right
;
322 if ( m_rect
.bottom
> r
.bottom
)
324 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
325 m_rect
.bottom
= r
.bottom
;
330 GDHandle origDevice
;
332 ClipRect( &m_rect
) ;
333 BackColor( whiteColor
) ;
334 ForeColor(blackColor
) ;
335 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
337 GetGWorld( &origPort
, &origDevice
) ;
338 SetGWorld( port
, NULL
) ;
340 m_backpict
= OpenPicture(&m_rect
);
342 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
343 GetPortBitMapForCopyBits(port
),
349 SetGWorld( origPort
, origDevice
) ;
350 DisposeGWorld( port
) ;
353 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
354 BackColor( whiteColor
) ;
355 RGBForeColor( &tooltipbackground
) ;
357 PaintRect( &m_rect
) ;
358 ForeColor(blackColor
) ;
359 FrameRect( &m_rect
) ;
360 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
361 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
369 if ( text
[i
] == 13 || text
[i
] == 10)
371 ::DrawText( text
, laststop
, i
- laststop
) ;
373 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
380 ::DrawText( text
, laststop
, i
- laststop
) ;
381 ::TextMode( srcOr
) ;
386 void wxToolTip::NotifyWindowDelete( WXHWND win
)
388 if ( win
== s_ToolTipWindowRef
)
389 s_ToolTipWindowRef
= NULL
;
392 void wxMacToolTip::Clear()
407 m_helpTextRef
.Release() ;
409 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
411 wxMacPortStateHelper
help( (GrafPtr
)GetWindowPort( m_window
) ) ;
415 BackColor( whiteColor
) ;
416 ForeColor( blackColor
) ;
417 DrawPicture( m_backpict
, &m_rect
);
418 KillPicture( m_backpict
);