1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolTip implementation
4 // Author: Stefan Csomor
6 // Copyright: (c) Stefan Csomor
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
, const 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 wxMacCFStringHolder 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
->MacGetTopLevelWindowRef() ) ;
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()
191 void wxMacToolTip::Setup( WindowRef win
, const wxString
& text
, wxPoint localPosition
)
195 m_position
= localPosition
;
198 s_ToolTipWindowRef
= m_window
;
202 m_timer
= new wxMacToolTipTimer( this , s_ToolTipDelay
) ;
205 wxMacToolTip::~wxMacToolTip()
215 const short kTipBorder
= 2 ;
216 const short kTipOffset
= 5 ;
218 void wxMacToolTip::Draw()
220 if ( m_label
.Length() == 0 )
223 if ( m_window
== s_ToolTipWindowRef
)
227 HMHelpContentRec tag
;
228 tag
.version
= kMacHelpVersion
;
229 SetRect( &tag
.absHotRect
, m_position
.x
- 2 , m_position
.y
- 2 , m_position
.x
+ 2 , m_position
.y
+ 2 ) ;
231 QDLocalToGlobalRect( GetWindowPort( m_window
) , &tag
.absHotRect
) ;
233 m_helpTextRef
.Assign( m_label
, wxFONTENCODING_DEFAULT
) ;
234 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
235 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= m_helpTextRef
;
236 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
237 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= m_helpTextRef
;
238 tag
.tagSide
= kHMDefaultSide
;
239 HMDisplayTag( &tag
);
241 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( m_window
) );
242 FontFamilyID fontId
;
246 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
247 GetFNum( fontName
, &fontId
);
250 TextSize( fontSize
) ;
251 TextFace( fontStyle
) ;
253 ::GetFontInfo(&fontInfo
);
254 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
258 int length
= m_label
.Length() ;
262 wxCharBuffer text
= m_label
.mb_str( wxConvLocal
) ;
266 if( text
[i
] == 13 || text
[i
] == 10)
268 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
269 if ( thiswidth
> width
)
277 if ( i
- laststop
> 0 )
279 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
280 if ( thiswidth
> width
)
285 m_rect
.left
= m_position
.x
+ kTipOffset
;
286 m_rect
.top
= m_position
.y
+ kTipOffset
;
287 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
289 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
291 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
292 if ( m_rect
.top
< 0 )
294 m_rect
.bottom
+= -m_rect
.top
;
297 if ( m_rect
.left
< 0 )
299 m_rect
.right
+= -m_rect
.left
;
302 if ( m_rect
.right
> r
.right
)
304 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
305 m_rect
.right
= r
.right
;
307 if ( m_rect
.bottom
> r
.bottom
)
309 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
310 m_rect
.bottom
= r
.bottom
;
312 ClipRect( &m_rect
) ;
313 BackColor( whiteColor
) ;
314 ForeColor(blackColor
) ;
316 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
318 GDHandle origDevice
;
320 GetGWorld( &origPort
, &origDevice
) ;
321 SetGWorld( port
, NULL
) ;
323 m_backpict
= OpenPicture(&m_rect
);
325 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
326 GetPortBitMapForCopyBits(port
),
332 SetGWorld( origPort
, origDevice
) ;
333 DisposeGWorld( port
) ;
336 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
337 BackColor( whiteColor
) ;
338 RGBForeColor( &tooltipbackground
) ;
340 PaintRect( &m_rect
) ;
341 ForeColor(blackColor
) ;
342 FrameRect( &m_rect
) ;
343 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
344 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
352 if( text
[i
] == 13 || text
[i
] == 10)
354 ::DrawText( text
, laststop
, i
- laststop
) ;
356 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
361 ::DrawText( text
, laststop
, i
- laststop
) ;
362 ::TextMode( srcOr
) ;
367 void wxToolTip::NotifyWindowDelete( WXHWND win
)
369 if ( win
== s_ToolTipWindowRef
)
371 s_ToolTipWindowRef
= NULL
;
375 void wxMacToolTip::Clear()
387 m_helpTextRef
.Release() ;
389 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
391 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(m_window
) ) ;
395 BackColor( whiteColor
) ;
396 ForeColor(blackColor
) ;
397 DrawPicture(m_backpict
, &m_rect
);
398 KillPicture(m_backpict
);