]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/tooltip.cpp
making tool enable consistent (was using mix of enable and activate controls before)
[wxWidgets.git] / src / mac / carbon / tooltip.cpp
CommitLineData
ee6b1d97
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: tooltip.cpp
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
10#ifdef __GNUG__
11 #pragma implementation "tooltip.h"
12#endif
13
853e3ce9 14#include "wx/defs.h"
ee6b1d97
SC
15
16#if wxUSE_TOOLTIPS
17
03e11df5 18#include "wx/app.h"
5fde6fcc 19#include "wx/dc.h"
ee6b1d97
SC
20#include "wx/window.h"
21#include "wx/tooltip.h"
03e11df5 22#include "wx/timer.h"
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{
e40298d5
JS
34 public :
35 wxMacToolTip( ) ;
36 ~wxMacToolTip() ;
37
427ff662 38 void Setup( WindowRef window , const wxString& text , wxPoint localPosition ) ;
e40298d5
JS
39 long GetMark() { return m_mark ; }
40 void Draw() ;
41 void Clear() ;
42 bool IsShown() { return m_shown ; }
43 private :
44
45 wxString m_label ;
46 wxPoint m_position ;
47 Rect m_rect ;
48 WindowRef m_window ;
49 PicHandle m_backpict ;
50 bool m_shown ;
51 long m_mark ;
52 wxMacToolTipTimer* m_timer ;
427ff662
SC
53#if TARGET_CARBON
54 wxMacCFStringHolder m_helpTextRef ;
6ef7c8e0 55#endif
ee6b1d97
SC
56} ;
57
853e3ce9 58class wxMacToolTipTimer : public wxTimer
ee6b1d97
SC
59{
60public:
853e3ce9
GD
61 wxMacToolTipTimer() {} ;
62 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
12cd5f34 63 virtual ~wxMacToolTipTimer() {} ;
853e3ce9
GD
64 void Notify()
65 {
66 if ( m_mark == m_tip->GetMark() )
67 m_tip->Draw() ;
68 }
ee6b1d97 69protected:
e40298d5
JS
70 wxMacToolTip* m_tip;
71 long m_mark ;
ee6b1d97
SC
72};
73
74//-----------------------------------------------------------------------------
75// wxToolTip
76//-----------------------------------------------------------------------------
77static long s_ToolTipDelay = 500 ;
78static bool s_ShowToolTips = true ;
79static wxMacToolTip s_ToolTip ;
80static wxWindow* s_LastWindowEntered = NULL ;
81static wxRect2DInt s_ToolTipArea ;
82static WindowRef s_ToolTipWindowRef = NULL ;
f5ebf253
SC
83
84IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
85
ee6b1d97
SC
86wxToolTip::wxToolTip( const wxString &tip )
87{
88 m_text = tip;
89 m_window = (wxWindow*) NULL;
90}
91
92wxToolTip::~wxToolTip()
93{
94}
95
96void wxToolTip::SetTip( const wxString &tip )
97{
e40298d5
JS
98 m_text = tip;
99
ee6b1d97
SC
100 if ( m_window )
101 {
e40298d5
JS
102 /*
103 // update it immediately
104 wxToolInfo ti(GetHwndOf(m_window));
105 ti.lpszText = (wxChar *)m_text.c_str();
106
107 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
ee6b1d97
SC
108 */
109 }
110}
111
112void wxToolTip::SetWindow( wxWindow *win )
113{
7eb67c00 114 m_window = win ;
ee6b1d97
SC
115}
116
117void wxToolTip::Enable( bool flag )
118{
e40298d5
JS
119 if ( s_ShowToolTips != flag )
120 {
121 s_ShowToolTips = flag ;
122 if ( s_ShowToolTips )
123 {
124 }
125 else
126 {
127 s_ToolTip.Clear() ;
128 }
129 }
ee6b1d97
SC
130}
131
132void wxToolTip::SetDelay( long msecs )
133{
e40298d5 134 s_ToolTipDelay = msecs ;
ee6b1d97
SC
135}
136
137void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
138{
e40298d5
JS
139 if ( s_ShowToolTips )
140 {
141 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
142 {
143 s_ToolTip.Clear() ;
144 }
145 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
146 {
147 wxPoint2DInt where( event.m_x , event.m_y ) ;
148 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
149 {
150 }
151 else
152 {
153 s_ToolTip.Clear() ;
154 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
155 s_LastWindowEntered = win ;
156
facd6764 157 WindowRef window = MAC_WXHWND( win->MacGetTopLevelWindowRef() ) ;
e40298d5
JS
158 int x = event.m_x ;
159 int y = event.m_y ;
160 wxPoint local( x , y ) ;
161 win->MacClientToRootWindow( &x, &y ) ;
162 wxPoint windowlocal( x , y ) ;
163 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
164 }
165 }
166 }
ee6b1d97
SC
167}
168
169void wxToolTip::RemoveToolTips()
170{
e40298d5 171 s_ToolTip.Clear() ;
ee6b1d97
SC
172}
173// --- mac specific
174
175wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
176{
e40298d5
JS
177 m_tip = tip;
178 m_mark = tip->GetMark() ;
179 Start(msec, true);
ee6b1d97
SC
180}
181
182wxMacToolTip::wxMacToolTip()
183{
e40298d5
JS
184 m_window = NULL ;
185 m_backpict = NULL ;
186 m_mark = 0 ;
187 m_shown = false ;
76a5e5d2 188 m_timer = NULL ;
ee6b1d97
SC
189}
190
427ff662 191void wxMacToolTip::Setup( WindowRef win , const wxString& text , wxPoint localPosition )
ee6b1d97 192{
e40298d5
JS
193 m_mark++ ;
194 Clear() ;
195 m_position = localPosition ;
427ff662 196 m_label = text ;
7eb67c00 197 m_window =win;
e40298d5
JS
198 s_ToolTipWindowRef = m_window ;
199 m_backpict = NULL ;
200 if ( m_timer )
201 delete m_timer ;
202 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
ee6b1d97
SC
203}
204
205wxMacToolTip::~wxMacToolTip()
206{
f5bb2251 207 if ( m_timer ) {
76a5e5d2 208 delete m_timer ;
f5bb2251
GD
209 m_timer = NULL;
210 }
211 if ( m_backpict )
212 Clear() ;
ee6b1d97
SC
213}
214
215const short kTipBorder = 2 ;
216const short kTipOffset = 5 ;
217
218void wxMacToolTip::Draw()
219{
e40298d5
JS
220 if ( m_label.Length() == 0 )
221 return ;
222
223 if ( m_window == s_ToolTipWindowRef )
224 {
225 m_shown = true ;
a7b04cfc 226#if TARGET_CARBON
427ff662
SC
227 HMHelpContentRec tag ;
228 tag.version = kMacHelpVersion;
229 SetRect( &tag.absHotRect , m_position.x - 2 , m_position.y - 2 , m_position.x + 2 , m_position.y + 2 ) ;
a9de2608
SC
230
231 QDLocalToGlobalRect( GetWindowPort( m_window ) , &tag.absHotRect ) ;
232
a9412f8f 233 m_helpTextRef.Assign( m_label , wxFONTENCODING_DEFAULT ) ;
427ff662
SC
234 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
235 tag.content[kHMMinimumContentIndex].u.tagCFString = m_helpTextRef ;
236 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
237 tag.content[kHMMaximumContentIndex].u.tagCFString = m_helpTextRef ;
238 tag.tagSide = kHMDefaultSide;
239 HMDisplayTag( &tag );
240#else
241 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
242 FontFamilyID fontId ;
243 Str255 fontName ;
244 SInt16 fontSize ;
245 Style fontStyle ;
246 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
247 GetFNum( fontName, &fontId );
248
249 TextFont( fontId ) ;
250 TextSize( fontSize ) ;
251 TextFace( fontStyle ) ;
252 FontInfo fontInfo;
253 ::GetFontInfo(&fontInfo);
254 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
255 short height = 0 ;
427ff662
SC
256
257 int i = 0 ;
258 int length = m_label.Length() ;
259 int width = 0 ;
260 int thiswidth = 0 ;
261 int laststop = 0 ;
939fba6c 262 wxCharBuffer text = m_label.mb_str( wxConvLocal) ;
427ff662
SC
263
264 while( i < length )
e40298d5 265 {
427ff662 266 if( text[i] == 13 || text[i] == 10)
e40298d5
JS
267 {
268 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
269 if ( thiswidth > width )
270 width = thiswidth ;
427ff662 271
e40298d5 272 height += lineh ;
427ff662 273 laststop = i+1 ;
e40298d5 274 }
427ff662
SC
275 i++ ;
276 }
277 if ( i - laststop > 0 )
278 {
279 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
280 if ( thiswidth > width )
281 width = thiswidth ;
282 height += lineh ;
283 }
284
285 m_rect.left = m_position.x + kTipOffset;
286 m_rect.top = m_position.y + kTipOffset;
287 m_rect.right = m_rect.left + width + 2 * kTipBorder;
288
289 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
290 Rect r ;
291 GetPortBounds( GetWindowPort( m_window ) , &r ) ;
292 if ( m_rect.top < 0 )
293 {
294 m_rect.bottom += -m_rect.top ;
295 m_rect.top = 0 ;
296 }
297 if ( m_rect.left < 0 )
298 {
299 m_rect.right += -m_rect.left ;
300 m_rect.left = 0 ;
301 }
302 if ( m_rect.right > r.right )
303 {
304 m_rect.left -= (m_rect.right - r.right ) ;
305 m_rect.right = r.right ;
306 }
307 if ( m_rect.bottom > r.bottom )
308 {
309 m_rect.top -= (m_rect.bottom - r.bottom) ;
310 m_rect.bottom = r.bottom ;
311 }
312 ClipRect( &m_rect ) ;
313 BackColor( whiteColor ) ;
314 ForeColor(blackColor ) ;
315 GWorldPtr port ;
316 NewGWorld( &port , wxDisplayDepth() , &m_rect , NULL , NULL , 0 ) ;
317 CGrafPtr origPort ;
318 GDHandle origDevice ;
319
320 GetGWorld( &origPort , &origDevice ) ;
321 SetGWorld( port , NULL ) ;
322
323 m_backpict = OpenPicture(&m_rect);
324
325 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
326 GetPortBitMapForCopyBits(port),
327 &m_rect,
328 &m_rect,
329 srcCopy,
330 NULL);
331 ClosePicture();
332 SetGWorld( origPort , origDevice ) ;
333 DisposeGWorld( port ) ;
334 PenNormal() ;
335
336 RGBColor tooltipbackground = { 0xFFFF , 0xFFFF , 0xC000 } ;
337 BackColor( whiteColor ) ;
338 RGBForeColor( &tooltipbackground ) ;
339
340 PaintRect( &m_rect ) ;
341 ForeColor(blackColor ) ;
342 FrameRect( &m_rect ) ;
343 SetThemeTextColor(kThemeTextColorNotification,wxDisplayDepth(),true) ;
344 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
345
346 i = 0 ;
347 laststop = 0 ;
348 height = 0 ;
349
350 while( i < length )
351 {
352 if( text[i] == 13 || text[i] == 10)
e40298d5
JS
353 {
354 ::DrawText( text , laststop , i - laststop ) ;
427ff662
SC
355 height += lineh ;
356 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
357 laststop = i+1 ;
e40298d5 358 }
427ff662
SC
359 i++ ;
360 }
361 ::DrawText( text , laststop , i - laststop ) ;
362 ::TextMode( srcOr ) ;
363#endif
e40298d5 364 }
ee6b1d97
SC
365}
366
76a5e5d2 367void wxToolTip::NotifyWindowDelete( WXHWND win )
ee6b1d97 368{
e40298d5
JS
369 if ( win == s_ToolTipWindowRef )
370 {
371 s_ToolTipWindowRef = NULL ;
372 }
ee6b1d97
SC
373}
374
375void wxMacToolTip::Clear()
376{
e40298d5
JS
377 m_mark++ ;
378 if ( m_timer )
379 {
380 delete m_timer ;
381 m_timer = NULL ;
382 }
383 if ( !m_shown )
384 return ;
a19f009b 385#if TARGET_CARBON
e40298d5 386 HMHideTag() ;
427ff662 387 m_helpTextRef.Release() ;
e40298d5
JS
388#else
389 if ( m_window == s_ToolTipWindowRef && m_backpict )
390 {
391 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
76a5e5d2 392
e40298d5 393 m_shown = false ;
ee6b1d97 394
e40298d5
JS
395 BackColor( whiteColor ) ;
396 ForeColor(blackColor ) ;
397 DrawPicture(m_backpict, &m_rect);
398 KillPicture(m_backpict);
399 m_backpict = NULL ;
400 }
a19f009b 401#endif
ee6b1d97
SC
402}
403
404#endif
405