]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/tooltip.cpp
fixed encoding handling in ANSI mode
[wxWidgets.git] / src / mac / carbon / 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 ;
f5ebf253
SC
80
81IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
82
ee6b1d97
SC
83wxToolTip::wxToolTip( const wxString &tip )
84{
85 m_text = tip;
86 m_window = (wxWindow*) NULL;
87}
88
89wxToolTip::~wxToolTip()
90{
91}
92
93void wxToolTip::SetTip( const wxString &tip )
94{
95 m_text = tip;
96
97 if ( m_window )
98 {
99 /*
100 // update it immediately
101 wxToolInfo ti(GetHwndOf(m_window));
102 ti.lpszText = (wxChar *)m_text.c_str();
103
104 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
105 */
106 }
107}
108
109void wxToolTip::SetWindow( wxWindow *win )
110{
7eb67c00 111 m_window = win ;
ee6b1d97
SC
112}
113
114void wxToolTip::Enable( bool flag )
115{
116 if ( s_ShowToolTips != flag )
117 {
118 s_ShowToolTips = flag ;
119 if ( s_ShowToolTips )
120 {
121 }
122 else
123 {
124 s_ToolTip.Clear() ;
125 }
126 }
127}
128
129void wxToolTip::SetDelay( long msecs )
130{
131 s_ToolTipDelay = msecs ;
132}
133
134void wxToolTip::RelayEvent( wxWindow *win , wxMouseEvent &event )
135{
136 if ( s_ShowToolTips )
137 {
138 if ( event.GetEventType() == wxEVT_LEAVE_WINDOW )
139 {
140 s_ToolTip.Clear() ;
141 }
142 else if (event.GetEventType() == wxEVT_ENTER_WINDOW || event.GetEventType() == wxEVT_MOTION )
143 {
144 wxPoint2DInt where( event.m_x , event.m_y ) ;
145 if ( s_LastWindowEntered == win && s_ToolTipArea.Contains( where ) )
146 {
147 }
148 else
149 {
150 s_ToolTip.Clear() ;
151 s_ToolTipArea = wxRect2DInt( event.m_x - 2 , event.m_y - 2 , 4 , 4 ) ;
152 s_LastWindowEntered = win ;
153
76a5e5d2 154 WindowRef window = MAC_WXHWND( win->MacGetRootWindow() ) ;
ee6b1d97
SC
155 int x = event.m_x ;
156 int y = event.m_y ;
157 wxPoint local( x , y ) ;
158 win->MacClientToRootWindow( &x, &y ) ;
159 wxPoint windowlocal( x , y ) ;
160 s_ToolTip.Setup( window , win->MacGetToolTipString( local ) , windowlocal ) ;
161 }
162 }
163 }
164}
165
166void wxToolTip::RemoveToolTips()
167{
168 s_ToolTip.Clear() ;
169}
170// --- mac specific
171
172wxMacToolTipTimer::wxMacToolTipTimer( wxMacToolTip *tip , int msec )
173{
174 m_tip = tip;
175 m_mark = tip->GetMark() ;
176 Start(msec, true);
177}
178
179wxMacToolTip::wxMacToolTip()
180{
181 m_window = NULL ;
182 m_backpict = NULL ;
183 m_mark = 0 ;
184 m_shown = false ;
76a5e5d2 185 m_timer = NULL ;
ee6b1d97
SC
186}
187
7eb67c00 188void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition )
ee6b1d97
SC
189{
190 m_mark++ ;
191 Clear() ;
12cd5f34 192 m_position = localPosition ;
678b2637
JS
193 if( wxApp::s_macDefaultEncodingIsPC )
194 m_label = wxMacMakeMacStringFromPC( text ) ;
195 else
196 m_label = text ;
7eb67c00
SC
197 m_window =win;
198 s_ToolTipWindowRef = m_window ;
ee6b1d97 199 m_backpict = NULL ;
76a5e5d2
SC
200 if ( m_timer )
201 delete m_timer ;
202 m_timer = new wxMacToolTipTimer( this , s_ToolTipDelay ) ;
ee6b1d97
SC
203}
204
205wxMacToolTip::~wxMacToolTip()
206{
f5bb2251 207 if ( m_timer ) {
76a5e5d2 208 delete m_timer ;
f5bb2251
GD
209 m_timer = NULL;
210 }
211 if ( m_backpict )
212 Clear() ;
ee6b1d97
SC
213}
214
215const short kTipBorder = 2 ;
216const short kTipOffset = 5 ;
217
218void wxMacToolTip::Draw()
219{
220 if ( m_label.Length() == 0 )
221 return ;
222
223 if ( m_window == s_ToolTipWindowRef )
224 {
a7b04cfc
SC
225#if TARGET_CARBON
226/*
227 if ( HMDisplayTag != (void*) kUnresolvedCFragSymbolAddress )
228 {
229 HMDisplayTag(
230 }
231 else
232*/
233#endif
234 {
66a09d47 235 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( m_window ) );
61eb9fa1 236#if TARGET_CARBON
7eb67c00 237 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
61eb9fa1 238#endif
a7b04cfc
SC
239 m_shown = true ;
240
7eb67c00
SC
241 FontFamilyID fontId ;
242 Str255 fontName ;
243 SInt16 fontSize ;
244 Style fontStyle ;
245 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
246 GetFNum( fontName, &fontId );
247
248 TextFont( fontId ) ;
249 TextSize( fontSize ) ;
250 TextFace( fontStyle ) ;
a7b04cfc
SC
251 FontInfo fontInfo;
252 ::GetFontInfo(&fontInfo);
253 short lineh = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
254 short height = 0 ;
255 // short width = TextWidth( m_label , 0 ,m_label.Length() ) ;
256
257 int i = 0 ;
258 int length = m_label.Length() ;
259 int width = 0 ;
260 int thiswidth = 0 ;
261 int laststop = 0 ;
262 const char *text = m_label ;
263 while( i < length )
264 {
265 if( text[i] == 13 || text[i] == 10)
266 {
267 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
268 if ( thiswidth > width )
269 width = thiswidth ;
270
271 height += lineh ;
272 laststop = i+1 ;
273 }
274 i++ ;
275 }
276 if ( i - laststop > 0 )
277 {
278 thiswidth = ::TextWidth( text , laststop , i - laststop ) ;
279 if ( thiswidth > width )
280 width = thiswidth ;
281 height += lineh ;
282 }
283
284
285 m_rect.left = m_position.x + kTipOffset;
286 m_rect.top = m_position.y + kTipOffset;
287 m_rect.right = m_rect.left + width + 2 * kTipBorder;
61eb9fa1 288#if TARGET_CARBON
7eb67c00
SC
289 if ( useDrawThemeText )
290 m_rect.right += kTipBorder ;
61eb9fa1 291#endif
a7b04cfc 292 m_rect.bottom = m_rect.top + height + 2 * kTipBorder;
2b5f62a0
VZ
293 Rect r ;
294 GetPortBounds( GetWindowPort( m_window ) , &r ) ;
295 if ( m_rect.top < 0 )
296 {
297 m_rect.bottom += -m_rect.top ;
298 m_rect.top = 0 ;
299 }
300 if ( m_rect.left < 0 )
301 {
302 m_rect.right += -m_rect.left ;
303 m_rect.left = 0 ;
304 }
305 if ( m_rect.right > r.right )
306 {
307 m_rect.left -= (m_rect.right - r.right ) ;
308 m_rect.right = r.right ;
309 }
310 if ( m_rect.bottom > r.bottom )
311 {
312 m_rect.top -= (m_rect.bottom - r.bottom) ;
313 m_rect.bottom = r.bottom ;
314 }
a7b04cfc
SC
315 ClipRect( &m_rect ) ;
316 BackColor( whiteColor ) ;
317 ForeColor(blackColor ) ;
2b5f62a0
VZ
318 GWorldPtr port ;
319 NewGWorld( &port , wxDisplayDepth() , &m_rect , NULL , NULL , 0 ) ;
320 CGrafPtr origPort ;
321 GDHandle origDevice ;
322
323 GetGWorld( &origPort , &origDevice ) ;
324 SetGWorld( port , NULL ) ;
80c82e01 325
a7b04cfc
SC
326 m_backpict = OpenPicture(&m_rect);
327
328 CopyBits(GetPortBitMapForCopyBits(GetWindowPort(m_window)),
80c82e01 329 GetPortBitMapForCopyBits(port),
a7b04cfc
SC
330 &m_rect,
331 &m_rect,
332 srcCopy,
333 NULL);
80c82e01
SC
334 ClosePicture();
335 SetGWorld( origPort , origDevice ) ;
336 DisposeGWorld( port ) ;
337 PenNormal() ;
7eb67c00
SC
338
339 RGBColor tooltipbackground = { 0xFFFF , 0xFFFF , 0xC000 } ;
340 BackColor( whiteColor ) ;
80c82e01 341 RGBForeColor( &tooltipbackground ) ;
7eb67c00
SC
342
343 PaintRect( &m_rect ) ;
344 ForeColor(blackColor ) ;
a7b04cfc 345 FrameRect( &m_rect ) ;
7eb67c00 346 SetThemeTextColor(kThemeTextColorNotification,wxDisplayDepth(),true) ;
a7b04cfc
SC
347 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder);
348
349 i = 0 ;
350 laststop = 0 ;
351 height = 0 ;
7eb67c00 352
a7b04cfc
SC
353 while( i < length )
354 {
355 if( text[i] == 13 || text[i] == 10)
356 {
61eb9fa1 357#if TARGET_CARBON
7eb67c00
SC
358 if ( useDrawThemeText )
359 {
360 Rect frame ;
361 frame.top = m_rect.top + kTipBorder + height ;
362 frame.left = m_rect.left + kTipBorder ;
363 frame.bottom = frame.top + 1000 ;
364 frame.right = frame.left + 1000 ;
365 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
366 ::DrawThemeTextBox( mString,
367 kThemeCurrentPortFont,
368 kThemeStateActive,
369 true,
370 &frame,
371 teJustLeft,
372 nil );
373 CFRelease( mString ) ;
374 height += lineh ;
375 }
376 else
61eb9fa1 377#endif
7eb67c00
SC
378 {
379 ::DrawText( text , laststop , i - laststop ) ;
380 height += lineh ;
381 ::MoveTo( m_rect.left + kTipBorder , m_rect.top + fontInfo.ascent + kTipBorder + height );
382 }
a7b04cfc
SC
383 laststop = i+1 ;
384 }
385 i++ ;
386 }
61eb9fa1 387#if TARGET_CARBON
7eb67c00
SC
388 if ( useDrawThemeText )
389 {
390 Rect frame ;
391 frame.top = m_rect.top + kTipBorder + height ;
392 frame.left = m_rect.left + kTipBorder ;
393 frame.bottom = frame.top + 1000 ;
394 frame.right = frame.left + 1000 ;
395 CFStringRef mString = CFStringCreateWithCString( NULL , text + laststop , kCFStringEncodingMacRoman ) ;
396 ::DrawThemeTextBox( mString,
397 kThemeCurrentPortFont,
398 kThemeStateActive,
399 true,
400 &frame,
401 teJustLeft,
402 nil );
403 CFRelease( mString ) ;
404 }
405 else
61eb9fa1 406#endif
7eb67c00
SC
407 {
408 ::DrawText( text , laststop , i - laststop ) ;
409 }
a7b04cfc 410 ::TextMode( srcOr ) ;
7eb67c00 411 }
ee6b1d97
SC
412 }
413}
414
76a5e5d2 415void wxToolTip::NotifyWindowDelete( WXHWND win )
ee6b1d97
SC
416{
417 if ( win == s_ToolTipWindowRef )
418 {
419 s_ToolTipWindowRef = NULL ;
420 }
421}
422
423void wxMacToolTip::Clear()
424{
425 m_mark++ ;
12cd5f34
SC
426 if ( m_timer )
427 {
428 delete m_timer ;
429 m_timer = NULL ;
430 }
ee6b1d97
SC
431 if ( !m_shown )
432 return ;
433
434 if ( m_window == s_ToolTipWindowRef && m_backpict )
435 {
66a09d47 436 wxMacPortStateHelper help( (GrafPtr) GetWindowPort(m_window) ) ;
76a5e5d2 437
ee6b1d97
SC
438 m_shown = false ;
439
ee6b1d97
SC
440 BackColor( whiteColor ) ;
441 ForeColor(blackColor ) ;
442 DrawPicture(m_backpict, &m_rect);
443 KillPicture(m_backpict);
444 m_backpict = NULL ;
445 }
446}
447
448#endif
449