]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/tooltip.cpp
ANSI 'C' fix (decs in "for" clauses no longer have any scope outside clause itself)
[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/setup.h"
15
16 #if wxUSE_TOOLTIPS
17
18 #include "wx/window.h"
19 #include "wx/tooltip.h"
20 #include "wx/geometry.h"
21 #include "wx/mac/aga.h"
22 #include "wx/mac/uma.h"
23
24 //-----------------------------------------------------------------------------
25 // global data
26 //-----------------------------------------------------------------------------
27
28 class wxMacToolTip
29 {
30 public :
31 wxMacToolTip( ) ;
32 ~wxMacToolTip() ;
33
34 void Setup( WindowRef window , wxString text , wxPoint localPosition ) ;
35 long GetMark() { return m_mark ; }
36 void Draw() ;
37 void Clear() ;
38 bool IsShown() { return m_shown ; }
39 private :
40
41 wxString m_label ;
42 wxPoint m_position ;
43 Rect m_rect ;
44 WindowRef m_window ;
45 PicHandle m_backpict ;
46 bool m_shown ;
47 long m_mark ;
48 } ;
49
50 class wxMacToolTipTimer : wxTimer
51 {
52 public:
53 wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ;
54
55 void Notify()
56 {
57 if ( m_mark == m_tip->GetMark() )
58 m_tip->Draw() ;
59
60 delete this;
61 }
62
63 protected:
64 wxMacToolTip* m_tip;
65 long m_mark ;
66 };
67
68 //-----------------------------------------------------------------------------
69 // wxToolTip
70 //-----------------------------------------------------------------------------
71 static long s_ToolTipDelay = 500 ;
72 static bool s_ShowToolTips = true ;
73 static wxMacToolTip s_ToolTip ;
74 static wxWindow* s_LastWindowEntered = NULL ;
75 static wxRect2DInt s_ToolTipArea ;
76 static WindowRef s_ToolTipWindowRef = NULL ;
77 wxToolTip::wxToolTip( const wxString &tip )
78 {
79 m_text = tip;
80 m_window = (wxWindow*) NULL;
81 }
82
83 wxToolTip::~wxToolTip()
84 {
85 }
86
87 void wxToolTip::SetTip( const wxString &tip )
88 {
89 m_text = tip;
90
91 if ( m_window )
92 {
93 /*
94 // update it immediately
95 wxToolInfo ti(GetHwndOf(m_window));
96 ti.lpszText = (wxChar *)m_text.c_str();
97
98 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
99 */
100 }
101 }
102
103 void wxToolTip::SetWindow( wxWindow *win )
104 {
105 m_window = win;
106 }
107
108 void wxToolTip::Enable( bool flag )
109 {
110 if ( s_ShowToolTips != flag )
111 {
112 s_ShowToolTips = flag ;
113 if ( s_ShowToolTips )
114 {
115 }
116 else
117 {
118 s_ToolTip.Clear() ;
119 }
120 }
121 }
122
123 void wxToolTip::SetDelay( long msecs )
124 {
125 s_ToolTipDelay = msecs ;
126 }
127
128 void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
129 {
130 if ( s_ShowToolTips )
131 {
132 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
133 {
134 s_ToolTip.Clear() ;
135 }
136 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
137 {
138 wxPoint2DInt where( event.m_x , event.m_y ) ;
139 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
140 {
141 }
142 else
143 {
144 s_ToolTip.Clear() ;
145 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
146 s_LastWindowEntered = win ;
147
148 WindowRef window = win->GetMacRootWindow() ;
149 int x = event.m_x ;
150 int y = event.m_y ;
151 wxPoint local( x , y ) ;
152 win->MacClientToRootWindow( &x, &y ) ;
153 wxPoint windowlocal( x , y ) ;
154 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
155 }
156 }
157 }
158 }
159
160 void wxToolTip::RemoveToolTips()
161 {
162 s_ToolTip.Clear() ;
163 }
164 // --- mac specific
165
166 wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
167 {
168 m_tip = tip;
169 m_mark = tip->GetMark() ;
170 Start(msec, true);
171 }
172
173 wxMacToolTip::wxMacToolTip()
174 {
175 m_window = NULL ;
176 m_backpict = NULL ;
177 m_mark = 0 ;
178 m_shown = false ;
179 }
180
181 void wxMacToolTip::Setup( WindowRef window , wxString text , wxPoint localPosition )
182 {
183 m_mark++ ;
184 Clear() ;
185 m_position = localPosition ;
186 m_label = wxMacMakeMacStringFromPC( text ) ;
187 m_window = window ;
188 s_ToolTipWindowRef = window ;
189 m_backpict = NULL ;
190 new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
191 }
192
193 wxMacToolTip::~wxMacToolTip()
194 {
195 if ( m_backpict )
196 Clear() ;
197 }
198
199 const short kTipBorder = 2 ;
200 const short kTipOffset = 5 ;
201
202 void wxMacToolTip::Draw()
203 {
204 if ( m_label.Length() == 0 )
205 return ;
206
207 if ( m_window == s_ToolTipWindowRef )
208 {
209 #if TARGET_CARBON
210 AGAPortHelper help( GetWindowPort( m_window ) );
211 #else
212 AGAPortHelper help( ( m_window ) );
213 #endif
214 m_shown = true ;
215
216 SetOrigin( 0 , 0 ) ;
217 TextFont( kFontIDGeneva ) ;
218 TextSize( 9 ) ;
219 TextFace( 0 ) ;
220 FontInfo fontInfo;
221 ::GetFontInfo(&fontInfo);
222 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
223 short height = 0 ;
224 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
225
226 int i = 0 ;
227 int length = m_label.Length() ;
228 int width = 0 ;
229 int thiswidth = 0 ;
230 int laststop = 0 ;
231 const char *text = m_label ;
232 while( i < length )
233 {
234 if( text[i] == 13 || text[i] == 10)
235 {
236 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
237 if ( thiswidth > width )
238 width = thiswidth ;
239
240 height += lineh ;
241 laststop = i+1 ;
242 }
243 i++ ;
244 }
245 if ( i - laststop > 0 )
246 {
247 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
248 if ( thiswidth > width )
249 width = thiswidth ;
250 height += lineh ;
251 }
252
253
254 m_rect.left = m_position.x + kTipOffset;
255 m_rect.top = m_position.y + kTipOffset;
256 m_rect.right = m_rect.left + width + 2 * kTipBorder;
257 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
258 ClipRect( &m_rect ) ;
259 BackColor( whiteColor ) ;
260 ForeColor(blackColor ) ;
261 m_backpict = OpenPicture(&m_rect);
262
263 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
264 GetPortBitMapForCopyBits(GetWindowPort(m_window)),
265 &m_rect,
266 &m_rect,
267 srcCopy,
268 NULL);
269
270 ClosePicture();
271 RGBColor yellow = { 0xFFFF , 0xFFFF , (153<<8)+153 } ;
272 RGBBackColor( &yellow ) ;
273 EraseRect( &m_rect ) ;
274 FrameRect( &m_rect ) ;
275 BackColor( whiteColor ) ;
276 ForeColor(blackColor ) ;
277 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
278
279 i = 0 ;
280 laststop = 0 ;
281 height = 0 ;
282 while( i < length )
283 {
284 if( text[i] == 13 || text[i] == 10)
285 {
286 ::DrawText( text , laststop , i - laststop ) ;
287 height += lineh ;
288 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
289 laststop = i+1 ;
290 }
291 i++ ;
292 }
293
294 ::DrawText( text , laststop , i - laststop ) ;
295 ::TextMode( srcOr ) ;
296
297 // DrawText( m_label , 0 , m_label.Length() ) ;
298 }
299 }
300
301 void wxToolTip::NotifyWindowDelete( WindowRef win )
302 {
303 if ( win == s_ToolTipWindowRef )
304 {
305 s_ToolTipWindowRef = NULL ;
306 }
307 }
308
309 void wxMacToolTip::Clear()
310 {
311 m_mark++ ;
312 if ( !m_shown )
313 return ;
314
315 if ( m_window == s_ToolTipWindowRef && m_backpict )
316 {
317 #if TARGET_CARBON
318 AGAPortHelper help( GetWindowPort(m_window) ) ;
319 #else
320 AGAPortHelper help( (m_window) ) ;
321 #endif
322 m_shown = false ;
323
324 SetOrigin( 0 , 0 ) ;
325 BackColor( whiteColor ) ;
326 ForeColor(blackColor ) ;
327 DrawPicture(m_backpict, &m_rect);
328 KillPicture(m_backpict);
329 m_backpict = NULL ;
330 }
331 }
332
333 #endif
334