1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolTip implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
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
, 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
->MacGetRootWindow() ) ;
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
, 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 ) ;
228 SetPortWindowPort(m_window
) ;
229 LocalToGlobal( (Point
*) &tag
.absHotRect
.top
);
230 LocalToGlobal( (Point
*) &tag
.absHotRect
.bottom
);
232 m_helpTextRef
.Assign( m_label
, wxFONTENCODING_DEFAULT
) ;
233 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
234 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= m_helpTextRef
;
235 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
236 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= m_helpTextRef
;
237 tag
.tagSide
= kHMDefaultSide
;
238 HMDisplayTag( &tag
);
240 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( m_window
) );
241 FontFamilyID fontId
;
245 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
246 GetFNum( fontName
, &fontId
);
249 TextSize( fontSize
) ;
250 TextFace( fontStyle
) ;
252 ::GetFontInfo(&fontInfo
);
253 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
257 int length
= m_label
.Length() ;
261 wxCharBuffer text
= m_label
.mb_str( wxConvLocal
) ;
265 if( text
[i
] == 13 || text
[i
] == 10)
267 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
268 if ( thiswidth
> width
)
276 if ( i
- laststop
> 0 )
278 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
279 if ( thiswidth
> width
)
284 m_rect
.left
= m_position
.x
+ kTipOffset
;
285 m_rect
.top
= m_position
.y
+ kTipOffset
;
286 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
288 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
290 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
291 if ( m_rect
.top
< 0 )
293 m_rect
.bottom
+= -m_rect
.top
;
296 if ( m_rect
.left
< 0 )
298 m_rect
.right
+= -m_rect
.left
;
301 if ( m_rect
.right
> r
.right
)
303 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
304 m_rect
.right
= r
.right
;
306 if ( m_rect
.bottom
> r
.bottom
)
308 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
309 m_rect
.bottom
= r
.bottom
;
311 ClipRect( &m_rect
) ;
312 BackColor( whiteColor
) ;
313 ForeColor(blackColor
) ;
315 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
317 GDHandle origDevice
;
319 GetGWorld( &origPort
, &origDevice
) ;
320 SetGWorld( port
, NULL
) ;
322 m_backpict
= OpenPicture(&m_rect
);
324 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
325 GetPortBitMapForCopyBits(port
),
331 SetGWorld( origPort
, origDevice
) ;
332 DisposeGWorld( port
) ;
335 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
336 BackColor( whiteColor
) ;
337 RGBForeColor( &tooltipbackground
) ;
339 PaintRect( &m_rect
) ;
340 ForeColor(blackColor
) ;
341 FrameRect( &m_rect
) ;
342 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
343 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
351 if( text
[i
] == 13 || text
[i
] == 10)
353 ::DrawText( text
, laststop
, i
- laststop
) ;
355 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
360 ::DrawText( text
, laststop
, i
- laststop
) ;
361 ::TextMode( srcOr
) ;
366 void wxToolTip::NotifyWindowDelete( WXHWND win
)
368 if ( win
== s_ToolTipWindowRef
)
370 s_ToolTipWindowRef
= NULL
;
374 void wxMacToolTip::Clear()
386 m_helpTextRef
.Release() ;
388 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
390 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(m_window
) ) ;
394 BackColor( whiteColor
) ;
395 ForeColor(blackColor
) ;
396 DrawPicture(m_backpict
, &m_rect
);
397 KillPicture(m_backpict
);