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