]>
Commit | Line | Data |
---|---|---|
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 SC |
23 | #include "wx/geometry.h" |
24 | #include "wx/mac/aga.h" | |
25 | #include "wx/mac/uma.h" | |
26 | ||
27 | //----------------------------------------------------------------------------- | |
28 | // global data | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | class wxMacToolTip | |
32 | { | |
33 | public : | |
34 | wxMacToolTip( ) ; | |
35 | ~wxMacToolTip() ; | |
36 | ||
37 | void Setup( WindowRef window , wxString text , wxPoint localPosition ) ; | |
38 | long GetMark() { return m_mark ; } | |
39 | void Draw() ; | |
40 | void Clear() ; | |
41 | bool IsShown() { return m_shown ; } | |
42 | private : | |
43 | ||
44 | wxString m_label ; | |
45 | wxPoint m_position ; | |
46 | Rect m_rect ; | |
47 | WindowRef m_window ; | |
48 | PicHandle m_backpict ; | |
49 | bool m_shown ; | |
50 | long m_mark ; | |
51 | } ; | |
52 | ||
53 | class wxMacToolTipTimer : wxTimer | |
54 | { | |
55 | public: | |
56 | wxMacToolTipTimer(wxMacToolTip* tip, int iMilliseconds) ; | |
57 | ||
58 | void Notify() | |
59 | { | |
60 | if ( m_mark == m_tip->GetMark() ) | |
61 | m_tip->Draw() ; | |
62 | ||
63 | delete this; | |
64 | } | |
65 | ||
66 | protected: | |
67 | wxMacToolTip* m_tip; | |
68 | long m_mark ; | |
69 | }; | |
70 | ||
71 | //----------------------------------------------------------------------------- | |
72 | // wxToolTip | |
73 | //----------------------------------------------------------------------------- | |
74 | static long s_ToolTipDelay = 500 ; | |
75 | static bool s_ShowToolTips = true ; | |
76 | static wxMacToolTip s_ToolTip ; | |
77 | static wxWindow* s_LastWindowEntered = NULL ; | |
78 | static wxRect2DInt s_ToolTipArea ; | |
79 | static WindowRef s_ToolTipWindowRef = NULL ; | |
80 | wxToolTip::wxToolTip( const wxString &tip ) | |
81 | { | |
82 | m_text = tip; | |
83 | m_window = (wxWindow*) NULL; | |
84 | } | |
85 | ||
86 | wxToolTip::~wxToolTip() | |
87 | { | |
88 | } | |
89 | ||
90 | void 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 | ||
106 | void wxToolTip::SetWindow( wxWindow *win ) | |
107 | { | |
108 | m_window = win; | |
109 | } | |
110 | ||
111 | void 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 | ||
126 | void wxToolTip::SetDelay( long msecs ) | |
127 | { | |
128 | s_ToolTipDelay = msecs ; | |
129 | } | |
130 | ||
131 | void 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 | ||
a7b04cfc | 151 | WindowRef window = 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 | ||
163 | void wxToolTip::RemoveToolTips() | |
164 | { | |
165 | s_ToolTip.Clear() ; | |
166 | } | |
167 | // --- mac specific | |
168 | ||
169 | wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec ) | |
170 | { | |
171 | m_tip = tip; | |
172 | m_mark = tip->GetMark() ; | |
173 | Start(msec, true); | |
174 | } | |
175 | ||
176 | wxMacToolTip::wxMacToolTip() | |
177 | { | |
178 | m_window = NULL ; | |
179 | m_backpict = NULL ; | |
180 | m_mark = 0 ; | |
181 | m_shown = false ; | |
182 | } | |
183 | ||
184 | void wxMacToolTip::Setup( WindowRef window , wxString text , wxPoint localPosition ) | |
185 | { | |
186 | m_mark++ ; | |
187 | Clear() ; | |
188 | m_position = localPosition ; | |
189 | m_label = wxMacMakeMacStringFromPC( text ) ; | |
190 | m_window = window ; | |
191 | s_ToolTipWindowRef = window ; | |
192 | m_backpict = NULL ; | |
193 | new wxMacToolTipTimer( this , s_ToolTipDelay ) ; | |
194 | } | |
195 | ||
196 | wxMacToolTip::~wxMacToolTip() | |
197 | { | |
198 | if ( m_backpict ) | |
199 | Clear() ; | |
200 | } | |
201 | ||
202 | const short kTipBorder = 2 ; | |
203 | const short kTipOffset = 5 ; | |
204 | ||
205 | void wxMacToolTip::Draw() | |
206 | { | |
207 | if ( m_label.Length() == 0 ) | |
208 | return ; | |
209 | ||
210 | if ( m_window == s_ToolTipWindowRef ) | |
211 | { | |
a7b04cfc SC |
212 | #if TARGET_CARBON |
213 | /* | |
214 | if ( HMDisplayTag != (void*) kUnresolvedCFragSymbolAddress ) | |
215 | { | |
216 | HMDisplayTag( | |
217 | } | |
218 | else | |
219 | */ | |
220 | #endif | |
221 | { | |
222 | #if TARGET_CARBON | |
223 | AGAPortHelper help( GetWindowPort( m_window ) ); | |
224 | #else | |
225 | AGAPortHelper help( ( m_window ) ); | |
226 | #endif | |
227 | m_shown = true ; | |
228 | ||
a7b04cfc SC |
229 | TextFont( kFontIDGeneva ) ; |
230 | TextSize( 10 ) ; | |
231 | TextFace( 0 ) ; | |
232 | FontInfo fontInfo; | |
233 | ::GetFontInfo(&fontInfo); | |
234 | short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading; | |
235 | short height = 0 ; | |
236 | // short width = TextWidth( m_label , 0 ,m_label.Length() ) ; | |
237 | ||
238 | int i = 0 ; | |
239 | int length = m_label.Length() ; | |
240 | int width = 0 ; | |
241 | int thiswidth = 0 ; | |
242 | int laststop = 0 ; | |
243 | const char *text = m_label ; | |
244 | while( i < length ) | |
245 | { | |
246 | if( text[i] == 13 || text[i] == 10) | |
247 | { | |
248 | thiswidth = ::TextWidth( text , laststop , i - laststop ) ; | |
249 | if ( thiswidth > width ) | |
250 | width = thiswidth ; | |
251 | ||
252 | height += lineh ; | |
253 | laststop = i+1 ; | |
254 | } | |
255 | i++ ; | |
256 | } | |
257 | if ( i - laststop > 0 ) | |
258 | { | |
259 | thiswidth = ::TextWidth( text , laststop , i - laststop ) ; | |
260 | if ( thiswidth > width ) | |
261 | width = thiswidth ; | |
262 | height += lineh ; | |
263 | } | |
264 | ||
265 | ||
266 | m_rect.left = m_position.x + kTipOffset; | |
267 | m_rect.top = m_position.y + kTipOffset; | |
268 | m_rect.right = m_rect.left + width + 2 * kTipBorder; | |
269 | m_rect.bottom = m_rect.top + height + 2 * kTipBorder; | |
270 | ClipRect( &m_rect ) ; | |
271 | BackColor( whiteColor ) ; | |
272 | ForeColor(blackColor ) ; | |
273 | m_backpict = OpenPicture(&m_rect); | |
274 | ||
275 | CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)), | |
276 | GetPortBitMapForCopyBits(GetWindowPort(m_window)), | |
277 | &m_rect, | |
278 | &m_rect, | |
279 | srcCopy, | |
280 | NULL); | |
281 | ||
282 | ClosePicture(); | |
283 | PenNormal() ; | |
284 | SetThemeBackground(kThemeBrushNotificationWindowBackground,32,true) ; | |
285 | SetThemeTextColor(kThemeTextColorNotification,32,true) ; | |
286 | EraseRect( &m_rect ) ; | |
287 | FrameRect( &m_rect ) ; | |
288 | ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder); | |
289 | ||
290 | i = 0 ; | |
291 | laststop = 0 ; | |
292 | height = 0 ; | |
293 | while( i < length ) | |
294 | { | |
295 | if( text[i] == 13 || text[i] == 10) | |
296 | { | |
297 | ::DrawText( text , laststop , i - laststop ) ; | |
298 | height += lineh ; | |
299 | ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height ); | |
300 | laststop = i+1 ; | |
301 | } | |
302 | i++ ; | |
303 | } | |
304 | ||
305 | ::DrawText( text , laststop , i - laststop ) ; | |
306 | ::TextMode( srcOr ) ; | |
307 | // DrawText( m_label , 0 , m_label.Length() ) ; | |
308 | } | |
ee6b1d97 SC |
309 | } |
310 | } | |
311 | ||
312 | void wxToolTip::NotifyWindowDelete( WindowRef win ) | |
313 | { | |
314 | if ( win == s_ToolTipWindowRef ) | |
315 | { | |
316 | s_ToolTipWindowRef = NULL ; | |
317 | } | |
318 | } | |
319 | ||
320 | void wxMacToolTip::Clear() | |
321 | { | |
322 | m_mark++ ; | |
323 | if ( !m_shown ) | |
324 | return ; | |
325 | ||
326 | if ( m_window == s_ToolTipWindowRef && m_backpict ) | |
327 | { | |
328 | #if TARGET_CARBON | |
329 | AGAPortHelper help( GetWindowPort(m_window) ) ; | |
330 | #else | |
331 | AGAPortHelper help( (m_window) ) ; | |
332 | #endif | |
333 | m_shown = false ; | |
334 | ||
ee6b1d97 SC |
335 | BackColor( whiteColor ) ; |
336 | ForeColor(blackColor ) ; | |
337 | DrawPicture(m_backpict, &m_rect); | |
338 | KillPicture(m_backpict); | |
339 | m_backpict = NULL ; | |
340 | } | |
341 | } | |
342 | ||
343 | #endif | |
344 |