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(__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 return HIViewGetNeedsDisplay( m_controlRef
) ;
1117 void wxMacControl::SetNeedsDisplay( RgnHandle where
)
1122 #if TARGET_API_MAC_OSX
1123 if ( m_isCompositing
)
1125 HIViewSetNeedsDisplayInRegion( m_controlRef
, where
, true ) ;
1130 Rect controlBounds
;
1131 GetControlBounds( m_controlRef
, &controlBounds
) ;
1132 RgnHandle update
= NewRgn() ;
1133 CopyRgn( where
, update
) ;
1134 OffsetRgn( update
, controlBounds
.left
, controlBounds
.top
) ;
1135 InvalWindowRgn( GetControlOwner( m_controlRef
) , update
) ;
1139 void wxMacControl::SetNeedsDisplay( Rect
* where
)
1144 #if TARGET_API_MAC_OSX
1145 if ( m_isCompositing
)
1147 if ( where
!= NULL
)
1149 RgnHandle update
= NewRgn() ;
1150 RectRgn( update
, where
) ;
1151 HIViewSetNeedsDisplayInRegion( m_controlRef
, update
, true ) ;
1152 DisposeRgn( update
) ;
1155 HIViewSetNeedsDisplay( m_controlRef
, true ) ;
1160 Rect controlBounds
;
1161 GetControlBounds( m_controlRef
, &controlBounds
) ;
1164 Rect whereLocal
= *where
;
1165 OffsetRect( &whereLocal
, controlBounds
.left
, controlBounds
.top
) ;
1166 SectRect( &controlBounds
, &whereLocal
, &controlBounds
) ;
1168 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1172 void wxMacControl::Convert( wxPoint
*pt
, wxMacControl
*from
, wxMacControl
*to
)
1174 #if TARGET_API_MAC_OSX
1175 if ( from
->m_peer
->MacGetTopLevelWindow()->MacUsesCompositing() )
1180 HIViewConvertPoint( &hiPoint
, from
->m_controlRef
, to
->m_controlRef
) ;
1181 pt
->x
= (int)hiPoint
.x
;
1182 pt
->y
= (int)hiPoint
.y
;
1189 GetControlBounds( from
->m_controlRef
, &fromRect
) ;
1190 GetControlBounds( to
->m_controlRef
, &toRect
) ;
1191 if ( from
->m_isRootControl
)
1192 fromRect
.left
= fromRect
.top
= 0 ;
1193 if ( to
->m_isRootControl
)
1194 toRect
.left
= toRect
.top
= 0 ;
1196 pt
->x
= pt
->x
+ fromRect
.left
- toRect
.left
;
1197 pt
->y
= pt
->y
+ fromRect
.top
- toRect
.top
;
1201 void wxMacControl::SetRect( Rect
*r
)
1203 #if TARGET_API_MAC_OSX
1204 if ( m_isCompositing
)
1206 //A HIRect is actually a CGRect on OSX - which consists of two structures -
1207 //CGPoint and CGSize, which have two floats each
1208 HIRect hir
= { { r
->left
, r
->top
}, { r
->right
- r
->left
, r
->bottom
- r
->top
} } ;
1209 HIViewSetFrame ( m_controlRef
, &hir
) ;
1210 // eventuall we might have to do a SetVisibility( false , true ) ;
1211 // before and a SetVisibility( true , true ) ; after
1216 bool vis
= IsVisible() ;
1220 GetControlBounds( m_controlRef
, &former
) ;
1221 InvalWindowRect( GetControlOwner( m_controlRef
) , &former
) ;
1224 Rect controlBounds
= *r
;
1226 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1227 if( parent
->m_isRootControl
== false )
1230 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1231 OffsetRect( &controlBounds
, superRect
.left
, superRect
.top
) ;
1234 SetControlBounds( m_controlRef
, &controlBounds
) ;
1237 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1242 void wxMacControl::GetRect( Rect
*r
)
1244 GetControlBounds( m_controlRef
, r
) ;
1245 if ( m_isCompositing
== false )
1247 // correct the case of the root control
1248 if ( m_isRootControl
)
1250 WindowRef wr
= GetControlOwner( m_controlRef
) ;
1251 GetWindowBounds( wr
, kWindowContentRgn
, r
) ;
1252 r
->right
-= r
->left
;
1253 r
->bottom
-= r
->top
;
1259 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1260 if( parent
->m_isRootControl
== false )
1263 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1264 OffsetRect( r
, -superRect
.left
, -superRect
.top
) ;
1270 void wxMacControl::GetRectInWindowCoords( Rect
*r
)
1272 UMAGetControlBoundsInWindowCoords( m_controlRef
, r
) ;
1275 void wxMacControl::GetBestRect( Rect
*r
)
1277 short baselineoffset
;
1278 GetBestControlRect( m_controlRef
, r
, &baselineoffset
) ;
1281 void wxMacControl::SetTitle( const wxString
&title
)
1283 wxFontEncoding encoding
;
1286 encoding
= m_font
.GetEncoding();
1288 encoding
= wxFont::GetDefaultEncoding();
1290 UMASetControlTitle( m_controlRef
, title
, encoding
) ;
1293 void wxMacControl::GetFeatures( UInt32
* features
)
1295 GetControlFeatures( m_controlRef
, features
) ;
1298 OSStatus
wxMacControl::GetRegion( ControlPartCode partCode
, RgnHandle region
)
1300 OSStatus err
= GetControlRegion( m_controlRef
, partCode
, region
) ;
1301 if ( m_isCompositing
== false )
1303 if ( !m_isRootControl
)
1306 GetControlBounds(m_controlRef
, &r
) ;
1307 if ( !EmptyRgn( region
) )
1308 OffsetRgn( region
, -r
.left
, -r
.top
) ;
1314 OSStatus
wxMacControl::SetZOrder( bool above
, wxMacControl
* other
)
1316 #if TARGET_API_MAC_OSX
1317 return HIViewSetZOrder( m_controlRef
,above
? kHIViewZOrderAbove
: kHIViewZOrderBelow
,
1318 (other
!= NULL
) ? other
->m_controlRef
: NULL
) ;
1325 #if TARGET_API_MAC_OSX
1326 // SetNeedsDisplay would not invalidate the children
1327 static void InvalidateControlAndChildren( HIViewRef control
)
1329 HIViewSetNeedsDisplay( control
, true ) ;
1330 UInt16 childrenCount
= 0 ;
1331 OSStatus err
= CountSubControls( control
, &childrenCount
) ;
1332 if ( err
== errControlIsNotEmbedder
)
1334 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
1336 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
1339 err
= GetIndexedSubControl( control
, i
, & child
) ;
1340 if ( err
== errControlIsNotEmbedder
)
1342 InvalidateControlAndChildren( child
) ;
1347 void wxMacControl::InvalidateWithChildren()
1349 #if TARGET_API_MAC_OSX
1350 InvalidateControlAndChildren( m_controlRef
) ;
1354 void wxMacControl::ScrollRect( const wxRect
&r
, int dx
, int dy
)
1356 #if TARGET_API_MAC_OSX
1357 HIRect scrollarea
= CGRectMake( r
.x
, r
.y
, r
.width
, r
.height
) ;
1358 HIViewScrollRect ( m_controlRef
, &scrollarea
, dx
,dy
) ;
1363 // SetNeedsDisplay would not invalidate the children
1369 OSStatus
wxMacControl::SetSelectionFlags( DataBrowserSelectionFlags options
)
1371 return SetDataBrowserSelectionFlags( m_controlRef
, options
) ;
1374 OSStatus
wxMacControl::AddListViewColumn( DataBrowserListViewColumnDesc
*columnDesc
,
1375 DataBrowserTableViewColumnIndex position
)
1377 return AddDataBrowserListViewColumn( m_controlRef
, columnDesc
, position
) ;
1380 OSStatus
wxMacControl::AutoSizeListViewColumns()
1382 return AutoSizeDataBrowserListViewColumns(m_controlRef
) ;
1385 OSStatus
wxMacControl::SetHasScrollBars( bool horiz
, bool vert
)
1387 return SetDataBrowserHasScrollBars( m_controlRef
, horiz
, vert
) ;
1390 OSStatus
wxMacControl::SetTableViewHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle
)
1392 return SetDataBrowserTableViewHiliteStyle( m_controlRef
, hiliteStyle
) ;
1395 OSStatus
wxMacControl::SetListViewHeaderBtnHeight(UInt16 height
)
1397 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef
,height
) ;
1400 OSStatus
wxMacControl::SetCallbacks(const DataBrowserCallbacks
* callbacks
)
1402 return SetDataBrowserCallbacks( m_controlRef
, callbacks
) ;
1405 OSStatus
wxMacControl::UpdateItems( DataBrowserItemID container
, UInt32 numItems
,
1406 const DataBrowserItemID
* items
,
1407 DataBrowserPropertyID preSortProperty
,
1408 DataBrowserPropertyID propertyID
)
1410 return UpdateDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
, propertyID
) ;
1413 bool wxMacControl::IsItemSelected( DataBrowserItemID item
)
1415 return IsDataBrowserItemSelected( m_controlRef
, item
) ;
1418 OSStatus
wxMacControl::AddItems( DataBrowserItemID container
, UInt32 numItems
,
1419 const DataBrowserItemID
* items
,
1420 DataBrowserPropertyID preSortProperty
)
1422 return AddDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1425 OSStatus
wxMacControl::RemoveItems( DataBrowserItemID container
, UInt32 numItems
,
1426 const DataBrowserItemID
* items
,
1427 DataBrowserPropertyID preSortProperty
)
1429 return RemoveDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1432 OSStatus
wxMacControl::RevealItem( DataBrowserItemID item
,
1433 DataBrowserPropertyID propertyID
,
1434 DataBrowserRevealOptions options
)
1436 return RevealDataBrowserItem( m_controlRef
, item
, propertyID
, options
) ;
1439 OSStatus
wxMacControl::SetSelectedItems(UInt32 numItems
,
1440 const DataBrowserItemID
* items
,
1441 DataBrowserSetOption operation
)
1443 return SetDataBrowserSelectedItems( m_controlRef
, numItems
, items
, operation
) ;
1446 OSStatus
wxMacControl::GetSelectionAnchor( DataBrowserItemID
* first
, DataBrowserItemID
* last
)
1448 return GetDataBrowserSelectionAnchor( m_controlRef
, first
, last
) ;
1455 OSStatus
wxMacControl::SetTabEnabled( SInt16 tabNo
, bool enable
)
1457 return ::SetTabEnabled( m_controlRef
, tabNo
, enable
) ;
1464 #ifdef __WXMAC_OSX__
1465 // snippets from Sketch Sample from Apple :
1467 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
1469 This function locates, opens, and returns the profile reference for the calibrated
1470 Generic RGB color space. It is up to the caller to call CMCloseProfile when done
1471 with the profile reference this function returns.
1473 CMProfileRef
wxMacOpenGenericProfile(void)
1475 static CMProfileRef cachedRGBProfileRef
= NULL
;
1477 // we only create the profile reference once
1478 if (cachedRGBProfileRef
== NULL
)
1480 CMProfileLocation loc
;
1482 loc
.locType
= cmPathBasedProfile
;
1483 strcpy(loc
.u
.pathLoc
.path
, kGenericRGBProfilePathStr
);
1485 verify_noerr( CMOpenProfile(&cachedRGBProfileRef
, &loc
) );
1488 if (cachedRGBProfileRef
)
1490 // clone the profile reference so that the caller has their own reference, not our cached one
1491 CMCloneProfileRef(cachedRGBProfileRef
);
1494 return cachedRGBProfileRef
;
1498 Return the generic RGB color space. This is a 'get' function and the caller should
1499 not release the returned value unless the caller retains it first. Usually callers
1500 of this routine will immediately use the returned colorspace with CoreGraphics
1501 so they typically do not need to retain it themselves.
1503 This function creates the generic RGB color space once and hangs onto it so it can
1504 return it whenever this function is called.
1507 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
1509 static CGColorSpaceRef genericRGBColorSpace
= NULL
;
1511 if (genericRGBColorSpace
== NULL
)
1513 CMProfileRef genericRGBProfile
= wxMacOpenGenericProfile();
1515 if (genericRGBProfile
)
1517 genericRGBColorSpace
= CGColorSpaceCreateWithPlatformColorSpace(genericRGBProfile
);
1518 wxASSERT_MSG( genericRGBColorSpace
!= NULL
, wxT("couldn't create the generic RGB color space") ) ;
1520 // we opened the profile so it is up to us to close it
1521 CMCloseProfile(genericRGBProfile
);
1524 return genericRGBColorSpace
;