]> git.saurik.com Git - wxWidgets.git/blob - src/mac/tooltip.cpp
wxMac Unicode support
[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 , const 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 #if TARGET_CARBON
54 wxMacCFStringHolder m_helpTextRef ;
55 #endif
56 } ;
57
58 class wxMacToolTipTimer : public wxTimer
59 {
60 public:
61 wxMacToolTipTimer() {} ;
62 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
63 virtual ~wxMacToolTipTimer() {} ;
64 void Notify()
65 {
66 if ( m_mark == m_tip->GetMark() )
67 m_tip->Draw() ;
68 }
69 protected:
70 wxMacToolTip* m_tip;
71 long m_mark ;
72 };
73
74 //-----------------------------------------------------------------------------
75 // wxToolTip
76 //-----------------------------------------------------------------------------
77 static long s_ToolTipDelay = 500 ;
78 static bool s_ShowToolTips = true ;
79 static wxMacToolTip s_ToolTip ;
80 static wxWindow* s_LastWindowEntered = NULL ;
81 static wxRect2DInt s_ToolTipArea ;
82 static WindowRef s_ToolTipWindowRef = NULL ;
83
84 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
85
86 wxToolTip::wxToolTip( const wxString &tip )
87 {
88 m_text = tip;
89 m_window = (wxWindow*) NULL;
90 }
91
92 wxToolTip::~wxToolTip()
93 {
94 }
95
96 void wxToolTip::SetTip( const wxString &tip )
97 {
98 m_text = tip;
99
100 if ( m_window )
101 {
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);
108 */
109 }
110 }
111
112 void wxToolTip::SetWindow( wxWindow *win )
113 {
114 m_window = win ;
115 }
116
117 void wxToolTip::Enable( bool flag )
118 {
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 }
130 }
131
132 void wxToolTip::SetDelay( long msecs )
133 {
134 s_ToolTipDelay = msecs ;
135 }
136
137 void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
138 {
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
157 WindowRef window = MAC_WXHWND( win->MacGetRootWindow() ) ;
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 }
167 }
168
169 void wxToolTip::RemoveToolTips()
170 {
171 s_ToolTip.Clear() ;
172 }
173 // --- mac specific
174
175 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
176 {
177 m_tip = tip;
178 m_mark = tip->GetMark() ;
179 Start(msec, true);
180 }
181
182 wxMacToolTip::wxMacToolTip()
183 {
184 m_window = NULL ;
185 m_backpict = NULL ;
186 m_mark = 0 ;
187 m_shown = false ;
188 m_timer = NULL ;
189 }
190
191 void wxMacToolTip::Setup( WindowRef win , const wxString& text , wxPoint localPosition )
192 {
193 m_mark++ ;
194 Clear() ;
195 m_position = localPosition ;
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 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 ) ;
230 GrafPtr port ;
231 GetPort( &port ) ;
232 SetPortWindowPort(m_window) ;
233 LocalToGlobal( (Point *) &tag.absHotRect.top );
234 LocalToGlobal( (Point *) &tag.absHotRect.bottom );
235 SetPort( port );
236 m_helpTextRef = m_label ;
237 tag.content[kHMMinimumContentIndex].contentType = kHMCFStringContent ;
238 tag.content[kHMMinimumContentIndex].u.tagCFString = m_helpTextRef ;
239 tag.content[kHMMaximumContentIndex].contentType = kHMCFStringContent ;
240 tag.content[kHMMaximumContentIndex].u.tagCFString = m_helpTextRef ;
241 tag.tagSide = kHMDefaultSide;
242 HMDisplayTag( &tag );
243 #else
244 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
245 FontFamilyID fontId ;
246 Str255 fontName ;
247 SInt16 fontSize ;
248 Style fontStyle ;
249 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
250 GetFNum( fontName, &fontId );
251
252 TextFont( fontId ) ;
253 TextSize( fontSize ) ;
254 TextFace( fontStyle ) ;
255 FontInfo fontInfo;
256 ::GetFontInfo(&fontInfo);
257 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
258 short height = 0 ;
259 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
260
261 int i = 0 ;
262 int length = m_label.Length() ;
263 int width = 0 ;
264 int thiswidth = 0 ;
265 int laststop = 0 ;
266 wxCharBuffer text = wxMacStringToCString( m_label ) ;
267
268 while( i < length )
269 {
270 if( text[i] == 13 || text[i] == 10)
271 {
272 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
273 if ( thiswidth > width )
274 width = thiswidth ;
275
276 height += lineh ;
277 laststop = i+1 ;
278 }
279 i++ ;
280 }
281 if ( i - laststop > 0 )
282 {
283 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
284 if ( thiswidth > width )
285 width = thiswidth ;
286 height += lineh ;
287 }
288
289 m_rect.left = m_position.x + kTipOffset;
290 m_rect.top = m_position.y + kTipOffset;
291 m_rect.right = m_rect.left + width + 2 * kTipBorder;
292
293 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
294 Rect r ;
295 GetPortBounds( GetWindowPort( m_window ) , &r ) ;
296 if ( m_rect.top < 0 )
297 {
298 m_rect.bottom += -m_rect.top ;
299 m_rect.top = 0 ;
300 }
301 if ( m_rect.left < 0 )
302 {
303 m_rect.right += -m_rect.left ;
304 m_rect.left = 0 ;
305 }
306 if ( m_rect.right > r.right )
307 {
308 m_rect.left -= (m_rect.right - r.right ) ;
309 m_rect.right = r.right ;
310 }
311 if ( m_rect.bottom > r.bottom )
312 {
313 m_rect.top -= (m_rect.bottom - r.bottom) ;
314 m_rect.bottom = r.bottom ;
315 }
316 ClipRect( &m_rect ) ;
317 BackColor( whiteColor ) ;
318 ForeColor(blackColor ) ;
319 GWorldPtr port ;
320 NewGWorld( &port , wxDisplayDepth() , &m_rect , NULL , NULL , 0 ) ;
321 CGrafPtr origPort ;
322 GDHandle origDevice ;
323
324 GetGWorld( &origPort , &origDevice ) ;
325 SetGWorld( port , NULL ) ;
326
327 m_backpict = OpenPicture(&m_rect);
328
329 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
330 GetPortBitMapForCopyBits(port),
331 &m_rect,
332 &m_rect,
333 srcCopy,
334 NULL);
335 ClosePicture();
336 SetGWorld( origPort , origDevice ) ;
337 DisposeGWorld( port ) ;
338 PenNormal() ;
339
340 RGBColor tooltipbackground = { 0xFFFF , 0xFFFF , 0xC000 } ;
341 BackColor( whiteColor ) ;
342 RGBForeColor( &tooltipbackground ) ;
343
344 PaintRect( &m_rect ) ;
345 ForeColor(blackColor ) ;
346 FrameRect( &m_rect ) ;
347 SetThemeTextColor(kThemeTextColorNotification,wxDisplayDepth(),true) ;
348 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
349
350 i = 0 ;
351 laststop = 0 ;
352 height = 0 ;
353
354 while( i < length )
355 {
356 if( text[i] == 13 || text[i] == 10)
357 {
358 ::DrawText( text , laststop , i - laststop ) ;
359 height += lineh ;
360 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
361 laststop = i+1 ;
362 }
363 i++ ;
364 }
365 ::DrawText( text , laststop , i - laststop ) ;
366 ::TextMode( srcOr ) ;
367 #endif
368 }
369 }
370
371 void wxToolTip::NotifyWindowDelete( WXHWND win )
372 {
373 if ( win == s_ToolTipWindowRef )
374 {
375 s_ToolTipWindowRef = NULL ;
376 }
377 }
378
379 void wxMacToolTip::Clear()
380 {
381 m_mark++ ;
382 if ( m_timer )
383 {
384 delete m_timer ;
385 m_timer = NULL ;
386 }
387 if ( !m_shown )
388 return ;
389 #if TARGET_CARBON
390 HMHideTag() ;
391 m_helpTextRef.Release() ;
392 #else
393 if ( m_window == s_ToolTipWindowRef && m_backpict )
394 {
395 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
396
397 m_shown = false ;
398
399 BackColor( whiteColor ) ;
400 ForeColor(blackColor ) ;
401 DrawPicture(m_backpict, &m_rect);
402 KillPicture(m_backpict);
403 m_backpict = NULL ;
404 }
405 #endif
406 }
407
408 #endif
409