]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/tooltip.cpp
clipboard
[wxWidgets.git] / src / mac / carbon / 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 if( wxApp::s_macDefaultEncodingIsPC )
194 m_label = wxMacMakeMacStringFromPC( text ) ;
195 else
196 m_label = text ;
197 m_window =win;
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 ) ;
203 }
204
205 wxMacToolTip::~wxMacToolTip()
206 {
207 if ( m_timer ) {
208 delete m_timer ;
209 m_timer = NULL;
210 }
211 if ( m_backpict )
212 Clear() ;
213 }
214
215 const short kTipBorder = 2 ;
216 const short kTipOffset = 5 ;
217
218 void wxMacToolTip::Draw()
219 {
220 if ( m_label.Length() == 0 )
221 return ;
222
223 if ( m_window == s_ToolTipWindowRef )
224 {
225 m_shown = true ;
226 #if TARGET_CARBON
227 if ( HMDisplayTag != (void*) kUnresolvedCFragSymbolAddress )
228 {
229 HMHelpContentRec tag ;
230 tag.version = kMacHelpVersion;
231 SetRect( &tag.absHotRect , m_position.x - 2 , m_position.y - 2 , m_position.x + 2 , m_position.y + 2 ) ;
232 GrafPtr port ;
233 GetPort( &port ) ;
234 SetPortWindowPort(m_window) ;
235 LocalToGlobal( (Point *) &tag.absHotRect.top );
236 LocalToGlobal( (Point *) &tag.absHotRect.bottom );
237 SetPort( port );
238 CFStringRef text = wxMacCreateCFString(m_label) ;
239 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
240 tag.content[kHMMinimumContentIndex].u.tagCFString = text ;
241 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
242 tag.content[kHMMaximumContentIndex].u.tagCFString = text ;
243 tag.tagSide = kHMDefaultSide;
244 HMDisplayTag( &tag );
245 CFRelease( text ) ;
246 }
247 else
248 #endif
249 {
250 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
251 #if TARGET_CARBON
252 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
253 #endif
254
255 FontFamilyID fontId ;
256 Str255 fontName ;
257 SInt16 fontSize ;
258 Style fontStyle ;
259 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
260 GetFNum( fontName, &fontId );
261
262 TextFont( fontId ) ;
263 TextSize( fontSize ) ;
264 TextFace( fontStyle ) ;
265 FontInfo fontInfo;
266 ::GetFontInfo(&fontInfo);
267 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
268 short height = 0 ;
269 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
270
271 int i = 0 ;
272 int length = m_label.Length() ;
273 int width = 0 ;
274 int thiswidth = 0 ;
275 int laststop = 0 ;
276 const char *text = m_label ;
277 while( i < length )
278 {
279 if( text[i] == 13 || text[i] == 10)
280 {
281 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
282 if ( thiswidth > width )
283 width = thiswidth ;
284
285 height += lineh ;
286 laststop = i+1 ;
287 }
288 i++ ;
289 }
290 if ( i - laststop > 0 )
291 {
292 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
293 if ( thiswidth > width )
294 width = thiswidth ;
295 height += lineh ;
296 }
297
298
299 m_rect.left = m_position.x + kTipOffset;
300 m_rect.top = m_position.y + kTipOffset;
301 m_rect.right = m_rect.left + width + 2 * kTipBorder;
302 #if TARGET_CARBON
303 if ( useDrawThemeText )
304 m_rect.right += kTipBorder ;
305 #endif
306 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
307 Rect r ;
308 GetPortBounds( GetWindowPort( m_window ) , &r ) ;
309 if ( m_rect.top < 0 )
310 {
311 m_rect.bottom += -m_rect.top ;
312 m_rect.top = 0 ;
313 }
314 if ( m_rect.left < 0 )
315 {
316 m_rect.right += -m_rect.left ;
317 m_rect.left = 0 ;
318 }
319 if ( m_rect.right > r.right )
320 {
321 m_rect.left -= (m_rect.right - r.right ) ;
322 m_rect.right = r.right ;
323 }
324 if ( m_rect.bottom > r.bottom )
325 {
326 m_rect.top -= (m_rect.bottom - r.bottom) ;
327 m_rect.bottom = r.bottom ;
328 }
329 ClipRect( &m_rect ) ;
330 BackColor( whiteColor ) ;
331 ForeColor(blackColor ) ;
332 GWorldPtr port ;
333 NewGWorld( &port , wxDisplayDepth() , &m_rect , NULL , NULL , 0 ) ;
334 CGrafPtr origPort ;
335 GDHandle origDevice ;
336
337 GetGWorld( &origPort , &origDevice ) ;
338 SetGWorld( port , NULL ) ;
339
340 m_backpict = OpenPicture(&m_rect);
341
342 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
343 GetPortBitMapForCopyBits(port),
344 &m_rect,
345 &m_rect,
346 srcCopy,
347 NULL);
348 ClosePicture();
349 SetGWorld( origPort , origDevice ) ;
350 DisposeGWorld( port ) ;
351 PenNormal() ;
352
353 RGBColor tooltipbackground = { 0xFFFF , 0xFFFF , 0xC000 } ;
354 BackColor( whiteColor ) ;
355 RGBForeColor( &tooltipbackground ) ;
356
357 PaintRect( &m_rect ) ;
358 ForeColor(blackColor ) ;
359 FrameRect( &m_rect ) ;
360 SetThemeTextColor(kThemeTextColorNotification,wxDisplayDepth(),true) ;
361 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
362
363 i = 0 ;
364 laststop = 0 ;
365 height = 0 ;
366
367 while( i < length )
368 {
369 if( text[i] == 13 || text[i] == 10)
370 {
371 #if TARGET_CARBON
372 if ( useDrawThemeText )
373 {
374 Rect frame ;
375 frame.top = m_rect.top + kTipBorder + height ;
376 frame.left = m_rect.left + kTipBorder ;
377 frame.bottom = frame.top + 1000 ;
378 frame.right = frame.left + 1000 ;
379 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
380 ::DrawThemeTextBox( mString,
381 kThemeCurrentPortFont,
382 kThemeStateActive,
383 true,
384 &frame,
385 teJustLeft,
386 nil );
387 CFRelease( mString ) ;
388 height += lineh ;
389 }
390 else
391 #endif
392 {
393 ::DrawText( text , laststop , i - laststop ) ;
394 height += lineh ;
395 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
396 }
397 laststop = i+1 ;
398 }
399 i++ ;
400 }
401 #if TARGET_CARBON
402 if ( useDrawThemeText )
403 {
404 Rect frame ;
405 frame.top = m_rect.top + kTipBorder + height ;
406 frame.left = m_rect.left + kTipBorder ;
407 frame.bottom = frame.top + 1000 ;
408 frame.right = frame.left + 1000 ;
409 CFStringRef mString = CFStringCreateWithCString( NULL , text + laststop , kCFStringEncodingMacRoman ) ;
410 ::DrawThemeTextBox( mString,
411 kThemeCurrentPortFont,
412 kThemeStateActive,
413 true,
414 &frame,
415 teJustLeft,
416 nil );
417 CFRelease( mString ) ;
418 }
419 else
420 #endif
421 {
422 ::DrawText( text , laststop , i - laststop ) ;
423 }
424 ::TextMode( srcOr ) ;
425 }
426 }
427 }
428
429 void wxToolTip::NotifyWindowDelete( WXHWND win )
430 {
431 if ( win == s_ToolTipWindowRef )
432 {
433 s_ToolTipWindowRef = NULL ;
434 }
435 }
436
437 void wxMacToolTip::Clear()
438 {
439 m_mark++ ;
440 if ( m_timer )
441 {
442 delete m_timer ;
443 m_timer = NULL ;
444 }
445 if ( !m_shown )
446 return ;
447 #if TARGET_CARBON
448 HMHideTag() ;
449 #else
450 if ( m_window == s_ToolTipWindowRef && m_backpict )
451 {
452 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
453
454 m_shown = false ;
455
456 BackColor( whiteColor ) ;
457 ForeColor(blackColor ) ;
458 DrawPicture(m_backpict, &m_rect);
459 KillPicture(m_backpict);
460 m_backpict = NULL ;
461 }
462 #endif
463 }
464
465 #endif
466