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
) ;
35 long GetMark() { return m_mark
; }
38 bool IsShown() { return m_shown
; }
45 PicHandle m_backpict
;
48 wxMacToolTipTimer
* m_timer
;
50 wxMacCFStringHolder m_helpTextRef
;
54 class wxMacToolTipTimer
: public wxTimer
57 wxMacToolTipTimer() {} ;
58 wxMacToolTipTimer(wxMacToolTip
* tip
, int iMilliseconds
) ;
59 virtual ~wxMacToolTipTimer() {} ;
62 if ( m_mark
== m_tip
->GetMark() )
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 static long s_ToolTipDelay
= 500 ;
74 static bool s_ShowToolTips
= true ;
75 static wxMacToolTip s_ToolTip
;
76 static wxWindow
* s_LastWindowEntered
= NULL
;
77 static wxRect2DInt s_ToolTipArea
;
78 static WindowRef s_ToolTipWindowRef
= NULL
;
80 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
82 wxToolTip::wxToolTip( const wxString
&tip
)
85 m_window
= (wxWindow
*) NULL
;
88 wxToolTip::~wxToolTip()
92 void wxToolTip::SetTip( const wxString
&tip
)
99 // update it immediately
100 wxToolInfo ti(GetHwndOf(m_window));
101 ti.lpszText = (wxChar *)m_text.c_str();
103 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
108 void wxToolTip::SetWindow( wxWindow
*win
)
113 void wxToolTip::Enable( bool flag
)
115 if ( s_ShowToolTips
!= flag
)
117 s_ShowToolTips
= flag
;
118 if ( s_ShowToolTips
)
128 void wxToolTip::SetDelay( long msecs
)
130 s_ToolTipDelay
= msecs
;
133 void wxToolTip::RelayEvent( wxWindow
*win
, wxMouseEvent
&event
)
135 if ( s_ShowToolTips
)
137 if ( event
.GetEventType() == wxEVT_LEAVE_WINDOW
)
141 else if (event
.GetEventType() == wxEVT_ENTER_WINDOW
|| event
.GetEventType() == wxEVT_MOTION
)
143 wxPoint2DInt
where( event
.m_x
, event
.m_y
) ;
144 if ( s_LastWindowEntered
== win
&& s_ToolTipArea
.Contains( where
) )
150 s_ToolTipArea
= wxRect2DInt( event
.m_x
- 2 , event
.m_y
- 2 , 4 , 4 ) ;
151 s_LastWindowEntered
= win
;
153 WindowRef window
= MAC_WXHWND( win
->MacGetTopLevelWindowRef() ) ;
156 wxPoint
local( x
, y
) ;
157 win
->MacClientToRootWindow( &x
, &y
) ;
158 wxPoint
windowlocal( x
, y
) ;
159 s_ToolTip
.Setup( window
, win
->MacGetToolTipString( local
) , windowlocal
) ;
165 void wxToolTip::RemoveToolTips()
171 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip
*tip
, int msec
)
174 m_mark
= tip
->GetMark() ;
178 wxMacToolTip::wxMacToolTip()
187 void wxMacToolTip::Setup( WindowRef win
, const wxString
& text
, const wxPoint
& localPosition
)
191 m_position
= localPosition
;
194 s_ToolTipWindowRef
= m_window
;
198 m_timer
= new wxMacToolTipTimer( this , s_ToolTipDelay
) ;
201 wxMacToolTip::~wxMacToolTip()
211 const short kTipBorder
= 2 ;
212 const short kTipOffset
= 5 ;
214 void wxMacToolTip::Draw()
216 if ( m_label
.Length() == 0 )
219 if ( m_window
== s_ToolTipWindowRef
)
223 HMHelpContentRec tag
;
224 tag
.version
= kMacHelpVersion
;
225 SetRect( &tag
.absHotRect
, m_position
.x
- 2 , m_position
.y
- 2 , m_position
.x
+ 2 , m_position
.y
+ 2 ) ;
227 QDLocalToGlobalRect( GetWindowPort( m_window
) , &tag
.absHotRect
) ;
229 m_helpTextRef
.Assign( m_label
, wxFONTENCODING_DEFAULT
) ;
230 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
231 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= m_helpTextRef
;
232 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
233 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= m_helpTextRef
;
234 tag
.tagSide
= kHMDefaultSide
;
235 HMDisplayTag( &tag
);
237 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( m_window
) );
238 FontFamilyID fontId
;
242 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
243 GetFNum( fontName
, &fontId
);
246 TextSize( fontSize
) ;
247 TextFace( fontStyle
) ;
249 ::GetFontInfo(&fontInfo
);
250 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
254 int length
= m_label
.Length() ;
258 wxCharBuffer text
= m_label
.mb_str( wxConvLocal
) ;
262 if( text
[i
] == 13 || text
[i
] == 10)
264 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
265 if ( thiswidth
> width
)
273 if ( i
- laststop
> 0 )
275 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
276 if ( thiswidth
> width
)
281 m_rect
.left
= m_position
.x
+ kTipOffset
;
282 m_rect
.top
= m_position
.y
+ kTipOffset
;
283 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
285 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
287 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
288 if ( m_rect
.top
< 0 )
290 m_rect
.bottom
+= -m_rect
.top
;
293 if ( m_rect
.left
< 0 )
295 m_rect
.right
+= -m_rect
.left
;
298 if ( m_rect
.right
> r
.right
)
300 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
301 m_rect
.right
= r
.right
;
303 if ( m_rect
.bottom
> r
.bottom
)
305 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
306 m_rect
.bottom
= r
.bottom
;
308 ClipRect( &m_rect
) ;
309 BackColor( whiteColor
) ;
310 ForeColor(blackColor
) ;
312 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
314 GDHandle origDevice
;
316 GetGWorld( &origPort
, &origDevice
) ;
317 SetGWorld( port
, NULL
) ;
319 m_backpict
= OpenPicture(&m_rect
);
321 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
322 GetPortBitMapForCopyBits(port
),
328 SetGWorld( origPort
, origDevice
) ;
329 DisposeGWorld( port
) ;
332 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
333 BackColor( whiteColor
) ;
334 RGBForeColor( &tooltipbackground
) ;
336 PaintRect( &m_rect
) ;
337 ForeColor(blackColor
) ;
338 FrameRect( &m_rect
) ;
339 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
340 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
348 if( text
[i
] == 13 || text
[i
] == 10)
350 ::DrawText( text
, laststop
, i
- laststop
) ;
352 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
357 ::DrawText( text
, laststop
, i
- laststop
) ;
358 ::TextMode( srcOr
) ;
363 void wxToolTip::NotifyWindowDelete( WXHWND win
)
365 if ( win
== s_ToolTipWindowRef
)
367 s_ToolTipWindowRef
= NULL
;
371 void wxMacToolTip::Clear()
383 m_helpTextRef
.Release() ;
385 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
387 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(m_window
) ) ;
391 BackColor( whiteColor
) ;
392 ForeColor(blackColor
) ;
393 DrawPicture(m_backpict
, &m_rect
);
394 KillPicture(m_backpict
);