]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/tooltip.cpp
fixing the shrinking embedded controls
[wxWidgets.git] / src / mac / carbon / tooltip.cpp
CommitLineData
ee6b1d97 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/mac/carbon/tooltip.cpp
ee6b1d97 3// Purpose: wxToolTip implementation
92e6d869 4// Author: Stefan Csomor
ee6b1d97 5// Id: $Id$
92e6d869 6// Copyright: (c) Stefan Csomor
65571936 7// Licence: wxWindows licence
ee6b1d97
SC
8/////////////////////////////////////////////////////////////////////////////
9
3d1a4878 10#include "wx/wxprec.h"
ee6b1d97
SC
11
12#if wxUSE_TOOLTIPS
13
85855db1
WS
14#include "wx/tooltip.h"
15
670f9935
WS
16#ifndef WX_PRECOMP
17 #include "wx/app.h"
cdccdfab 18 #include "wx/window.h"
da80ae71 19 #include "wx/dc.h"
c0badb70 20 #include "wx/timer.h"
670f9935
WS
21#endif // WX_PRECOMP
22
ee6b1d97 23#include "wx/geometry.h"
ee6b1d97
SC
24#include "wx/mac/uma.h"
25
26//-----------------------------------------------------------------------------
27// global data
28//-----------------------------------------------------------------------------
29
76a5e5d2
SC
30class wxMacToolTipTimer ;
31
ee6b1d97
SC
32class wxMacToolTip
33{
991f71dc
DS
34public :
35 wxMacToolTip() ;
36 ~wxMacToolTip() ;
670f9935 37
991f71dc
DS
38 void Setup( WindowRef window , const wxString& text , const wxPoint& localPosition ) ;
39 void Draw() ;
40 void Clear() ;
41
42 long GetMark()
43 { return m_mark ; }
44
45 bool IsShown()
46 { return m_shown ; }
47
48private :
49 wxString m_label ;
50 wxPoint m_position ;
51 Rect m_rect ;
670f9935 52 WindowRef m_window ;
991f71dc
DS
53 PicHandle m_backpict ;
54 bool m_shown ;
55 long m_mark ;
72c1ba98 56#if wxUSE_TIMER
991f71dc 57 wxMacToolTipTimer* m_timer ;
72c1ba98 58#endif
427ff662 59#if TARGET_CARBON
991f71dc 60 wxMacCFStringHolder m_helpTextRef ;
6ef7c8e0 61#endif
ee6b1d97
SC
62} ;
63
72c1ba98 64#if wxUSE_TIMER
853e3ce9 65class wxMacToolTipTimer : public wxTimer
ee6b1d97
SC
66{
67public:
853e3ce9 68 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
991f71dc 69 wxMacToolTipTimer() {} ;
12cd5f34 70 virtual ~wxMacToolTipTimer() {} ;
991f71dc 71
853e3ce9
GD
72 void Notify()
73 {
74 if ( m_mark == m_tip->GetMark() )
670f9935 75 m_tip->Draw() ;
853e3ce9 76 }
991f71dc 77
ee6b1d97 78protected:
e40298d5
JS
79 wxMacToolTip* m_tip;
80 long m_mark ;
ee6b1d97 81};
72c1ba98 82#endif // wxUSE_TIMER
ee6b1d97
SC
83
84//-----------------------------------------------------------------------------
85// wxToolTip
86//-----------------------------------------------------------------------------
87static long s_ToolTipDelay = 500 ;
88static bool s_ShowToolTips = true ;
89static wxMacToolTip s_ToolTip ;
90static wxWindow* s_LastWindowEntered = NULL ;
91static wxRect2DInt s_ToolTipArea ;
92static WindowRef s_ToolTipWindowRef = NULL ;
f5ebf253
SC
93
94IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
95
991f71dc 96
ee6b1d97
SC
97wxToolTip::wxToolTip( const wxString &tip )
98{
99 m_text = tip;
100 m_window = (wxWindow*) NULL;
101}
102
103wxToolTip::~wxToolTip()
104{
105}
106
107void wxToolTip::SetTip( const wxString &tip )
108{
e40298d5 109 m_text = tip;
670f9935 110
ee6b1d97
SC
111 if ( m_window )
112 {
991f71dc 113#if 0
e40298d5
JS
114 // update it immediately
115 wxToolInfo ti(GetHwndOf(m_window));
116 ti.lpszText = (wxChar *)m_text.c_str();
670f9935
WS
117
118 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
991f71dc 119#endif
ee6b1d97
SC
120 }
121}
122
123void wxToolTip::SetWindow( wxWindow *win )
124{
7eb67c00 125 m_window = win ;
ee6b1d97
SC
126}
127
128void wxToolTip::Enable( bool flag )
129{
e40298d5
JS
130 if ( s_ShowToolTips != flag )
131 {
132 s_ShowToolTips = flag ;
670f9935 133
e40298d5
JS
134 if ( s_ShowToolTips )
135 {
136 }
137 else
138 {
139 s_ToolTip.Clear() ;
140 }
141 }
ee6b1d97
SC
142}
143
144void wxToolTip::SetDelay( long msecs )
145{
e40298d5 146 s_ToolTipDelay = msecs ;
ee6b1d97
SC
147}
148
149void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
150{
e40298d5
JS
151 if ( s_ShowToolTips )
152 {
153 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
154 {
155 s_ToolTip.Clear() ;
156 }
157 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
158 {
159 wxPoint2DInt where( event.m_x , event.m_y ) ;
160 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
161 {
162 }
163 else
164 {
165 s_ToolTip.Clear() ;
166 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
167 s_LastWindowEntered = win ;
670f9935 168
facd6764 169 WindowRef window = MAC_WXHWND( win->MacGetTopLevelWindowRef() ) ;
e40298d5
JS
170 int x = event.m_x ;
171 int y = event.m_y ;
172 wxPoint local( x , y ) ;
173 win->MacClientToRootWindow( &x, &y ) ;
174 wxPoint windowlocal( x , y ) ;
175 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
176 }
177 }
178 }
ee6b1d97
SC
179}
180
181void wxToolTip::RemoveToolTips()
182{
e40298d5 183 s_ToolTip.Clear() ;
ee6b1d97 184}
991f71dc 185
ee6b1d97 186// --- mac specific
72c1ba98 187#if wxUSE_TIMER
ee6b1d97
SC
188wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
189{
e40298d5
JS
190 m_tip = tip;
191 m_mark = tip->GetMark() ;
192 Start(msec, true);
ee6b1d97 193}
72c1ba98 194#endif // wxUSE_TIMER
ee6b1d97
SC
195
196wxMacToolTip::wxMacToolTip()
197{
e40298d5
JS
198 m_window = NULL ;
199 m_backpict = NULL ;
72c1ba98 200#if wxUSE_TIMER
991f71dc 201 m_timer = NULL ;
72c1ba98 202#endif
e40298d5
JS
203 m_mark = 0 ;
204 m_shown = false ;
ee6b1d97
SC
205}
206
670f9935 207void wxMacToolTip::Setup( WindowRef win , const wxString& text , const wxPoint& localPosition )
ee6b1d97 208{
e40298d5 209 m_mark++ ;
991f71dc 210
e40298d5
JS
211 Clear() ;
212 m_position = localPosition ;
427ff662 213 m_label = text ;
7eb67c00 214 m_window =win;
e40298d5
JS
215 s_ToolTipWindowRef = m_window ;
216 m_backpict = NULL ;
72c1ba98 217#if wxUSE_TIMER
e40298d5
JS
218 if ( m_timer )
219 delete m_timer ;
991f71dc 220
e40298d5 221 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
72c1ba98 222#endif // wxUSE_TIMER
ee6b1d97
SC
223}
224
670f9935 225wxMacToolTip::~wxMacToolTip()
ee6b1d97 226{
72c1ba98 227#if wxUSE_TIMER
991f71dc
DS
228 if ( m_timer )
229 {
76a5e5d2 230 delete m_timer ;
f5bb2251
GD
231 m_timer = NULL;
232 }
72c1ba98 233#endif // wxUSE_TIMER
670f9935 234 if ( m_backpict )
f5bb2251 235 Clear() ;
ee6b1d97
SC
236}
237
238const short kTipBorder = 2 ;
239const short kTipOffset = 5 ;
240
241void wxMacToolTip::Draw()
242{
670f9935 243 if ( m_label.empty() )
e40298d5 244 return ;
670f9935 245
e40298d5
JS
246 if ( m_window == s_ToolTipWindowRef )
247 {
248 m_shown = true ;
991f71dc 249
427ff662
SC
250 HMHelpContentRec tag ;
251 tag.version = kMacHelpVersion;
a9de2608 252
fb743cab
SC
253 Point p = { m_position.y , m_position.x };
254 wxMacLocalToGlobal( m_window , &p ) ;
255 SetRect( &tag.absHotRect , p.h - 2 , p.v - 2 , p.h + 2 , p.v + 2 );
a9de2608 256
991f71dc 257 m_helpTextRef.Assign( m_label , wxFONTENCODING_DEFAULT ) ;
427ff662
SC
258 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
259 tag.content[kHMMinimumContentIndex].u.tagCFString = m_helpTextRef ;
260 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
261 tag.content[kHMMaximumContentIndex].u.tagCFString = m_helpTextRef ;
262 tag.tagSide = kHMDefaultSide;
263 HMDisplayTag( &tag );
e40298d5 264 }
ee6b1d97
SC
265}
266
670f9935 267void wxToolTip::NotifyWindowDelete( WXHWND win )
ee6b1d97 268{
e40298d5 269 if ( win == s_ToolTipWindowRef )
e40298d5 270 s_ToolTipWindowRef = NULL ;
ee6b1d97
SC
271}
272
273void wxMacToolTip::Clear()
274{
e40298d5 275 m_mark++ ;
72c1ba98 276#if wxUSE_TIMER
e40298d5
JS
277 if ( m_timer )
278 {
279 delete m_timer ;
280 m_timer = NULL ;
281 }
72c1ba98 282#endif // wxUSE_TIMER
e40298d5
JS
283 if ( !m_shown )
284 return ;
670f9935 285
e40298d5 286 HMHideTag() ;
427ff662 287 m_helpTextRef.Release() ;
ee6b1d97
SC
288}
289
c0badb70 290#endif // wxUSE_TOOLTIPS