]> git.saurik.com Git - wxWidgets.git/blob - src/mac/tooltip.cpp
317fe723fdbb36e64855ab0e1d5e5455190eb3c5
[wxWidgets.git] / src / mac / tooltip.cpp
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
14 #include "wx/defs.h"
15
16 #if wxUSE_TOOLTIPS
17
18 #include "wx/app.h"
19 #include "wx/dc.h"
20 #include "wx/window.h"
21 #include "wx/tooltip.h"
22 #include "wx/timer.h"
23 #include "wx/geometry.h"
24 #include "wx/mac/uma.h"
25
26 //-----------------------------------------------------------------------------
27 // global data
28 //-----------------------------------------------------------------------------
29
30 class wxMacToolTipTimer ;
31
32 class 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 ;
52 wxMacToolTipTimer* m_timer ;
53 } ;
54
55 class wxMacToolTipTimer : public wxTimer
56 {
57 public:
58 wxMacToolTipTimer() {} ;
59 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
60 virtual ~wxMacToolTipTimer() {} ;
61 void Notify()
62 {
63 if ( m_mark == m_tip->GetMark() )
64 m_tip->Draw() ;
65 }
66 protected:
67 wxMacToolTip* m_tip;
68 long m_mark ;
69 };
70
71 //-----------------------------------------------------------------------------
72 // wxToolTip
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 ;
80
81 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
82
83 wxToolTip::wxToolTip( const wxString &tip )
84 {
85 m_text = tip;
86 m_window = (wxWindow*) NULL;
87 }
88
89 wxToolTip::~wxToolTip()
90 {
91 }
92
93 void 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
109 void wxToolTip::SetWindow( wxWindow *win )
110 {
111 m_window = win ;
112 }
113
114 void 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
129 void wxToolTip::SetDelay( long msecs )
130 {
131 s_ToolTipDelay = msecs ;
132 }
133
134 void 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
154 WindowRef window = MAC_WXHWND( win->MacGetRootWindow() ) ;
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
166 void wxToolTip::RemoveToolTips()
167 {
168 s_ToolTip.Clear() ;
169 }
170 // --- mac specific
171
172 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
173 {
174 m_tip = tip;
175 m_mark = tip->GetMark() ;
176 Start(msec, true);
177 }
178
179 wxMacToolTip::wxMacToolTip()
180 {
181 m_window = NULL ;
182 m_backpict = NULL ;
183 m_mark = 0 ;
184 m_shown = false ;
185 m_timer = NULL ;
186 }
187
188 void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition )
189 {
190 m_mark++ ;
191 Clear() ;
192 m_position = localPosition ;
193 m_label = wxMacMakeMacStringFromPC( text ) ;
194 m_window =win;
195 s_ToolTipWindowRef = m_window ;
196 m_backpict = NULL ;
197 if ( m_timer )
198 delete m_timer ;
199 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
200 }
201
202 wxMacToolTip::~wxMacToolTip()
203 {
204 if ( m_timer ) {
205 delete m_timer ;
206 m_timer = NULL;
207 }
208 if ( m_backpict )
209 Clear() ;
210 }
211
212 const short kTipBorder = 2 ;
213 const short kTipOffset = 5 ;
214
215 void wxMacToolTip::Draw()
216 {
217 if ( m_label.Length() == 0 )
218 return ;
219
220 if ( m_window == s_ToolTipWindowRef )
221 {
222 #if TARGET_CARBON
223 /*
224 if ( HMDisplayTag != (void*) kUnresolvedCFragSymbolAddress )
225 {
226 HMDisplayTag(
227 }
228 else
229 */
230 #endif
231 {
232 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
233 #if TARGET_CARBON
234 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
235 #endif
236 m_shown = true ;
237
238 FontFamilyID fontId ;
239 Str255 fontName ;
240 SInt16 fontSize ;
241 Style fontStyle ;
242 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
243 GetFNum( fontName, &fontId );
244
245 TextFont( fontId ) ;
246 TextSize( fontSize ) ;
247 TextFace( fontStyle ) ;
248 FontInfo fontInfo;
249 ::GetFontInfo(&fontInfo);
250 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
251 short height = 0 ;
252 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
253
254 int i = 0 ;
255 int length = m_label.Length() ;
256 int width = 0 ;
257 int thiswidth = 0 ;
258 int laststop = 0 ;
259 const char *text = m_label ;
260 while( i < length )
261 {
262 if( text[i] == 13 || text[i] == 10)
263 {
264 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
265 if ( thiswidth > width )
266 width = thiswidth ;
267
268 height += lineh ;
269 laststop = i+1 ;
270 }
271 i++ ;
272 }
273 if ( i - laststop > 0 )
274 {
275 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
276 if ( thiswidth > width )
277 width = thiswidth ;
278 height += lineh ;
279 }
280
281
282 m_rect.left = m_position.x + kTipOffset;
283 m_rect.top = m_position.y + kTipOffset;
284 m_rect.right = m_rect.left + width + 2 * kTipBorder;
285 #if TARGET_CARBON
286 if ( useDrawThemeText )
287 m_rect.right += kTipBorder ;
288 #endif
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)
353 {
354 #if TARGET_CARBON
355 if ( useDrawThemeText )
356 {
357 Rect frame ;
358 frame.top = m_rect.top + kTipBorder + height ;
359 frame.left = m_rect.left + kTipBorder ;
360 frame.bottom = frame.top + 1000 ;
361 frame.right = frame.left + 1000 ;
362 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
363 ::DrawThemeTextBox( mString,
364 kThemeCurrentPortFont,
365 kThemeStateActive,
366 true,
367 &frame,
368 teJustLeft,
369 nil );
370 CFRelease( mString ) ;
371 height += lineh ;
372 }
373 else
374 #endif
375 {
376 ::DrawText( text , laststop , i - laststop ) ;
377 height += lineh ;
378 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
379 }
380 laststop = i+1 ;
381 }
382 i++ ;
383 }
384 #if TARGET_CARBON
385 if ( useDrawThemeText )
386 {
387 Rect frame ;
388 frame.top = m_rect.top + kTipBorder + height ;
389 frame.left = m_rect.left + kTipBorder ;
390 frame.bottom = frame.top + 1000 ;
391 frame.right = frame.left + 1000 ;
392 CFStringRef mString = CFStringCreateWithCString( NULL , text + laststop , kCFStringEncodingMacRoman ) ;
393 ::DrawThemeTextBox( mString,
394 kThemeCurrentPortFont,
395 kThemeStateActive,
396 true,
397 &frame,
398 teJustLeft,
399 nil );
400 CFRelease( mString ) ;
401 }
402 else
403 #endif
404 {
405 ::DrawText( text , laststop , i - laststop ) ;
406 }
407 ::TextMode( srcOr ) ;
408 }
409 }
410 }
411
412 void wxToolTip::NotifyWindowDelete( WXHWND win )
413 {
414 if ( win == s_ToolTipWindowRef )
415 {
416 s_ToolTipWindowRef = NULL ;
417 }
418 }
419
420 void wxMacToolTip::Clear()
421 {
422 m_mark++ ;
423 if ( m_timer )
424 {
425 delete m_timer ;
426 m_timer = NULL ;
427 }
428 if ( !m_shown )
429 return ;
430
431 if ( m_window == s_ToolTipWindowRef && m_backpict )
432 {
433 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
434
435 m_shown = false ;
436
437 BackColor( whiteColor ) ;
438 ForeColor(blackColor ) ;
439 DrawPicture(m_backpict, &m_rect);
440 KillPicture(m_backpict);
441 m_backpict = NULL ;
442 }
443 }
444
445 #endif
446