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"
16 #include "wx/window.h"
20 #include "wx/tooltip.h"
22 #include "wx/geometry.h"
23 #include "wx/mac/uma.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 class wxMacToolTipTimer
;
37 void Setup( WindowRef window
, const wxString
& text
, const wxPoint
& localPosition
) ;
52 PicHandle m_backpict
;
55 wxMacToolTipTimer
* m_timer
;
58 wxMacCFStringHolder m_helpTextRef
;
62 class wxMacToolTipTimer
: public wxTimer
65 wxMacToolTipTimer(wxMacToolTip
* tip
, int iMilliseconds
) ;
66 wxMacToolTipTimer() {} ;
67 virtual ~wxMacToolTipTimer() {} ;
71 if ( m_mark
== m_tip
->GetMark() )
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 static long s_ToolTipDelay
= 500 ;
84 static bool s_ShowToolTips
= true ;
85 static wxMacToolTip s_ToolTip
;
86 static wxWindow
* s_LastWindowEntered
= NULL
;
87 static wxRect2DInt s_ToolTipArea
;
88 static WindowRef s_ToolTipWindowRef
= NULL
;
90 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
93 wxToolTip::wxToolTip( const wxString
&tip
)
96 m_window
= (wxWindow
*) NULL
;
99 wxToolTip::~wxToolTip()
103 void wxToolTip::SetTip( const wxString
&tip
)
110 // update it immediately
111 wxToolInfo
ti(GetHwndOf(m_window
));
112 ti
.lpszText
= (wxChar
*)m_text
.c_str();
114 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT
, 0, &ti
);
119 void wxToolTip::SetWindow( wxWindow
*win
)
124 void wxToolTip::Enable( bool flag
)
126 if ( s_ShowToolTips
!= flag
)
128 s_ShowToolTips
= flag
;
130 if ( s_ShowToolTips
)
140 void wxToolTip::SetDelay( long msecs
)
142 s_ToolTipDelay
= msecs
;
145 void wxToolTip::RelayEvent( wxWindow
*win
, wxMouseEvent
&event
)
147 if ( s_ShowToolTips
)
149 if ( event
.GetEventType() == wxEVT_LEAVE_WINDOW
)
153 else if (event
.GetEventType() == wxEVT_ENTER_WINDOW
|| event
.GetEventType() == wxEVT_MOTION
)
155 wxPoint2DInt
where( event
.m_x
, event
.m_y
) ;
156 if ( s_LastWindowEntered
== win
&& s_ToolTipArea
.Contains( where
) )
162 s_ToolTipArea
= wxRect2DInt( event
.m_x
- 2 , event
.m_y
- 2 , 4 , 4 ) ;
163 s_LastWindowEntered
= win
;
165 WindowRef window
= MAC_WXHWND( win
->MacGetTopLevelWindowRef() ) ;
168 wxPoint
local( x
, y
) ;
169 win
->MacClientToRootWindow( &x
, &y
) ;
170 wxPoint
windowlocal( x
, y
) ;
171 s_ToolTip
.Setup( window
, win
->MacGetToolTipString( local
) , windowlocal
) ;
177 void wxToolTip::RemoveToolTips()
184 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip
*tip
, int msec
)
187 m_mark
= tip
->GetMark() ;
191 wxMacToolTip::wxMacToolTip()
200 void wxMacToolTip::Setup( WindowRef win
, const wxString
& text
, const wxPoint
& localPosition
)
205 m_position
= localPosition
;
208 s_ToolTipWindowRef
= m_window
;
214 m_timer
= new wxMacToolTipTimer( this , s_ToolTipDelay
) ;
217 wxMacToolTip::~wxMacToolTip()
229 const short kTipBorder
= 2 ;
230 const short kTipOffset
= 5 ;
232 void wxMacToolTip::Draw()
234 if ( m_label
.empty() )
237 if ( m_window
== s_ToolTipWindowRef
)
242 HMHelpContentRec tag
;
243 tag
.version
= kMacHelpVersion
;
244 SetRect( &tag
.absHotRect
, m_position
.x
- 2 , m_position
.y
- 2 , m_position
.x
+ 2 , m_position
.y
+ 2 ) ;
246 QDLocalToGlobalRect( GetWindowPort( m_window
) , &tag
.absHotRect
) ;
248 m_helpTextRef
.Assign( m_label
, wxFONTENCODING_DEFAULT
) ;
249 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
250 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= m_helpTextRef
;
251 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
252 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= m_helpTextRef
;
253 tag
.tagSide
= kHMDefaultSide
;
254 HMDisplayTag( &tag
);
256 wxMacPortStateHelper
help( (GrafPtr
)GetWindowPort( m_window
) );
257 FontFamilyID fontId
;
261 GetThemeFont( kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
262 GetFNum( fontName
, &fontId
);
265 TextSize( fontSize
) ;
266 TextFace( fontStyle
) ;
268 ::GetFontInfo(&fontInfo
);
269 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
273 int length
= m_label
.length() ;
277 wxCharBuffer text
= m_label
.mb_str( wxConvLocal
) ;
281 if ( text
[i
] == 13 || text
[i
] == 10)
283 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
284 if ( thiswidth
> width
)
294 if ( i
- laststop
> 0 )
296 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
297 if ( thiswidth
> width
)
302 m_rect
.left
= m_position
.x
+ kTipOffset
;
303 m_rect
.top
= m_position
.y
+ kTipOffset
;
304 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
306 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
309 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
310 if ( m_rect
.top
< 0 )
312 m_rect
.bottom
+= -m_rect
.top
;
315 if ( m_rect
.left
< 0 )
317 m_rect
.right
+= -m_rect
.left
;
320 if ( m_rect
.right
> r
.right
)
322 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
323 m_rect
.right
= r
.right
;
325 if ( m_rect
.bottom
> r
.bottom
)
327 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
328 m_rect
.bottom
= r
.bottom
;
333 GDHandle origDevice
;
335 ClipRect( &m_rect
) ;
336 BackColor( whiteColor
) ;
337 ForeColor(blackColor
) ;
338 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
340 GetGWorld( &origPort
, &origDevice
) ;
341 SetGWorld( port
, NULL
) ;
343 m_backpict
= OpenPicture(&m_rect
);
345 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
346 GetPortBitMapForCopyBits(port
),
352 SetGWorld( origPort
, origDevice
) ;
353 DisposeGWorld( port
) ;
356 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
357 BackColor( whiteColor
) ;
358 RGBForeColor( &tooltipbackground
) ;
360 PaintRect( &m_rect
) ;
361 ForeColor(blackColor
) ;
362 FrameRect( &m_rect
) ;
363 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
364 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
372 if ( text
[i
] == 13 || text
[i
] == 10)
374 ::DrawText( text
, laststop
, i
- laststop
) ;
376 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
383 ::DrawText( text
, laststop
, i
- laststop
) ;
384 ::TextMode( srcOr
) ;
389 void wxToolTip::NotifyWindowDelete( WXHWND win
)
391 if ( win
== s_ToolTipWindowRef
)
392 s_ToolTipWindowRef
= NULL
;
395 void wxMacToolTip::Clear()
410 m_helpTextRef
.Release() ;
412 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
414 wxMacPortStateHelper
help( (GrafPtr
)GetWindowPort( m_window
) ) ;
418 BackColor( whiteColor
) ;
419 ForeColor( blackColor
) ;
420 DrawPicture( m_backpict
, &m_rect
);
421 KillPicture( m_backpict
);