]> git.saurik.com Git - wxWidgets.git/blame - src/mac/tooltip.cpp
use wxMac provided wxGetOsDescription with Apple DevTools
[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
14#include "wx/setup.h"
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
55class wxMacToolTipTimer : wxTimer
56{
57public:
12cd5f34 58 wxMacToolTipTimer() {} ;
ee6b1d97 59 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
12cd5f34 60 virtual ~wxMacToolTipTimer() {} ;
ee6b1d97
SC
61 void Notify()
62 {
63 if ( m_mark == m_tip->GetMark() )
64 m_tip->Draw() ;
65
ee6b1d97 66 }
ee6b1d97
SC
67protected:
68 wxMacToolTip* m_tip;
69 long m_mark ;
70};
71
72//-----------------------------------------------------------------------------
73// wxToolTip
74//-----------------------------------------------------------------------------
75static long s_ToolTipDelay = 500 ;
76static bool s_ShowToolTips = true ;
77static wxMacToolTip s_ToolTip ;
78static wxWindow* s_LastWindowEntered = NULL ;
79static wxRect2DInt s_ToolTipArea ;
80static WindowRef s_ToolTipWindowRef = NULL ;
81wxToolTip::wxToolTip( const wxString &tip )
82{
83 m_text = tip;
84 m_window = (wxWindow*) NULL;
85}
86
87wxToolTip::~wxToolTip()
88{
89}
90
91void wxToolTip::SetTip( const wxString &tip )
92{
93 m_text = tip;
94
95 if ( m_window )
96 {
97 /*
98 // update it immediately
99 wxToolInfo ti(GetHwndOf(m_window));
100 ti.lpszText = (wxChar *)m_text.c_str();
101
102 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
103 */
104 }
105}
106
107void wxToolTip::SetWindow( wxWindow *win )
108{
109 m_window = win;
110}
111
112void wxToolTip::Enable( bool flag )
113{
114 if ( s_ShowToolTips != flag )
115 {
116 s_ShowToolTips = flag ;
117 if ( s_ShowToolTips )
118 {
119 }
120 else
121 {
122 s_ToolTip.Clear() ;
123 }
124 }
125}
126
127void wxToolTip::SetDelay( long msecs )
128{
129 s_ToolTipDelay = msecs ;
130}
131
132void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
133{
134 if ( s_ShowToolTips )
135 {
136 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
137 {
138 s_ToolTip.Clear() ;
139 }
140 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
141 {
142 wxPoint2DInt where( event.m_x , event.m_y ) ;
143 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
144 {
145 }
146 else
147 {
148 s_ToolTip.Clear() ;
149 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
150 s_LastWindowEntered = win ;
151
76a5e5d2 152 WindowRef window = MAC_WXHWND( win->MacGetRootWindow() ) ;
ee6b1d97
SC
153 int x = event.m_x ;
154 int y = event.m_y ;
155 wxPoint local( x , y ) ;
156 win->MacClientToRootWindow( &x, &y ) ;
157 wxPoint windowlocal( x , y ) ;
158 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
159 }
160 }
161 }
162}
163
164void wxToolTip::RemoveToolTips()
165{
166 s_ToolTip.Clear() ;
167}
168// --- mac specific
169
170wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
171{
172 m_tip = tip;
173 m_mark = tip->GetMark() ;
174 Start(msec, true);
175}
176
177wxMacToolTip::wxMacToolTip()
178{
179 m_window = NULL ;
180 m_backpict = NULL ;
181 m_mark = 0 ;
182 m_shown = false ;
76a5e5d2 183 m_timer = NULL ;
ee6b1d97
SC
184}
185
186void wxMacToolTip::Setup( WindowRef window , wxString text , wxPoint localPosition )
187{
188 m_mark++ ;
189 Clear() ;
12cd5f34 190 m_position = localPosition ;
ee6b1d97
SC
191 m_label = wxMacMakeMacStringFromPC( text ) ;
192 m_window = window ;
193 s_ToolTipWindowRef = window ;
194 m_backpict = NULL ;
76a5e5d2
SC
195 if ( m_timer )
196 delete m_timer ;
197 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
ee6b1d97
SC
198}
199
200wxMacToolTip::~wxMacToolTip()
201{
76a5e5d2
SC
202 if ( m_timer )
203 delete m_timer ;
ee6b1d97
SC
204 if ( m_backpict )
205 Clear() ;
206}
207
208const short kTipBorder = 2 ;
209const short kTipOffset = 5 ;
210
211void wxMacToolTip::Draw()
212{
213 if ( m_label.Length() == 0 )
214 return ;
215
216 if ( m_window == s_ToolTipWindowRef )
217 {
a7b04cfc
SC
218#if TARGET_CARBON
219/*
220 if ( HMDisplayTag != (void*) kUnresolvedCFragSymbolAddress )
221 {
222 HMDisplayTag(
223 }
224 else
225*/
226#endif
227 {
66a09d47 228 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
76a5e5d2 229
a7b04cfc
SC
230 m_shown = true ;
231
a7b04cfc
SC
232 TextFont( kFontIDGeneva ) ;
233 TextSize( 10 ) ;
234 TextFace( 0 ) ;
235 FontInfo fontInfo;
236 ::GetFontInfo(&fontInfo);
237 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
238 short height = 0 ;
239 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
240
241 int i = 0 ;
242 int length = m_label.Length() ;
243 int width = 0 ;
244 int thiswidth = 0 ;
245 int laststop = 0 ;
246 const char *text = m_label ;
247 while( i < length )
248 {
249 if( text[i] == 13 || text[i] == 10)
250 {
251 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
252 if ( thiswidth > width )
253 width = thiswidth ;
254
255 height += lineh ;
256 laststop = i+1 ;
257 }
258 i++ ;
259 }
260 if ( i - laststop > 0 )
261 {
262 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
263 if ( thiswidth > width )
264 width = thiswidth ;
265 height += lineh ;
266 }
267
268
269 m_rect.left = m_position.x + kTipOffset;
270 m_rect.top = m_position.y + kTipOffset;
271 m_rect.right = m_rect.left + width + 2 * kTipBorder;
272 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
273 ClipRect( &m_rect ) ;
274 BackColor( whiteColor ) ;
275 ForeColor(blackColor ) ;
276 m_backpict = OpenPicture(&m_rect);
277
278 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
279 GetPortBitMapForCopyBits(GetWindowPort(m_window)),
280 &m_rect,
281 &m_rect,
282 srcCopy,
283 NULL);
284
285 ClosePicture();
286 PenNormal() ;
287 SetThemeBackground(kThemeBrushNotificationWindowBackground,32,true) ;
288 SetThemeTextColor(kThemeTextColorNotification,32,true) ;
289 EraseRect( &m_rect ) ;
290 FrameRect( &m_rect ) ;
291 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
292
293 i = 0 ;
294 laststop = 0 ;
295 height = 0 ;
296 while( i < length )
297 {
298 if( text[i] == 13 || text[i] == 10)
299 {
300 ::DrawText( text , laststop , i - laststop ) ;
301 height += lineh ;
302 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
303 laststop = i+1 ;
304 }
305 i++ ;
306 }
307
308 ::DrawText( text , laststop , i - laststop ) ;
309 ::TextMode( srcOr ) ;
310 // DrawText( m_label , 0 , m_label.Length() ) ;
311 }
ee6b1d97
SC
312 }
313}
314
76a5e5d2 315void wxToolTip::NotifyWindowDelete( WXHWND win )
ee6b1d97
SC
316{
317 if ( win == s_ToolTipWindowRef )
318 {
319 s_ToolTipWindowRef = NULL ;
320 }
321}
322
323void wxMacToolTip::Clear()
324{
325 m_mark++ ;
12cd5f34
SC
326 if ( m_timer )
327 {
328 delete m_timer ;
329 m_timer = NULL ;
330 }
ee6b1d97
SC
331 if ( !m_shown )
332 return ;
333
334 if ( m_window == s_ToolTipWindowRef && m_backpict )
335 {
66a09d47 336 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
76a5e5d2 337
ee6b1d97
SC
338 m_shown = false ;
339
ee6b1d97
SC
340 BackColor( whiteColor ) ;
341 ForeColor(blackColor ) ;
342 DrawPicture(m_backpict, &m_rect);
343 KillPicture(m_backpict);
344 m_backpict = NULL ;
345 }
346}
347
348#endif
349