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