1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolTip implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
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
, wxString text
, wxPoint localPosition
) ;
39 long GetMark() { return m_mark
; }
42 bool IsShown() { return m_shown
; }
49 PicHandle m_backpict
;
52 wxMacToolTipTimer
* m_timer
;
55 class wxMacToolTipTimer
: public wxTimer
58 wxMacToolTipTimer() {} ;
59 wxMacToolTipTimer(wxMacToolTip
* tip
, int iMilliseconds
) ;
60 virtual ~wxMacToolTipTimer() {} ;
63 if ( m_mark
== m_tip
->GetMark() )
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 static long s_ToolTipDelay
= 500 ;
75 static bool s_ShowToolTips
= true ;
76 static wxMacToolTip s_ToolTip
;
77 static wxWindow
* s_LastWindowEntered
= NULL
;
78 static wxRect2DInt s_ToolTipArea
;
79 static WindowRef s_ToolTipWindowRef
= NULL
;
81 IMPLEMENT_ABSTRACT_CLASS(wxToolTip
, wxObject
)
83 wxToolTip::wxToolTip( const wxString
&tip
)
86 m_window
= (wxWindow
*) NULL
;
89 wxToolTip::~wxToolTip()
93 void wxToolTip::SetTip( const wxString
&tip
)
100 // update it immediately
101 wxToolInfo ti(GetHwndOf(m_window));
102 ti.lpszText = (wxChar *)m_text.c_str();
104 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
109 void wxToolTip::SetWindow( wxWindow
*win
)
114 void wxToolTip::Enable( bool flag
)
116 if ( s_ShowToolTips
!= flag
)
118 s_ShowToolTips
= flag
;
119 if ( s_ShowToolTips
)
129 void wxToolTip::SetDelay( long msecs
)
131 s_ToolTipDelay
= msecs
;
134 void wxToolTip::RelayEvent( wxWindow
*win
, wxMouseEvent
&event
)
136 if ( s_ShowToolTips
)
138 if ( event
.GetEventType() == wxEVT_LEAVE_WINDOW
)
142 else if (event
.GetEventType() == wxEVT_ENTER_WINDOW
|| event
.GetEventType() == wxEVT_MOTION
)
144 wxPoint2DInt
where( event
.m_x
, event
.m_y
) ;
145 if ( s_LastWindowEntered
== win
&& s_ToolTipArea
.Contains( where
) )
151 s_ToolTipArea
= wxRect2DInt( event
.m_x
- 2 , event
.m_y
- 2 , 4 , 4 ) ;
152 s_LastWindowEntered
= win
;
154 WindowRef window
= MAC_WXHWND( win
->MacGetRootWindow() ) ;
157 wxPoint
local( x
, y
) ;
158 win
->MacClientToRootWindow( &x
, &y
) ;
159 wxPoint
windowlocal( x
, y
) ;
160 s_ToolTip
.Setup( window
, win
->MacGetToolTipString( local
) , windowlocal
) ;
166 void wxToolTip::RemoveToolTips()
172 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip
*tip
, int msec
)
175 m_mark
= tip
->GetMark() ;
179 wxMacToolTip::wxMacToolTip()
188 void wxMacToolTip::Setup( WindowRef win
, wxString text
, wxPoint localPosition
)
192 m_position
= localPosition
;
193 if( wxApp::s_macDefaultEncodingIsPC
)
194 m_label
= wxMacMakeMacStringFromPC( text
) ;
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 if ( HMDisplayTag
!= (void*) kUnresolvedCFragSymbolAddress
)
229 HMHelpContentRec tag
;
230 tag
.version
= kMacHelpVersion
;
231 SetRect( &tag
.absHotRect
, m_position
.x
- 2 , m_position
.y
- 2 , m_position
.x
+ 2 , m_position
.y
+ 2 ) ;
234 SetPortWindowPort(m_window
) ;
235 LocalToGlobal( (Point
*) &tag
.absHotRect
.top
);
236 LocalToGlobal( (Point
*) &tag
.absHotRect
.bottom
);
238 CFStringRef text
= wxMacCreateCFString(m_label
) ;
239 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
240 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= text
;
241 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
242 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= text
;
243 tag
.tagSide
= kHMDefaultSide
;
244 HMDisplayTag( &tag
);
250 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( m_window
) );
252 bool useDrawThemeText
= ( DrawThemeTextBox
!= (void*) kUnresolvedCFragSymbolAddress
) ;
255 FontFamilyID fontId
;
259 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
260 GetFNum( fontName
, &fontId
);
263 TextSize( fontSize
) ;
264 TextFace( fontStyle
) ;
266 ::GetFontInfo(&fontInfo
);
267 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
269 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
272 int length
= m_label
.Length() ;
276 const char *text
= m_label
;
279 if( text
[i
] == 13 || text
[i
] == 10)
281 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
282 if ( thiswidth
> width
)
290 if ( i
- laststop
> 0 )
292 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
293 if ( thiswidth
> width
)
299 m_rect
.left
= m_position
.x
+ kTipOffset
;
300 m_rect
.top
= m_position
.y
+ kTipOffset
;
301 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
303 if ( useDrawThemeText
)
304 m_rect
.right
+= kTipBorder
;
306 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
308 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
309 if ( m_rect
.top
< 0 )
311 m_rect
.bottom
+= -m_rect
.top
;
314 if ( m_rect
.left
< 0 )
316 m_rect
.right
+= -m_rect
.left
;
319 if ( m_rect
.right
> r
.right
)
321 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
322 m_rect
.right
= r
.right
;
324 if ( m_rect
.bottom
> r
.bottom
)
326 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
327 m_rect
.bottom
= r
.bottom
;
329 ClipRect( &m_rect
) ;
330 BackColor( whiteColor
) ;
331 ForeColor(blackColor
) ;
333 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
335 GDHandle origDevice
;
337 GetGWorld( &origPort
, &origDevice
) ;
338 SetGWorld( port
, NULL
) ;
340 m_backpict
= OpenPicture(&m_rect
);
342 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
343 GetPortBitMapForCopyBits(port
),
349 SetGWorld( origPort
, origDevice
) ;
350 DisposeGWorld( port
) ;
353 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
354 BackColor( whiteColor
) ;
355 RGBForeColor( &tooltipbackground
) ;
357 PaintRect( &m_rect
) ;
358 ForeColor(blackColor
) ;
359 FrameRect( &m_rect
) ;
360 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
361 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
369 if( text
[i
] == 13 || text
[i
] == 10)
372 if ( useDrawThemeText
)
375 frame
.top
= m_rect
.top
+ kTipBorder
+ height
;
376 frame
.left
= m_rect
.left
+ kTipBorder
;
377 frame
.bottom
= frame
.top
+ 1000 ;
378 frame
.right
= frame
.left
+ 1000 ;
379 CFStringRef mString
= CFStringCreateWithBytes( NULL
, (UInt8
*) text
+ laststop
, i
- laststop
, CFStringGetSystemEncoding(), false ) ;
380 ::DrawThemeTextBox( mString
,
381 kThemeCurrentPortFont
,
387 CFRelease( mString
) ;
393 ::DrawText( text
, laststop
, i
- laststop
) ;
395 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
402 if ( useDrawThemeText
)
405 frame
.top
= m_rect
.top
+ kTipBorder
+ height
;
406 frame
.left
= m_rect
.left
+ kTipBorder
;
407 frame
.bottom
= frame
.top
+ 1000 ;
408 frame
.right
= frame
.left
+ 1000 ;
409 CFStringRef mString
= CFStringCreateWithCString( NULL
, text
+ laststop
, kCFStringEncodingMacRoman
) ;
410 ::DrawThemeTextBox( mString
,
411 kThemeCurrentPortFont
,
417 CFRelease( mString
) ;
422 ::DrawText( text
, laststop
, i
- laststop
) ;
424 ::TextMode( srcOr
) ;
429 void wxToolTip::NotifyWindowDelete( WXHWND win
)
431 if ( win
== s_ToolTipWindowRef
)
433 s_ToolTipWindowRef
= NULL
;
437 void wxMacToolTip::Clear()
450 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
452 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(m_window
) ) ;
456 BackColor( whiteColor
) ;
457 ForeColor(blackColor
) ;
458 DrawPicture(m_backpict
, &m_rect
);
459 KillPicture(m_backpict
);