]> git.saurik.com Git - wxWidgets.git/blame - src/mac/tooltip.cpp
changed key handling to new carbon-savvy implementation
[wxWidgets.git] / src / mac / tooltip.cpp
CommitLineData
ee6b1d97
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: tooltip.cpp
3// Purpose: wxToolTip implementation
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
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{
34 public :
35 wxMacToolTip( ) ;
36 ~wxMacToolTip() ;
37
38 void Setup( WindowRef window , wxString text , wxPoint localPosition ) ;
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 ;
76a5e5d2 52 wxMacToolTipTimer* m_timer ;
ee6b1d97
SC
53} ;
54
853e3ce9 55class wxMacToolTipTimer : public wxTimer
ee6b1d97
SC
56{
57public:
853e3ce9
GD
58 wxMacToolTipTimer() {} ;
59 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
12cd5f34 60 virtual ~wxMacToolTipTimer() {} ;
853e3ce9
GD
61 void Notify()
62 {
63 if ( m_mark == m_tip->GetMark() )
64 m_tip->Draw() ;
65 }
ee6b1d97 66protected:
853e3ce9
GD
67 wxMacToolTip* m_tip;
68 long m_mark ;
ee6b1d97
SC
69};
70
71//-----------------------------------------------------------------------------
72// wxToolTip
73//-----------------------------------------------------------------------------
74static long s_ToolTipDelay = 500 ;
75static bool s_ShowToolTips = true ;
76static wxMacToolTip s_ToolTip ;
77static wxWindow* s_LastWindowEntered = NULL ;
78static wxRect2DInt s_ToolTipArea ;
79static WindowRef s_ToolTipWindowRef = NULL ;
f5ebf253
SC
80
81IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
82
ee6b1d97
SC
83wxToolTip::wxToolTip( const wxString &tip )
84{
85 m_text = tip;
86 m_window = (wxWindow*) NULL;
87}
88
89wxToolTip::~wxToolTip()
90{
91}
92
93void wxToolTip::SetTip( const wxString &tip )
94{
95 m_text = tip;
96
97 if ( m_window )
98 {
99 /*
100 // update it immediately
101 wxToolInfo ti(GetHwndOf(m_window));
102 ti.lpszText = (wxChar *)m_text.c_str();
103
104 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
105 */
106 }
107}
108
109void wxToolTip::SetWindow( wxWindow *win )
110{
7eb67c00 111 m_window = win ;
ee6b1d97
SC
112}
113
114void wxToolTip::Enable( bool flag )
115{
116 if ( s_ShowToolTips != flag )
117 {
118 s_ShowToolTips = flag ;
119 if ( s_ShowToolTips )
120 {
121 }
122 else
123 {
124 s_ToolTip.Clear() ;
125 }
126 }
127}
128
129void wxToolTip::SetDelay( long msecs )
130{
131 s_ToolTipDelay = msecs ;
132}
133
134void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
135{
136 if ( s_ShowToolTips )
137 {
138 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
139 {
140 s_ToolTip.Clear() ;
141 }
142 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
143 {
144 wxPoint2DInt where( event.m_x , event.m_y ) ;
145 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
146 {
147 }
148 else
149 {
150 s_ToolTip.Clear() ;
151 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
152 s_LastWindowEntered = win ;
153
76a5e5d2 154 WindowRef window = MAC_WXHWND( win->MacGetRootWindow() ) ;
ee6b1d97
SC
155 int x = event.m_x ;
156 int y = event.m_y ;
157 wxPoint local( x , y ) ;
158 win->MacClientToRootWindow( &x, &y ) ;
159 wxPoint windowlocal( x , y ) ;
160 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
161 }
162 }
163 }
164}
165
166void wxToolTip::RemoveToolTips()
167{
168 s_ToolTip.Clear() ;
169}
170// --- mac specific
171
172wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
173{
174 m_tip = tip;
175 m_mark = tip->GetMark() ;
176 Start(msec, true);
177}
178
179wxMacToolTip::wxMacToolTip()
180{
181 m_window = NULL ;
182 m_backpict = NULL ;
183 m_mark = 0 ;
184 m_shown = false ;
76a5e5d2 185 m_timer = NULL ;
ee6b1d97
SC
186}
187
7eb67c00 188void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition )
ee6b1d97
SC
189{
190 m_mark++ ;
191 Clear() ;
12cd5f34 192 m_position = localPosition ;
ee6b1d97 193 m_label = wxMacMakeMacStringFromPC( text ) ;
7eb67c00
SC
194 m_window =win;
195 s_ToolTipWindowRef = m_window ;
ee6b1d97 196 m_backpict = NULL ;
76a5e5d2
SC
197 if ( m_timer )
198 delete m_timer ;
199 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
ee6b1d97
SC
200}
201
202wxMacToolTip::~wxMacToolTip()
203{
76a5e5d2
SC
204 if ( m_timer )
205 delete m_timer ;
ee6b1d97
SC
206 if ( m_backpict )
207 Clear() ;
208}
209
210const short kTipBorder = 2 ;
211const short kTipOffset = 5 ;
212
213void wxMacToolTip::Draw()
214{
215 if ( m_label.Length() == 0 )
216 return ;
217
218 if ( m_window == s_ToolTipWindowRef )
219 {
a7b04cfc
SC
220#if TARGET_CARBON
221/*
222 if ( HMDisplayTag != (void*) kUnresolvedCFragSymbolAddress )
223 {
224 HMDisplayTag(
225 }
226 else
227*/
228#endif
229 {
66a09d47 230 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
61eb9fa1 231#if TARGET_CARBON
7eb67c00 232 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
61eb9fa1 233#endif
a7b04cfc
SC
234 m_shown = true ;
235
7eb67c00
SC
236 FontFamilyID fontId ;
237 Str255 fontName ;
238 SInt16 fontSize ;
239 Style fontStyle ;
240 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
241 GetFNum( fontName, &fontId );
242
243 TextFont( fontId ) ;
244 TextSize( fontSize ) ;
245 TextFace( fontStyle ) ;
a7b04cfc
SC
246 FontInfo fontInfo;
247 ::GetFontInfo(&fontInfo);
248 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
249 short height = 0 ;
250 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
251
252 int i = 0 ;
253 int length = m_label.Length() ;
254 int width = 0 ;
255 int thiswidth = 0 ;
256 int laststop = 0 ;
257 const char *text = m_label ;
258 while( i < length )
259 {
260 if( text[i] == 13 || text[i] == 10)
261 {
262 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
263 if ( thiswidth > width )
264 width = thiswidth ;
265
266 height += lineh ;
267 laststop = i+1 ;
268 }
269 i++ ;
270 }
271 if ( i - laststop > 0 )
272 {
273 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
274 if ( thiswidth > width )
275 width = thiswidth ;
276 height += lineh ;
277 }
278
279
280 m_rect.left = m_position.x + kTipOffset;
281 m_rect.top = m_position.y + kTipOffset;
282 m_rect.right = m_rect.left + width + 2 * kTipBorder;
61eb9fa1 283#if TARGET_CARBON
7eb67c00
SC
284 if ( useDrawThemeText )
285 m_rect.right += kTipBorder ;
61eb9fa1 286#endif
a7b04cfc
SC
287 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
288 ClipRect( &m_rect ) ;
289 BackColor( whiteColor ) ;
290 ForeColor(blackColor ) ;
291 m_backpict = OpenPicture(&m_rect);
292
293 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
294 GetPortBitMapForCopyBits(GetWindowPort(m_window)),
295 &m_rect,
296 &m_rect,
297 srcCopy,
298 NULL);
299
300 ClosePicture();
7eb67c00
SC
301 PenNormal() ;
302
303 RGBColor tooltipbackground = { 0xFFFF , 0xFFFF , 0xC000 } ;
304 BackColor( whiteColor ) ;
305 RGBForeColor( &tooltipbackground ) ;
306
307 PaintRect( &m_rect ) ;
308 ForeColor(blackColor ) ;
a7b04cfc 309 FrameRect( &m_rect ) ;
7eb67c00 310 SetThemeTextColor(kThemeTextColorNotification,wxDisplayDepth(),true) ;
a7b04cfc
SC
311 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
312
313 i = 0 ;
314 laststop = 0 ;
315 height = 0 ;
7eb67c00 316
a7b04cfc
SC
317 while( i < length )
318 {
319 if( text[i] == 13 || text[i] == 10)
320 {
61eb9fa1 321#if TARGET_CARBON
7eb67c00
SC
322 if ( useDrawThemeText )
323 {
324 Rect frame ;
325 frame.top = m_rect.top + kTipBorder + height ;
326 frame.left = m_rect.left + kTipBorder ;
327 frame.bottom = frame.top + 1000 ;
328 frame.right = frame.left + 1000 ;
329 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
330 ::DrawThemeTextBox( mString,
331 kThemeCurrentPortFont,
332 kThemeStateActive,
333 true,
334 &frame,
335 teJustLeft,
336 nil );
337 CFRelease( mString ) ;
338 height += lineh ;
339 }
340 else
61eb9fa1 341#endif
7eb67c00
SC
342 {
343 ::DrawText( text , laststop , i - laststop ) ;
344 height += lineh ;
345 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
346 }
a7b04cfc
SC
347 laststop = i+1 ;
348 }
349 i++ ;
350 }
61eb9fa1 351#if TARGET_CARBON
7eb67c00
SC
352 if ( useDrawThemeText )
353 {
354 Rect frame ;
355 frame.top = m_rect.top + kTipBorder + height ;
356 frame.left = m_rect.left + kTipBorder ;
357 frame.bottom = frame.top + 1000 ;
358 frame.right = frame.left + 1000 ;
359 CFStringRef mString = CFStringCreateWithCString( NULL , text + laststop , kCFStringEncodingMacRoman ) ;
360 ::DrawThemeTextBox( mString,
361 kThemeCurrentPortFont,
362 kThemeStateActive,
363 true,
364 &frame,
365 teJustLeft,
366 nil );
367 CFRelease( mString ) ;
368 }
369 else
61eb9fa1 370#endif
7eb67c00
SC
371 {
372 ::DrawText( text , laststop , i - laststop ) ;
373 }
a7b04cfc 374 ::TextMode( srcOr ) ;
7eb67c00 375 }
ee6b1d97
SC
376 }
377}
378
76a5e5d2 379void wxToolTip::NotifyWindowDelete( WXHWND win )
ee6b1d97
SC
380{
381 if ( win == s_ToolTipWindowRef )
382 {
383 s_ToolTipWindowRef = NULL ;
384 }
385}
386
387void wxMacToolTip::Clear()
388{
389 m_mark++ ;
12cd5f34
SC
390 if ( m_timer )
391 {
392 delete m_timer ;
393 m_timer = NULL ;
394 }
ee6b1d97
SC
395 if ( !m_shown )
396 return ;
397
398 if ( m_window == s_ToolTipWindowRef && m_backpict )
399 {
66a09d47 400 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
76a5e5d2 401
ee6b1d97
SC
402 m_shown = false ;
403
ee6b1d97
SC
404 BackColor( whiteColor ) ;
405 ForeColor(blackColor ) ;
406 DrawPicture(m_backpict, &m_rect);
407 KillPicture(m_backpict);
408 m_backpict = NULL ;
409 }
410}
411
412#endif
413