1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/utils.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/apptrait.h"
19 #include "wx/mac/uma.h"
21 #include "wx/toplevel.h"
33 #include "MoreFilesX.h"
41 #if TARGET_API_MAC_OSX
42 #include <CoreServices/CoreServices.h>
44 #include <DriverServices.h>
45 #include <Multiprocessing.h>
49 #include <Carbon/Carbon.h>
51 #include <ATSUnicode.h>
52 #include <TextCommon.h>
53 #include <TextEncodingConverter.h>
57 #include "wx/mac/private.h" // includes mac headers
59 #if defined(__MWERKS__) && wxUSE_UNICODE
63 // ---------------------------------------------------------------------------
64 // code used in both base and GUI compilation
65 // ---------------------------------------------------------------------------
67 // our OS version is the same in non GUI and GUI cases
68 static int DoGetOSVersion(int *majorVsn
, int *minorVsn
)
72 // are there x-platform conventions ?
74 Gestalt(gestaltSystemVersion
, &theSystem
) ;
75 if (minorVsn
!= NULL
) {
76 *minorVsn
= (theSystem
& 0xFF ) ;
78 if (majorVsn
!= NULL
) {
79 *majorVsn
= (theSystem
>> 8 ) ;
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 #if defined(__WXDEBUG__) && defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
97 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
99 #ifndef __MetroNubUtils__
100 #include "MetroNubUtils.h"
107 #if TARGET_API_MAC_CARBON
109 #include <CodeFragments.h>
111 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr
, ProcInfoType procInfo
, ...);
113 ProcPtr gCallUniversalProc_Proc
= NULL
;
117 static MetroNubUserEntryBlock
* gMetroNubEntry
= NULL
;
119 static long fRunOnce
= false;
121 /* ---------------------------------------------------------------------------
123 --------------------------------------------------------------------------- */
125 Boolean
IsMetroNubInstalled()
132 gMetroNubEntry
= NULL
;
134 if (Gestalt(gestaltSystemVersion
, &value
) == noErr
&& value
< 0x1000)
136 /* look for MetroNub's Gestalt selector */
137 if (Gestalt(kMetroNubUserSignature
, &result
) == noErr
)
140 #if TARGET_API_MAC_CARBON
141 if (gCallUniversalProc_Proc
== NULL
)
143 CFragConnectionID connectionID
;
146 ProcPtr symbolAddress
;
148 CFragSymbolClass symbolClass
;
150 symbolAddress
= NULL
;
151 err
= GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch
, kFindCFrag
,
152 &connectionID
, &mainAddress
, errorString
);
156 gCallUniversalProc_Proc
= NULL
;
160 err
= FindSymbol(connectionID
, "\pCallUniversalProc",
161 (Ptr
*) &gCallUniversalProc_Proc
, &symbolClass
);
165 gCallUniversalProc_Proc
= NULL
;
172 MetroNubUserEntryBlock
* block
= (MetroNubUserEntryBlock
*)result
;
174 /* make sure the version of the API is compatible */
175 if (block
->apiLowVersion
<= kMetroNubUserAPIVersion
&&
176 kMetroNubUserAPIVersion
<= block
->apiHiVersion
)
177 gMetroNubEntry
= block
; /* success! */
186 #if TARGET_API_MAC_CARBON
187 return (gMetroNubEntry
!= NULL
&& gCallUniversalProc_Proc
!= NULL
);
189 return (gMetroNubEntry
!= NULL
);
193 /* ---------------------------------------------------------------------------
194 IsMWDebuggerRunning [v1 API]
195 --------------------------------------------------------------------------- */
197 Boolean
IsMWDebuggerRunning()
199 if (IsMetroNubInstalled())
200 return CallIsDebuggerRunningProc(gMetroNubEntry
->isDebuggerRunning
);
205 /* ---------------------------------------------------------------------------
206 AmIBeingMWDebugged [v1 API]
207 --------------------------------------------------------------------------- */
209 Boolean
AmIBeingMWDebugged()
211 if (IsMetroNubInstalled())
212 return CallAmIBeingDebuggedProc(gMetroNubEntry
->amIBeingDebugged
);
217 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
219 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
224 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
229 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
233 // defined in unix/utilsunx.cpp for Mac OS X
235 // get full hostname (with domain name if possible)
236 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
238 return wxGetHostName(buf
, maxSize
);
241 // Get hostname only (without domain name)
242 bool wxGetHostName(wxChar
*buf
, int maxSize
)
244 // Gets Chooser name of user by examining a System resource.
246 const short kComputerNameID
= -16413;
248 short oldResFile
= CurResFile() ;
250 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
251 UseResFile(oldResFile
);
253 if (chooserName
&& *chooserName
)
255 HLock( (Handle
) chooserName
) ;
256 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
257 HUnlock( (Handle
) chooserName
) ;
258 ReleaseResource( (Handle
) chooserName
) ;
259 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
267 // Get user ID e.g. jacs
268 bool wxGetUserId(wxChar
*buf
, int maxSize
)
270 return wxGetUserName( buf
, maxSize
) ;
273 const wxChar
* wxGetHomeDir(wxString
*pstr
)
275 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
276 return pstr
->c_str() ;
279 // Get user name e.g. Stefan Csomor
280 bool wxGetUserName(wxChar
*buf
, int maxSize
)
282 // Gets Chooser name of user by examining a System resource.
284 const short kChooserNameID
= -16096;
286 short oldResFile
= CurResFile() ;
288 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
289 UseResFile(oldResFile
);
291 if (chooserName
&& *chooserName
)
293 HLock( (Handle
) chooserName
) ;
294 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
295 HUnlock( (Handle
) chooserName
) ;
296 ReleaseResource( (Handle
) chooserName
) ;
297 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
305 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
, int flags
)
311 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
313 // TODO : under classic there is no environement support, under X yes
317 // set the env var name to the given value, return true on success
318 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
320 // TODO : under classic there is no environement support, under X yes
325 // Execute a program in an Interactive Shell
327 bool wxShell(const wxString
& command
)
333 // Shutdown or reboot the PC
334 bool wxShutdown(wxShutdownFlags wFlags
)
340 wxPowerType
wxGetPowerType()
343 return wxPOWER_UNKNOWN
;
346 wxBatteryState
wxGetBatteryState()
349 return wxBATTERY_UNKNOWN_STATE
;
352 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
353 wxMemorySize
wxGetFreeMemory()
355 return (wxMemorySize
)FreeMem() ;
360 void wxMicroSleep(unsigned long microseconds
)
362 AbsoluteTime wakeup
= AddDurationToAbsolute( microseconds
* durationMicrosecond
, UpTime());
363 MPDelayUntil( & wakeup
);
366 void wxMilliSleep(unsigned long milliseconds
)
368 AbsoluteTime wakeup
= AddDurationToAbsolute( milliseconds
, UpTime());
369 MPDelayUntil( & wakeup
);
372 void wxSleep(int nSecs
)
374 wxMilliSleep(1000*nSecs
);
379 // Consume all events until no more left
384 #endif // !__DARWIN__
392 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
394 static wxToolkitInfo info
;
395 info
.os
= DoGetOSVersion(&info
.versionMajor
, &info
.versionMinor
);
396 info
.name
= _T("wxBase");
404 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
406 static wxToolkitInfo info
;
407 info
.os
= DoGetOSVersion(&info
.versionMajor
, &info
.versionMinor
);
408 info
.shortName
= _T("mac");
409 info
.name
= _T("wxMac");
410 #ifdef __WXUNIVERSAL__
411 info
.shortName
<< _T("univ");
412 info
.name
<< _T("/wxUniversal");
417 // Reading and writing resources (eg WIN.INI, .Xdefaults)
419 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
425 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
428 buf
.Printf(wxT("%.4f"), value
);
430 return wxWriteResource(section
, entry
, buf
, file
);
433 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
436 buf
.Printf(wxT("%ld"), value
);
438 return wxWriteResource(section
, entry
, buf
, file
);
441 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
444 buf
.Printf(wxT("%d"), value
);
446 return wxWriteResource(section
, entry
, buf
, file
);
449 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
455 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
458 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
461 *value
= (float)strtod(s
, NULL
);
468 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
471 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
474 *value
= strtol(s
, NULL
, 10);
481 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
484 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
487 *value
= (int)strtol(s
, NULL
, 10);
493 #endif // wxUSE_RESOURCES
495 int gs_wxBusyCursorCount
= 0;
496 extern wxCursor gMacCurrentCursor
;
497 wxCursor gMacStoredActiveCursor
;
499 // Set the cursor to the busy cursor for all windows
500 void wxBeginBusyCursor(wxCursor
*cursor
)
502 if (gs_wxBusyCursorCount
++ == 0)
504 gMacStoredActiveCursor
= gMacCurrentCursor
;
505 cursor
->MacInstall() ;
507 //else: nothing to do, already set
510 // Restore cursor to normal
511 void wxEndBusyCursor()
513 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
514 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
516 if (--gs_wxBusyCursorCount
== 0)
518 gMacStoredActiveCursor
.MacInstall() ;
519 gMacStoredActiveCursor
= wxNullCursor
;
523 // true if we're between the above two calls
526 return (gs_wxBusyCursorCount
> 0);
533 wxString
wxMacFindFolder( short vol
,
535 Boolean createFolder
)
540 if ( FSFindFolder( vol
, folderType
, createFolder
, &fsRef
) == noErr
)
541 strDir
= wxMacFSRefToPath( &fsRef
) + wxFILE_SEP_PATH
;
550 // Check whether this window wants to process messages, e.g. Stop button
551 // in long calculations.
552 bool wxCheckForInterrupt(wxWindow
*wnd
)
558 void wxGetMousePosition( int* x
, int* y
)
563 LocalToGlobal( &pt
) ;
568 // Return true if we have a colour display
569 bool wxColourDisplay()
574 // Returns depth of screen
578 SetRect(&globRect
, -32760, -32760, 32760, 32760);
579 GDHandle theMaxDevice
;
582 theMaxDevice
= GetMaxDevice(&globRect
);
583 if (theMaxDevice
!= nil
)
584 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
589 // Get size of display
590 void wxDisplaySize(int *width
, int *height
)
593 GetQDGlobalsScreenBits( &screenBits
);
596 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
598 if (height
!= NULL
) {
599 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
603 void wxDisplaySizeMM(int *width
, int *height
)
605 wxDisplaySize(width
, height
);
606 // on mac 72 is fixed (at least now ;-)
607 float cvPt2Mm
= 25.4 / 72;
610 *width
= int( *width
* cvPt2Mm
);
612 if (height
!= NULL
) {
613 *height
= int( *height
* cvPt2Mm
);
617 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
620 GetAvailableWindowPositioningBounds( GetMainDevice() , &r
) ;
626 *width
= r
.right
- r
.left
;
628 *height
= r
.bottom
- r
.top
;
631 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
633 return wxGenericFindWindowAtPoint(pt
);
640 wxString
wxGetOsDescription()
642 #ifdef WXWIN_OS_DESCRIPTION
643 // use configure generated description if available
644 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION
) + wxString(wxT(")"));
646 return wxT("MacOS") ; //TODO:define further
651 wxChar
*wxGetUserHome (const wxString
& user
)
657 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
667 int pos
= p
.Find(':') ;
668 if ( pos
!= wxNOT_FOUND
) {
677 err
= wxMacPathToFSRef( p
, &fsRef
) ;
680 FSVolumeRefNum vRefNum
;
681 err
= FSGetVRefNum( &fsRef
, &vRefNum
) ;
684 UInt64 freeBytes
, totalBytes
;
685 err
= FSGetVInfo( vRefNum
, NULL
, &freeBytes
, &totalBytes
) ;
689 *pTotal
= wxLongLong( totalBytes
) ;
691 *pFree
= wxLongLong( freeBytes
) ;
696 return err
== noErr
;
698 #endif // !__DARWIN__
700 //---------------------------------------------------------------------------
701 // wxMac Specific utility functions
702 //---------------------------------------------------------------------------
704 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
706 wxCharBuffer buf
= from
.mb_str( wxConvLocal
) ;
707 int len
= strlen(buf
) ;
712 memcpy( (char*) &to
[1] , buf
, len
) ;
715 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
717 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] ) ;
721 // ----------------------------------------------------------------------------
722 // Common Event Support
723 // ----------------------------------------------------------------------------
726 extern ProcessSerialNumber gAppProcess
;
730 ProcessSerialNumber psn
;
732 psn
.highLongOfPSN
= 0 ;
733 psn
.lowLongOfPSN
= kCurrentProcess
;
734 SameProcess( &gAppProcess
, &psn
, &isSame
) ;
738 OSStatus err
= noErr
;
740 // lead sometimes to race conditions, although all calls used should be thread safe ...
741 static wxMacCarbonEvent s_wakeupEvent
;
742 if ( !s_wakeupEvent
.IsValid() )
744 err
= s_wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
745 kEventAttributeNone
) ;
750 if ( IsEventInQueue( GetMainEventQueue() , s_wakeupEvent
) )
752 s_wakeupEvent
.SetCurrentTime() ;
753 err
= PostEventToQueue(GetMainEventQueue(), s_wakeupEvent
,
754 kEventPriorityHigh
);
757 wxMacCarbonEvent wakeupEvent
;
758 wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
759 kEventAttributeNone
) ;
760 err
= PostEventToQueue(GetMainEventQueue(), wakeupEvent
,
761 kEventPriorityHigh
);
764 PostEvent( nullEvent
, 0 ) ;
769 WakeUpProcess( &gAppProcess
) ;
777 // ----------------------------------------------------------------------------
778 // Native Struct Conversions
779 // ----------------------------------------------------------------------------
782 void wxMacRectToNative( const wxRect
*wx
, Rect
*n
)
786 n
->right
= wx
->x
+ wx
->width
;
787 n
->bottom
= wx
->y
+ wx
->height
;
790 void wxMacNativeToRect( const Rect
*n
, wxRect
* wx
)
794 wx
->width
= n
->right
- n
->left
;
795 wx
->height
= n
->bottom
- n
->top
;
798 void wxMacPointToNative( const wxPoint
* wx
, Point
*n
)
804 void wxMacNativeToPoint( const Point
*n
, wxPoint
* wx
)
810 // ----------------------------------------------------------------------------
811 // Carbon Event Support
812 // ----------------------------------------------------------------------------
815 OSStatus
wxMacCarbonEvent::GetParameter(EventParamName inName
, EventParamType inDesiredType
, UInt32 inBufferSize
, void * outData
)
817 return ::GetEventParameter( m_eventRef
, inName
, inDesiredType
, NULL
, inBufferSize
, NULL
, outData
) ;
820 OSStatus
wxMacCarbonEvent::SetParameter(EventParamName inName
, EventParamType inType
, UInt32 inBufferSize
, const void * inData
)
822 return ::SetEventParameter( m_eventRef
, inName
, inType
, inBufferSize
, inData
) ;
825 // ----------------------------------------------------------------------------
826 // Control Access Support
827 // ----------------------------------------------------------------------------
829 wxMacControl::wxMacControl(wxWindow
* peer
, bool isRootControl
)
833 m_isRootControl
= isRootControl
;
834 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
837 wxMacControl::wxMacControl( wxWindow
* peer
, ControlRef control
)
841 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
842 m_controlRef
= control
;
845 wxMacControl::wxMacControl( wxWindow
* peer
, WXWidget control
)
849 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
850 m_controlRef
= (ControlRef
) control
;
853 wxMacControl::~wxMacControl()
857 void wxMacControl::Init()
860 m_controlRef
= NULL
;
861 m_needsFocusRect
= false ;
862 m_isCompositing
= false ;
863 m_isRootControl
= false ;
866 void wxMacControl::Dispose()
868 ::DisposeControl( m_controlRef
) ;
869 m_controlRef
= NULL
;
872 void wxMacControl::SetReference( SInt32 data
)
874 SetControlReference( m_controlRef
, data
) ;
877 OSStatus
wxMacControl::GetData(ControlPartCode inPartCode
, ResType inTag
, Size inBufferSize
, void * inOutBuffer
, Size
* outActualSize
) const
879 return ::GetControlData( m_controlRef
, inPartCode
, inTag
, inBufferSize
, inOutBuffer
, outActualSize
) ;
882 OSStatus
wxMacControl::GetDataSize(ControlPartCode inPartCode
, ResType inTag
, Size
* outActualSize
) const
884 return ::GetControlDataSize( m_controlRef
, inPartCode
, inTag
, outActualSize
) ;
887 OSStatus
wxMacControl::SetData(ControlPartCode inPartCode
, ResType inTag
, Size inSize
, const void * inData
)
889 return ::SetControlData( m_controlRef
, inPartCode
, inTag
, inSize
, inData
) ;
892 OSStatus
wxMacControl::SendEvent( EventRef event
, OptionBits inOptions
)
894 #if TARGET_API_MAC_OSX
895 return SendEventToEventTargetWithOptions( event
,
896 HIObjectGetEventTarget( (HIObjectRef
) m_controlRef
), inOptions
);
898 #pragma unused(inOptions)
899 return SendEventToEventTarget(event
,GetControlEventTarget( m_controlRef
) ) ;
903 OSStatus
wxMacControl::SendHICommand( HICommand
&command
, OptionBits inOptions
)
905 wxMacCarbonEvent
event( kEventClassCommand
, kEventCommandProcess
) ;
906 event
.SetParameter
<HICommand
>(kEventParamDirectObject
,command
) ;
907 return SendEvent( event
, inOptions
) ;
910 OSStatus
wxMacControl::SendHICommand( UInt32 commandID
, OptionBits inOptions
)
913 memset( &command
, 0 , sizeof(command
) ) ;
914 command
.commandID
= commandID
;
915 return SendHICommand( command
, inOptions
) ;
918 void wxMacControl::Flash( ControlPartCode part
, UInt32 ticks
)
920 HiliteControl( m_controlRef
, part
) ;
921 unsigned long finalTicks
;
922 Delay( ticks
, &finalTicks
) ;
923 HiliteControl( m_controlRef
, kControlNoPart
) ;
926 SInt32
wxMacControl::GetValue() const
928 return ::GetControl32BitValue( m_controlRef
) ;
931 SInt32
wxMacControl::GetMaximum() const
933 return ::GetControl32BitMaximum( m_controlRef
) ;
936 SInt32
wxMacControl::GetMinimum() const
938 return ::GetControl32BitMinimum( m_controlRef
) ;
941 void wxMacControl::SetValue( SInt32 v
)
943 ::SetControl32BitValue( m_controlRef
, v
) ;
946 void wxMacControl::SetMinimum( SInt32 v
)
948 ::SetControl32BitMinimum( m_controlRef
, v
) ;
951 void wxMacControl::SetMaximum( SInt32 v
)
953 ::SetControl32BitMaximum( m_controlRef
, v
) ;
956 void wxMacControl::SetValueAndRange( SInt32 value
, SInt32 minimum
, SInt32 maximum
)
958 ::SetControl32BitMinimum( m_controlRef
, minimum
) ;
959 ::SetControl32BitMaximum( m_controlRef
, maximum
) ;
960 ::SetControl32BitValue( m_controlRef
, value
) ;
963 OSStatus
wxMacControl::SetFocus( ControlFocusPart focusPart
)
965 return SetKeyboardFocus( GetControlOwner( m_controlRef
) ,
966 m_controlRef
, focusPart
) ;
969 bool wxMacControl::HasFocus() const
972 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
973 return control
== m_controlRef
;
976 void wxMacControl::SetNeedsFocusRect( bool needs
)
978 m_needsFocusRect
= needs
;
981 bool wxMacControl::NeedsFocusRect() const
983 return m_needsFocusRect
;
986 void wxMacControl::VisibilityChanged(bool shown
)
990 void wxMacControl::SuperChangedPosition()
994 void wxMacControl::SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
)
997 ControlFontStyleRec fontStyle
;
998 if ( font
.MacGetThemeFontID() != kThemeCurrentPortFont
)
1000 switch( font
.MacGetThemeFontID() )
1002 case kThemeSmallSystemFont
: fontStyle
.font
= kControlFontSmallSystemFont
; break ;
1003 case 109 /*mini font */ : fontStyle
.font
= -5 ; break ;
1004 case kThemeSystemFont
: fontStyle
.font
= kControlFontBigSystemFont
; break ;
1005 default : fontStyle
.font
= kControlFontBigSystemFont
; break ;
1007 fontStyle
.flags
= kControlUseFontMask
;
1011 fontStyle
.font
= font
.MacGetFontNum() ;
1012 fontStyle
.style
= font
.MacGetFontStyle() ;
1013 fontStyle
.size
= font
.MacGetFontSize() ;
1014 fontStyle
.flags
= kControlUseFontMask
| kControlUseFaceMask
| kControlUseSizeMask
;
1017 fontStyle
.just
= teJustLeft
;
1018 fontStyle
.flags
|= kControlUseJustMask
;
1019 if ( ( windowStyle
& wxALIGN_MASK
) & wxALIGN_CENTER_HORIZONTAL
)
1020 fontStyle
.just
= teJustCenter
;
1021 else if ( ( windowStyle
& wxALIGN_MASK
) & wxALIGN_RIGHT
)
1022 fontStyle
.just
= teJustRight
;
1025 // we only should do this in case of a non-standard color, as otherwise 'disabled' controls
1026 // won't get grayed out by the system anymore
1028 if ( foreground
!= *wxBLACK
)
1030 fontStyle
.foreColor
= MAC_WXCOLORREF(foreground
.GetPixel() ) ;
1031 fontStyle
.flags
|= kControlUseForeColorMask
;
1034 ::SetControlFontStyle( m_controlRef
, &fontStyle
);
1037 void wxMacControl::SetBackground( const wxBrush
&WXUNUSED(brush
) )
1040 // setting up a color proc is not recommended anymore
1043 void wxMacControl::SetRange( SInt32 minimum
, SInt32 maximum
)
1045 ::SetControl32BitMinimum( m_controlRef
, minimum
) ;
1046 ::SetControl32BitMaximum( m_controlRef
, maximum
) ;
1049 short wxMacControl::HandleKey( SInt16 keyCode
, SInt16 charCode
, EventModifiers modifiers
)
1051 return HandleControlKey( m_controlRef
, keyCode
, charCode
, modifiers
) ;
1054 void wxMacControl::SetActionProc( ControlActionUPP actionProc
)
1056 SetControlAction( m_controlRef
, actionProc
) ;
1059 void wxMacControl::SetViewSize( SInt32 viewSize
)
1061 SetControlViewSize(m_controlRef
, viewSize
) ;
1064 SInt32
wxMacControl::GetViewSize() const
1066 return GetControlViewSize( m_controlRef
) ;
1069 bool wxMacControl::IsVisible() const
1071 return IsControlVisible( m_controlRef
) ;
1074 void wxMacControl::SetVisibility( bool visible
, bool redraw
)
1076 SetControlVisibility( m_controlRef
, visible
, redraw
) ;
1079 bool wxMacControl::IsEnabled() const
1081 #if TARGET_API_MAC_OSX
1082 return IsControlEnabled( m_controlRef
) ;
1084 return IsControlActive( m_controlRef
) ;
1088 bool wxMacControl::IsActive() const
1090 return IsControlActive( m_controlRef
) ;
1093 void wxMacControl::Enable( bool enable
)
1095 #if TARGET_API_MAC_OSX
1097 EnableControl( m_controlRef
) ;
1099 DisableControl( m_controlRef
) ;
1102 ActivateControl( m_controlRef
) ;
1104 DeactivateControl( m_controlRef
) ;
1108 void wxMacControl::SetDrawingEnabled( bool enable
)
1110 #if TARGET_API_MAC_OSX
1111 HIViewSetDrawingEnabled( m_controlRef
, enable
) ;
1115 #if TARGET_API_MAC_OSX
1116 bool wxMacControl::GetNeedsDisplay() const
1118 #if TARGET_API_MAC_OSX
1119 if ( m_isCompositing
)
1121 return HIViewGetNeedsDisplay( m_controlRef
) ;
1129 Rect controlBounds
;
1130 GetControlBounds( m_controlRef
, &controlBounds
) ;
1131 RgnHandle rgn
= NewRgn() ;
1132 GetWindowRegion ( GetControlOwner( m_controlRef
) , kWindowUpdateRgn
, rgn
) ;
1133 Boolean intersect
= RectInRgn ( &controlBounds
, rgn
) ;
1141 void wxMacControl::SetNeedsDisplay( RgnHandle where
)
1146 #if TARGET_API_MAC_OSX
1147 if ( m_isCompositing
)
1149 HIViewSetNeedsDisplayInRegion( m_controlRef
, where
, true ) ;
1154 Rect controlBounds
;
1155 GetControlBounds( m_controlRef
, &controlBounds
) ;
1156 RgnHandle update
= NewRgn() ;
1157 CopyRgn( where
, update
) ;
1158 OffsetRgn( update
, controlBounds
.left
, controlBounds
.top
) ;
1159 InvalWindowRgn( GetControlOwner( m_controlRef
) , update
) ;
1163 void wxMacControl::SetNeedsDisplay( Rect
* where
)
1168 #if TARGET_API_MAC_OSX
1169 if ( m_isCompositing
)
1171 if ( where
!= NULL
)
1173 RgnHandle update
= NewRgn() ;
1174 RectRgn( update
, where
) ;
1175 HIViewSetNeedsDisplayInRegion( m_controlRef
, update
, true ) ;
1176 DisposeRgn( update
) ;
1179 HIViewSetNeedsDisplay( m_controlRef
, true ) ;
1184 Rect controlBounds
;
1185 GetControlBounds( m_controlRef
, &controlBounds
) ;
1188 Rect whereLocal
= *where
;
1189 OffsetRect( &whereLocal
, controlBounds
.left
, controlBounds
.top
) ;
1190 SectRect( &controlBounds
, &whereLocal
, &controlBounds
) ;
1192 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1196 void wxMacControl::Convert( wxPoint
*pt
, wxMacControl
*from
, wxMacControl
*to
)
1198 #if TARGET_API_MAC_OSX
1199 if ( from
->m_peer
->MacGetTopLevelWindow()->MacUsesCompositing() )
1204 HIViewConvertPoint( &hiPoint
, from
->m_controlRef
, to
->m_controlRef
) ;
1205 pt
->x
= (int)hiPoint
.x
;
1206 pt
->y
= (int)hiPoint
.y
;
1213 GetControlBounds( from
->m_controlRef
, &fromRect
) ;
1214 GetControlBounds( to
->m_controlRef
, &toRect
) ;
1215 if ( from
->m_isRootControl
)
1216 fromRect
.left
= fromRect
.top
= 0 ;
1217 if ( to
->m_isRootControl
)
1218 toRect
.left
= toRect
.top
= 0 ;
1220 pt
->x
= pt
->x
+ fromRect
.left
- toRect
.left
;
1221 pt
->y
= pt
->y
+ fromRect
.top
- toRect
.top
;
1225 void wxMacControl::SetRect( Rect
*r
)
1227 #if TARGET_API_MAC_OSX
1228 if ( m_isCompositing
)
1230 //A HIRect is actually a CGRect on OSX - which consists of two structures -
1231 //CGPoint and CGSize, which have two floats each
1232 HIRect hir
= { { r
->left
, r
->top
}, { r
->right
- r
->left
, r
->bottom
- r
->top
} } ;
1233 HIViewSetFrame ( m_controlRef
, &hir
) ;
1234 // eventuall we might have to do a SetVisibility( false , true ) ;
1235 // before and a SetVisibility( true , true ) ; after
1240 bool vis
= IsVisible() ;
1244 GetControlBounds( m_controlRef
, &former
) ;
1245 InvalWindowRect( GetControlOwner( m_controlRef
) , &former
) ;
1248 Rect controlBounds
= *r
;
1250 // since the rect passed in is always (even in non-compositing) relative
1251 // to the (native) parent, we have to adjust to window relative here
1252 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1253 if( parent
->m_isRootControl
== false )
1256 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1257 OffsetRect( &controlBounds
, superRect
.left
, superRect
.top
) ;
1260 SetControlBounds( m_controlRef
, &controlBounds
) ;
1263 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1268 void wxMacControl::GetRect( Rect
*r
)
1270 GetControlBounds( m_controlRef
, r
) ;
1271 if ( m_isCompositing
== false )
1273 // correct the case of the root control
1274 if ( m_isRootControl
)
1276 WindowRef wr
= GetControlOwner( m_controlRef
) ;
1277 GetWindowBounds( wr
, kWindowContentRgn
, r
) ;
1278 r
->right
-= r
->left
;
1279 r
->bottom
-= r
->top
;
1285 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1286 if( parent
->m_isRootControl
== false )
1289 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1290 OffsetRect( r
, -superRect
.left
, -superRect
.top
) ;
1296 void wxMacControl::GetRectInWindowCoords( Rect
*r
)
1298 UMAGetControlBoundsInWindowCoords( m_controlRef
, r
) ;
1301 void wxMacControl::GetBestRect( Rect
*r
)
1303 short baselineoffset
;
1304 GetBestControlRect( m_controlRef
, r
, &baselineoffset
) ;
1307 void wxMacControl::SetLabel( const wxString
&title
)
1309 wxFontEncoding encoding
;
1312 encoding
= m_font
.GetEncoding();
1314 encoding
= wxFont::GetDefaultEncoding();
1316 UMASetControlTitle( m_controlRef
, title
, encoding
) ;
1319 void wxMacControl::GetFeatures( UInt32
* features
)
1321 GetControlFeatures( m_controlRef
, features
) ;
1324 OSStatus
wxMacControl::GetRegion( ControlPartCode partCode
, RgnHandle region
)
1326 OSStatus err
= GetControlRegion( m_controlRef
, partCode
, region
) ;
1327 if ( m_isCompositing
== false )
1329 if ( !m_isRootControl
)
1332 GetControlBounds(m_controlRef
, &r
) ;
1333 if ( !EmptyRgn( region
) )
1334 OffsetRgn( region
, -r
.left
, -r
.top
) ;
1340 OSStatus
wxMacControl::SetZOrder( bool above
, wxMacControl
* other
)
1342 #if TARGET_API_MAC_OSX
1343 return HIViewSetZOrder( m_controlRef
,above
? kHIViewZOrderAbove
: kHIViewZOrderBelow
,
1344 (other
!= NULL
) ? other
->m_controlRef
: NULL
) ;
1351 #if TARGET_API_MAC_OSX
1352 // SetNeedsDisplay would not invalidate the children
1353 static void InvalidateControlAndChildren( HIViewRef control
)
1355 HIViewSetNeedsDisplay( control
, true ) ;
1356 UInt16 childrenCount
= 0 ;
1357 OSStatus err
= CountSubControls( control
, &childrenCount
) ;
1358 if ( err
== errControlIsNotEmbedder
)
1360 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
1362 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
1365 err
= GetIndexedSubControl( control
, i
, & child
) ;
1366 if ( err
== errControlIsNotEmbedder
)
1368 InvalidateControlAndChildren( child
) ;
1373 void wxMacControl::InvalidateWithChildren()
1375 #if TARGET_API_MAC_OSX
1376 InvalidateControlAndChildren( m_controlRef
) ;
1380 void wxMacControl::ScrollRect( wxRect
*r
, int dx
, int dy
)
1382 wxASSERT( r
!= NULL
) ;
1383 #if TARGET_API_MAC_OSX
1384 if ( m_isCompositing
)
1386 HIRect scrollarea
= CGRectMake( r
->x
, r
->y
, r
->width
, r
->height
) ;
1387 HIViewScrollRect ( m_controlRef
, &scrollarea
, dx
,dy
) ;
1393 GetControlBounds( m_controlRef
, &bounds
) ;
1394 bounds
.left
+= r
->x
;
1395 bounds
.top
+= r
->y
;
1396 bounds
.bottom
= bounds
.top
+ r
->height
;
1397 bounds
.right
= bounds
.left
+ r
->width
;
1398 wxMacWindowClipper
clip( m_peer
) ;
1399 RgnHandle updateRgn
= NewRgn() ;
1400 ::ScrollRect( &bounds
, dx
, dy
, updateRgn
) ;
1401 InvalWindowRgn( GetControlOwner( m_controlRef
) , updateRgn
) ;
1406 // SetNeedsDisplay would not invalidate the children
1412 OSStatus
wxMacControl::SetSelectionFlags( DataBrowserSelectionFlags options
)
1414 return SetDataBrowserSelectionFlags( m_controlRef
, options
) ;
1417 OSStatus
wxMacControl::AddListViewColumn( DataBrowserListViewColumnDesc
*columnDesc
,
1418 DataBrowserTableViewColumnIndex position
)
1420 return AddDataBrowserListViewColumn( m_controlRef
, columnDesc
, position
) ;
1423 OSStatus
wxMacControl::AutoSizeListViewColumns()
1425 return AutoSizeDataBrowserListViewColumns(m_controlRef
) ;
1428 OSStatus
wxMacControl::SetHasScrollBars( bool horiz
, bool vert
)
1430 return SetDataBrowserHasScrollBars( m_controlRef
, horiz
, vert
) ;
1433 OSStatus
wxMacControl::SetTableViewHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle
)
1435 return SetDataBrowserTableViewHiliteStyle( m_controlRef
, hiliteStyle
) ;
1438 OSStatus
wxMacControl::SetListViewHeaderBtnHeight(UInt16 height
)
1440 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef
,height
) ;
1443 OSStatus
wxMacControl::SetCallbacks(const DataBrowserCallbacks
* callbacks
)
1445 return SetDataBrowserCallbacks( m_controlRef
, callbacks
) ;
1448 OSStatus
wxMacControl::UpdateItems( DataBrowserItemID container
, UInt32 numItems
,
1449 const DataBrowserItemID
* items
,
1450 DataBrowserPropertyID preSortProperty
,
1451 DataBrowserPropertyID propertyID
)
1453 return UpdateDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
, propertyID
) ;
1456 bool wxMacControl::IsItemSelected( DataBrowserItemID item
)
1458 return IsDataBrowserItemSelected( m_controlRef
, item
) ;
1461 OSStatus
wxMacControl::AddItems( DataBrowserItemID container
, UInt32 numItems
,
1462 const DataBrowserItemID
* items
,
1463 DataBrowserPropertyID preSortProperty
)
1465 return AddDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1468 OSStatus
wxMacControl::RemoveItems( DataBrowserItemID container
, UInt32 numItems
,
1469 const DataBrowserItemID
* items
,
1470 DataBrowserPropertyID preSortProperty
)
1472 return RemoveDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1475 OSStatus
wxMacControl::RevealItem( DataBrowserItemID item
,
1476 DataBrowserPropertyID propertyID
,
1477 DataBrowserRevealOptions options
)
1479 return RevealDataBrowserItem( m_controlRef
, item
, propertyID
, options
) ;
1482 OSStatus
wxMacControl::SetSelectedItems(UInt32 numItems
,
1483 const DataBrowserItemID
* items
,
1484 DataBrowserSetOption operation
)
1486 return SetDataBrowserSelectedItems( m_controlRef
, numItems
, items
, operation
) ;
1489 OSStatus
wxMacControl::GetSelectionAnchor( DataBrowserItemID
* first
, DataBrowserItemID
* last
)
1491 return GetDataBrowserSelectionAnchor( m_controlRef
, first
, last
) ;
1498 OSStatus
wxMacControl::SetTabEnabled( SInt16 tabNo
, bool enable
)
1500 return ::SetTabEnabled( m_controlRef
, tabNo
, enable
) ;
1507 #ifdef __WXMAC_OSX__
1508 // snippets from Sketch Sample from Apple :
1510 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
1512 This function locates, opens, and returns the profile reference for the calibrated
1513 Generic RGB color space. It is up to the caller to call CMCloseProfile when done
1514 with the profile reference this function returns.
1516 CMProfileRef
wxMacOpenGenericProfile(void)
1518 static CMProfileRef cachedRGBProfileRef
= NULL
;
1520 // we only create the profile reference once
1521 if (cachedRGBProfileRef
== NULL
)
1523 CMProfileLocation loc
;
1525 loc
.locType
= cmPathBasedProfile
;
1526 strcpy(loc
.u
.pathLoc
.path
, kGenericRGBProfilePathStr
);
1528 verify_noerr( CMOpenProfile(&cachedRGBProfileRef
, &loc
) );
1531 if (cachedRGBProfileRef
)
1533 // clone the profile reference so that the caller has their own reference, not our cached one
1534 CMCloneProfileRef(cachedRGBProfileRef
);
1537 return cachedRGBProfileRef
;
1541 Return the generic RGB color space. This is a 'get' function and the caller should
1542 not release the returned value unless the caller retains it first. Usually callers
1543 of this routine will immediately use the returned colorspace with CoreGraphics
1544 so they typically do not need to retain it themselves.
1546 This function creates the generic RGB color space once and hangs onto it so it can
1547 return it whenever this function is called.
1550 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
1551 #define kCGColorSpaceGenericRGB CFSTR("kCGColorSpaceGenericRGB")
1554 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
1556 static wxMacCFRefHolder
<CGColorSpaceRef
> genericRGBColorSpace
;
1558 if (genericRGBColorSpace
== NULL
)
1560 if ( UMAGetSystemVersion() >= 0x1040 )
1562 genericRGBColorSpace
.Set( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) ) ;
1566 CMProfileRef genericRGBProfile
= wxMacOpenGenericProfile();
1568 if (genericRGBProfile
)
1570 genericRGBColorSpace
.Set( CGColorSpaceCreateWithPlatformColorSpace(genericRGBProfile
) ) ;
1571 wxASSERT_MSG( genericRGBColorSpace
!= NULL
, wxT("couldn't create the generic RGB color space") ) ;
1573 // we opened the profile so it is up to us to close it
1574 CMCloseProfile(genericRGBProfile
);
1578 return genericRGBColorSpace
;