1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // Note: this is done in utilscmn.cpp now.
14 // #pragma implementation "utils.h"
20 #include "wx/apptrait.h"
23 #include "wx/mac/uma.h"
34 # include "MoreFilesX.h"
36 # include "MoreFiles.h"
37 # include "MoreFilesExtras.h"
45 #include "ATSUnicode.h"
46 #include "TextCommon.h"
47 #include "TextEncodingConverter.h"
49 #if defined(__WXMAC__)
50 #include "wx/mac/private.h" // includes mac headers
53 #if defined(__MWERKS__) && wxUSE_UNICODE
60 // defined in unix/utilsunx.cpp for Mac OS X
62 // get full hostname (with domain name if possible)
63 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
65 return wxGetHostName(buf
, maxSize
);
68 // Get hostname only (without domain name)
69 bool wxGetHostName(wxChar
*buf
, int maxSize
)
71 // Gets Chooser name of user by examining a System resource.
73 const short kComputerNameID
= -16413;
75 short oldResFile
= CurResFile() ;
77 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
78 UseResFile(oldResFile
);
80 if (chooserName
&& *chooserName
)
82 HLock( (Handle
) chooserName
) ;
83 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
84 HUnlock( (Handle
) chooserName
) ;
85 ReleaseResource( (Handle
) chooserName
) ;
86 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
94 // Get user ID e.g. jacs
95 bool wxGetUserId(wxChar
*buf
, int maxSize
)
97 return wxGetUserName( buf
, maxSize
) ;
100 const wxChar
* wxGetHomeDir(wxString
*pstr
)
102 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
103 return pstr
->c_str() ;
106 // Get user name e.g. Stefan Csomor
107 bool wxGetUserName(wxChar
*buf
, int maxSize
)
109 // Gets Chooser name of user by examining a System resource.
111 const short kChooserNameID
= -16096;
113 short oldResFile
= CurResFile() ;
115 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
116 UseResFile(oldResFile
);
118 if (chooserName
&& *chooserName
)
120 HLock( (Handle
) chooserName
) ;
121 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
122 HUnlock( (Handle
) chooserName
) ;
123 ReleaseResource( (Handle
) chooserName
) ;
124 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
132 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
138 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
140 // TODO : under classic there is no environement support, under X yes
144 // set the env var name to the given value, return TRUE on success
145 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
147 // TODO : under classic there is no environement support, under X yes
152 // Execute a program in an Interactive Shell
154 bool wxShell(const wxString
& command
)
160 // Shutdown or reboot the PC
161 bool wxShutdown(wxShutdownFlags wFlags
)
167 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
168 long wxGetFreeMemory()
173 void wxUsleep(unsigned long milliseconds
)
175 clock_t start
= clock() ;
179 } while( clock() - start
< milliseconds
/ 1000.0 * CLOCKS_PER_SEC
) ;
182 void wxSleep(int nSecs
)
184 wxUsleep(1000*nSecs
);
187 // Consume all events until no more left
192 #endif // !__DARWIN__
200 int wxAppTraits::GetOSVersion(int *majorVsn
, int *minorVsn
)
204 // are there x-platform conventions ?
206 Gestalt(gestaltSystemVersion
, &theSystem
) ;
207 if (minorVsn
!= NULL
) {
208 *minorVsn
= (theSystem
& 0xFF ) ;
210 if (majorVsn
!= NULL
) {
211 *majorVsn
= (theSystem
>> 8 ) ;
224 // Reading and writing resources (eg WIN.INI, .Xdefaults)
226 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
232 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
235 buf
.Printf(wxT("%.4f"), value
);
237 return wxWriteResource(section
, entry
, buf
, file
);
240 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
243 buf
.Printf(wxT("%ld"), value
);
245 return wxWriteResource(section
, entry
, buf
, file
);
248 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
251 buf
.Printf(wxT("%d"), value
);
253 return wxWriteResource(section
, entry
, buf
, file
);
256 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
262 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
265 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
268 *value
= (float)strtod(s
, NULL
);
275 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
278 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
281 *value
= strtol(s
, NULL
, 10);
288 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
291 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
294 *value
= (int)strtol(s
, NULL
, 10);
300 #endif // wxUSE_RESOURCES
302 int gs_wxBusyCursorCount
= 0;
303 extern wxCursor gMacCurrentCursor
;
304 wxCursor gMacStoredActiveCursor
;
306 // Set the cursor to the busy cursor for all windows
307 void wxBeginBusyCursor(wxCursor
*cursor
)
309 if (gs_wxBusyCursorCount
++ == 0)
311 gMacStoredActiveCursor
= gMacCurrentCursor
;
312 cursor
->MacInstall() ;
314 //else: nothing to do, already set
317 // Restore cursor to normal
318 void wxEndBusyCursor()
320 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
321 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
323 if (--gs_wxBusyCursorCount
== 0)
325 gMacStoredActiveCursor
.MacInstall() ;
326 gMacStoredActiveCursor
= wxNullCursor
;
330 // TRUE if we're between the above two calls
333 return (gs_wxBusyCursorCount
> 0);
336 wxString
wxMacFindFolder( short vol
,
338 Boolean createFolder
)
344 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
347 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
349 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
355 // Check whether this window wants to process messages, e.g. Stop button
356 // in long calculations.
357 bool wxCheckForInterrupt(wxWindow
*wnd
)
363 void wxGetMousePosition( int* x
, int* y
)
368 LocalToGlobal( &pt
) ;
373 // Return TRUE if we have a colour display
374 bool wxColourDisplay()
379 // Returns depth of screen
383 SetRect(&globRect
, -32760, -32760, 32760, 32760);
384 GDHandle theMaxDevice
;
387 theMaxDevice
= GetMaxDevice(&globRect
);
388 if (theMaxDevice
!= nil
)
389 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
394 // Get size of display
395 void wxDisplaySize(int *width
, int *height
)
398 GetQDGlobalsScreenBits( &screenBits
);
401 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
403 if (height
!= NULL
) {
404 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
408 void wxDisplaySizeMM(int *width
, int *height
)
410 wxDisplaySize(width
, height
);
411 // on mac 72 is fixed (at least now ;-)
412 float cvPt2Mm
= 25.4 / 72;
415 *width
= int( *width
* cvPt2Mm
);
417 if (height
!= NULL
) {
418 *height
= int( *height
* cvPt2Mm
);
422 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
425 GetQDGlobalsScreenBits( &screenBits
);
431 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
433 if (height
!= NULL
) {
434 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
439 GetThemeMenuBarHeight( &mheight
) ;
441 mheight
= LMGetMBarHeight() ;
443 if (height
!= NULL
) {
450 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
452 return wxGenericFindWindowAtPoint(pt
);
459 wxString
wxGetOsDescription()
461 #ifdef WXWIN_OS_DESCRIPTION
462 // use configure generated description if available
463 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
465 return wxT("MacOS") ; //TODO:define further
470 wxChar
*wxGetUserHome (const wxString
& user
)
476 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
486 int pos
= p
.Find(':') ;
487 if ( pos
!= wxNOT_FOUND
) {
496 wxMacStringToPascal( p
, volumeName
) ;
497 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
498 if ( err
== noErr
) {
500 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
503 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
507 return err
== noErr
;
509 #endif // !__DARWIN__
515 //---------------------------------------------------------------------------
516 // wxMac Specific utility functions
517 //---------------------------------------------------------------------------
519 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
520 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
521 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
522 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
523 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
524 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
526 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
527 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
528 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
529 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
530 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
531 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
533 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
538 for( int i
= 0 ; i
< len
; ++ i
)
540 c
= strchr( StringANSI
, *from
) ;
543 *to
= StringMac
[ c
- StringANSI
] ;
551 for( int i
= 0 ; i
< len
; ++ i
)
553 c
= strchr( StringANSI
, *from
) ;
556 *to
= StringMac
[ c
- StringANSI
] ;
568 void wxMacConvertToPC( const char *from
, char *to
, int len
)
573 for( int i
= 0 ; i
< len
; ++ i
)
575 c
= strchr( StringMac
, *from
) ;
578 *to
= StringANSI
[ c
- StringMac
] ;
586 for( int i
= 0 ; i
< len
; ++ i
)
588 c
= strchr( StringMac
, *from
) ;
591 *to
= StringANSI
[ c
- StringMac
] ;
603 TECObjectRef s_TECNativeCToUnicode
= NULL
;
604 TECObjectRef s_TECUnicodeToNativeC
= NULL
;
605 TECObjectRef s_TECPlatformToNativeC
= NULL
;
606 TECObjectRef s_TECNativeCToPlatform
= NULL
;
607 void wxMacSetupConverters()
609 // if we assume errors are happening here we need low level debugging
610 // since the high level assert will use the encoders that are not yet
612 const int kEncoding
= wxApp::s_macDefaultEncodingIsPC
613 ? (int)kTextEncodingWindowsLatin1
614 : (int)kTextEncodingMacRoman
;
616 OSStatus status
= noErr
;
617 status
= TECCreateConverter(&s_TECNativeCToUnicode
,
619 kTextEncodingUnicodeDefault
);
622 status
= TECCreateConverter(&s_TECUnicodeToNativeC
,
623 kTextEncodingUnicodeDefault
,
626 if ( wxApp::s_macDefaultEncodingIsPC
)
628 status
= TECCreateConverter(&s_TECPlatformToNativeC
,
629 kTextEncodingMacRoman
,
630 kTextEncodingWindowsLatin1
);
633 status
= TECCreateConverter(&s_TECNativeCToPlatform
,
634 kTextEncodingWindowsLatin1
,
635 kTextEncodingMacRoman
);
639 void wxMacCleanupConverters()
641 OSStatus status
= noErr
;
642 status
= TECDisposeConverter(s_TECNativeCToUnicode
);
644 status
= TECDisposeConverter(s_TECUnicodeToNativeC
);
646 status
= TECDisposeConverter(s_TECPlatformToNativeC
);
648 status
= TECDisposeConverter(s_TECNativeCToPlatform
);
651 wxWCharBuffer
wxMacStringToWString( const wxString
&from
)
654 wxWCharBuffer
result( from
.wc_str() ) ;
656 OSStatus status
= noErr
;
657 ByteCount byteOutLen
;
658 ByteCount byteInLen
= from
.Length() ;
659 ByteCount byteBufferLen
= byteInLen
*2 ;
660 wxWCharBuffer
result( from
.Length() ) ;
661 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
662 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
663 result
.data()[byteOutLen
/2] = 0 ;
668 wxString
wxMacMakeStringFromCString( const char * from
, int len
)
670 OSStatus status
= noErr
;
672 wxChar
* buf
= result
.GetWriteBuf( len
) ;
674 ByteCount byteOutLen
;
675 ByteCount byteInLen
= len
;
676 ByteCount byteBufferLen
= len
*2 ;
678 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
679 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
681 if ( !wxApp::s_macDefaultEncodingIsPC
)
682 memcpy( buf
, from
, len
) ;
685 ByteCount byteOutLen
;
686 ByteCount byteInLen
= len
;
687 ByteCount byteBufferLen
= byteInLen
;
689 status
= TECConvertText(s_TECPlatformToNativeC
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
690 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
694 result
.UngetWriteBuf() ;
698 wxString
wxMacMakeStringFromCString( const char * from
)
700 return wxMacMakeStringFromCString( from
, strlen(from
) ) ;
703 wxCharBuffer
wxMacStringToCString( const wxString
&from
)
706 OSStatus status
= noErr
;
707 ByteCount byteOutLen
;
708 ByteCount byteInLen
= from
.Length() * 2 ;
709 ByteCount byteBufferLen
= from
.Length() ;
710 wxCharBuffer
result( from
.Length() ) ;
711 status
= TECConvertText(s_TECUnicodeToNativeC
, (ConstTextPtr
)from
.wc_str() , byteInLen
, &byteInLen
,
712 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
715 if ( !wxApp::s_macDefaultEncodingIsPC
)
716 return wxCharBuffer( from
.c_str() ) ;
719 wxCharBuffer
result( from
.Length() ) ;
720 OSStatus status
= noErr
;
721 ByteCount byteOutLen
;
722 ByteCount byteInLen
= from
.Length() ;
723 ByteCount byteBufferLen
= byteInLen
;
725 status
= TECConvertText(s_TECNativeCToPlatform
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
726 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
732 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
734 wxCharBuffer buf
= wxMacStringToCString( from
) ;
735 int len
= strlen(buf
) ;
740 memcpy( (char*) &to
[1] , buf
, len
) ;
743 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
745 return wxMacMakeStringFromCString( (char*) &from
[1] , from
[0] ) ;
749 // CFStringRefs (Carbon only)
753 // converts this string into a carbon foundation string with optional pc 2 mac encoding
754 void wxMacCFStringHolder::Assign( const wxString
&str
)
757 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
758 (const unsigned short*)str
.wc_str(), str
.Len() );
760 m_cfs
= CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
761 wxApp::s_macDefaultEncodingIsPC
?
762 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
767 wxString
wxMacCFStringHolder::AsString()
770 Size len
= CFStringGetLength( m_cfs
) ;
771 wxChar
* buf
= result
.GetWriteBuf( len
) ;
773 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , len
) , (UniChar
*) buf
) ;
775 CFStringGetCString( m_cfs
, buf
, len
+1 , wxApp::s_macDefaultEncodingIsPC
?
776 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
779 result
.UngetWriteBuf() ;
785 wxString
wxMacMakeMacStringFromPC( const wxChar
* p
)
788 int len
= wxStrlen ( p
) ;
791 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
792 wxMacConvertFromPC( p
, ptr
, len
) ;
794 result
.UngetWriteBuf( len
) ;
799 wxString
wxMacMakePCStringFromMac( const wxChar
* p
)
802 int len
= wxStrlen ( p
) ;
805 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
806 wxMacConvertToPC( p
, ptr
, len
) ;
808 result
.UngetWriteBuf( len
) ;
813 wxString
wxMacMakeStringFromMacString( const wxChar
* from
, bool mac2pcEncoding
)
817 return wxMacMakePCStringFromMac( from
) ;
821 return wxString( from
) ;
829 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
, bool mac2pcEncoding
)
831 // this is safe since a pascal string can never be larger than 256 bytes
833 CopyPascalStringToC( from
, s
) ;
836 return wxMacMakePCStringFromMac( s
) ;
840 return wxString( s
) ;
844 void wxMacStringToPascal( const wxChar
* from
, StringPtr to
, bool pc2macEncoding
)
848 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
852 CopyCStringToPascal( from
, to
) ;
858 #endif //TARGET_CARBON
860 // ----------------------------------------------------------------------------
862 // ----------------------------------------------------------------------------
864 #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
866 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
868 #ifndef __MetroNubUtils__
869 #include "MetroNubUtils.h"
876 #if TARGET_API_MAC_CARBON
878 #include <CodeFragments.h>
880 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr
, ProcInfoType procInfo
, ...);
882 ProcPtr gCallUniversalProc_Proc
= NULL
;
886 static MetroNubUserEntryBlock
* gMetroNubEntry
= NULL
;
888 static long fRunOnce
= false;
890 /* ---------------------------------------------------------------------------
892 --------------------------------------------------------------------------- */
894 Boolean
IsMetroNubInstalled()
901 gMetroNubEntry
= NULL
;
903 if (Gestalt(gestaltSystemVersion
, &value
) == noErr
&& value
< 0x1000)
905 /* look for MetroNub's Gestalt selector */
906 if (Gestalt(kMetroNubUserSignature
, &result
) == noErr
)
909 #if TARGET_API_MAC_CARBON
910 if (gCallUniversalProc_Proc
== NULL
)
912 CFragConnectionID connectionID
;
915 ProcPtr symbolAddress
;
917 CFragSymbolClass symbolClass
;
919 symbolAddress
= NULL
;
920 err
= GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch
, kFindCFrag
,
921 &connectionID
, &mainAddress
, errorString
);
925 gCallUniversalProc_Proc
= NULL
;
929 err
= FindSymbol(connectionID
, "\pCallUniversalProc",
930 (Ptr
*) &gCallUniversalProc_Proc
, &symbolClass
);
934 gCallUniversalProc_Proc
= NULL
;
941 MetroNubUserEntryBlock
* block
= (MetroNubUserEntryBlock
*)result
;
943 /* make sure the version of the API is compatible */
944 if (block
->apiLowVersion
<= kMetroNubUserAPIVersion
&&
945 kMetroNubUserAPIVersion
<= block
->apiHiVersion
)
946 gMetroNubEntry
= block
; /* success! */
955 #if TARGET_API_MAC_CARBON
956 return (gMetroNubEntry
!= NULL
&& gCallUniversalProc_Proc
!= NULL
);
958 return (gMetroNubEntry
!= NULL
);
962 /* ---------------------------------------------------------------------------
963 IsMWDebuggerRunning [v1 API]
964 --------------------------------------------------------------------------- */
966 Boolean
IsMWDebuggerRunning()
968 if (IsMetroNubInstalled())
969 return CallIsDebuggerRunningProc(gMetroNubEntry
->isDebuggerRunning
);
974 /* ---------------------------------------------------------------------------
975 AmIBeingMWDebugged [v1 API]
976 --------------------------------------------------------------------------- */
978 Boolean
AmIBeingMWDebugged()
980 if (IsMetroNubInstalled())
981 return CallAmIBeingDebuggedProc(gMetroNubEntry
->amIBeingDebugged
);
986 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
988 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
993 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
998 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)