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
;
54 CFStringRef 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
->MacGetRootWindow() ) ;
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()
189 m_helpTextRef
= NULL
;
192 void wxMacToolTip::Setup( WindowRef win
, wxString text
, wxPoint localPosition
)
196 m_position
= localPosition
;
197 if( wxApp::s_macDefaultEncodingIsPC
)
198 m_label
= wxMacMakeMacStringFromPC( text
) ;
202 s_ToolTipWindowRef
= m_window
;
206 m_timer
= new wxMacToolTipTimer( this , s_ToolTipDelay
) ;
209 wxMacToolTip::~wxMacToolTip()
219 const short kTipBorder
= 2 ;
220 const short kTipOffset
= 5 ;
222 void wxMacToolTip::Draw()
224 if ( m_label
.Length() == 0 )
227 if ( m_window
== s_ToolTipWindowRef
)
231 if ( HMDisplayTag
!= (void*) kUnresolvedCFragSymbolAddress
)
233 HMHelpContentRec tag
;
234 tag
.version
= kMacHelpVersion
;
235 SetRect( &tag
.absHotRect
, m_position
.x
- 2 , m_position
.y
- 2 , m_position
.x
+ 2 , m_position
.y
+ 2 ) ;
238 SetPortWindowPort(m_window
) ;
239 LocalToGlobal( (Point
*) &tag
.absHotRect
.top
);
240 LocalToGlobal( (Point
*) &tag
.absHotRect
.bottom
);
243 CFRelease( m_helpTextRef
)
244 m_helpTextRef
= wxMacCreateCFString(m_label
) ;
245 tag
.content
[kHMMinimumContentIndex
].contentType
= kHMCFStringContent
;
246 tag
.content
[kHMMinimumContentIndex
].u
.tagCFString
= text
;
247 tag
.content
[kHMMaximumContentIndex
].contentType
= kHMCFStringContent
;
248 tag
.content
[kHMMaximumContentIndex
].u
.tagCFString
= text
;
249 tag
.tagSide
= kHMDefaultSide
;
250 HMDisplayTag( &tag
);
255 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort( m_window
) );
257 bool useDrawThemeText
= ( DrawThemeTextBox
!= (void*) kUnresolvedCFragSymbolAddress
) ;
260 FontFamilyID fontId
;
264 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
265 GetFNum( fontName
, &fontId
);
268 TextSize( fontSize
) ;
269 TextFace( fontStyle
) ;
271 ::GetFontInfo(&fontInfo
);
272 short lineh
= fontInfo
.ascent
+ fontInfo
.descent
+ fontInfo
.leading
;
274 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
277 int length
= m_label
.Length() ;
281 const char *text
= m_label
;
284 if( text
[i
] == 13 || text
[i
] == 10)
286 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
287 if ( thiswidth
> width
)
295 if ( i
- laststop
> 0 )
297 thiswidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
298 if ( thiswidth
> width
)
304 m_rect
.left
= m_position
.x
+ kTipOffset
;
305 m_rect
.top
= m_position
.y
+ kTipOffset
;
306 m_rect
.right
= m_rect
.left
+ width
+ 2 * kTipBorder
;
308 if ( useDrawThemeText
)
309 m_rect
.right
+= kTipBorder
;
311 m_rect
.bottom
= m_rect
.top
+ height
+ 2 * kTipBorder
;
313 GetPortBounds( GetWindowPort( m_window
) , &r
) ;
314 if ( m_rect
.top
< 0 )
316 m_rect
.bottom
+= -m_rect
.top
;
319 if ( m_rect
.left
< 0 )
321 m_rect
.right
+= -m_rect
.left
;
324 if ( m_rect
.right
> r
.right
)
326 m_rect
.left
-= (m_rect
.right
- r
.right
) ;
327 m_rect
.right
= r
.right
;
329 if ( m_rect
.bottom
> r
.bottom
)
331 m_rect
.top
-= (m_rect
.bottom
- r
.bottom
) ;
332 m_rect
.bottom
= r
.bottom
;
334 ClipRect( &m_rect
) ;
335 BackColor( whiteColor
) ;
336 ForeColor(blackColor
) ;
338 NewGWorld( &port
, wxDisplayDepth() , &m_rect
, NULL
, NULL
, 0 ) ;
340 GDHandle origDevice
;
342 GetGWorld( &origPort
, &origDevice
) ;
343 SetGWorld( port
, NULL
) ;
345 m_backpict
= OpenPicture(&m_rect
);
347 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window
)),
348 GetPortBitMapForCopyBits(port
),
354 SetGWorld( origPort
, origDevice
) ;
355 DisposeGWorld( port
) ;
358 RGBColor tooltipbackground
= { 0xFFFF , 0xFFFF , 0xC000 } ;
359 BackColor( whiteColor
) ;
360 RGBForeColor( &tooltipbackground
) ;
362 PaintRect( &m_rect
) ;
363 ForeColor(blackColor
) ;
364 FrameRect( &m_rect
) ;
365 SetThemeTextColor(kThemeTextColorNotification
,wxDisplayDepth(),true) ;
366 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
);
374 if( text
[i
] == 13 || text
[i
] == 10)
377 if ( useDrawThemeText
)
380 frame
.top
= m_rect
.top
+ kTipBorder
+ height
;
381 frame
.left
= m_rect
.left
+ kTipBorder
;
382 frame
.bottom
= frame
.top
+ 1000 ;
383 frame
.right
= frame
.left
+ 1000 ;
384 CFStringRef mString
= CFStringCreateWithBytes( NULL
, (UInt8
*) text
+ laststop
, i
- laststop
, CFStringGetSystemEncoding(), false ) ;
385 ::DrawThemeTextBox( mString
,
386 kThemeCurrentPortFont
,
392 CFRelease( mString
) ;
398 ::DrawText( text
, laststop
, i
- laststop
) ;
400 ::MoveTo( m_rect
.left
+ kTipBorder
, m_rect
.top
+ fontInfo
.ascent
+ kTipBorder
+ height
);
407 if ( useDrawThemeText
)
410 frame
.top
= m_rect
.top
+ kTipBorder
+ height
;
411 frame
.left
= m_rect
.left
+ kTipBorder
;
412 frame
.bottom
= frame
.top
+ 1000 ;
413 frame
.right
= frame
.left
+ 1000 ;
414 CFStringRef mString
= CFStringCreateWithCString( NULL
, text
+ laststop
, kCFStringEncodingMacRoman
) ;
415 ::DrawThemeTextBox( mString
,
416 kThemeCurrentPortFont
,
422 CFRelease( mString
) ;
427 ::DrawText( text
, laststop
, i
- laststop
) ;
429 ::TextMode( srcOr
) ;
434 void wxToolTip::NotifyWindowDelete( WXHWND win
)
436 if ( win
== s_ToolTipWindowRef
)
438 s_ToolTipWindowRef
= NULL
;
442 void wxMacToolTip::Clear()
455 CFRelease( m_helpTextRef
)
457 if ( m_window
== s_ToolTipWindowRef
&& m_backpict
)
459 wxMacPortStateHelper
help( (GrafPtr
) GetWindowPort(m_window
) ) ;
463 BackColor( whiteColor
) ;
464 ForeColor(blackColor
) ;
465 DrawPicture(m_backpict
, &m_rect
);
466 KillPicture(m_backpict
);