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>
54 #include <Carbon/Carbon.h>
56 #include <ATSUnicode.h>
57 #include <TextCommon.h>
58 #include <TextEncodingConverter.h>
62 #include "wx/mac/private.h" // includes mac headers
64 #if defined(__MWERKS__) && wxUSE_UNICODE
68 // ---------------------------------------------------------------------------
69 // code used in both base and GUI compilation
70 // ---------------------------------------------------------------------------
72 // our OS version is the same in non GUI and GUI cases
73 static int DoGetOSVersion(int *majorVsn
, int *minorVsn
)
77 // are there x-platform conventions ?
79 Gestalt(gestaltSystemVersion
, &theSystem
) ;
80 if (minorVsn
!= NULL
) {
81 *minorVsn
= (theSystem
& 0xFF ) ;
83 if (majorVsn
!= NULL
) {
84 *majorVsn
= (theSystem
>> 8 ) ;
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 #if defined(__WXDEBUG__) && defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
102 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
104 #ifndef __MetroNubUtils__
105 #include "MetroNubUtils.h"
112 #if TARGET_API_MAC_CARBON
114 #include <CodeFragments.h>
116 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr
, ProcInfoType procInfo
, ...);
118 ProcPtr gCallUniversalProc_Proc
= NULL
;
122 static MetroNubUserEntryBlock
* gMetroNubEntry
= NULL
;
124 static long fRunOnce
= false;
126 /* ---------------------------------------------------------------------------
128 --------------------------------------------------------------------------- */
130 Boolean
IsMetroNubInstalled()
137 gMetroNubEntry
= NULL
;
139 if (Gestalt(gestaltSystemVersion
, &value
) == noErr
&& value
< 0x1000)
141 /* look for MetroNub's Gestalt selector */
142 if (Gestalt(kMetroNubUserSignature
, &result
) == noErr
)
145 #if TARGET_API_MAC_CARBON
146 if (gCallUniversalProc_Proc
== NULL
)
148 CFragConnectionID connectionID
;
151 ProcPtr symbolAddress
;
153 CFragSymbolClass symbolClass
;
155 symbolAddress
= NULL
;
156 err
= GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch
, kFindCFrag
,
157 &connectionID
, &mainAddress
, errorString
);
161 gCallUniversalProc_Proc
= NULL
;
165 err
= FindSymbol(connectionID
, "\pCallUniversalProc",
166 (Ptr
*) &gCallUniversalProc_Proc
, &symbolClass
);
170 gCallUniversalProc_Proc
= NULL
;
177 MetroNubUserEntryBlock
* block
= (MetroNubUserEntryBlock
*)result
;
179 /* make sure the version of the API is compatible */
180 if (block
->apiLowVersion
<= kMetroNubUserAPIVersion
&&
181 kMetroNubUserAPIVersion
<= block
->apiHiVersion
)
182 gMetroNubEntry
= block
; /* success! */
191 #if TARGET_API_MAC_CARBON
192 return (gMetroNubEntry
!= NULL
&& gCallUniversalProc_Proc
!= NULL
);
194 return (gMetroNubEntry
!= NULL
);
198 /* ---------------------------------------------------------------------------
199 IsMWDebuggerRunning [v1 API]
200 --------------------------------------------------------------------------- */
202 Boolean
IsMWDebuggerRunning()
204 if (IsMetroNubInstalled())
205 return CallIsDebuggerRunningProc(gMetroNubEntry
->isDebuggerRunning
);
210 /* ---------------------------------------------------------------------------
211 AmIBeingMWDebugged [v1 API]
212 --------------------------------------------------------------------------- */
214 Boolean
AmIBeingMWDebugged()
216 if (IsMetroNubInstalled())
217 return CallAmIBeingDebuggedProc(gMetroNubEntry
->amIBeingDebugged
);
222 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
224 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
229 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
234 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
238 // defined in unix/utilsunx.cpp for Mac OS X
240 // get full hostname (with domain name if possible)
241 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
243 return wxGetHostName(buf
, maxSize
);
246 // Get hostname only (without domain name)
247 bool wxGetHostName(wxChar
*buf
, int maxSize
)
249 // Gets Chooser name of user by examining a System resource.
251 const short kComputerNameID
= -16413;
253 short oldResFile
= CurResFile() ;
255 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
256 UseResFile(oldResFile
);
258 if (chooserName
&& *chooserName
)
260 HLock( (Handle
) chooserName
) ;
261 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
262 HUnlock( (Handle
) chooserName
) ;
263 ReleaseResource( (Handle
) chooserName
) ;
264 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
272 // Get user ID e.g. jacs
273 bool wxGetUserId(wxChar
*buf
, int maxSize
)
275 return wxGetUserName( buf
, maxSize
) ;
278 const wxChar
* wxGetHomeDir(wxString
*pstr
)
280 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
281 return pstr
->c_str() ;
284 // Get user name e.g. Stefan Csomor
285 bool wxGetUserName(wxChar
*buf
, int maxSize
)
287 // Gets Chooser name of user by examining a System resource.
289 const short kChooserNameID
= -16096;
291 short oldResFile
= CurResFile() ;
293 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
294 UseResFile(oldResFile
);
296 if (chooserName
&& *chooserName
)
298 HLock( (Handle
) chooserName
) ;
299 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
300 HUnlock( (Handle
) chooserName
) ;
301 ReleaseResource( (Handle
) chooserName
) ;
302 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
310 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
, int flags
)
316 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
318 // TODO : under classic there is no environement support, under X yes
322 // set the env var name to the given value, return true on success
323 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
325 // TODO : under classic there is no environement support, under X yes
330 // Execute a program in an Interactive Shell
332 bool wxShell(const wxString
& command
)
338 // Shutdown or reboot the PC
339 bool wxShutdown(wxShutdownFlags wFlags
)
345 wxPowerType
wxGetPowerType()
348 return wxPOWER_UNKNOWN
;
351 wxBatteryState
wxGetBatteryState()
354 return wxBATTERY_UNKNOWN_STATE
;
357 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
358 wxMemorySize
wxGetFreeMemory()
360 return (wxMemorySize
)FreeMem() ;
365 void wxMicroSleep(unsigned long microseconds
)
367 AbsoluteTime wakeup
= AddDurationToAbsolute( microseconds
* durationMicrosecond
, UpTime());
368 MPDelayUntil( & wakeup
);
371 void wxMilliSleep(unsigned long milliseconds
)
373 AbsoluteTime wakeup
= AddDurationToAbsolute( milliseconds
, UpTime());
374 MPDelayUntil( & wakeup
);
377 void wxSleep(int nSecs
)
379 wxMilliSleep(1000*nSecs
);
384 // Consume all events until no more left
389 #endif // !__DARWIN__
397 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
399 static wxToolkitInfo info
;
400 info
.os
= DoGetOSVersion(&info
.versionMajor
, &info
.versionMinor
);
401 info
.name
= _T("wxBase");
409 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
411 static wxToolkitInfo info
;
412 info
.os
= DoGetOSVersion(&info
.versionMajor
, &info
.versionMinor
);
413 info
.shortName
= _T("mac");
414 info
.name
= _T("wxMac");
415 #ifdef __WXUNIVERSAL__
416 info
.shortName
<< _T("univ");
417 info
.name
<< _T("/wxUniversal");
422 // Reading and writing resources (eg WIN.INI, .Xdefaults)
424 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
430 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
433 buf
.Printf(wxT("%.4f"), value
);
435 return wxWriteResource(section
, entry
, buf
, file
);
438 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
441 buf
.Printf(wxT("%ld"), value
);
443 return wxWriteResource(section
, entry
, buf
, file
);
446 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
449 buf
.Printf(wxT("%d"), value
);
451 return wxWriteResource(section
, entry
, buf
, file
);
454 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
460 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
463 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
466 *value
= (float)strtod(s
, NULL
);
473 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
476 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
479 *value
= strtol(s
, NULL
, 10);
486 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
489 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
492 *value
= (int)strtol(s
, NULL
, 10);
498 #endif // wxUSE_RESOURCES
500 int gs_wxBusyCursorCount
= 0;
501 extern wxCursor gMacCurrentCursor
;
502 wxCursor gMacStoredActiveCursor
;
504 // Set the cursor to the busy cursor for all windows
505 void wxBeginBusyCursor(wxCursor
*cursor
)
507 if (gs_wxBusyCursorCount
++ == 0)
509 gMacStoredActiveCursor
= gMacCurrentCursor
;
510 cursor
->MacInstall() ;
512 //else: nothing to do, already set
515 // Restore cursor to normal
516 void wxEndBusyCursor()
518 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
519 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
521 if (--gs_wxBusyCursorCount
== 0)
523 gMacStoredActiveCursor
.MacInstall() ;
524 gMacStoredActiveCursor
= wxNullCursor
;
528 // true if we're between the above two calls
531 return (gs_wxBusyCursorCount
> 0);
538 wxString
wxMacFindFolder( short vol
,
540 Boolean createFolder
)
545 if ( FSFindFolder( vol
, folderType
, createFolder
, &fsRef
) == noErr
)
546 strDir
= wxMacFSRefToPath( &fsRef
) + wxFILE_SEP_PATH
;
555 // Check whether this window wants to process messages, e.g. Stop button
556 // in long calculations.
557 bool wxCheckForInterrupt(wxWindow
*wnd
)
563 void wxGetMousePosition( int* x
, int* y
)
568 LocalToGlobal( &pt
) ;
573 // Return true if we have a colour display
574 bool wxColourDisplay()
579 // Returns depth of screen
583 SetRect(&globRect
, -32760, -32760, 32760, 32760);
584 GDHandle theMaxDevice
;
587 theMaxDevice
= GetMaxDevice(&globRect
);
588 if (theMaxDevice
!= nil
)
589 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
594 // Get size of display
595 void wxDisplaySize(int *width
, int *height
)
598 GetQDGlobalsScreenBits( &screenBits
);
601 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
603 if (height
!= NULL
) {
604 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
608 void wxDisplaySizeMM(int *width
, int *height
)
610 wxDisplaySize(width
, height
);
611 // on mac 72 is fixed (at least now ;-)
612 float cvPt2Mm
= 25.4 / 72;
615 *width
= int( *width
* cvPt2Mm
);
617 if (height
!= NULL
) {
618 *height
= int( *height
* cvPt2Mm
);
622 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
625 GetAvailableWindowPositioningBounds( GetMainDevice() , &r
) ;
631 *width
= r
.right
- r
.left
;
633 *height
= r
.bottom
- r
.top
;
636 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
638 return wxGenericFindWindowAtPoint(pt
);
645 wxString
wxGetOsDescription()
647 #ifdef WXWIN_OS_DESCRIPTION
648 // use configure generated description if available
649 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION
) + wxString(wxT(")"));
651 return wxT("MacOS") ; //TODO:define further
656 wxChar
*wxGetUserHome (const wxString
& user
)
662 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
672 int pos
= p
.Find(':') ;
673 if ( pos
!= wxNOT_FOUND
) {
682 err
= wxMacPathToFSRef( p
, &fsRef
) ;
685 FSVolumeRefNum vRefNum
;
686 err
= FSGetVRefNum( &fsRef
, &vRefNum
) ;
689 UInt64 freeBytes
, totalBytes
;
690 err
= FSGetVInfo( vRefNum
, NULL
, &freeBytes
, &totalBytes
) ;
694 *pTotal
= wxLongLong( totalBytes
) ;
696 *pFree
= wxLongLong( freeBytes
) ;
701 return err
== noErr
;
703 #endif // !__DARWIN__
705 //---------------------------------------------------------------------------
706 // wxMac Specific utility functions
707 //---------------------------------------------------------------------------
709 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
711 wxCharBuffer buf
= from
.mb_str( wxConvLocal
) ;
712 int len
= strlen(buf
) ;
717 memcpy( (char*) &to
[1] , buf
, len
) ;
720 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
722 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] ) ;
726 // ----------------------------------------------------------------------------
727 // Common Event Support
728 // ----------------------------------------------------------------------------
731 extern ProcessSerialNumber gAppProcess
;
735 ProcessSerialNumber psn
;
737 psn
.highLongOfPSN
= 0 ;
738 psn
.lowLongOfPSN
= kCurrentProcess
;
739 SameProcess( &gAppProcess
, &psn
, &isSame
) ;
743 static wxMacCarbonEvent s_wakeupEvent
;
744 OSStatus err
= noErr
;
745 if ( !s_wakeupEvent
.IsValid() )
747 err
= s_wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
748 kEventAttributeNone
) ;
752 if ( IsEventInQueue( GetMainEventQueue() , s_wakeupEvent
) )
754 s_wakeupEvent
.SetTime(0) ;
755 err
= PostEventToQueue(GetMainEventQueue(), s_wakeupEvent
,
759 PostEvent( nullEvent
, 0 ) ;
764 WakeUpProcess( &gAppProcess
) ;
772 // ----------------------------------------------------------------------------
773 // Native Struct Conversions
774 // ----------------------------------------------------------------------------
777 void wxMacRectToNative( const wxRect
*wx
, Rect
*n
)
781 n
->right
= wx
->x
+ wx
->width
;
782 n
->bottom
= wx
->y
+ wx
->height
;
785 void wxMacNativeToRect( const Rect
*n
, wxRect
* wx
)
789 wx
->width
= n
->right
- n
->left
;
790 wx
->height
= n
->bottom
- n
->top
;
793 void wxMacPointToNative( const wxPoint
* wx
, Point
*n
)
799 void wxMacNativeToPoint( const Point
*n
, wxPoint
* wx
)
805 // ----------------------------------------------------------------------------
806 // Carbon Event Support
807 // ----------------------------------------------------------------------------
810 OSStatus
wxMacCarbonEvent::GetParameter(EventParamName inName
, EventParamType inDesiredType
, UInt32 inBufferSize
, void * outData
)
812 return ::GetEventParameter( m_eventRef
, inName
, inDesiredType
, NULL
, inBufferSize
, NULL
, outData
) ;
815 OSStatus
wxMacCarbonEvent::SetParameter(EventParamName inName
, EventParamType inType
, UInt32 inBufferSize
, const void * inData
)
817 return ::SetEventParameter( m_eventRef
, inName
, inType
, inBufferSize
, inData
) ;
820 // ----------------------------------------------------------------------------
821 // Control Access Support
822 // ----------------------------------------------------------------------------
824 wxMacControl::wxMacControl(wxWindow
* peer
, bool isRootControl
)
828 m_isRootControl
= isRootControl
;
829 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
832 wxMacControl::wxMacControl( wxWindow
* peer
, ControlRef control
)
836 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
837 m_controlRef
= control
;
840 wxMacControl::wxMacControl( wxWindow
* peer
, WXWidget control
)
844 m_isCompositing
= peer
->MacGetTopLevelWindow()->MacUsesCompositing() ;
845 m_controlRef
= (ControlRef
) control
;
848 wxMacControl::~wxMacControl()
852 void wxMacControl::Init()
855 m_controlRef
= NULL
;
856 m_needsFocusRect
= false ;
857 m_isCompositing
= false ;
858 m_isRootControl
= false ;
861 void wxMacControl::Dispose()
863 ::DisposeControl( m_controlRef
) ;
864 m_controlRef
= NULL
;
867 void wxMacControl::SetReference( SInt32 data
)
869 SetControlReference( m_controlRef
, data
) ;
872 OSStatus
wxMacControl::GetData(ControlPartCode inPartCode
, ResType inTag
, Size inBufferSize
, void * inOutBuffer
, Size
* outActualSize
) const
874 return ::GetControlData( m_controlRef
, inPartCode
, inTag
, inBufferSize
, inOutBuffer
, outActualSize
) ;
877 OSStatus
wxMacControl::GetDataSize(ControlPartCode inPartCode
, ResType inTag
, Size
* outActualSize
) const
879 return ::GetControlDataSize( m_controlRef
, inPartCode
, inTag
, outActualSize
) ;
882 OSStatus
wxMacControl::SetData(ControlPartCode inPartCode
, ResType inTag
, Size inSize
, const void * inData
)
884 return ::SetControlData( m_controlRef
, inPartCode
, inTag
, inSize
, inData
) ;
887 OSStatus
wxMacControl::SendEvent( EventRef event
, OptionBits inOptions
)
889 #if TARGET_API_MAC_OSX
890 return SendEventToEventTargetWithOptions( event
,
891 HIObjectGetEventTarget( (HIObjectRef
) m_controlRef
), inOptions
);
893 #pragma unused(inOptions)
894 return SendEventToEventTarget(event
,GetControlEventTarget( m_controlRef
) ) ;
898 OSStatus
wxMacControl::SendHICommand( HICommand
&command
, OptionBits inOptions
)
900 wxMacCarbonEvent
event( kEventClassCommand
, kEventCommandProcess
) ;
901 event
.SetParameter
<HICommand
>(kEventParamDirectObject
,command
) ;
902 return SendEvent( event
, inOptions
) ;
905 OSStatus
wxMacControl::SendHICommand( UInt32 commandID
, OptionBits inOptions
)
908 memset( &command
, 0 , sizeof(command
) ) ;
909 command
.commandID
= commandID
;
910 return SendHICommand( command
, inOptions
) ;
913 void wxMacControl::Flash( ControlPartCode part
, UInt32 ticks
)
915 HiliteControl( m_controlRef
, part
) ;
916 unsigned long finalTicks
;
917 Delay( ticks
, &finalTicks
) ;
918 HiliteControl( m_controlRef
, kControlNoPart
) ;
921 SInt32
wxMacControl::GetValue() const
923 return ::GetControl32BitValue( m_controlRef
) ;
926 SInt32
wxMacControl::GetMaximum() const
928 return ::GetControl32BitMaximum( m_controlRef
) ;
931 SInt32
wxMacControl::GetMinimum() const
933 return ::GetControl32BitMinimum( m_controlRef
) ;
936 void wxMacControl::SetValue( SInt32 v
)
938 ::SetControl32BitValue( m_controlRef
, v
) ;
941 void wxMacControl::SetMinimum( SInt32 v
)
943 ::SetControl32BitMinimum( m_controlRef
, v
) ;
946 void wxMacControl::SetMaximum( SInt32 v
)
948 ::SetControl32BitMaximum( m_controlRef
, v
) ;
951 void wxMacControl::SetValueAndRange( SInt32 value
, SInt32 minimum
, SInt32 maximum
)
953 ::SetControl32BitMinimum( m_controlRef
, minimum
) ;
954 ::SetControl32BitMaximum( m_controlRef
, maximum
) ;
955 ::SetControl32BitValue( m_controlRef
, value
) ;
958 OSStatus
wxMacControl::SetFocus( ControlFocusPart focusPart
)
960 return SetKeyboardFocus( GetControlOwner( m_controlRef
) ,
961 m_controlRef
, focusPart
) ;
964 bool wxMacControl::HasFocus() const
967 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
968 return control
== m_controlRef
;
971 void wxMacControl::SetNeedsFocusRect( bool needs
)
973 m_needsFocusRect
= needs
;
976 bool wxMacControl::NeedsFocusRect() const
978 return m_needsFocusRect
;
981 void wxMacControl::VisibilityChanged(bool shown
)
985 void wxMacControl::SuperChangedPosition()
989 void wxMacControl::SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
)
992 ControlFontStyleRec fontStyle
;
993 if ( font
.MacGetThemeFontID() != kThemeCurrentPortFont
)
995 switch( font
.MacGetThemeFontID() )
997 case kThemeSmallSystemFont
: fontStyle
.font
= kControlFontSmallSystemFont
; break ;
998 case 109 /*mini font */ : fontStyle
.font
= -5 ; break ;
999 case kThemeSystemFont
: fontStyle
.font
= kControlFontBigSystemFont
; break ;
1000 default : fontStyle
.font
= kControlFontBigSystemFont
; break ;
1002 fontStyle
.flags
= kControlUseFontMask
;
1006 fontStyle
.font
= font
.MacGetFontNum() ;
1007 fontStyle
.style
= font
.MacGetFontStyle() ;
1008 fontStyle
.size
= font
.MacGetFontSize() ;
1009 fontStyle
.flags
= kControlUseFontMask
| kControlUseFaceMask
| kControlUseSizeMask
;
1012 fontStyle
.just
= teJustLeft
;
1013 fontStyle
.flags
|= kControlUseJustMask
;
1014 if ( ( windowStyle
& wxALIGN_MASK
) & wxALIGN_CENTER_HORIZONTAL
)
1015 fontStyle
.just
= teJustCenter
;
1016 else if ( ( windowStyle
& wxALIGN_MASK
) & wxALIGN_RIGHT
)
1017 fontStyle
.just
= teJustRight
;
1020 // we only should do this in case of a non-standard color, as otherwise 'disabled' controls
1021 // won't get grayed out by the system anymore
1023 if ( foreground
!= *wxBLACK
)
1025 fontStyle
.foreColor
= MAC_WXCOLORREF(foreground
.GetPixel() ) ;
1026 fontStyle
.flags
|= kControlUseForeColorMask
;
1029 ::SetControlFontStyle( m_controlRef
, &fontStyle
);
1032 void wxMacControl::SetBackground( const wxBrush
&WXUNUSED(brush
) )
1035 // setting up a color proc is not recommended anymore
1038 void wxMacControl::SetRange( SInt32 minimum
, SInt32 maximum
)
1040 ::SetControl32BitMinimum( m_controlRef
, minimum
) ;
1041 ::SetControl32BitMaximum( m_controlRef
, maximum
) ;
1044 short wxMacControl::HandleKey( SInt16 keyCode
, SInt16 charCode
, EventModifiers modifiers
)
1046 return HandleControlKey( m_controlRef
, keyCode
, charCode
, modifiers
) ;
1049 void wxMacControl::SetActionProc( ControlActionUPP actionProc
)
1051 SetControlAction( m_controlRef
, actionProc
) ;
1054 void wxMacControl::SetViewSize( SInt32 viewSize
)
1056 SetControlViewSize(m_controlRef
, viewSize
) ;
1059 SInt32
wxMacControl::GetViewSize() const
1061 return GetControlViewSize( m_controlRef
) ;
1064 bool wxMacControl::IsVisible() const
1066 return IsControlVisible( m_controlRef
) ;
1069 void wxMacControl::SetVisibility( bool visible
, bool redraw
)
1071 SetControlVisibility( m_controlRef
, visible
, redraw
) ;
1074 bool wxMacControl::IsEnabled() const
1076 #if TARGET_API_MAC_OSX
1077 return IsControlEnabled( m_controlRef
) ;
1079 return IsControlActive( m_controlRef
) ;
1083 bool wxMacControl::IsActive() const
1085 return IsControlActive( m_controlRef
) ;
1088 void wxMacControl::Enable( bool enable
)
1090 #if TARGET_API_MAC_OSX
1092 EnableControl( m_controlRef
) ;
1094 DisableControl( m_controlRef
) ;
1097 ActivateControl( m_controlRef
) ;
1099 DeactivateControl( m_controlRef
) ;
1103 void wxMacControl::SetDrawingEnabled( bool enable
)
1105 #if TARGET_API_MAC_OSX
1106 HIViewSetDrawingEnabled( m_controlRef
, enable
) ;
1110 #if TARGET_API_MAC_OSX
1111 bool wxMacControl::GetNeedsDisplay() const
1113 #if TARGET_API_MAC_OSX
1114 if ( m_isCompositing
)
1116 return HIViewGetNeedsDisplay( m_controlRef
) ;
1124 Rect controlBounds
;
1125 GetControlBounds( m_controlRef
, &controlBounds
) ;
1126 RgnHandle rgn
= NewRgn() ;
1127 GetWindowRegion ( GetControlOwner( m_controlRef
) , kWindowUpdateRgn
, rgn
) ;
1128 Boolean intersect
= RectInRgn ( &controlBounds
, rgn
) ;
1136 void wxMacControl::SetNeedsDisplay( RgnHandle where
)
1141 #if TARGET_API_MAC_OSX
1142 if ( m_isCompositing
)
1144 HIViewSetNeedsDisplayInRegion( m_controlRef
, where
, true ) ;
1149 Rect controlBounds
;
1150 GetControlBounds( m_controlRef
, &controlBounds
) ;
1151 RgnHandle update
= NewRgn() ;
1152 CopyRgn( where
, update
) ;
1153 OffsetRgn( update
, controlBounds
.left
, controlBounds
.top
) ;
1154 InvalWindowRgn( GetControlOwner( m_controlRef
) , update
) ;
1158 void wxMacControl::SetNeedsDisplay( Rect
* where
)
1163 #if TARGET_API_MAC_OSX
1164 if ( m_isCompositing
)
1166 if ( where
!= NULL
)
1168 RgnHandle update
= NewRgn() ;
1169 RectRgn( update
, where
) ;
1170 HIViewSetNeedsDisplayInRegion( m_controlRef
, update
, true ) ;
1171 DisposeRgn( update
) ;
1174 HIViewSetNeedsDisplay( m_controlRef
, true ) ;
1179 Rect controlBounds
;
1180 GetControlBounds( m_controlRef
, &controlBounds
) ;
1183 Rect whereLocal
= *where
;
1184 OffsetRect( &whereLocal
, controlBounds
.left
, controlBounds
.top
) ;
1185 SectRect( &controlBounds
, &whereLocal
, &controlBounds
) ;
1187 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1191 void wxMacControl::Convert( wxPoint
*pt
, wxMacControl
*from
, wxMacControl
*to
)
1193 #if TARGET_API_MAC_OSX
1194 if ( from
->m_peer
->MacGetTopLevelWindow()->MacUsesCompositing() )
1199 HIViewConvertPoint( &hiPoint
, from
->m_controlRef
, to
->m_controlRef
) ;
1200 pt
->x
= (int)hiPoint
.x
;
1201 pt
->y
= (int)hiPoint
.y
;
1208 GetControlBounds( from
->m_controlRef
, &fromRect
) ;
1209 GetControlBounds( to
->m_controlRef
, &toRect
) ;
1210 if ( from
->m_isRootControl
)
1211 fromRect
.left
= fromRect
.top
= 0 ;
1212 if ( to
->m_isRootControl
)
1213 toRect
.left
= toRect
.top
= 0 ;
1215 pt
->x
= pt
->x
+ fromRect
.left
- toRect
.left
;
1216 pt
->y
= pt
->y
+ fromRect
.top
- toRect
.top
;
1220 void wxMacControl::SetRect( Rect
*r
)
1222 #if TARGET_API_MAC_OSX
1223 if ( m_isCompositing
)
1225 //A HIRect is actually a CGRect on OSX - which consists of two structures -
1226 //CGPoint and CGSize, which have two floats each
1227 HIRect hir
= { { r
->left
, r
->top
}, { r
->right
- r
->left
, r
->bottom
- r
->top
} } ;
1228 HIViewSetFrame ( m_controlRef
, &hir
) ;
1229 // eventuall we might have to do a SetVisibility( false , true ) ;
1230 // before and a SetVisibility( true , true ) ; after
1235 bool vis
= IsVisible() ;
1239 GetControlBounds( m_controlRef
, &former
) ;
1240 InvalWindowRect( GetControlOwner( m_controlRef
) , &former
) ;
1243 Rect controlBounds
= *r
;
1245 // since the rect passed in is always (even in non-compositing) relative
1246 // to the (native) parent, we have to adjust to window relative here
1247 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1248 if( parent
->m_isRootControl
== false )
1251 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1252 OffsetRect( &controlBounds
, superRect
.left
, superRect
.top
) ;
1255 SetControlBounds( m_controlRef
, &controlBounds
) ;
1258 InvalWindowRect( GetControlOwner( m_controlRef
) , &controlBounds
) ;
1263 void wxMacControl::GetRect( Rect
*r
)
1265 GetControlBounds( m_controlRef
, r
) ;
1266 if ( m_isCompositing
== false )
1268 // correct the case of the root control
1269 if ( m_isRootControl
)
1271 WindowRef wr
= GetControlOwner( m_controlRef
) ;
1272 GetWindowBounds( wr
, kWindowContentRgn
, r
) ;
1273 r
->right
-= r
->left
;
1274 r
->bottom
-= r
->top
;
1280 wxMacControl
* parent
= m_peer
->GetParent()->GetPeer() ;
1281 if( parent
->m_isRootControl
== false )
1284 GetControlBounds( parent
->m_controlRef
, &superRect
) ;
1285 OffsetRect( r
, -superRect
.left
, -superRect
.top
) ;
1291 void wxMacControl::GetRectInWindowCoords( Rect
*r
)
1293 UMAGetControlBoundsInWindowCoords( m_controlRef
, r
) ;
1296 void wxMacControl::GetBestRect( Rect
*r
)
1298 short baselineoffset
;
1299 GetBestControlRect( m_controlRef
, r
, &baselineoffset
) ;
1302 void wxMacControl::SetTitle( const wxString
&title
)
1304 wxFontEncoding encoding
;
1307 encoding
= m_font
.GetEncoding();
1309 encoding
= wxFont::GetDefaultEncoding();
1311 UMASetControlTitle( m_controlRef
, title
, encoding
) ;
1314 void wxMacControl::GetFeatures( UInt32
* features
)
1316 GetControlFeatures( m_controlRef
, features
) ;
1319 OSStatus
wxMacControl::GetRegion( ControlPartCode partCode
, RgnHandle region
)
1321 OSStatus err
= GetControlRegion( m_controlRef
, partCode
, region
) ;
1322 if ( m_isCompositing
== false )
1324 if ( !m_isRootControl
)
1327 GetControlBounds(m_controlRef
, &r
) ;
1328 if ( !EmptyRgn( region
) )
1329 OffsetRgn( region
, -r
.left
, -r
.top
) ;
1335 OSStatus
wxMacControl::SetZOrder( bool above
, wxMacControl
* other
)
1337 #if TARGET_API_MAC_OSX
1338 return HIViewSetZOrder( m_controlRef
,above
? kHIViewZOrderAbove
: kHIViewZOrderBelow
,
1339 (other
!= NULL
) ? other
->m_controlRef
: NULL
) ;
1346 #if TARGET_API_MAC_OSX
1347 // SetNeedsDisplay would not invalidate the children
1348 static void InvalidateControlAndChildren( HIViewRef control
)
1350 HIViewSetNeedsDisplay( control
, true ) ;
1351 UInt16 childrenCount
= 0 ;
1352 OSStatus err
= CountSubControls( control
, &childrenCount
) ;
1353 if ( err
== errControlIsNotEmbedder
)
1355 wxASSERT_MSG( err
== noErr
, wxT("Unexpected error when accessing subcontrols") ) ;
1357 for ( UInt16 i
= childrenCount
; i
>=1 ; --i
)
1360 err
= GetIndexedSubControl( control
, i
, & child
) ;
1361 if ( err
== errControlIsNotEmbedder
)
1363 InvalidateControlAndChildren( child
) ;
1368 void wxMacControl::InvalidateWithChildren()
1370 #if TARGET_API_MAC_OSX
1371 InvalidateControlAndChildren( m_controlRef
) ;
1375 void wxMacControl::ScrollRect( wxRect
*r
, int dx
, int dy
)
1377 wxASSERT( r
!= NULL
) ;
1378 #if TARGET_API_MAC_OSX
1379 if ( m_isCompositing
)
1381 HIRect scrollarea
= CGRectMake( r
->x
, r
->y
, r
->width
, r
->height
) ;
1382 HIViewScrollRect ( m_controlRef
, &scrollarea
, dx
,dy
) ;
1388 GetControlBounds( m_controlRef
, &bounds
) ;
1389 bounds
.left
+= r
->x
;
1390 bounds
.top
+= r
->y
;
1391 bounds
.bottom
= bounds
.top
+ r
->height
;
1392 bounds
.right
= bounds
.left
+ r
->width
;
1393 wxMacWindowClipper
clip( m_peer
) ;
1394 RgnHandle updateRgn
= NewRgn() ;
1395 ::ScrollRect( &bounds
, dx
, dy
, updateRgn
) ;
1396 InvalWindowRgn( GetControlOwner( m_controlRef
) , updateRgn
) ;
1401 // SetNeedsDisplay would not invalidate the children
1407 OSStatus
wxMacControl::SetSelectionFlags( DataBrowserSelectionFlags options
)
1409 return SetDataBrowserSelectionFlags( m_controlRef
, options
) ;
1412 OSStatus
wxMacControl::AddListViewColumn( DataBrowserListViewColumnDesc
*columnDesc
,
1413 DataBrowserTableViewColumnIndex position
)
1415 return AddDataBrowserListViewColumn( m_controlRef
, columnDesc
, position
) ;
1418 OSStatus
wxMacControl::AutoSizeListViewColumns()
1420 return AutoSizeDataBrowserListViewColumns(m_controlRef
) ;
1423 OSStatus
wxMacControl::SetHasScrollBars( bool horiz
, bool vert
)
1425 return SetDataBrowserHasScrollBars( m_controlRef
, horiz
, vert
) ;
1428 OSStatus
wxMacControl::SetTableViewHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle
)
1430 return SetDataBrowserTableViewHiliteStyle( m_controlRef
, hiliteStyle
) ;
1433 OSStatus
wxMacControl::SetListViewHeaderBtnHeight(UInt16 height
)
1435 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef
,height
) ;
1438 OSStatus
wxMacControl::SetCallbacks(const DataBrowserCallbacks
* callbacks
)
1440 return SetDataBrowserCallbacks( m_controlRef
, callbacks
) ;
1443 OSStatus
wxMacControl::UpdateItems( DataBrowserItemID container
, UInt32 numItems
,
1444 const DataBrowserItemID
* items
,
1445 DataBrowserPropertyID preSortProperty
,
1446 DataBrowserPropertyID propertyID
)
1448 return UpdateDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
, propertyID
) ;
1451 bool wxMacControl::IsItemSelected( DataBrowserItemID item
)
1453 return IsDataBrowserItemSelected( m_controlRef
, item
) ;
1456 OSStatus
wxMacControl::AddItems( DataBrowserItemID container
, UInt32 numItems
,
1457 const DataBrowserItemID
* items
,
1458 DataBrowserPropertyID preSortProperty
)
1460 return AddDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1463 OSStatus
wxMacControl::RemoveItems( DataBrowserItemID container
, UInt32 numItems
,
1464 const DataBrowserItemID
* items
,
1465 DataBrowserPropertyID preSortProperty
)
1467 return RemoveDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
) ;
1470 OSStatus
wxMacControl::RevealItem( DataBrowserItemID item
,
1471 DataBrowserPropertyID propertyID
,
1472 DataBrowserRevealOptions options
)
1474 return RevealDataBrowserItem( m_controlRef
, item
, propertyID
, options
) ;
1477 OSStatus
wxMacControl::SetSelectedItems(UInt32 numItems
,
1478 const DataBrowserItemID
* items
,
1479 DataBrowserSetOption operation
)
1481 return SetDataBrowserSelectedItems( m_controlRef
, numItems
, items
, operation
) ;
1484 OSStatus
wxMacControl::GetSelectionAnchor( DataBrowserItemID
* first
, DataBrowserItemID
* last
)
1486 return GetDataBrowserSelectionAnchor( m_controlRef
, first
, last
) ;
1493 OSStatus
wxMacControl::SetTabEnabled( SInt16 tabNo
, bool enable
)
1495 return ::SetTabEnabled( m_controlRef
, tabNo
, enable
) ;
1502 #ifdef __WXMAC_OSX__
1503 // snippets from Sketch Sample from Apple :
1505 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
1507 This function locates, opens, and returns the profile reference for the calibrated
1508 Generic RGB color space. It is up to the caller to call CMCloseProfile when done
1509 with the profile reference this function returns.
1511 CMProfileRef
wxMacOpenGenericProfile(void)
1513 static CMProfileRef cachedRGBProfileRef
= NULL
;
1515 // we only create the profile reference once
1516 if (cachedRGBProfileRef
== NULL
)
1518 CMProfileLocation loc
;
1520 loc
.locType
= cmPathBasedProfile
;
1521 strcpy(loc
.u
.pathLoc
.path
, kGenericRGBProfilePathStr
);
1523 verify_noerr( CMOpenProfile(&cachedRGBProfileRef
, &loc
) );
1526 if (cachedRGBProfileRef
)
1528 // clone the profile reference so that the caller has their own reference, not our cached one
1529 CMCloneProfileRef(cachedRGBProfileRef
);
1532 return cachedRGBProfileRef
;
1536 Return the generic RGB color space. This is a 'get' function and the caller should
1537 not release the returned value unless the caller retains it first. Usually callers
1538 of this routine will immediately use the returned colorspace with CoreGraphics
1539 so they typically do not need to retain it themselves.
1541 This function creates the generic RGB color space once and hangs onto it so it can
1542 return it whenever this function is called.
1545 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
1547 static CGColorSpaceRef genericRGBColorSpace
= NULL
;
1549 if (genericRGBColorSpace
== NULL
)
1551 CMProfileRef genericRGBProfile
= wxMacOpenGenericProfile();
1553 if (genericRGBProfile
)
1555 genericRGBColorSpace
= CGColorSpaceCreateWithPlatformColorSpace(genericRGBProfile
);
1556 wxASSERT_MSG( genericRGBColorSpace
!= NULL
, wxT("couldn't create the generic RGB color space") ) ;
1558 // we opened the profile so it is up to us to close it
1559 CMCloseProfile(genericRGBProfile
);
1562 return genericRGBColorSpace
;