1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 // Note: this is done in utilscmn.cpp now.
14 // #pragma implementation "utils.h"
17 #include "wx/wxprec.h"
21 #include "wx/apptrait.h"
24 #include "wx/mac/uma.h"
26 #include "wx/toplevel.h"
38 #include "MoreFilesX.h"
46 #if TARGET_API_MAC_OSX
47 #include <CoreServices/CoreServices.h>
49 #include <DriverServices.h>
50 #include <Multiprocessing.h>
53 #include <ATSUnicode.h>
54 #include <TextCommon.h>
55 #include <TextEncodingConverter.h>
58 #include "wx/mac/private.h" // includes mac headers
60 #if defined(__MWERKS__) && wxUSE_UNICODE
64 // ---------------------------------------------------------------------------
65 // code used in both base and GUI compilation
66 // ---------------------------------------------------------------------------
68 // our OS version is the same in non GUI and GUI cases
69 static int DoGetOSVersion(int *majorVsn
, int *minorVsn
)
73 // are there x-platform conventions ?
75 Gestalt(gestaltSystemVersion
, &theSystem
) ;
76 if (minorVsn
!= NULL
) {
77 *minorVsn
= (theSystem
& 0xFF ) ;
79 if (majorVsn
!= NULL
) {
80 *majorVsn
= (theSystem
>> 8 ) ;
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 #if defined(__WXDEBUG__) && defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
98 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
100 #ifndef __MetroNubUtils__
101 #include "MetroNubUtils.h"
108 #if TARGET_API_MAC_CARBON
110 #include <CodeFragments.h>
112 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr
, ProcInfoType procInfo
, ...);
114 ProcPtr gCallUniversalProc_Proc
= NULL
;
118 static MetroNubUserEntryBlock
* gMetroNubEntry
= NULL
;
120 static long fRunOnce
= false;
122 /* ---------------------------------------------------------------------------
124 --------------------------------------------------------------------------- */
126 Boolean
IsMetroNubInstalled()
133 gMetroNubEntry
= NULL
;
135 if (Gestalt(gestaltSystemVersion
, &value
) == noErr
&& value
< 0x1000)
137 /* look for MetroNub's Gestalt selector */
138 if (Gestalt(kMetroNubUserSignature
, &result
) == noErr
)
141 #if TARGET_API_MAC_CARBON
142 if (gCallUniversalProc_Proc
== NULL
)
144 CFragConnectionID connectionID
;
147 ProcPtr symbolAddress
;
149 CFragSymbolClass symbolClass
;
151 symbolAddress
= NULL
;
152 err
= GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch
, kFindCFrag
,
153 &connectionID
, &mainAddress
, errorString
);
157 gCallUniversalProc_Proc
= NULL
;
161 err
= FindSymbol(connectionID
, "\pCallUniversalProc",
162 (Ptr
*) &gCallUniversalProc_Proc
, &symbolClass
);
166 gCallUniversalProc_Proc
= NULL
;
173 MetroNubUserEntryBlock
* block
= (MetroNubUserEntryBlock
*)result
;
175 /* make sure the version of the API is compatible */
176 if (block
->apiLowVersion
<= kMetroNubUserAPIVersion
&&
177 kMetroNubUserAPIVersion
<= block
->apiHiVersion
)
178 gMetroNubEntry
= block
; /* success! */
187 #if TARGET_API_MAC_CARBON
188 return (gMetroNubEntry
!= NULL
&& gCallUniversalProc_Proc
!= NULL
);
190 return (gMetroNubEntry
!= NULL
);
194 /* ---------------------------------------------------------------------------
195 IsMWDebuggerRunning [v1 API]
196 --------------------------------------------------------------------------- */
198 Boolean
IsMWDebuggerRunning()
200 if (IsMetroNubInstalled())
201 return CallIsDebuggerRunningProc(gMetroNubEntry
->isDebuggerRunning
);
206 /* ---------------------------------------------------------------------------
207 AmIBeingMWDebugged [v1 API]
208 --------------------------------------------------------------------------- */
210 Boolean
AmIBeingMWDebugged()
212 if (IsMetroNubInstalled())
213 return CallAmIBeingDebuggedProc(gMetroNubEntry
->amIBeingDebugged
);
218 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
220 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
225 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
230 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
234 // defined in unix/utilsunx.cpp for Mac OS X
236 // get full hostname (with domain name if possible)
237 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
239 return wxGetHostName(buf
, maxSize
);
242 // Get hostname only (without domain name)
243 bool wxGetHostName(wxChar
*buf
, int maxSize
)
245 // Gets Chooser name of user by examining a System resource.
247 const short kComputerNameID
= -16413;
249 short oldResFile
= CurResFile() ;
251 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
252 UseResFile(oldResFile
);
254 if (chooserName
&& *chooserName
)
256 HLock( (Handle
) chooserName
) ;
257 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
258 HUnlock( (Handle
) chooserName
) ;
259 ReleaseResource( (Handle
) chooserName
) ;
260 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
268 // Get user ID e.g. jacs
269 bool wxGetUserId(wxChar
*buf
, int maxSize
)
271 return wxGetUserName( buf
, maxSize
) ;
274 const wxChar
* wxGetHomeDir(wxString
*pstr
)
276 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
277 return pstr
->c_str() ;
280 // Get user name e.g. Stefan Csomor
281 bool wxGetUserName(wxChar
*buf
, int maxSize
)
283 // Gets Chooser name of user by examining a System resource.
285 const short kChooserNameID
= -16096;
287 short oldResFile
= CurResFile() ;
289 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
290 UseResFile(oldResFile
);
292 if (chooserName
&& *chooserName
)
294 HLock( (Handle
) chooserName
) ;
295 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
296 HUnlock( (Handle
) chooserName
) ;
297 ReleaseResource( (Handle
) chooserName
) ;
298 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
306 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
, int flags
)
312 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
314 // TODO : under classic there is no environement support, under X yes
318 // set the env var name to the given value, return true on success
319 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
321 // TODO : under classic there is no environement support, under X yes
326 // Execute a program in an Interactive Shell
328 bool wxShell(const wxString
& command
)
334 // Shutdown or reboot the PC
335 bool wxShutdown(wxShutdownFlags wFlags
)
341 wxPowerType
wxGetPowerType()
344 return wxPOWER_UNKNOWN
;
347 wxBatteryState
wxGetBatteryState()
350 return wxBATTERY_UNKNOWN_STATE
;
353 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
354 wxMemorySize
wxGetFreeMemory()
356 return (wxMemorySize
)FreeMem() ;
361 void wxMicroSleep(unsigned long microseconds
)
363 AbsoluteTime wakeup
= AddDurationToAbsolute( microseconds
* durationMicrosecond
, UpTime());
364 MPDelayUntil( & wakeup
);
367 void wxMilliSleep(unsigned long milliseconds
)
369 AbsoluteTime wakeup
= AddDurationToAbsolute( milliseconds
, UpTime());
370 MPDelayUntil( & wakeup
);
373 void wxSleep(int nSecs
)
375 wxMilliSleep(1000*nSecs
);
380 // Consume all events until no more left
385 #endif // !__DARWIN__
393 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
395 static wxToolkitInfo info
;
396 info
.os
= DoGetOSVersion(&info
.versionMajor
, &info
.versionMinor
);
397 info
.name
= _T("wxBase");
405 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
407 static wxToolkitInfo info
;
408 info
.os
= DoGetOSVersion(&info
.versionMajor
, &info
.versionMinor
);
409 info
.shortName
= _T("mac");
410 info
.name
= _T("wxMac");
411 #ifdef __WXUNIVERSAL__
412 info
.shortName
<< _T("univ");
413 info
.name
<< _T("/wxUniversal");
418 // Reading and writing resources (eg WIN.INI, .Xdefaults)
420 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
426 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
429 buf
.Printf(wxT("%.4f"), value
);
431 return wxWriteResource(section
, entry
, buf
, file
);
434 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
437 buf
.Printf(wxT("%ld"), value
);
439 return wxWriteResource(section
, entry
, buf
, file
);
442 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
445 buf
.Printf(wxT("%d"), value
);
447 return wxWriteResource(section
, entry
, buf
, file
);
450 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
456 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
459 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
462 *value
= (float)strtod(s
, NULL
);
469 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
472 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
475 *value
= strtol(s
, NULL
, 10);
482 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
485 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
488 *value
= (int)strtol(s
, NULL
, 10);
494 #endif // wxUSE_RESOURCES
496 int gs_wxBusyCursorCount
= 0;
497 extern wxCursor gMacCurrentCursor
;
498 wxCursor gMacStoredActiveCursor
;
500 // Set the cursor to the busy cursor for all windows
501 void wxBeginBusyCursor(wxCursor
*cursor
)
503 if (gs_wxBusyCursorCount
++ == 0)
505 gMacStoredActiveCursor
= gMacCurrentCursor
;
506 cursor
->MacInstall() ;
508 //else: nothing to do, already set
511 // Restore cursor to normal
512 void wxEndBusyCursor()
514 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
515 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
517 if (--gs_wxBusyCursorCount
== 0)
519 gMacStoredActiveCursor
.MacInstall() ;
520 gMacStoredActiveCursor
= wxNullCursor
;
524 // true if we're between the above two calls
527 return (gs_wxBusyCursorCount
> 0);
534 wxString
wxMacFindFolder( short vol
,
536 Boolean createFolder
)
541 if ( FSFindFolder( vol
, folderType
, createFolder
, &fsRef
) == noErr
)
542 strDir
= wxMacFSRefToPath( &fsRef
) + wxFILE_SEP_PATH
;
551 // Check whether this window wants to process messages, e.g. Stop button
552 // in long calculations.
553 bool wxCheckForInterrupt(wxWindow
*wnd
)
559 void wxGetMousePosition( int* x
, int* y
)
564 LocalToGlobal( &pt
) ;
569 // Return true if we have a colour display
570 bool wxColourDisplay()
575 // Returns depth of screen
579 SetRect(&globRect
, -32760, -32760, 32760, 32760);
580 GDHandle theMaxDevice
;
583 theMaxDevice
= GetMaxDevice(&globRect
);
584 if (theMaxDevice
!= nil
)
585 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
590 // Get size of display
591 void wxDisplaySize(int *width
, int *height
)
594 GetQDGlobalsScreenBits( &screenBits
);
597 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
599 if (height
!= NULL
) {
600 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
604 void wxDisplaySizeMM(int *width
, int *height
)
606 wxDisplaySize(width
, height
);
607 // on mac 72 is fixed (at least now ;-)
608 float cvPt2Mm
= 25.4 / 72;
611 *width
= int( *width
* cvPt2Mm
);
613 if (height
!= NULL
) {
614 *height
= int( *height
* cvPt2Mm
);
618 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
621 GetAvailableWindowPositioningBounds( GetMainDevice() , &r
) ;
627 *width
= r
.right
- r
.left
;
629 *height
= r
.bottom
- r
.top
;
632 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
634 return wxGenericFindWindowAtPoint(pt
);
641 wxString
wxGetOsDescription()
643 #ifdef WXWIN_OS_DESCRIPTION
644 // use configure generated description if available
645 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION
) + wxString(wxT(")"));
647 return wxT("MacOS") ; //TODO:define further
652 wxChar
*wxGetUserHome (const wxString
& user
)
658 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
668 int pos
= p
.Find(':') ;
669 if ( pos
!= wxNOT_FOUND
) {
678 err
= wxMacPathToFSRef( p
, &fsRef
) ;
681 FSVolumeRefNum vRefNum
;
682 err
= FSGetVRefNum( &fsRef
, &vRefNum
) ;
685 UInt64 freeBytes
, totalBytes
;
686 err
= FSGetVInfo( vRefNum
, NULL
, &freeBytes
, &totalBytes
) ;
690 *pTotal
= wxLongLong( totalBytes
) ;
692 *pFree
= wxLongLong( freeBytes
) ;
697 return err
== noErr
;
699 #endif // !__DARWIN__
701 //---------------------------------------------------------------------------
702 // wxMac Specific utility functions
703 //---------------------------------------------------------------------------
705 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
707 wxCharBuffer buf
= from
.mb_str( wxConvLocal
) ;
708 int len
= strlen(buf
) ;
713 memcpy( (char*) &to
[1] , buf
, len
) ;
716 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
718 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] ) ;
722 // ----------------------------------------------------------------------------
723 // Common Event Support
724 // ----------------------------------------------------------------------------
727 extern ProcessSerialNumber gAppProcess
;
731 ProcessSerialNumber psn
;
733 psn
.highLongOfPSN
= 0 ;
734 psn
.lowLongOfPSN
= kCurrentProcess
;
735 SameProcess( &gAppProcess
, &psn
, &isSame
) ;
739 static wxMacCarbonEvent s_wakeupEvent
;
740 OSStatus err
= noErr
;
741 if ( !s_wakeupEvent
.IsValid() )
743 err
= s_wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
744 kEventAttributeNone
) ;
748 if ( IsEventInQueue( GetMainEventQueue() , s_wakeupEvent
) )
750 s_wakeupEvent
.SetTime(0) ;
751 err
= PostEventToQueue(GetMainEventQueue(), s_wakeupEvent
,
755 PostEvent( nullEvent
, 0 ) ;
760 WakeUpProcess( &gAppProcess
) ;
768 // ----------------------------------------------------------------------------
769 // Native Struct Conversions
770 // ----------------------------------------------------------------------------
773 void wxMacRectToNative( const wxRect
*wx
, Rect
*n
)
777 n
->right
= wx
->x
+ wx
->width
;
778 n
->bottom
= wx
->y
+ wx
->height
;
781 void wxMacNativeToRect( const Rect
*n
, wxRect
* wx
)
785 wx
->width
= n
->right
- n
->left
;
786 wx
->height
= n
->bottom
- n
->top
;
789 void wxMacPointToNative( const wxPoint
* wx
, Point
*n
)
795 void wxMacNativeToPoint( const Point
*n
, wxPoint
* wx
)
801 // ----------------------------------------------------------------------------
802 // Carbon Event Support
803 // ----------------------------------------------------------------------------
806 OSStatus
wxMacCarbonEvent::GetParameter(EventParamName inName
, EventParamType inDesiredType
, UInt32 inBufferSize
, void * outData
)
808 return ::GetEventParameter( m_eventRef
, inName
, inDesiredType
, NULL
, inBufferSize
, NULL
, outData
) ;
811 OSStatus
wxMacCarbonEvent::SetParameter(EventParamName inName
, EventParamType inType
, UInt32 inBufferSize
, const void * inData
)
813 return ::SetEventParameter( m_eventRef
, inName
, inType
, inBufferSize
, inData
) ;
816 // ----------------------------------------------------------------------------
817 // Control Access Support
818 // ----------------------------------------------------------------------------
820 wxMacControl::wxMacControl(wxWindow
* peer
, bool isRootControl
)
824 m_isRootControl
= isRootControl
;
825 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
828 wxMacControl::wxMacControl( wxWindow
* peer
, ControlRef control
)
832 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
833 m_controlRef
= control
;
836 wxMacControl::wxMacControl( wxWindow
* peer
, WXWidget control
)
840 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
841 m_controlRef
= (ControlRef
) control
;
844 wxMacControl::~wxMacControl()
848 void wxMacControl::Init()
851 m_controlRef
= NULL
;
852 m_needsFocusRect
= false ;
853 m_isCompositing
= false ;
854 m_isRootControl
= false ;
857 void wxMacControl::Dispose()
859 ::DisposeControl( m_controlRef
) ;
860 m_controlRef
= NULL
;
863 void wxMacControl::SetReference( SInt32 data
)
865 SetControlReference( m_controlRef
, data
) ;
868 OSStatus
wxMacControl::GetData(ControlPartCode inPartCode
, ResType inTag
, Size inBufferSize
, void * inOutBuffer
, Size
* outActualSize
) const
870 return ::GetControlData( m_controlRef
, inPartCode
, inTag
, inBufferSize
, inOutBuffer
, outActualSize
) ;
873 OSStatus
wxMacControl::GetDataSize(ControlPartCode inPartCode
, ResType inTag
, Size
* outActualSize
) const
875 return ::GetControlDataSize( m_controlRef
, inPartCode
, inTag
, outActualSize
) ;
878 OSStatus
wxMacControl::SetData(ControlPartCode inPartCode
, ResType inTag
, Size inSize
, const void * inData
)
880 return ::SetControlData( m_controlRef
, inPartCode
, inTag
, inSize
, inData
) ;
883 OSStatus
wxMacControl::SendEvent( EventRef event
, OptionBits inOptions
)
885 #if TARGET_API_MAC_OSX
886 return SendEventToEventTargetWithOptions( event
,
887 HIObjectGetEventTarget( (HIObjectRef
) m_controlRef
), inOptions
);
889 #pragma unused(inOptions)
890 return SendEventToEventTarget(event
,GetControlEventTarget( m_controlRef
) ) ;
894 OSStatus
wxMacControl::SendHICommand( HICommand
&command
, OptionBits inOptions
)
896 wxMacCarbonEvent
event( kEventClassCommand
, kEventCommandProcess
) ;
897 event
.SetParameter
<HICommand
>(kEventParamDirectObject
,command
) ;
898 return SendEvent( event
, inOptions
) ;
901 OSStatus
wxMacControl::SendHICommand( UInt32 commandID
, OptionBits inOptions
)
904 memset( &command
, 0 , sizeof(command
) ) ;
905 command
.commandID
= commandID
;
906 return SendHICommand( command
, inOptions
) ;
909 void wxMacControl::Flash( ControlPartCode part
, UInt32 ticks
)
911 HiliteControl( m_controlRef
, part
) ;
912 unsigned long finalTicks
;
913 Delay( ticks
, &finalTicks
) ;
914 HiliteControl( m_controlRef
, kControlNoPart
) ;
917 SInt32
wxMacControl::GetValue() const
919 return ::GetControl32BitValue( m_controlRef
) ;
922 SInt32
wxMacControl::GetMaximum() const
924 return ::GetControl32BitMaximum( m_controlRef
) ;
927 SInt32
wxMacControl::GetMinimum() const
929 return ::GetControl32BitMinimum( m_controlRef
) ;
932 void wxMacControl::SetValue( SInt32 v
)
934 ::SetControl32BitValue( m_controlRef
, v
) ;
937 void wxMacControl::SetMinimum( SInt32 v
)
939 ::SetControl32BitMinimum( m_controlRef
, v
) ;
942 void wxMacControl::SetMaximum( SInt32 v
)
944 ::SetControl32BitMaximum( m_controlRef
, v
) ;
947 void wxMacControl::SetValueAndRange( SInt32 value
, SInt32 minimum
, SInt32 maximum
)
949 ::SetControl32BitMinimum( m_controlRef
, minimum
) ;
950 ::SetControl32BitMaximum( m_controlRef
, maximum
) ;
951 ::SetControl32BitValue( m_controlRef
, value
) ;
954 OSStatus
wxMacControl::SetFocus( ControlFocusPart focusPart
)
956 return SetKeyboardFocus( GetControlOwner( m_controlRef
) ,
957 m_controlRef
, focusPart
) ;
960 bool wxMacControl::HasFocus() const
963 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
964 return control
== m_controlRef
;
967 void wxMacControl::SetNeedsFocusRect( bool needs
)
969 m_needsFocusRect
= needs
;
972 bool wxMacControl::NeedsFocusRect() const
974 return m_needsFocusRect
;
977 void wxMacControl::VisibilityChanged(bool shown
)
981 void wxMacControl::SuperChangedPosition()
985 void wxMacControl::SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
)
988 ControlFontStyleRec fontStyle
;
989 if ( font
.MacGetThemeFontID() != kThemeCurrentPortFont
)
991 switch( font
.MacGetThemeFontID() )
993 case kThemeSmallSystemFont
: fontStyle
.font
= kControlFontSmallSystemFont
; break ;
994 case 109 /*mini font */ : fontStyle
.font
= -5 ; break ;
995 case kThemeSystemFont
: fontStyle
.font
= kControlFontBigSystemFont
; break ;
996 default : fontStyle
.font
= kControlFontBigSystemFont
; break ;
998 fontStyle
.flags
= kControlUseFontMask
;
1002 fontStyle
.font
= font
.MacGetFontNum() ;
1003 fontStyle
.style
= font
.MacGetFontStyle() ;
1004 fontStyle
.size
= font
.MacGetFontSize() ;
1005 fontStyle
.flags
= kControlUseFontMask
| kControlUseFaceMask
| kControlUseSizeMask
;
1008 fontStyle
.just
= teJustLeft
;
1009 fontStyle
.flags
|= kControlUseJustMask
;
1010 if ( ( windowStyle
& wxALIGN_MASK
) & wxALIGN_CENTER_HORIZONTAL
)
1011 fontStyle
.just
= teJustCenter
;
1012 else if ( ( windowStyle
& wxALIGN_MASK
) & wxALIGN_RIGHT
)
1013 fontStyle
.just
= teJustRight
;
1016 // we only should do this in case of a non-standard color, as otherwise 'disabled' controls
1017 // won't get grayed out by the system anymore
1019 if ( foreground
!= *wxBLACK
)
1021 fontStyle
.foreColor
= MAC_WXCOLORREF(foreground
.GetPixel() ) ;
1022 fontStyle
.flags
|= kControlUseForeColorMask
;
1025 ::SetControlFontStyle( m_controlRef
, &fontStyle
);
1028 void wxMacControl::SetBackground( const wxBrush
&WXUNUSED(brush
) )
1031 // setting up a color proc is not recommended anymore
1034 void wxMacControl::SetRange( SInt32 minimum
, SInt32 maximum
)
1036 ::SetControl32BitMinimum( m_controlRef
, minimum
) ;
1037 ::SetControl32BitMaximum( m_controlRef
, maximum
) ;
1040 short wxMacControl::HandleKey( SInt16 keyCode
, SInt16 charCode
, EventModifiers modifiers
)
1042 return HandleControlKey( m_controlRef
, keyCode
, charCode
, modifiers
) ;
1045 void wxMacControl::SetActionProc( ControlActionUPP actionProc
)
1047 SetControlAction( m_controlRef
, actionProc
) ;
1050 void wxMacControl::SetViewSize( SInt32 viewSize
)
1052 SetControlViewSize(m_controlRef
, viewSize
) ;
1055 SInt32
wxMacControl::GetViewSize() const
1057 return GetControlViewSize( m_controlRef
) ;
1060 bool wxMacControl::IsVisible() const
1062 return IsControlVisible( m_controlRef
) ;
1065 void wxMacControl::SetVisibility( bool visible
, bool redraw
)
1067 SetControlVisibility( m_controlRef
, visible
, redraw
) ;
1070 bool wxMacControl::IsEnabled() const
1072 #if TARGET_API_MAC_OSX
1073 return IsControlEnabled( m_controlRef
) ;
1075 return IsControlActive( m_controlRef
) ;
1079 bool wxMacControl::IsActive() const
1081 return IsControlActive( m_controlRef
) ;
1084 void wxMacControl::Enable( bool enable
)
1086 #if TARGET_API_MAC_OSX
1088 EnableControl( m_controlRef
) ;
1090 DisableControl( m_controlRef
) ;
1093 ActivateControl( m_controlRef
) ;
1095 DeactivateControl( m_controlRef
) ;
1099 void wxMacControl::SetDrawingEnabled( bool enable
)
1101 #if TARGET_API_MAC_OSX
1102 HIViewSetDrawingEnabled( m_controlRef
, enable
) ;
1106 #if TARGET_API_MAC_OSX
1107 bool wxMacControl::GetNeedsDisplay() const
1109 #if TARGET_API_MAC_OSX
1110 if ( m_isCompositing
)
1112 return HIViewGetNeedsDisplay( m_controlRef
) ;
1120 Rect controlBounds
;
1121 GetControlBounds( m_controlRef
, &controlBounds
) ;
1122 RgnHandle rgn
= NewRgn() ;
1123 GetWindowRegion ( GetControlOwner( m_controlRef
) , kWindowUpdateRgn
, rgn
) ;
1124 Boolean intersect
= RectInRgn ( &controlBounds
, rgn
) ;
1132 void wxMacControl::SetNeedsDisplay( RgnHandle where
)
1137 #if TARGET_API_MAC_OSX
1138 if ( m_isCompositing
)
1140 HIViewSetNeedsDisplayInRegion( m_controlRef
, where
, true ) ;
1145 Rect controlBounds
;
1146 GetControlBounds( m_controlRef
, &controlBounds
) ;
1147 RgnHandle update
= NewRgn() ;
1148 CopyRgn( where
, update
) ;
1149 OffsetRgn( update
, controlBounds
.left
, controlBounds
.top
) ;
1150 InvalWindowRgn( GetControlOwner( m_controlRef
) , update
) ;
1154 void wxMacControl::SetNeedsDisplay( Rect
* where
)
1159 #if TARGET_API_MAC_OSX
1160 if ( m_isCompositing
)
1162 if ( where
!= NULL
)
1164 RgnHandle update
= NewRgn() ;
1165 RectRgn( update
, where
) ;
1166 HIViewSetNeedsDisplayInRegion( m_controlRef
, update
, true ) ;
1167 DisposeRgn( update
) ;
1170 HIViewSetNeedsDisplay( m_controlRef
, true ) ;
1175 Rect controlBounds
;
1176 GetControlBounds( m_controlRef
, &controlBounds
) ;
1179 Rect whereLocal
= *where
;
1180 OffsetRect( &whereLocal
, controlBounds
.left
, controlBounds
.top
) ;
1181 SectRect( &controlBounds
, &whereLocal
, &controlBounds
) ;
1183 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1187 void wxMacControl::Convert( wxPoint
*pt
, wxMacControl
*from
, wxMacControl
*to
)
1189 #if TARGET_API_MAC_OSX
1190 if ( from
->m_peer
->MacGetTopLevelWindow()->MacUsesCompositing() )
1195 HIViewConvertPoint( &hiPoint
, from
->m_controlRef
, to
->m_controlRef
) ;
1196 pt
->x
= (int)hiPoint
.x
;
1197 pt
->y
= (int)hiPoint
.y
;
1204 GetControlBounds( from
->m_controlRef
, &fromRect
) ;
1205 GetControlBounds( to
->m_controlRef
, &toRect
) ;
1206 if ( from
->m_isRootControl
)
1207 fromRect
.left
= fromRect
.top
= 0 ;
1208 if ( to
->m_isRootControl
)
1209 toRect
.left
= toRect
.top
= 0 ;
1211 pt
->x
= pt
->x
+ fromRect
.left
- toRect
.left
;
1212 pt
->y
= pt
->y
+ fromRect
.top
- toRect
.top
;
1216 void wxMacControl::SetRect( Rect
*r
)
1218 #if TARGET_API_MAC_OSX
1219 if ( m_isCompositing
)
1221 //A HIRect is actually a CGRect on OSX - which consists of two structures -
1222 //CGPoint and CGSize, which have two floats each
1223 HIRect hir
= { { r
->left
, r
->top
}, { r
->right
- r
->left
, r
->bottom
- r
->top
} } ;
1224 HIViewSetFrame ( m_controlRef
, &hir
) ;
1225 // eventuall we might have to do a SetVisibility( false , true ) ;
1226 // before and a SetVisibility( true , true ) ; after
1231 bool vis
= IsVisible() ;
1235 GetControlBounds( m_controlRef
, &former
) ;
1236 InvalWindowRect( GetControlOwner( m_controlRef
) , &former
) ;
1239 Rect controlBounds
= *r
;
1241 // since the rect passed in is always (even in non-compositing) relative
1242 // to the (native) parent, we have to adjust to window relative here
1243 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1244 if( parent
->m_isRootControl
== false )
1247 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1248 OffsetRect( &controlBounds
, superRect
.left
, superRect
.top
) ;
1251 SetControlBounds( m_controlRef
, &controlBounds
) ;
1254 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1259 void wxMacControl::GetRect( Rect
*r
)
1261 GetControlBounds( m_controlRef
, r
) ;
1262 if ( m_isCompositing
== false )
1264 // correct the case of the root control
1265 if ( m_isRootControl
)
1267 WindowRef wr
= GetControlOwner( m_controlRef
) ;
1268 GetWindowBounds( wr
, kWindowContentRgn
, r
) ;
1269 r
->right
-= r
->left
;
1270 r
->bottom
-= r
->top
;
1276 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1277 if( parent
->m_isRootControl
== false )
1280 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1281 OffsetRect( r
, -superRect
.left
, -superRect
.top
) ;
1287 void wxMacControl::GetRectInWindowCoords( Rect
*r
)
1289 UMAGetControlBoundsInWindowCoords( m_controlRef
, r
) ;
1292 void wxMacControl::GetBestRect( Rect
*r
)
1294 short baselineoffset
;
1295 GetBestControlRect( m_controlRef
, r
, &baselineoffset
) ;
1298 void wxMacControl::SetTitle( const wxString
&title
)
1300 wxFontEncoding encoding
;
1303 encoding
= m_font
.GetEncoding();
1305 encoding
= wxFont::GetDefaultEncoding();
1307 UMASetControlTitle( m_controlRef
, title
, encoding
) ;
1310 void wxMacControl::GetFeatures( UInt32
* features
)
1312 GetControlFeatures( m_controlRef
, features
) ;
1315 OSStatus
wxMacControl::GetRegion( ControlPartCode partCode
, RgnHandle region
)
1317 OSStatus err
= GetControlRegion( m_controlRef
, partCode
, region
) ;
1318 if ( m_isCompositing
== false )
1320 if ( !m_isRootControl
)
1323 GetControlBounds(m_controlRef
, &r
) ;
1324 if ( !EmptyRgn( region
) )
1325 OffsetRgn( region
, -r
.left
, -r
.top
) ;
1331 OSStatus
wxMacControl::SetZOrder( bool above
, wxMacControl
* other
)
1333 #if TARGET_API_MAC_OSX
1334 return HIViewSetZOrder( m_controlRef
,above
? kHIViewZOrderAbove
: kHIViewZOrderBelow
,
1335 (other
!= NULL
) ? other
->m_controlRef
: NULL
) ;
1342 #if TARGET_API_MAC_OSX
1343 // SetNeedsDisplay would not invalidate the children
1344 static void InvalidateControlAndChildren( HIViewRef control
)
1346 HIViewSetNeedsDisplay( control
, true ) ;
1347 UInt16 childrenCount
= 0 ;
1348 OSStatus err
= CountSubControls( control
, &childrenCount
) ;
1349 if ( err
== errControlIsNotEmbedder
)
1351 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
1353 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
1356 err
= GetIndexedSubControl( control
, i
, & child
) ;
1357 if ( err
== errControlIsNotEmbedder
)
1359 InvalidateControlAndChildren( child
) ;
1364 void wxMacControl::InvalidateWithChildren()
1366 #if TARGET_API_MAC_OSX
1367 InvalidateControlAndChildren( m_controlRef
) ;
1371 void wxMacControl::ScrollRect( wxRect
*r
, int dx
, int dy
)
1373 wxASSERT( r
!= NULL
) ;
1374 #if TARGET_API_MAC_OSX
1375 if ( m_isCompositing
)
1377 HIRect scrollarea
= CGRectMake( r
->x
, r
->y
, r
->width
, r
->height
) ;
1378 HIViewScrollRect ( m_controlRef
, &scrollarea
, dx
,dy
) ;
1384 GetControlBounds( m_controlRef
, &bounds
) ;
1385 bounds
.left
+= r
->x
;
1386 bounds
.top
+= r
->y
;
1387 bounds
.bottom
= bounds
.top
+ r
->height
;
1388 bounds
.right
= bounds
.left
+ r
->width
;
1389 wxMacWindowClipper
clip( m_peer
) ;
1390 RgnHandle updateRgn
= NewRgn() ;
1391 ::ScrollRect( &bounds
, dx
, dy
, updateRgn
) ;
1392 InvalWindowRgn( GetControlOwner( m_controlRef
) , updateRgn
) ;
1397 // SetNeedsDisplay would not invalidate the children
1403 OSStatus
wxMacControl::SetSelectionFlags( DataBrowserSelectionFlags options
)
1405 return SetDataBrowserSelectionFlags( m_controlRef
, options
) ;
1408 OSStatus
wxMacControl::AddListViewColumn( DataBrowserListViewColumnDesc
*columnDesc
,
1409 DataBrowserTableViewColumnIndex position
)
1411 return AddDataBrowserListViewColumn( m_controlRef
, columnDesc
, position
) ;
1414 OSStatus
wxMacControl::AutoSizeListViewColumns()
1416 return AutoSizeDataBrowserListViewColumns(m_controlRef
) ;
1419 OSStatus
wxMacControl::SetHasScrollBars( bool horiz
, bool vert
)
1421 return SetDataBrowserHasScrollBars( m_controlRef
, horiz
, vert
) ;
1424 OSStatus
wxMacControl::SetTableViewHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle
)
1426 return SetDataBrowserTableViewHiliteStyle( m_controlRef
, hiliteStyle
) ;
1429 OSStatus
wxMacControl::SetListViewHeaderBtnHeight(UInt16 height
)
1431 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef
,height
) ;
1434 OSStatus
wxMacControl::SetCallbacks(const DataBrowserCallbacks
* callbacks
)
1436 return SetDataBrowserCallbacks( m_controlRef
, callbacks
) ;
1439 OSStatus
wxMacControl::UpdateItems( DataBrowserItemID container
, UInt32 numItems
,
1440 const DataBrowserItemID
* items
,
1441 DataBrowserPropertyID preSortProperty
,
1442 DataBrowserPropertyID propertyID
)
1444 return UpdateDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
, propertyID
) ;
1447 bool wxMacControl::IsItemSelected( DataBrowserItemID item
)
1449 return IsDataBrowserItemSelected( m_controlRef
, item
) ;
1452 OSStatus
wxMacControl::AddItems( DataBrowserItemID container
, UInt32 numItems
,
1453 const DataBrowserItemID
* items
,
1454 DataBrowserPropertyID preSortProperty
)
1456 return AddDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1459 OSStatus
wxMacControl::RemoveItems( DataBrowserItemID container
, UInt32 numItems
,
1460 const DataBrowserItemID
* items
,
1461 DataBrowserPropertyID preSortProperty
)
1463 return RemoveDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1466 OSStatus
wxMacControl::RevealItem( DataBrowserItemID item
,
1467 DataBrowserPropertyID propertyID
,
1468 DataBrowserRevealOptions options
)
1470 return RevealDataBrowserItem( m_controlRef
, item
, propertyID
, options
) ;
1473 OSStatus
wxMacControl::SetSelectedItems(UInt32 numItems
,
1474 const DataBrowserItemID
* items
,
1475 DataBrowserSetOption operation
)
1477 return SetDataBrowserSelectedItems( m_controlRef
, numItems
, items
, operation
) ;
1480 OSStatus
wxMacControl::GetSelectionAnchor( DataBrowserItemID
* first
, DataBrowserItemID
* last
)
1482 return GetDataBrowserSelectionAnchor( m_controlRef
, first
, last
) ;
1489 OSStatus
wxMacControl::SetTabEnabled( SInt16 tabNo
, bool enable
)
1491 return ::SetTabEnabled( m_controlRef
, tabNo
, enable
) ;
1498 #ifdef __WXMAC_OSX__
1499 // snippets from Sketch Sample from Apple :
1501 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
1503 This function locates, opens, and returns the profile reference for the calibrated
1504 Generic RGB color space. It is up to the caller to call CMCloseProfile when done
1505 with the profile reference this function returns.
1507 CMProfileRef
wxMacOpenGenericProfile(void)
1509 static CMProfileRef cachedRGBProfileRef
= NULL
;
1511 // we only create the profile reference once
1512 if (cachedRGBProfileRef
== NULL
)
1514 CMProfileLocation loc
;
1516 loc
.locType
= cmPathBasedProfile
;
1517 strcpy(loc
.u
.pathLoc
.path
, kGenericRGBProfilePathStr
);
1519 verify_noerr( CMOpenProfile(&cachedRGBProfileRef
, &loc
) );
1522 if (cachedRGBProfileRef
)
1524 // clone the profile reference so that the caller has their own reference, not our cached one
1525 CMCloneProfileRef(cachedRGBProfileRef
);
1528 return cachedRGBProfileRef
;
1532 Return the generic RGB color space. This is a 'get' function and the caller should
1533 not release the returned value unless the caller retains it first. Usually callers
1534 of this routine will immediately use the returned colorspace with CoreGraphics
1535 so they typically do not need to retain it themselves.
1537 This function creates the generic RGB color space once and hangs onto it so it can
1538 return it whenever this function is called.
1541 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
1543 static CGColorSpaceRef genericRGBColorSpace
= NULL
;
1545 if (genericRGBColorSpace
== NULL
)
1547 CMProfileRef genericRGBProfile
= wxMacOpenGenericProfile();
1549 if (genericRGBProfile
)
1551 genericRGBColorSpace
= CGColorSpaceCreateWithPlatformColorSpace(genericRGBProfile
);
1552 wxASSERT_MSG( genericRGBColorSpace
!= NULL
, wxT("couldn't create the generic RGB color space") ) ;
1554 // we opened the profile so it is up to us to close it
1555 CMCloseProfile(genericRGBProfile
);
1558 return genericRGBColorSpace
;