]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/utils.cpp
moving tlw list up, cleanup
[wxWidgets.git] / src / mac / carbon / utils.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/carbon/utils.cpp
3// Purpose: Various utilities
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/utils.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/app.h"
19 #if wxUSE_GUI
20 #include "wx/toplevel.h"
21 #include "wx/font.h"
22 #endif
23#endif
24
25#include "wx/apptrait.h"
26
27#if wxUSE_GUI
28 #include "wx/mac/uma.h"
29#endif
30
31#include <ctype.h>
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <stdarg.h>
37
38// #include "MoreFilesX.h"
39
40#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
41 #include <AudioToolbox/AudioServices.h>
42#endif
43
44#if wxUSE_GUI
45 #include <CoreServices/CoreServices.h>
46 #include <Carbon/Carbon.h>
47 #include "wx/mac/private/timer.h"
48#endif // wxUSE_GUI
49
50#include "wx/evtloop.h"
51#include "wx/mac/private.h"
52
53#if defined(__MWERKS__) && wxUSE_UNICODE
54#if __MWERKS__ < 0x4100
55 #include <wtime.h>
56#endif
57#endif
58
59#if wxUSE_BASE
60
61// our OS version is the same in non GUI and GUI cases
62wxOperatingSystemId wxGetOsVersion(int *majorVsn, int *minorVsn)
63{
64 SInt32 theSystem;
65 Gestalt(gestaltSystemVersion, &theSystem);
66
67 if ( majorVsn != NULL )
68 *majorVsn = (theSystem >> 8);
69
70 if ( minorVsn != NULL )
71 *minorVsn = (theSystem & 0xFF);
72
73#if defined( __DARWIN__ )
74 return wxOS_MAC_OSX_DARWIN;
75#else
76 return wxOS_MAC_OS;
77#endif
78}
79
80extern bool WXDLLEXPORT wxIsDebuggerRunning()
81{
82 // TODO : try to find out ...
83 return false;
84}
85
86// Emit a beeeeeep
87void wxBell()
88{
89#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
90 if ( AudioServicesPlayAlertSound )
91 AudioServicesPlayAlertSound(kUserPreferredAlert);
92 else
93#endif
94#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
95 SysBeep(30);
96#else
97 {
98 }
99#endif
100}
101
102
103#endif // wxUSE_BASE
104
105#if wxUSE_GUI
106
107wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
108{
109 // We suppose that toolkit version is the same as OS version under Mac
110 wxGetOsVersion(verMaj, verMin);
111
112 return wxPORT_MAC;
113}
114
115wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
116{
117 return new wxEventLoop;
118}
119
120wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
121{
122 return new wxCarbonTimerImpl(timer);
123}
124
125int gs_wxBusyCursorCount = 0;
126extern wxCursor gMacCurrentCursor;
127wxCursor gMacStoredActiveCursor;
128
129// Set the cursor to the busy cursor for all windows
130void wxBeginBusyCursor(const wxCursor *cursor)
131{
132 if (gs_wxBusyCursorCount++ == 0)
133 {
134 gMacStoredActiveCursor = gMacCurrentCursor;
135 cursor->MacInstall();
136
137 wxSetCursor(*cursor);
138 }
139 //else: nothing to do, already set
140}
141
142// Restore cursor to normal
143void wxEndBusyCursor()
144{
145 wxCHECK_RET( gs_wxBusyCursorCount > 0,
146 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
147
148 if (--gs_wxBusyCursorCount == 0)
149 {
150 gMacStoredActiveCursor.MacInstall();
151 gMacStoredActiveCursor = wxNullCursor;
152
153 wxSetCursor(wxNullCursor);
154 }
155}
156
157// true if we're between the above two calls
158bool wxIsBusy()
159{
160 return (gs_wxBusyCursorCount > 0);
161}
162
163#endif // wxUSE_GUI
164
165#if wxUSE_BASE
166
167wxString wxMacFindFolderNoSeparator( short vol,
168 OSType folderType,
169 Boolean createFolder)
170{
171 FSRef fsRef;
172 wxString strDir;
173
174 if ( FSFindFolder( vol, folderType, createFolder, &fsRef) == noErr)
175 {
176 strDir = wxMacFSRefToPath( &fsRef );
177 }
178
179 return strDir;
180}
181
182wxString wxMacFindFolder( short vol,
183 OSType folderType,
184 Boolean createFolder)
185{
186 return wxMacFindFolderNoSeparator(vol, folderType, createFolder) + wxFILE_SEP_PATH;
187}
188
189#endif // wxUSE_BASE
190
191#if wxUSE_GUI
192
193// Check whether this window wants to process messages, e.g. Stop button
194// in long calculations.
195bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd))
196{
197 // TODO
198 return false;
199}
200
201void wxGetMousePosition( int* x, int* y )
202{
203#if wxMAC_USE_QUICKDRAW
204 Point pt;
205 GetGlobalMouse(&pt);
206 *x = pt.h;
207 *y = pt.v;
208#else
209 // TODO
210#endif
211};
212
213// Return true if we have a colour display
214bool wxColourDisplay()
215{
216 return true;
217}
218
219// Returns depth of screen
220int wxDisplayDepth()
221{
222#if wxMAC_USE_QUICKDRAW
223 int theDepth = (int) CGDisplayBitsPerPixel(CGMainDisplayID());
224 Rect globRect;
225 SetRect(&globRect, -32760, -32760, 32760, 32760);
226 GDHandle theMaxDevice;
227
228 theMaxDevice = GetMaxDevice(&globRect);
229 if (theMaxDevice != NULL)
230 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
231
232 return theDepth;
233#else
234 return 32; // TODO
235#endif
236}
237
238// Get size of display
239void wxDisplaySize(int *width, int *height)
240{
241 // TODO adapt for multi-displays
242 CGRect bounds = CGDisplayBounds(CGMainDisplayID());
243 if ( width )
244 *width = (int)bounds.size.width ;
245 if ( height )
246 *height = (int)bounds.size.height;
247}
248
249void wxDisplaySizeMM(int *width, int *height)
250{
251 wxDisplaySize(width, height);
252 // on mac 72 is fixed (at least now;-)
253 float cvPt2Mm = 25.4 / 72;
254
255 if (width != NULL)
256 *width = int( *width * cvPt2Mm );
257
258 if (height != NULL)
259 *height = int( *height * cvPt2Mm );
260}
261
262void wxClientDisplayRect(int *x, int *y, int *width, int *height)
263{
264#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
265#if wxMAC_USE_QUICKDRAW
266
267 HIRect bounds ;
268 HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay,kHICoordSpace72DPIGlobal,
269 &bounds);
270 if ( x )
271 *x = bounds.origin.x;
272 if ( y )
273 *y = bounds.origin.y;
274 if ( width )
275 *width = bounds.size.width;
276 if ( height )
277 *height = bounds.size.height;
278#else
279 int w, h;
280 wxDisplaySize(&w,&h);
281 if ( x )
282 *x = 0;
283 if ( y )
284 *y = 24;
285 if ( width )
286 *width = w;
287 if ( height )
288 *height = h-24;
289#endif
290#else
291 Rect r;
292 GetAvailableWindowPositioningBounds( GetMainDevice() , &r );
293 if ( x )
294 *x = r.left;
295 if ( y )
296 *y = r.top;
297 if ( width )
298 *width = r.right - r.left;
299 if ( height )
300 *height = r.bottom - r.top;
301#endif
302}
303
304wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
305{
306 return wxGenericFindWindowAtPoint(pt);
307}
308
309#endif // wxUSE_GUI
310
311#if wxUSE_BASE
312
313#include <sys/utsname.h>
314
315wxString wxGetOsDescription()
316{
317 struct utsname name;
318 uname(&name);
319 return wxString::Format(_T("Mac OS X (%s %s %s)"),
320 wxString::FromAscii(name.sysname).c_str(),
321 wxString::FromAscii(name.release).c_str(),
322 wxString::FromAscii(name.machine).c_str());
323}
324
325#ifndef __DARWIN__
326wxString wxGetUserHome (const wxString& user)
327{
328 // TODO
329 return wxString();
330}
331
332bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree)
333{
334 if ( path.empty() )
335 return false;
336
337 wxString p = path;
338 if (p[0u] == ':' )
339 p = wxGetCwd() + p;
340
341 int pos = p.Find(':');
342 if ( pos != wxNOT_FOUND )
343 p = p.Mid(1,pos);
344
345 p = p + wxT(":");
346
347 OSErr err = noErr;
348
349 FSRef fsRef;
350 err = wxMacPathToFSRef( p , &fsRef );
351 if ( noErr == err )
352 {
353 FSVolumeRefNum vRefNum;
354 err = FSGetVRefNum( &fsRef , &vRefNum );
355 if ( noErr == err )
356 {
357 UInt64 freeBytes , totalBytes;
358 err = FSGetVInfo( vRefNum , NULL , &freeBytes , &totalBytes );
359 if ( noErr == err )
360 {
361 if ( pTotal )
362 *pTotal = wxDiskspaceSize_t( totalBytes );
363 if ( pFree )
364 *pFree = wxDiskspaceSize_t( freeBytes );
365 }
366 }
367 }
368
369 return err == noErr;
370}
371#endif // !__DARWIN__
372
373//---------------------------------------------------------------------------
374// wxMac Specific utility functions
375//---------------------------------------------------------------------------
376
377void wxMacStringToPascal( const wxString&from , StringPtr to )
378{
379 wxCharBuffer buf = from.mb_str( wxConvLocal );
380 int len = strlen(buf);
381
382 if ( len > 255 )
383 len = 255;
384 to[0] = len;
385 memcpy( (char*) &to[1] , buf , len );
386}
387
388wxString wxMacMakeStringFromPascal( ConstStringPtr from )
389{
390 return wxString( (char*) &from[1] , wxConvLocal , from[0] );
391}
392
393// ----------------------------------------------------------------------------
394// Common Event Support
395// ----------------------------------------------------------------------------
396
397void wxMacWakeUp()
398{
399 OSStatus err = noErr;
400
401#if 0
402 // lead sometimes to race conditions, although all calls used should be thread safe ...
403 static wxMacCarbonEvent s_wakeupEvent;
404 if ( !s_wakeupEvent.IsValid() )
405 {
406 err = s_wakeupEvent.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
407 kEventAttributeNone );
408 }
409 if ( err == noErr )
410 {
411
412 if ( IsEventInQueue( GetMainEventQueue() , s_wakeupEvent ) )
413 return;
414 s_wakeupEvent.SetCurrentTime();
415 err = PostEventToQueue(GetMainEventQueue(), s_wakeupEvent,
416 kEventPriorityHigh );
417 }
418#else
419 wxMacCarbonEvent wakeupEvent;
420 wakeupEvent.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
421 kEventAttributeNone );
422 err = PostEventToQueue(GetMainEventQueue(), wakeupEvent,
423 kEventPriorityHigh );
424#endif
425}
426
427#endif // wxUSE_BASE
428
429#if wxUSE_GUI
430
431// ----------------------------------------------------------------------------
432// Native Struct Conversions
433// ----------------------------------------------------------------------------
434
435void wxMacRectToNative( const wxRect *wx , Rect *n )
436{
437 n->left = wx->x;
438 n->top = wx->y;
439 n->right = wx->x + wx->width;
440 n->bottom = wx->y + wx->height;
441}
442
443void wxMacNativeToRect( const Rect *n , wxRect* wx )
444{
445 wx->x = n->left;
446 wx->y = n->top;
447 wx->width = n->right - n->left;
448 wx->height = n->bottom - n->top;
449}
450
451void wxMacPointToNative( const wxPoint* wx , Point *n )
452{
453 n->h = wx->x;
454 n->v = wx->y;
455}
456
457void wxMacNativeToPoint( const Point *n , wxPoint* wx )
458{
459 wx->x = n->h;
460 wx->y = n->v;
461}
462
463// ----------------------------------------------------------------------------
464// Carbon Event Support
465// ----------------------------------------------------------------------------
466
467OSStatus wxMacCarbonEvent::GetParameter(EventParamName inName, EventParamType inDesiredType, UInt32 inBufferSize, void * outData)
468{
469 return ::GetEventParameter( m_eventRef , inName , inDesiredType , NULL , inBufferSize , NULL , outData );
470}
471
472OSStatus wxMacCarbonEvent::SetParameter(EventParamName inName, EventParamType inType, UInt32 inBufferSize, const void * inData)
473{
474 return ::SetEventParameter( m_eventRef , inName , inType , inBufferSize , inData );
475}
476
477// ----------------------------------------------------------------------------
478// Control Access Support
479// ----------------------------------------------------------------------------
480
481#if wxMAC_USE_QUICKDRAW
482
483IMPLEMENT_DYNAMIC_CLASS( wxMacControl , wxObject )
484
485wxMacControl::wxMacControl()
486{
487 Init();
488}
489
490wxMacControl::wxMacControl(wxWindow* peer , bool isRootControl )
491{
492 Init();
493 m_peer = peer;
494 m_isRootControl = isRootControl;
495}
496
497wxMacControl::wxMacControl( wxWindow* peer , ControlRef control )
498{
499 Init();
500 m_peer = peer;
501 m_controlRef = control;
502}
503
504wxMacControl::wxMacControl( wxWindow* peer , WXWidget control )
505{
506 Init();
507 m_peer = peer;
508 m_controlRef = (ControlRef) control;
509}
510
511wxMacControl::~wxMacControl()
512{
513}
514
515void wxMacControl::Init()
516{
517 m_peer = NULL;
518 m_controlRef = NULL;
519 m_needsFocusRect = false;
520 m_isRootControl = false;
521}
522
523void wxMacControl::Dispose()
524{
525 wxASSERT_MSG( m_controlRef != NULL , wxT("Control Handle already NULL, Dispose called twice ?") );
526 wxASSERT_MSG( IsValidControlHandle(m_controlRef) , wxT("Invalid Control Handle (maybe already released) in Dispose") );
527
528 // we cannot check the ref count here anymore, as autorelease objects might delete their refs later
529 // we can have situations when being embedded, where the control gets deleted behind our back, so only
530 // CFRelease if we are safe
531 if ( IsValidControlHandle(m_controlRef) )
532 CFRelease(m_controlRef);
533 m_controlRef = NULL;
534}
535
536void wxMacControl::SetReference( URefCon data )
537{
538 SetControlReference( m_controlRef , data );
539}
540
541OSStatus wxMacControl::GetData(ControlPartCode inPartCode , ResType inTag , Size inBufferSize , void * inOutBuffer , Size * outActualSize ) const
542{
543 return ::GetControlData( m_controlRef , inPartCode , inTag , inBufferSize , inOutBuffer , outActualSize );
544}
545
546OSStatus wxMacControl::GetDataSize(ControlPartCode inPartCode , ResType inTag , Size * outActualSize ) const
547{
548 return ::GetControlDataSize( m_controlRef , inPartCode , inTag , outActualSize );
549}
550
551OSStatus wxMacControl::SetData(ControlPartCode inPartCode , ResType inTag , Size inSize , const void * inData)
552{
553 return ::SetControlData( m_controlRef , inPartCode , inTag , inSize , inData );
554}
555
556OSStatus wxMacControl::SendEvent( EventRef event , OptionBits inOptions )
557{
558 return SendEventToEventTargetWithOptions( event,
559 HIObjectGetEventTarget( (HIObjectRef) m_controlRef ), inOptions );
560}
561
562OSStatus wxMacControl::SendHICommand( HICommand &command , OptionBits inOptions )
563{
564 wxMacCarbonEvent event( kEventClassCommand , kEventCommandProcess );
565
566 event.SetParameter<HICommand>(kEventParamDirectObject,command);
567
568 return SendEvent( event , inOptions );
569}
570
571OSStatus wxMacControl::SendHICommand( UInt32 commandID , OptionBits inOptions )
572{
573 HICommand command;
574
575 memset( &command, 0 , sizeof(command) );
576 command.commandID = commandID;
577 return SendHICommand( command , inOptions );
578}
579
580void wxMacControl::Flash( ControlPartCode part , UInt32 ticks )
581{
582 unsigned long finalTicks;
583
584 HiliteControl( m_controlRef , part );
585 Delay( ticks , &finalTicks );
586 HiliteControl( m_controlRef , kControlNoPart );
587}
588
589SInt32 wxMacControl::GetValue() const
590{
591 return ::GetControl32BitValue( m_controlRef );
592}
593
594SInt32 wxMacControl::GetMaximum() const
595{
596 return ::GetControl32BitMaximum( m_controlRef );
597}
598
599SInt32 wxMacControl::GetMinimum() const
600{
601 return ::GetControl32BitMinimum( m_controlRef );
602}
603
604void wxMacControl::SetValue( SInt32 v )
605{
606 ::SetControl32BitValue( m_controlRef , v );
607}
608
609void wxMacControl::SetMinimum( SInt32 v )
610{
611 ::SetControl32BitMinimum( m_controlRef , v );
612}
613
614void wxMacControl::SetMaximum( SInt32 v )
615{
616 ::SetControl32BitMaximum( m_controlRef , v );
617}
618
619void wxMacControl::SetValueAndRange( SInt32 value , SInt32 minimum , SInt32 maximum )
620{
621 ::SetControl32BitMinimum( m_controlRef , minimum );
622 ::SetControl32BitMaximum( m_controlRef , maximum );
623 ::SetControl32BitValue( m_controlRef , value );
624}
625
626OSStatus wxMacControl::SetFocus( ControlFocusPart focusPart )
627{
628 return SetKeyboardFocus( GetControlOwner( m_controlRef ), m_controlRef, focusPart );
629}
630
631bool wxMacControl::HasFocus() const
632{
633 ControlRef control;
634 GetKeyboardFocus( GetUserFocusWindow() , &control );
635 return control == m_controlRef;
636}
637
638void wxMacControl::SetNeedsFocusRect( bool needs )
639{
640 m_needsFocusRect = needs;
641}
642
643bool wxMacControl::NeedsFocusRect() const
644{
645 return m_needsFocusRect;
646}
647
648void wxMacControl::VisibilityChanged(bool WXUNUSED(shown))
649{
650}
651
652void wxMacControl::SuperChangedPosition()
653{
654}
655
656void wxMacControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
657{
658 m_font = font;
659#if wxMAC_USE_CORE_TEXT
660 if ( UMAGetSystemVersion() >= 0x1050 )
661 {
662 HIViewPartCode part = 0;
663 HIThemeTextHorizontalFlush flush = kHIThemeTextHorizontalFlushDefault;
664 if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_CENTER_HORIZONTAL )
665 flush = kHIThemeTextHorizontalFlushCenter;
666 else if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_RIGHT )
667 flush = kHIThemeTextHorizontalFlushRight;
668 HIViewSetTextFont( m_controlRef , part , (CTFontRef) font.MacGetCTFont() );
669 HIViewSetTextHorizontalFlush( m_controlRef, part, flush );
670
671 if ( foreground != *wxBLACK )
672 {
673 ControlFontStyleRec fontStyle;
674 foreground.GetRGBColor( &fontStyle.foreColor );
675 fontStyle.flags = kControlUseForeColorMask;
676 ::SetControlFontStyle( m_controlRef , &fontStyle );
677 }
678
679 }
680#endif
681#if wxMAC_USE_ATSU_TEXT
682 ControlFontStyleRec fontStyle;
683 if ( font.MacGetThemeFontID() != kThemeCurrentPortFont )
684 {
685 switch ( font.MacGetThemeFontID() )
686 {
687 case kThemeSmallSystemFont :
688 fontStyle.font = kControlFontSmallSystemFont;
689 break;
690
691 case 109 : // mini font
692 fontStyle.font = -5;
693 break;
694
695 case kThemeSystemFont :
696 fontStyle.font = kControlFontBigSystemFont;
697 break;
698
699 default :
700 fontStyle.font = kControlFontBigSystemFont;
701 break;
702 }
703
704 fontStyle.flags = kControlUseFontMask;
705 }
706 else
707 {
708 fontStyle.font = font.MacGetFontNum();
709 fontStyle.style = font.MacGetFontStyle();
710 fontStyle.size = font.MacGetFontSize();
711 fontStyle.flags = kControlUseFontMask | kControlUseFaceMask | kControlUseSizeMask;
712 }
713
714 fontStyle.just = teJustLeft;
715 fontStyle.flags |= kControlUseJustMask;
716 if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_CENTER_HORIZONTAL )
717 fontStyle.just = teJustCenter;
718 else if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_RIGHT )
719 fontStyle.just = teJustRight;
720
721
722 // we only should do this in case of a non-standard color, as otherwise 'disabled' controls
723 // won't get grayed out by the system anymore
724
725 if ( foreground != *wxBLACK )
726 {
727 foreground.GetRGBColor( &fontStyle.foreColor );
728 fontStyle.flags |= kControlUseForeColorMask;
729 }
730
731 ::SetControlFontStyle( m_controlRef , &fontStyle );
732#endif
733}
734
735void wxMacControl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
736{
737// HITextViewSetBackgroundColor( m_textView , color );
738}
739
740void wxMacControl::SetRange( SInt32 minimum , SInt32 maximum )
741{
742 ::SetControl32BitMinimum( m_controlRef , minimum );
743 ::SetControl32BitMaximum( m_controlRef , maximum );
744}
745
746short wxMacControl::HandleKey( SInt16 keyCode, SInt16 charCode, EventModifiers modifiers )
747{
748#ifndef __LP64__
749 return HandleControlKey( m_controlRef , keyCode , charCode , modifiers );
750#else
751 return 0;
752#endif
753}
754
755void wxMacControl::SetActionProc( ControlActionUPP actionProc )
756{
757 SetControlAction( m_controlRef , actionProc );
758}
759
760void wxMacControl::SetViewSize( SInt32 viewSize )
761{
762 SetControlViewSize(m_controlRef , viewSize );
763}
764
765SInt32 wxMacControl::GetViewSize() const
766{
767 return GetControlViewSize( m_controlRef );
768}
769
770bool wxMacControl::IsVisible() const
771{
772 return IsControlVisible( m_controlRef );
773}
774
775void wxMacControl::SetVisibility( bool visible , bool redraw )
776{
777 SetControlVisibility( m_controlRef , visible , redraw );
778}
779
780bool wxMacControl::IsEnabled() const
781{
782 return IsControlEnabled( m_controlRef );
783}
784
785bool wxMacControl::IsActive() const
786{
787 return IsControlActive( m_controlRef );
788}
789
790void wxMacControl::Enable( bool enable )
791{
792 if ( enable )
793 EnableControl( m_controlRef );
794 else
795 DisableControl( m_controlRef );
796}
797
798void wxMacControl::SetDrawingEnabled( bool enable )
799{
800 HIViewSetDrawingEnabled( m_controlRef , enable );
801}
802
803bool wxMacControl::GetNeedsDisplay() const
804{
805 return HIViewGetNeedsDisplay( m_controlRef );
806}
807
808void wxMacControl::SetNeedsDisplay( RgnHandle where )
809{
810 if ( !IsVisible() )
811 return;
812
813 HIViewSetNeedsDisplayInRegion( m_controlRef , where , true );
814}
815
816void wxMacControl::SetNeedsDisplay( Rect* where )
817{
818 if ( !IsVisible() )
819 return;
820
821 if ( where != NULL )
822 {
823 RgnHandle update = NewRgn();
824 RectRgn( update , where );
825 HIViewSetNeedsDisplayInRegion( m_controlRef , update , true );
826 DisposeRgn( update );
827 }
828 else
829 HIViewSetNeedsDisplay( m_controlRef , true );
830}
831
832void wxMacControl::Convert( wxPoint *pt , wxMacControl *from , wxMacControl *to )
833{
834 HIPoint hiPoint;
835
836 hiPoint.x = pt->x;
837 hiPoint.y = pt->y;
838 HIViewConvertPoint( &hiPoint , from->m_controlRef , to->m_controlRef );
839 pt->x = (int)hiPoint.x;
840 pt->y = (int)hiPoint.y;
841}
842
843void wxMacControl::SetRect( Rect *r )
844{
845 //A HIRect is actually a CGRect on OSX - which consists of two structures -
846 //CGPoint and CGSize, which have two floats each
847 HIRect hir = { { r->left , r->top }, { r->right - r->left , r->bottom - r->top } };
848 HIViewSetFrame ( m_controlRef , &hir );
849 // eventuall we might have to do a SetVisibility( false , true );
850 // before and a SetVisibility( true , true ); after
851}
852
853void wxMacControl::GetRect( Rect *r )
854{
855 GetControlBounds( m_controlRef , r );
856}
857
858void wxMacControl::GetRectInWindowCoords( Rect *r )
859{
860 GetControlBounds( m_controlRef , r ) ;
861
862 WindowRef tlwref = GetControlOwner( m_controlRef ) ;
863
864 wxNonOwnedWindow* tlwwx = wxFindWinFromMacWindow( tlwref ) ;
865 if ( tlwwx != NULL )
866 {
867 ControlRef rootControl = tlwwx->GetPeer()->GetControlRef() ;
868 HIPoint hiPoint = CGPointMake( 0 , 0 ) ;
869 HIViewConvertPoint( &hiPoint , HIViewGetSuperview(m_controlRef) , rootControl ) ;
870 OffsetRect( r , (short) hiPoint.x , (short) hiPoint.y ) ;
871 }
872}
873
874void wxMacControl::GetBestRect( Rect *r )
875{
876 short baselineoffset;
877
878 GetBestControlRect( m_controlRef , r , &baselineoffset );
879}
880
881void wxMacControl::SetLabel( const wxString &title )
882{
883 wxFontEncoding encoding;
884
885 if ( m_font.Ok() )
886 encoding = m_font.GetEncoding();
887 else
888 encoding = wxFont::GetDefaultEncoding();
889
890 SetControlTitleWithCFString( m_controlRef , wxCFStringRef( title , encoding ) );
891}
892
893void wxMacControl::GetFeatures( UInt32 * features )
894{
895 GetControlFeatures( m_controlRef , features );
896}
897
898OSStatus wxMacControl::GetRegion( ControlPartCode partCode , RgnHandle region )
899{
900 OSStatus err = GetControlRegion( m_controlRef , partCode , region );
901 return err;
902}
903
904OSStatus wxMacControl::SetZOrder( bool above , wxMacControl* other )
905{
906 return HIViewSetZOrder( m_controlRef,above ? kHIViewZOrderAbove : kHIViewZOrderBelow,
907 (other != NULL) ? other->m_controlRef : NULL);
908}
909
910// SetNeedsDisplay would not invalidate the children
911static void InvalidateControlAndChildren( HIViewRef control )
912{
913 HIViewSetNeedsDisplay( control , true );
914 UInt16 childrenCount = 0;
915 OSStatus err = CountSubControls( control , &childrenCount );
916 if ( err == errControlIsNotEmbedder )
917 return;
918
919 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") );
920
921 for ( UInt16 i = childrenCount; i >=1; --i )
922 {
923 HIViewRef child;
924
925 err = GetIndexedSubControl( control , i , & child );
926 if ( err == errControlIsNotEmbedder )
927 return;
928
929 InvalidateControlAndChildren( child );
930 }
931}
932
933void wxMacControl::InvalidateWithChildren()
934{
935 InvalidateControlAndChildren( m_controlRef );
936}
937
938void wxMacControl::ScrollRect( wxRect *r , int dx , int dy )
939{
940 wxASSERT( r != NULL );
941
942 HIRect scrollarea = CGRectMake( r->x , r->y , r->width , r->height);
943 HIViewScrollRect ( m_controlRef , &scrollarea , dx ,dy );
944}
945
946OSType wxMacCreator = 'WXMC';
947OSType wxMacControlProperty = 'MCCT';
948
949void wxMacControl::SetReferenceInNativeControl()
950{
951 void * data = this;
952 verify_noerr( SetControlProperty ( m_controlRef ,
953 wxMacCreator,wxMacControlProperty, sizeof(data), &data ) );
954}
955
956wxMacControl* wxMacControl::GetReferenceFromNativeControl(ControlRef control)
957{
958 wxMacControl* ctl = NULL;
959 ByteCount actualSize;
960 if ( GetControlProperty( control ,wxMacCreator,wxMacControlProperty, sizeof(ctl) ,
961 &actualSize , &ctl ) == noErr )
962 {
963 return ctl;
964 }
965 return NULL;
966}
967
968// ============================================================================
969// DataBrowser Wrapper
970// ============================================================================
971//
972// basing on DataBrowserItemIDs
973//
974
975IMPLEMENT_ABSTRACT_CLASS( wxMacDataBrowserControl , wxMacControl )
976
977pascal void wxMacDataBrowserControl::DataBrowserItemNotificationProc(
978 ControlRef browser,
979 DataBrowserItemID itemID,
980 DataBrowserItemNotification message,
981 DataBrowserItemDataRef itemData )
982{
983 wxMacDataBrowserControl* ctl = wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser ), wxMacDataBrowserControl);
984 if ( ctl != 0 )
985 {
986 ctl->ItemNotification(itemID, message, itemData);
987 }
988}
989
990pascal OSStatus wxMacDataBrowserControl::DataBrowserGetSetItemDataProc(
991 ControlRef browser,
992 DataBrowserItemID itemID,
993 DataBrowserPropertyID property,
994 DataBrowserItemDataRef itemData,
995 Boolean changeValue )
996{
997 OSStatus err = errDataBrowserPropertyNotSupported;
998 wxMacDataBrowserControl* ctl = wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser ), wxMacDataBrowserControl);
999 if ( ctl != 0 )
1000 {
1001 err = ctl->GetSetItemData(itemID, property, itemData, changeValue);
1002 }
1003 return err;
1004}
1005
1006pascal Boolean wxMacDataBrowserControl::DataBrowserCompareProc(
1007 ControlRef browser,
1008 DataBrowserItemID itemOneID,
1009 DataBrowserItemID itemTwoID,
1010 DataBrowserPropertyID sortProperty)
1011{
1012 wxMacDataBrowserControl* ctl = wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser ), wxMacDataBrowserControl);
1013 if ( ctl != 0 )
1014 {
1015 return ctl->CompareItems(itemOneID, itemTwoID, sortProperty);
1016 }
1017 return false;
1018}
1019
1020DataBrowserItemDataUPP gDataBrowserItemDataUPP = NULL;
1021DataBrowserItemNotificationUPP gDataBrowserItemNotificationUPP = NULL;
1022DataBrowserItemCompareUPP gDataBrowserItemCompareUPP = NULL;
1023
1024wxMacDataBrowserControl::wxMacDataBrowserControl( wxWindow* peer,
1025 const wxPoint& pos,
1026 const wxSize& size,
1027 long WXUNUSED(style))
1028 : wxMacControl( peer )
1029{
1030 Rect bounds = wxMacGetBoundsForControl( peer, pos, size );
1031 OSStatus err = ::CreateDataBrowserControl(
1032 MAC_WXHWND(peer->MacGetTopLevelWindowRef()),
1033 &bounds, kDataBrowserListView, &m_controlRef );
1034 SetReferenceInNativeControl();
1035 verify_noerr( err );
1036 if ( gDataBrowserItemCompareUPP == NULL )
1037 gDataBrowserItemCompareUPP = NewDataBrowserItemCompareUPP(DataBrowserCompareProc);
1038 if ( gDataBrowserItemDataUPP == NULL )
1039 gDataBrowserItemDataUPP = NewDataBrowserItemDataUPP(DataBrowserGetSetItemDataProc);
1040 if ( gDataBrowserItemNotificationUPP == NULL )
1041 {
1042 gDataBrowserItemNotificationUPP =
1043 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc);
1044 }
1045
1046 DataBrowserCallbacks callbacks;
1047 InitializeDataBrowserCallbacks( &callbacks, kDataBrowserLatestCallbacks );
1048
1049 callbacks.u.v1.itemDataCallback = gDataBrowserItemDataUPP;
1050 callbacks.u.v1.itemCompareCallback = gDataBrowserItemCompareUPP;
1051 callbacks.u.v1.itemNotificationCallback = gDataBrowserItemNotificationUPP;
1052 SetCallbacks( &callbacks );
1053
1054}
1055
1056OSStatus wxMacDataBrowserControl::GetItemCount( DataBrowserItemID container,
1057 Boolean recurse,
1058 DataBrowserItemState state,
1059 ItemCount *numItems) const
1060{
1061 return GetDataBrowserItemCount( m_controlRef, container, recurse, state, numItems );
1062}
1063
1064OSStatus wxMacDataBrowserControl::GetItems( DataBrowserItemID container,
1065 Boolean recurse,
1066 DataBrowserItemState state,
1067 Handle items) const
1068{
1069 return GetDataBrowserItems( m_controlRef, container, recurse, state, items );
1070}
1071
1072OSStatus wxMacDataBrowserControl::SetSelectionFlags( DataBrowserSelectionFlags options )
1073{
1074 return SetDataBrowserSelectionFlags( m_controlRef, options );
1075}
1076
1077OSStatus wxMacDataBrowserControl::AddColumn( DataBrowserListViewColumnDesc *columnDesc,
1078 DataBrowserTableViewColumnIndex position )
1079{
1080 return AddDataBrowserListViewColumn( m_controlRef, columnDesc, position );
1081}
1082
1083OSStatus wxMacDataBrowserControl::GetColumnIDFromIndex( DataBrowserTableViewColumnIndex position, DataBrowserTableViewColumnID* id ){
1084 return GetDataBrowserTableViewColumnProperty( m_controlRef, position, id );
1085}
1086
1087OSStatus wxMacDataBrowserControl::RemoveColumn( DataBrowserTableViewColumnIndex position )
1088{
1089 DataBrowserTableViewColumnID id;
1090 GetColumnIDFromIndex( position, &id );
1091 return RemoveDataBrowserTableViewColumn( m_controlRef, id );
1092}
1093
1094OSStatus wxMacDataBrowserControl::AutoSizeColumns()
1095{
1096 return AutoSizeDataBrowserListViewColumns(m_controlRef);
1097}
1098
1099OSStatus wxMacDataBrowserControl::SetHasScrollBars( bool horiz, bool vert )
1100{
1101 return SetDataBrowserHasScrollBars( m_controlRef, horiz, vert );
1102}
1103
1104OSStatus wxMacDataBrowserControl::SetHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle )
1105{
1106 return SetDataBrowserTableViewHiliteStyle( m_controlRef, hiliteStyle );
1107}
1108
1109OSStatus wxMacDataBrowserControl::SetHeaderButtonHeight(UInt16 height)
1110{
1111 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef, height );
1112}
1113
1114OSStatus wxMacDataBrowserControl::GetHeaderButtonHeight(UInt16 *height)
1115{
1116 return GetDataBrowserListViewHeaderBtnHeight( m_controlRef, height );
1117}
1118
1119OSStatus wxMacDataBrowserControl::SetCallbacks(const DataBrowserCallbacks *callbacks)
1120{
1121 return SetDataBrowserCallbacks( m_controlRef, callbacks );
1122}
1123
1124OSStatus wxMacDataBrowserControl::UpdateItems(
1125 DataBrowserItemID container,
1126 UInt32 numItems,
1127 const DataBrowserItemID *items,
1128 DataBrowserPropertyID preSortProperty,
1129 DataBrowserPropertyID propertyID ) const
1130{
1131 return UpdateDataBrowserItems( m_controlRef, container, numItems, items, preSortProperty, propertyID );
1132}
1133
1134bool wxMacDataBrowserControl::IsItemSelected( DataBrowserItemID item ) const
1135{
1136 return IsDataBrowserItemSelected( m_controlRef, item );
1137}
1138
1139OSStatus wxMacDataBrowserControl::AddItems(
1140 DataBrowserItemID container,
1141 UInt32 numItems,
1142 const DataBrowserItemID *items,
1143 DataBrowserPropertyID preSortProperty )
1144{
1145 return AddDataBrowserItems( m_controlRef, container, numItems, items, preSortProperty );
1146}
1147
1148OSStatus wxMacDataBrowserControl::RemoveItems(
1149 DataBrowserItemID container,
1150 UInt32 numItems,
1151 const DataBrowserItemID *items,
1152 DataBrowserPropertyID preSortProperty )
1153{
1154 return RemoveDataBrowserItems( m_controlRef, container, numItems, items, preSortProperty );
1155}
1156
1157OSStatus wxMacDataBrowserControl::RevealItem(
1158 DataBrowserItemID item,
1159 DataBrowserPropertyID propertyID,
1160 DataBrowserRevealOptions options ) const
1161{
1162 return RevealDataBrowserItem( m_controlRef, item, propertyID, options );
1163}
1164
1165OSStatus wxMacDataBrowserControl::SetSelectedItems(
1166 UInt32 numItems,
1167 const DataBrowserItemID *items,
1168 DataBrowserSetOption operation )
1169{
1170 return SetDataBrowserSelectedItems( m_controlRef, numItems, items, operation );
1171}
1172
1173OSStatus wxMacDataBrowserControl::GetSelectionAnchor( DataBrowserItemID *first, DataBrowserItemID *last ) const
1174{
1175 return GetDataBrowserSelectionAnchor( m_controlRef, first, last );
1176}
1177
1178OSStatus wxMacDataBrowserControl::GetItemID( DataBrowserTableViewRowIndex row, DataBrowserItemID * item ) const
1179{
1180 return GetDataBrowserTableViewItemID( m_controlRef, row, item );
1181}
1182
1183OSStatus wxMacDataBrowserControl::GetItemRow( DataBrowserItemID item, DataBrowserTableViewRowIndex * row ) const
1184{
1185 return GetDataBrowserTableViewItemRow( m_controlRef, item, row );
1186}
1187
1188OSStatus wxMacDataBrowserControl::SetDefaultRowHeight( UInt16 height )
1189{
1190 return SetDataBrowserTableViewRowHeight( m_controlRef , height );
1191}
1192
1193OSStatus wxMacDataBrowserControl::GetDefaultRowHeight( UInt16 * height ) const
1194{
1195 return GetDataBrowserTableViewRowHeight( m_controlRef, height );
1196}
1197
1198OSStatus wxMacDataBrowserControl::SetRowHeight( DataBrowserItemID item , UInt16 height)
1199{
1200 return SetDataBrowserTableViewItemRowHeight( m_controlRef, item , height );
1201}
1202
1203OSStatus wxMacDataBrowserControl::GetRowHeight( DataBrowserItemID item , UInt16 *height) const
1204{
1205 return GetDataBrowserTableViewItemRowHeight( m_controlRef, item , height);
1206}
1207
1208OSStatus wxMacDataBrowserControl::GetColumnWidth( DataBrowserPropertyID column , UInt16 *width ) const
1209{
1210 return GetDataBrowserTableViewNamedColumnWidth( m_controlRef , column , width );
1211}
1212
1213OSStatus wxMacDataBrowserControl::SetColumnWidth( DataBrowserPropertyID column , UInt16 width )
1214{
1215 return SetDataBrowserTableViewNamedColumnWidth( m_controlRef , column , width );
1216}
1217
1218OSStatus wxMacDataBrowserControl::GetDefaultColumnWidth( UInt16 *width ) const
1219{
1220 return GetDataBrowserTableViewColumnWidth( m_controlRef , width );
1221}
1222
1223OSStatus wxMacDataBrowserControl::SetDefaultColumnWidth( UInt16 width )
1224{
1225 return SetDataBrowserTableViewColumnWidth( m_controlRef , width );
1226}
1227
1228OSStatus wxMacDataBrowserControl::GetColumnCount(UInt32* numColumns) const
1229{
1230 return GetDataBrowserTableViewColumnCount( m_controlRef, numColumns);
1231}
1232
1233OSStatus wxMacDataBrowserControl::GetColumnPosition( DataBrowserPropertyID column,
1234 DataBrowserTableViewColumnIndex *position) const
1235{
1236 return GetDataBrowserTableViewColumnPosition( m_controlRef , column , position);
1237}
1238
1239OSStatus wxMacDataBrowserControl::SetColumnPosition( DataBrowserPropertyID column, DataBrowserTableViewColumnIndex position)
1240{
1241 return SetDataBrowserTableViewColumnPosition( m_controlRef , column , position);
1242}
1243
1244OSStatus wxMacDataBrowserControl::GetScrollPosition( UInt32 *top , UInt32 *left ) const
1245{
1246 return GetDataBrowserScrollPosition( m_controlRef , top , left );
1247}
1248
1249OSStatus wxMacDataBrowserControl::SetScrollPosition( UInt32 top , UInt32 left )
1250{
1251 return SetDataBrowserScrollPosition( m_controlRef , top , left );
1252}
1253
1254OSStatus wxMacDataBrowserControl::GetSortProperty( DataBrowserPropertyID *column ) const
1255{
1256 return GetDataBrowserSortProperty( m_controlRef , column );
1257}
1258
1259OSStatus wxMacDataBrowserControl::SetSortProperty( DataBrowserPropertyID column )
1260{
1261 return SetDataBrowserSortProperty( m_controlRef , column );
1262}
1263
1264OSStatus wxMacDataBrowserControl::GetSortOrder( DataBrowserSortOrder *order ) const
1265{
1266 return GetDataBrowserSortOrder( m_controlRef , order );
1267}
1268
1269OSStatus wxMacDataBrowserControl::SetSortOrder( DataBrowserSortOrder order )
1270{
1271 return SetDataBrowserSortOrder( m_controlRef , order );
1272}
1273
1274OSStatus wxMacDataBrowserControl::GetPropertyFlags( DataBrowserPropertyID property,
1275 DataBrowserPropertyFlags *flags ) const
1276{
1277 return GetDataBrowserPropertyFlags( m_controlRef , property , flags );
1278}
1279
1280OSStatus wxMacDataBrowserControl::SetPropertyFlags( DataBrowserPropertyID property,
1281 DataBrowserPropertyFlags flags )
1282{
1283 return SetDataBrowserPropertyFlags( m_controlRef , property , flags );
1284}
1285
1286OSStatus wxMacDataBrowserControl::GetHeaderDesc( DataBrowserPropertyID property,
1287 DataBrowserListViewHeaderDesc *desc ) const
1288{
1289 return GetDataBrowserListViewHeaderDesc( m_controlRef , property , desc );
1290}
1291
1292OSStatus wxMacDataBrowserControl::SetHeaderDesc( DataBrowserPropertyID property,
1293 DataBrowserListViewHeaderDesc *desc )
1294{
1295 return SetDataBrowserListViewHeaderDesc( m_controlRef , property , desc );
1296}
1297
1298OSStatus wxMacDataBrowserControl::SetDisclosureColumn( DataBrowserPropertyID property ,
1299 Boolean expandableRows )
1300{
1301 return SetDataBrowserListViewDisclosureColumn( m_controlRef, property, expandableRows);
1302}
1303
1304// ============================================================================
1305// Higher-level Databrowser
1306// ============================================================================
1307//
1308// basing on data item objects
1309//
1310
1311wxMacDataItem::wxMacDataItem()
1312{
1313 m_data = NULL;
1314
1315 m_order = 0;
1316 m_colId = kTextColumnId; // for compat with existing wx*ListBox impls.
1317}
1318
1319wxMacDataItem::~wxMacDataItem()
1320{
1321}
1322
1323void wxMacDataItem::SetOrder( SInt32 order )
1324{
1325 m_order = order;
1326}
1327
1328SInt32 wxMacDataItem::GetOrder() const
1329{
1330 return m_order;
1331}
1332
1333void wxMacDataItem::SetData( void* data)
1334{
1335 m_data = data;
1336}
1337
1338void* wxMacDataItem::GetData() const
1339{
1340 return m_data;
1341}
1342
1343short wxMacDataItem::GetColumn()
1344{
1345 return m_colId;
1346}
1347
1348void wxMacDataItem::SetColumn( short col )
1349{
1350 m_colId = col;
1351}
1352
1353void wxMacDataItem::SetLabel( const wxString& str)
1354{
1355 m_label = str;
1356 m_cfLabel = wxCFStringRef( str , wxLocale::GetSystemEncoding());
1357}
1358
1359const wxString& wxMacDataItem::GetLabel() const
1360{
1361 return m_label;
1362}
1363
1364bool wxMacDataItem::IsLessThan(wxMacDataItemBrowserControl *WXUNUSED(owner) ,
1365 const wxMacDataItem* rhs,
1366 DataBrowserPropertyID sortProperty) const
1367{
1368 const wxMacDataItem* otherItem = wx_const_cast(wxMacDataItem*,rhs);
1369 bool retval = false;
1370
1371 if ( sortProperty == m_colId ){
1372 retval = m_label.CmpNoCase( otherItem->m_label) < 0;
1373 }
1374
1375 else if ( sortProperty == kNumericOrderColumnId )
1376 retval = m_order < otherItem->m_order;
1377
1378 return retval;
1379}
1380
1381OSStatus wxMacDataItem::GetSetData( wxMacDataItemBrowserControl *WXUNUSED(owner) ,
1382 DataBrowserPropertyID property,
1383 DataBrowserItemDataRef itemData,
1384 bool changeValue )
1385{
1386 OSStatus err = errDataBrowserPropertyNotSupported;
1387 if ( !changeValue )
1388 {
1389 if ( property == m_colId ){
1390 err = ::SetDataBrowserItemDataText( itemData, m_cfLabel );
1391 err = noErr;
1392 }
1393 else if ( property == kNumericOrderColumnId ){
1394 err = ::SetDataBrowserItemDataValue( itemData, m_order );
1395 err = noErr;
1396 }
1397 else{
1398 }
1399 }
1400 else
1401 {
1402 switch (property)
1403 {
1404 // no editable props here
1405 default:
1406 break;
1407 }
1408 }
1409
1410 return err;
1411}
1412
1413void wxMacDataItem::Notification(wxMacDataItemBrowserControl *WXUNUSED(owner) ,
1414 DataBrowserItemNotification WXUNUSED(message),
1415 DataBrowserItemDataRef WXUNUSED(itemData) ) const
1416{
1417}
1418
1419IMPLEMENT_DYNAMIC_CLASS( wxMacDataItemBrowserControl , wxMacDataBrowserControl )
1420
1421wxMacDataItemBrowserControl::wxMacDataItemBrowserControl( wxWindow* peer , const wxPoint& pos, const wxSize& size, long style) :
1422 wxMacDataBrowserControl( peer, pos, size, style )
1423{
1424 m_suppressSelection = false;
1425 m_sortOrder = SortOrder_None;
1426 m_clientDataItemsType = wxClientData_None;
1427}
1428
1429wxMacDataItem* wxMacDataItemBrowserControl::CreateItem()
1430{
1431 return new wxMacDataItem();
1432}
1433
1434wxMacDataItemBrowserSelectionSuppressor::wxMacDataItemBrowserSelectionSuppressor(wxMacDataItemBrowserControl *browser)
1435{
1436 m_former = browser->SuppressSelection(true);
1437 m_browser = browser;
1438}
1439
1440wxMacDataItemBrowserSelectionSuppressor::~wxMacDataItemBrowserSelectionSuppressor()
1441{
1442 m_browser->SuppressSelection(m_former);
1443}
1444
1445bool wxMacDataItemBrowserControl::SuppressSelection( bool suppress )
1446{
1447 bool former = m_suppressSelection;
1448 m_suppressSelection = suppress;
1449
1450 return former;
1451}
1452
1453Boolean wxMacDataItemBrowserControl::CompareItems(DataBrowserItemID itemOneID,
1454 DataBrowserItemID itemTwoID,
1455 DataBrowserPropertyID sortProperty)
1456{
1457 wxMacDataItem* itemOne = (wxMacDataItem*) itemOneID;
1458 wxMacDataItem* itemTwo = (wxMacDataItem*) itemTwoID;
1459 return CompareItems( itemOne , itemTwo , sortProperty );
1460}
1461
1462Boolean wxMacDataItemBrowserControl::CompareItems(const wxMacDataItem* itemOne,
1463 const wxMacDataItem* itemTwo,
1464 DataBrowserPropertyID sortProperty)
1465{
1466 Boolean retval = false;
1467 if ( itemOne != NULL )
1468 retval = itemOne->IsLessThan( this , itemTwo , sortProperty);
1469 return retval;
1470}
1471
1472OSStatus wxMacDataItemBrowserControl::GetSetItemData(
1473 DataBrowserItemID itemID,
1474 DataBrowserPropertyID property,
1475 DataBrowserItemDataRef itemData,
1476 Boolean changeValue )
1477{
1478 wxMacDataItem* item = (wxMacDataItem*) itemID;
1479 return GetSetItemData(item, property, itemData , changeValue );
1480}
1481
1482OSStatus wxMacDataItemBrowserControl::GetSetItemData(
1483 wxMacDataItem* item,
1484 DataBrowserPropertyID property,
1485 DataBrowserItemDataRef itemData,
1486 Boolean changeValue )
1487{
1488 OSStatus err = errDataBrowserPropertyNotSupported;
1489 switch( property )
1490 {
1491 case kDataBrowserContainerIsClosableProperty :
1492 case kDataBrowserContainerIsSortableProperty :
1493 case kDataBrowserContainerIsOpenableProperty :
1494 // right now default behaviour on these
1495 break;
1496 default :
1497
1498 if ( item != NULL ){
1499 err = item->GetSetData( this, property , itemData , changeValue );
1500 }
1501 break;
1502
1503 }
1504 return err;
1505}
1506
1507void wxMacDataItemBrowserControl::ItemNotification(
1508 DataBrowserItemID itemID,
1509 DataBrowserItemNotification message,
1510 DataBrowserItemDataRef itemData)
1511{
1512 wxMacDataItem* item = (wxMacDataItem*) itemID;
1513 ItemNotification( item , message, itemData);
1514}
1515
1516void wxMacDataItemBrowserControl::ItemNotification(
1517 const wxMacDataItem* item,
1518 DataBrowserItemNotification message,
1519 DataBrowserItemDataRef itemData)
1520{
1521 if (item != NULL)
1522 item->Notification( this, message, itemData);
1523}
1524
1525unsigned int wxMacDataItemBrowserControl::GetItemCount(const wxMacDataItem* container,
1526 bool recurse , DataBrowserItemState state) const
1527{
1528 ItemCount numItems = 0;
1529 verify_noerr( wxMacDataBrowserControl::GetItemCount( (DataBrowserItemID)container,
1530 recurse, state, &numItems ) );
1531 return numItems;
1532}
1533
1534unsigned int wxMacDataItemBrowserControl::GetSelectedItemCount( const wxMacDataItem* container,
1535 bool recurse ) const
1536{
1537 return GetItemCount( container, recurse, kDataBrowserItemIsSelected );
1538
1539}
1540
1541void wxMacDataItemBrowserControl::GetItems(const wxMacDataItem* container,
1542 bool recurse , DataBrowserItemState state, wxArrayMacDataItemPtr &items) const
1543{
1544 Handle handle = NewHandle(0);
1545 verify_noerr( wxMacDataBrowserControl::GetItems( (DataBrowserItemID)container ,
1546 recurse , state, handle) );
1547
1548 int itemCount = GetHandleSize(handle)/sizeof(DataBrowserItemID);
1549 HLock( handle );
1550 wxMacDataItemPtr* itemsArray = (wxMacDataItemPtr*) *handle;
1551 for ( int i = 0; i < itemCount; ++i)
1552 {
1553 items.Add(itemsArray[i]);
1554 }
1555 HUnlock( handle );
1556 DisposeHandle( handle );
1557}
1558
1559unsigned int wxMacDataItemBrowserControl::GetLineFromItem(const wxMacDataItem* item) const
1560{
1561 DataBrowserTableViewRowIndex row;
1562 OSStatus err = GetItemRow( (DataBrowserItemID) item , &row);
1563 wxCHECK( err == noErr, (unsigned)-1 );
1564 return row;
1565}
1566
1567wxMacDataItem* wxMacDataItemBrowserControl::GetItemFromLine(unsigned int n) const
1568{
1569 DataBrowserItemID id;
1570 OSStatus err = GetItemID( (DataBrowserTableViewRowIndex) n , &id);
1571 wxCHECK( err == noErr, NULL );
1572 return (wxMacDataItem*) id;
1573}
1574
1575void wxMacDataItemBrowserControl::UpdateItem(const wxMacDataItem *container,
1576 const wxMacDataItem *item , DataBrowserPropertyID property) const
1577{
1578 verify_noerr( wxMacDataBrowserControl::UpdateItems((DataBrowserItemID)container, 1,
1579 (DataBrowserItemID*) &item, kDataBrowserItemNoProperty /* notSorted */, property ) );
1580}
1581
1582void wxMacDataItemBrowserControl::UpdateItems(const wxMacDataItem *container,
1583 wxArrayMacDataItemPtr &itemArray , DataBrowserPropertyID property) const
1584{
1585 unsigned int noItems = itemArray.GetCount();
1586 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1587 for ( unsigned int i = 0; i < noItems; ++i )
1588 items[i] = (DataBrowserItemID) itemArray[i];
1589
1590 verify_noerr( wxMacDataBrowserControl::UpdateItems((DataBrowserItemID)container, noItems,
1591 items, kDataBrowserItemNoProperty /* notSorted */, property ) );
1592 delete [] items;
1593}
1594
1595void wxMacDataItemBrowserControl::InsertColumn(int colId, DataBrowserPropertyType colType,
1596 const wxString& title, SInt16 just, int defaultWidth)
1597{
1598 DataBrowserListViewColumnDesc columnDesc;
1599 columnDesc.headerBtnDesc.titleOffset = 0;
1600 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
1601
1602 columnDesc.headerBtnDesc.btnFontStyle.flags =
1603 kControlUseFontMask | kControlUseJustMask;
1604
1605 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlContentTextOnly;
1606 columnDesc.headerBtnDesc.btnFontStyle.just = just;
1607 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
1608 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
1609
1610 // TODO: Why is m_font not defined when we enter wxLC_LIST mode, but is
1611 // defined for other modes?
1612 wxFontEncoding enc;
1613 if ( m_font.Ok() )
1614 enc = m_font.GetEncoding();
1615 else
1616 enc = wxLocale::GetSystemEncoding();
1617 wxCFStringRef cfTitle( title, enc );
1618 columnDesc.headerBtnDesc.titleString = cfTitle;
1619
1620 columnDesc.headerBtnDesc.minimumWidth = 0;
1621 columnDesc.headerBtnDesc.maximumWidth = 30000;
1622
1623 columnDesc.propertyDesc.propertyID = (kMinColumnId + colId);
1624 columnDesc.propertyDesc.propertyType = colType;
1625 columnDesc.propertyDesc.propertyFlags = kDataBrowserListViewSortableColumn;
1626 columnDesc.propertyDesc.propertyFlags |= kDataBrowserListViewTypeSelectColumn;
1627 columnDesc.propertyDesc.propertyFlags |= kDataBrowserListViewNoGapForIconInHeaderButton;
1628
1629 verify_noerr( AddColumn( &columnDesc, kDataBrowserListViewAppendColumn ) );
1630
1631 if (defaultWidth > 0){
1632 SetColumnWidth(colId, defaultWidth);
1633 }
1634
1635}
1636
1637void wxMacDataItemBrowserControl::SetColumnWidth(int colId, int width)
1638{
1639 DataBrowserPropertyID id;
1640 GetColumnIDFromIndex(colId, &id);
1641 verify_noerr( wxMacDataBrowserControl::SetColumnWidth(id, width));
1642}
1643
1644int wxMacDataItemBrowserControl::GetColumnWidth(int colId)
1645{
1646 DataBrowserPropertyID id;
1647 GetColumnIDFromIndex(colId, &id);
1648 UInt16 result;
1649 verify_noerr( wxMacDataBrowserControl::GetColumnWidth(id, &result));
1650 return result;
1651}
1652
1653void wxMacDataItemBrowserControl::AddItem(wxMacDataItem *container, wxMacDataItem *item)
1654{
1655 verify_noerr( wxMacDataBrowserControl::AddItems( (DataBrowserItemID)container, 1,
1656 (DataBrowserItemID*) &item, kDataBrowserItemNoProperty ) );
1657}
1658
1659void wxMacDataItemBrowserControl::AddItems(wxMacDataItem *container, wxArrayMacDataItemPtr &itemArray )
1660{
1661 unsigned int noItems = itemArray.GetCount();
1662 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1663 for ( unsigned int i = 0; i < noItems; ++i )
1664 items[i] = (DataBrowserItemID) itemArray[i];
1665
1666 verify_noerr( wxMacDataBrowserControl::AddItems( (DataBrowserItemID)container, noItems,
1667 (DataBrowserItemID*) items, kDataBrowserItemNoProperty ) );
1668 delete [] items;
1669}
1670
1671void wxMacDataItemBrowserControl::RemoveItem(wxMacDataItem *container, wxMacDataItem* item)
1672{
1673 OSStatus err = wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID)container, 1,
1674 (DataBrowserItemID*) &item, kDataBrowserItemNoProperty );
1675 verify_noerr( err );
1676}
1677
1678void wxMacDataItemBrowserControl::RemoveItems(wxMacDataItem *container, wxArrayMacDataItemPtr &itemArray)
1679{
1680 unsigned int noItems = itemArray.GetCount();
1681 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1682 for ( unsigned int i = 0; i < noItems; ++i )
1683 items[i] = (DataBrowserItemID) itemArray[i];
1684
1685 OSStatus err = wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID)container, noItems,
1686 (DataBrowserItemID*) items, kDataBrowserItemNoProperty );
1687 verify_noerr( err );
1688 delete [] items;
1689}
1690
1691void wxMacDataItemBrowserControl::RemoveAllItems(wxMacDataItem *container)
1692{
1693 OSStatus err = wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID)container, 0 , NULL , kDataBrowserItemNoProperty );
1694 verify_noerr( err );
1695}
1696
1697void wxMacDataItemBrowserControl::SetSelectedItem(wxMacDataItem* item , DataBrowserSetOption option)
1698{
1699 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( 1, (DataBrowserItemID*) &item, option ));
1700}
1701
1702void wxMacDataItemBrowserControl::SetSelectedAllItems(DataBrowserSetOption option)
1703{
1704 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( 0 , NULL , option ));
1705}
1706
1707void wxMacDataItemBrowserControl::SetSelectedItems(wxArrayMacDataItemPtr &itemArray , DataBrowserSetOption option)
1708{
1709 unsigned int noItems = itemArray.GetCount();
1710 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1711 for ( unsigned int i = 0; i < noItems; ++i )
1712 items[i] = (DataBrowserItemID) itemArray[i];
1713
1714 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( noItems, (DataBrowserItemID*) items, option ));
1715 delete [] items;
1716}
1717
1718Boolean wxMacDataItemBrowserControl::IsItemSelected( const wxMacDataItem* item) const
1719{
1720 return wxMacDataBrowserControl::IsItemSelected( (DataBrowserItemID) item);
1721}
1722
1723void wxMacDataItemBrowserControl::RevealItem( wxMacDataItem* item, DataBrowserRevealOptions options)
1724{
1725 verify_noerr(wxMacDataBrowserControl::RevealItem( (DataBrowserItemID) item, kDataBrowserNoItem , options ) );
1726}
1727
1728void wxMacDataItemBrowserControl::GetSelectionAnchor( wxMacDataItemPtr* first , wxMacDataItemPtr* last) const
1729{
1730 verify_noerr(wxMacDataBrowserControl::GetSelectionAnchor( (DataBrowserItemID*) first, (DataBrowserItemID*) last) );
1731}
1732
1733wxClientDataType wxMacDataItemBrowserControl::GetClientDataType() const
1734{
1735 return m_clientDataItemsType;
1736}
1737void wxMacDataItemBrowserControl::SetClientDataType(wxClientDataType clientDataItemsType)
1738{
1739 m_clientDataItemsType = clientDataItemsType;
1740}
1741
1742unsigned int wxMacDataItemBrowserControl::MacGetCount() const
1743{
1744 return GetItemCount(wxMacDataBrowserRootContainer,false,kDataBrowserItemAnyState);
1745}
1746
1747void wxMacDataItemBrowserControl::MacDelete( unsigned int n )
1748{
1749 wxMacDataItem* item = (wxMacDataItem*)GetItemFromLine( n );
1750 RemoveItem( wxMacDataBrowserRootContainer, item );
1751}
1752
1753void wxMacDataItemBrowserControl::MacInsert( unsigned int n,
1754 const wxArrayStringsAdapter& items,
1755 int column )
1756{
1757 size_t itemsCount = items.GetCount();
1758 if ( itemsCount == 0 )
1759 return;
1760
1761 SInt32 frontLineOrder = 0;
1762
1763 if ( m_sortOrder == SortOrder_None )
1764 {
1765 // increase the order of the lines to be shifted
1766 unsigned int lines = MacGetCount();
1767 for ( unsigned int i = n; i < lines; ++i)
1768 {
1769 wxMacDataItem* iter = (wxMacDataItem*) GetItemFromLine(i);
1770 iter->SetOrder( iter->GetOrder() + itemsCount );
1771 }
1772 if ( n > 0 )
1773 {
1774 wxMacDataItem* iter = (wxMacDataItem*) GetItemFromLine(n-1);
1775 frontLineOrder = iter->GetOrder();
1776 }
1777 }
1778
1779 wxArrayMacDataItemPtr ids;
1780 ids.SetCount( itemsCount );
1781
1782 for ( unsigned int i = 0; i < itemsCount; ++i )
1783 {
1784 wxMacDataItem* item = CreateItem();
1785 item->SetLabel( items[i]);
1786 if ( column != -1 )
1787 item->SetColumn( kMinColumnId + column );
1788
1789 if ( m_sortOrder == SortOrder_None )
1790 item->SetOrder( frontLineOrder + 1 + i );
1791
1792 ids[i] = item;
1793 }
1794
1795 AddItems( wxMacDataBrowserRootContainer, ids );
1796}
1797
1798int wxMacDataItemBrowserControl::MacAppend( const wxString& text)
1799{
1800 wxMacDataItem* item = CreateItem();
1801 item->SetLabel( text );
1802 if ( m_sortOrder == SortOrder_None )
1803 {
1804 unsigned int lines = MacGetCount();
1805 if ( lines == 0 )
1806 item->SetOrder( 1 );
1807 else
1808 {
1809 wxMacDataItem* frontItem = (wxMacDataItem*) GetItemFromLine(lines-1);
1810 item->SetOrder( frontItem->GetOrder() + 1 );
1811 }
1812 }
1813 AddItem( wxMacDataBrowserRootContainer, item );
1814
1815 return GetLineFromItem(item);
1816}
1817
1818void wxMacDataItemBrowserControl::MacClear()
1819{
1820 wxMacDataItemBrowserSelectionSuppressor suppressor(this);
1821 RemoveAllItems(wxMacDataBrowserRootContainer);
1822}
1823
1824void wxMacDataItemBrowserControl::MacDeselectAll()
1825{
1826 wxMacDataItemBrowserSelectionSuppressor suppressor(this);
1827 SetSelectedAllItems( kDataBrowserItemsRemove );
1828}
1829
1830void wxMacDataItemBrowserControl::MacSetSelection( unsigned int n, bool select, bool multi )
1831{
1832 wxMacDataItem* item = (wxMacDataItem*) GetItemFromLine(n);
1833 wxMacDataItemBrowserSelectionSuppressor suppressor(this);
1834
1835 if ( IsItemSelected( item ) != select )
1836 {
1837 if ( select )
1838 SetSelectedItem( item, multi ? kDataBrowserItemsAdd : kDataBrowserItemsAssign );
1839 else
1840 SetSelectedItem( item, kDataBrowserItemsRemove );
1841 }
1842
1843 MacScrollTo( n );
1844}
1845
1846bool wxMacDataItemBrowserControl::MacIsSelected( unsigned int n ) const
1847{
1848 wxMacDataItem* item = (wxMacDataItem*) GetItemFromLine(n);
1849 return IsItemSelected( item );
1850}
1851
1852int wxMacDataItemBrowserControl::MacGetSelection() const
1853{
1854 wxMacDataItemPtr first, last;
1855 GetSelectionAnchor( &first, &last );
1856
1857 if ( first != NULL )
1858 {
1859 return GetLineFromItem( first );
1860 }
1861
1862 return -1;
1863}
1864
1865int wxMacDataItemBrowserControl::MacGetSelections( wxArrayInt& aSelections ) const
1866{
1867 aSelections.Empty();
1868 wxArrayMacDataItemPtr selectedItems;
1869 GetItems( wxMacDataBrowserRootContainer, false , kDataBrowserItemIsSelected, selectedItems);
1870
1871 int count = selectedItems.GetCount();
1872
1873 for ( int i = 0; i < count; ++i)
1874 {
1875 aSelections.Add(GetLineFromItem(selectedItems[i]));
1876 }
1877
1878 return count;
1879}
1880
1881void wxMacDataItemBrowserControl::MacSetString( unsigned int n, const wxString& text )
1882{
1883 // as we don't store the strings we only have to issue a redraw
1884 wxMacDataItem* item = (wxMacDataItem*) GetItemFromLine( n);
1885 item->SetLabel( text );
1886 UpdateItem( wxMacDataBrowserRootContainer, item , kTextColumnId );
1887}
1888
1889wxString wxMacDataItemBrowserControl::MacGetString( unsigned int n ) const
1890{
1891 wxMacDataItem * item = (wxMacDataItem*) GetItemFromLine( n );
1892 return item->GetLabel();
1893}
1894
1895void wxMacDataItemBrowserControl::MacSetClientData( unsigned int n, void * data)
1896{
1897 wxMacDataItem* item = (wxMacDataItem*) GetItemFromLine( n);
1898 item->SetData( data );
1899 // not displayed, therefore no Update infos to DataBrowser
1900}
1901
1902void * wxMacDataItemBrowserControl::MacGetClientData( unsigned int n) const
1903{
1904 wxMacDataItem * item = (wxMacDataItem*) GetItemFromLine( n );
1905 return item->GetData();
1906}
1907
1908void wxMacDataItemBrowserControl::MacScrollTo( unsigned int n )
1909{
1910 UInt32 top , left ;
1911 GetScrollPosition( &top , &left ) ;
1912 wxMacDataItem * item = (wxMacDataItem*) GetItemFromLine( n );
1913
1914 // there is a bug in RevealItem that leads to situations
1915 // in large lists, where the item does not get scrolled
1916 // into sight, so we do a pre-scroll if necessary
1917 UInt16 height ;
1918 GetRowHeight( (DataBrowserItemID) item , &height ) ;
1919 UInt32 linetop = n * ((UInt32) height );
1920 UInt32 linebottom = linetop + height;
1921 Rect rect ;
1922 GetRect( &rect );
1923
1924 if ( linetop < top || linebottom > (top + rect.bottom - rect.top ) )
1925 SetScrollPosition( wxMax( n-2, 0 ) * ((UInt32)height) , left ) ;
1926
1927 RevealItem( item , kDataBrowserRevealWithoutSelecting );
1928}
1929
1930
1931
1932//
1933// Tab Control
1934//
1935
1936OSStatus wxMacControl::SetTabEnabled( SInt16 tabNo , bool enable )
1937{
1938 return ::SetTabEnabled( m_controlRef , tabNo , enable );
1939}
1940
1941#endif
1942
1943//
1944// Quartz Support
1945//
1946
1947/*
1948 Return the generic RGB color space. This is a 'get' function and the caller should
1949 not release the returned value unless the caller retains it first. Usually callers
1950 of this routine will immediately use the returned colorspace with CoreGraphics
1951 so they typically do not need to retain it themselves.
1952
1953 This function creates the generic RGB color space once and hangs onto it so it can
1954 return it whenever this function is called.
1955*/
1956
1957CGColorSpaceRef wxMacGetGenericRGBColorSpace()
1958{
1959 static wxCFRef<CGColorSpaceRef> genericRGBColorSpace;
1960
1961 if (genericRGBColorSpace == NULL)
1962 {
1963 genericRGBColorSpace.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ) );
1964 }
1965
1966 return genericRGBColorSpace;
1967}
1968
1969CGColorRef wxMacCreateCGColorFromHITheme( ThemeBrush brush )
1970{
1971 CGColorRef color ;
1972 HIThemeBrushCreateCGColor( brush, &color );
1973 return color;
1974}
1975
1976#if wxMAC_USE_QUICKDRAW
1977
1978static inline void PointFromHIPoint(const HIPoint& p, Point *pt)
1979{
1980 pt->h = wx_static_cast(short, p.x);
1981 pt->v = wx_static_cast(short, p.y);
1982}
1983
1984void wxMacGlobalToLocal( WindowRef window , Point*pt )
1985{
1986 HIPoint p = CGPointMake( pt->h, pt->v );
1987 HIViewRef contentView ;
1988 // TODO check toolbar offset
1989 HIViewFindByID( HIViewGetRoot( window ), kHIViewWindowContentID , &contentView) ;
1990 HIPointConvert( &p, kHICoordSpace72DPIGlobal, NULL, kHICoordSpaceView, contentView );
1991 PointFromHIPoint(p, pt);
1992}
1993
1994void wxMacLocalToGlobal( WindowRef window , Point*pt )
1995{
1996 HIPoint p = CGPointMake( pt->h, pt->v );
1997 HIViewRef contentView ;
1998 // TODO check toolbar offset
1999 HIViewFindByID( HIViewGetRoot( window ), kHIViewWindowContentID , &contentView) ;
2000 HIPointConvert( &p, kHICoordSpaceView, contentView, kHICoordSpace72DPIGlobal, NULL );
2001 PointFromHIPoint(p, pt);
2002}
2003
2004#endif // wxMAC_USE_QUICKDRAW
2005
2006#endif // wxUSE_GUI
2007
2008#if wxUSE_BASE
2009
2010#endif