1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/tooltip.cpp
3 // Purpose: wxToolTip implementation
4 // Author: Stefan Csomor
6 // Copyright: (c) Stefan Csomor
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
14 #include "wx/tooltip.h"
18 #include "wx/window.h"
23 #include "wx/geometry.h"
24 #include "wx/mac/uma.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 class wxMacToolTipTimer
;
38 void Setup( WindowRef window
, const wxString
& text
, const wxPoint
& localPosition
) ;
53 PicHandle m_backpict
;
56 wxMacToolTipTimer
* m_timer
;
59 wxMacCFStringHolder m_helpTextRef
;
63 class wxMacToolTipTimer
: public wxTimer
66 wxMacToolTipTimer(wxMacToolTip
* tip
, int iMilliseconds
) ;
67 wxMacToolTipTimer() {} ;
68 virtual ~wxMacToolTipTimer() {} ;
72 if ( m_mark
== m_tip
->GetMark() )
81 //-----------------------------------------------------------------------------
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
;
91 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
94 wxToolTip::wxToolTip( const wxString
&tip
)
97 m_window
= (wxWindow
*) NULL
;
100 wxToolTip::~wxToolTip()
104 void wxToolTip::SetTip( const wxString
&tip
)
111 // update it immediately
112 wxToolInfo
ti(GetHwndOf(m_window
));
113 ti
.lpszText
= (wxChar
*)m_text
.c_str();
115 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, 0, &ti
);
120 void wxToolTip::SetWindow( wxWindow
*win
)
125 void wxToolTip::Enable( bool flag
)
127 if ( s_ShowToolTips
!= flag
)
129 s_ShowToolTips
= flag
;
131 if ( s_ShowToolTips
)
141 void wxToolTip::SetDelay( long msecs
)
143 s_ToolTipDelay
= msecs
;
146 void wxToolTip::RelayEvent( wxWindow
*win
, wxMouseEvent
&event
)
148 if ( s_ShowToolTips
)
150 if ( event
.GetEventType() == wxEVT_LEAVE_WINDOW
)
154 else if (event
.GetEventType() == wxEVT_ENTER_WINDOW
|| event
.GetEventType() == wxEVT_MOTION
)
156 wxPoint2DInt
where( event
.m_x
, event
.m_y
) ;
157 if ( s_LastWindowEntered
== win
&& s_ToolTipArea
.Contains( where
) )
163 s_ToolTipArea
= wxRect2DInt( event
.m_x
- 2 , event
.m_y
- 2 , 4 , 4 ) ;
164 s_LastWindowEntered
= win
;
166 WindowRef window
= MAC_WXHWND( win
->MacGetTopLevelWindowRef() ) ;
169 wxPoint
local( x
, y
) ;
170 win
->MacClientToRootWindow( &x
, &y
) ;
171 wxPoint
windowlocal( x
, y
) ;
172 s_ToolTip
.Setup( window
, win
->MacGetToolTipString( local
) , windowlocal
) ;
178 void wxToolTip::RemoveToolTips()
185 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip
*tip
, int msec
)
188 m_mark
= tip
->GetMark() ;
192 wxMacToolTip::wxMacToolTip()
201 void wxMacToolTip::Setup( WindowRef win
, const wxString
& text
, const wxPoint
& localPosition
)
206 m_position
= localPosition
;
209 s_ToolTipWindowRef
= m_window
;
215 m_timer
= new wxMacToolTipTimer( this , s_ToolTipDelay
) ;
218 wxMacToolTip::~wxMacToolTip()
230 const short kTipBorder
= 2 ;
231 const short kTipOffset
= 5 ;
233 void wxMacToolTip::Draw()
235 if ( m_label
.empty() )
238 if ( m_window
== s_ToolTipWindowRef
)
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 ) ;
247 QDLocalToGlobalRect( GetWindowPort( m_window
) , &tag
.absHotRect
) ;
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
);
257 wxMacPortStateHelper
help( (GrafPtr
)GetWindowPort( m_window
) );
258 FontFamilyID fontId
;
262 GetThemeFont( kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
263 GetFNum( fontName
, &fontId
);
266 TextSize( fontSize
) ;
267 TextFace( fontStyle
) ;
269 ::GetFontInfo(&fontInfo
);
270 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
274 int length
= m_label
.length() ;
278 wxCharBuffer text
= m_label
.mb_str( wxConvLocal
) ;
282 if ( text
[i
] == 13 || text
[i
] == 10)
284 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
285 if ( thiswidth
> width
)
295 if ( i
- laststop
> 0 )
297 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
298 if ( thiswidth
> width
)
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
;
307 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
310 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
311 if ( m_rect
.top
< 0 )
313 m_rect
.bottom
+= -m_rect
.top
;
316 if ( m_rect
.left
< 0 )
318 m_rect
.right
+= -m_rect
.left
;
321 if ( m_rect
.right
> r
.right
)
323 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
324 m_rect
.right
= r
.right
;
326 if ( m_rect
.bottom
> r
.bottom
)
328 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
329 m_rect
.bottom
= r
.bottom
;
334 GDHandle origDevice
;
336 ClipRect( &m_rect
) ;
337 BackColor( whiteColor
) ;
338 ForeColor(blackColor
) ;
339 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
341 GetGWorld( &origPort
, &origDevice
) ;
342 SetGWorld( port
, NULL
) ;
344 m_backpict
= OpenPicture(&m_rect
);
346 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
347 GetPortBitMapForCopyBits(port
),
353 SetGWorld( origPort
, origDevice
) ;
354 DisposeGWorld( port
) ;
357 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
358 BackColor( whiteColor
) ;
359 RGBForeColor( &tooltipbackground
) ;
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
);
373 if ( text
[i
] == 13 || text
[i
] == 10)
375 ::DrawText( text
, laststop
, i
- laststop
) ;
377 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
384 ::DrawText( text
, laststop
, i
- laststop
) ;
385 ::TextMode( srcOr
) ;
390 void wxToolTip::NotifyWindowDelete( WXHWND win
)
392 if ( win
== s_ToolTipWindowRef
)
393 s_ToolTipWindowRef
= NULL
;
396 void wxMacToolTip::Clear()
411 m_helpTextRef
.Release() ;
413 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
415 wxMacPortStateHelper
help( (GrafPtr
)GetWindowPort( m_window
) ) ;
419 BackColor( whiteColor
) ;
420 ForeColor( blackColor
) ;
421 DrawPicture( m_backpict
, &m_rect
);
422 KillPicture( m_backpict
);