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"
21 #include "wx/mac/uma.h"
31 # include "MoreFilesX.h"
33 # include "MoreFiles.h"
34 # include "MoreFilesExtras.h"
42 #include "ATSUnicode.h"
43 #include "TextCommon.h"
44 #include "TextEncodingConverter.h"
46 #if defined(__WXMAC__)
47 #include "wx/mac/private.h" // includes mac headers
50 #if defined(__MWERKS__) && wxUSE_UNICODE
56 // defined in unix/utilsunx.cpp for Mac OS X
58 // get full hostname (with domain name if possible)
59 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
61 return wxGetHostName(buf
, maxSize
);
64 // Get hostname only (without domain name)
65 bool wxGetHostName(wxChar
*buf
, int maxSize
)
67 // Gets Chooser name of user by examining a System resource.
69 const short kComputerNameID
= -16413;
71 short oldResFile
= CurResFile() ;
73 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
74 UseResFile(oldResFile
);
76 if (chooserName
&& *chooserName
)
78 HLock( (Handle
) chooserName
) ;
79 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
80 HUnlock( (Handle
) chooserName
) ;
81 ReleaseResource( (Handle
) chooserName
) ;
82 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
90 // Get user ID e.g. jacs
91 bool wxGetUserId(wxChar
*buf
, int maxSize
)
93 return wxGetUserName( buf
, maxSize
) ;
96 const wxChar
* wxGetHomeDir(wxString
*pstr
)
98 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
99 return pstr
->c_str() ;
102 // Get user name e.g. Stefan Csomor
103 bool wxGetUserName(wxChar
*buf
, int maxSize
)
105 // Gets Chooser name of user by examining a System resource.
107 const short kChooserNameID
= -16096;
109 short oldResFile
= CurResFile() ;
111 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
112 UseResFile(oldResFile
);
114 if (chooserName
&& *chooserName
)
116 HLock( (Handle
) chooserName
) ;
117 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
118 HUnlock( (Handle
) chooserName
) ;
119 ReleaseResource( (Handle
) chooserName
) ;
120 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
128 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
134 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
136 // TODO : under classic there is no environement support, under X yes
140 // set the env var name to the given value, return TRUE on success
141 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
143 // TODO : under classic there is no environement support, under X yes
148 // Execute a program in an Interactive Shell
150 bool wxShell(const wxString
& command
)
156 // Shutdown or reboot the PC
157 bool wxShutdown(wxShutdownFlags wFlags
)
163 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
164 long wxGetFreeMemory()
169 void wxUsleep(unsigned long milliseconds
)
171 clock_t start
= clock() ;
175 } while( clock() - start
< milliseconds
/ 1000.0 * CLOCKS_PER_SEC
) ;
178 void wxSleep(int nSecs
)
180 wxUsleep(1000*nSecs
);
183 // Consume all events until no more left
188 #endif // !__DARWIN__
196 int wxGUIAppTraits::GetOSVersion(int *majorVsn
, int *minorVsn
)
200 // are there x-platform conventions ?
202 Gestalt(gestaltSystemVersion
, &theSystem
) ;
203 if (minorVsn
!= NULL
) {
204 *minorVsn
= (theSystem
& 0xFF ) ;
206 if (majorVsn
!= NULL
) {
207 *majorVsn
= (theSystem
>> 8 ) ;
216 // Reading and writing resources (eg WIN.INI, .Xdefaults)
218 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
224 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
227 buf
.Printf(wxT("%.4f"), value
);
229 return wxWriteResource(section
, entry
, buf
, file
);
232 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
235 buf
.Printf(wxT("%ld"), value
);
237 return wxWriteResource(section
, entry
, buf
, file
);
240 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
243 buf
.Printf(wxT("%d"), value
);
245 return wxWriteResource(section
, entry
, buf
, file
);
248 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
254 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
257 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
260 *value
= (float)strtod(s
, NULL
);
267 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
270 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
273 *value
= strtol(s
, NULL
, 10);
280 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
283 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
286 *value
= (int)strtol(s
, NULL
, 10);
292 #endif // wxUSE_RESOURCES
294 int gs_wxBusyCursorCount
= 0;
295 extern wxCursor gMacCurrentCursor
;
296 wxCursor gMacStoredActiveCursor
;
298 // Set the cursor to the busy cursor for all windows
299 void wxBeginBusyCursor(wxCursor
*cursor
)
301 if (gs_wxBusyCursorCount
++ == 0)
303 gMacStoredActiveCursor
= gMacCurrentCursor
;
304 cursor
->MacInstall() ;
306 //else: nothing to do, already set
309 // Restore cursor to normal
310 void wxEndBusyCursor()
312 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
313 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
315 if (--gs_wxBusyCursorCount
== 0)
317 gMacStoredActiveCursor
.MacInstall() ;
318 gMacStoredActiveCursor
= wxNullCursor
;
322 // TRUE if we're between the above two calls
325 return (gs_wxBusyCursorCount
> 0);
328 wxString
wxMacFindFolder( short vol
,
330 Boolean createFolder
)
336 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
339 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
341 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
348 wxChar
*wxGetUserHome (const wxString
& user
)
354 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
364 int pos
= p
.Find(':') ;
365 if ( pos
!= wxNOT_FOUND
) {
374 wxMacStringToPascal( p
, volumeName
) ;
375 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
376 if ( err
== noErr
) {
378 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
381 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
385 return err
== noErr
;
389 // Check whether this window wants to process messages, e.g. Stop button
390 // in long calculations.
391 bool wxCheckForInterrupt(wxWindow
*wnd
)
397 void wxGetMousePosition( int* x
, int* y
)
402 LocalToGlobal( &pt
) ;
407 // Return TRUE if we have a colour display
408 bool wxColourDisplay()
413 // Returns depth of screen
417 SetRect(&globRect
, -32760, -32760, 32760, 32760);
418 GDHandle theMaxDevice
;
421 theMaxDevice
= GetMaxDevice(&globRect
);
422 if (theMaxDevice
!= nil
)
423 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
428 // Get size of display
429 void wxDisplaySize(int *width
, int *height
)
432 GetQDGlobalsScreenBits( &screenBits
);
435 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
437 if (height
!= NULL
) {
438 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
442 void wxDisplaySizeMM(int *width
, int *height
)
444 wxDisplaySize(width
, height
);
445 // on mac 72 is fixed (at least now ;-)
446 float cvPt2Mm
= 25.4 / 72;
449 *width
= int( *width
* cvPt2Mm
);
451 if (height
!= NULL
) {
452 *height
= int( *height
* cvPt2Mm
);
456 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
459 GetQDGlobalsScreenBits( &screenBits
);
465 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
467 if (height
!= NULL
) {
468 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
473 GetThemeMenuBarHeight( &mheight
) ;
475 mheight
= LMGetMBarHeight() ;
477 if (height
!= NULL
) {
484 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
486 return wxGenericFindWindowAtPoint(pt
);
489 wxString
wxGetOsDescription()
491 #ifdef WXWIN_OS_DESCRIPTION
492 // use configure generated description if available
493 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
495 return wxT("MacOS") ; //TODO:define further
499 //---------------------------------------------------------------------------
500 // wxMac Specific utility functions
501 //---------------------------------------------------------------------------
503 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
504 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
505 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
506 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
507 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
508 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
510 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
511 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
512 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
513 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
514 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
515 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
517 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
522 for( int i
= 0 ; i
< len
; ++ i
)
524 c
= strchr( StringANSI
, *from
) ;
527 *to
= StringMac
[ c
- StringANSI
] ;
535 for( int i
= 0 ; i
< len
; ++ i
)
537 c
= strchr( StringANSI
, *from
) ;
540 *to
= StringMac
[ c
- StringANSI
] ;
552 void wxMacConvertToPC( const char *from
, char *to
, int len
)
557 for( int i
= 0 ; i
< len
; ++ i
)
559 c
= strchr( StringMac
, *from
) ;
562 *to
= StringANSI
[ c
- StringMac
] ;
570 for( int i
= 0 ; i
< len
; ++ i
)
572 c
= strchr( StringMac
, *from
) ;
575 *to
= StringANSI
[ c
- StringMac
] ;
587 TECObjectRef s_TECNativeCToUnicode
= NULL
;
588 TECObjectRef s_TECUnicodeToNativeC
= NULL
;
589 TECObjectRef s_TECPlatformToNativeC
= NULL
;
590 TECObjectRef s_TECNativeCToPlatform
= NULL
;
591 void wxMacSetupConverters()
593 // if we assume errors are happening here we need low level debugging
594 // since the high level assert will use the encoders that are not yet
596 const int kEncoding
= wxApp::s_macDefaultEncodingIsPC
597 ? (int)kTextEncodingWindowsLatin1
598 : (int)kTextEncodingMacRoman
;
600 OSStatus status
= noErr
;
601 status
= TECCreateConverter(&s_TECNativeCToUnicode
,
603 kTextEncodingUnicodeDefault
);
606 status
= TECCreateConverter(&s_TECUnicodeToNativeC
,
607 kTextEncodingUnicodeDefault
,
610 if ( wxApp::s_macDefaultEncodingIsPC
)
612 status
= TECCreateConverter(&s_TECPlatformToNativeC
,
613 kTextEncodingMacRoman
,
614 kTextEncodingWindowsLatin1
);
617 status
= TECCreateConverter(&s_TECNativeCToPlatform
,
618 kTextEncodingWindowsLatin1
,
619 kTextEncodingMacRoman
);
623 void wxMacCleanupConverters()
625 OSStatus status
= noErr
;
626 status
= TECDisposeConverter(s_TECNativeCToUnicode
);
628 status
= TECDisposeConverter(s_TECUnicodeToNativeC
);
630 status
= TECDisposeConverter(s_TECPlatformToNativeC
);
632 status
= TECDisposeConverter(s_TECNativeCToPlatform
);
635 wxWCharBuffer
wxMacStringToWString( const wxString
&from
)
638 wxWCharBuffer
result( from
.wc_str() ) ;
640 OSStatus status
= noErr
;
641 ByteCount byteOutLen
;
642 ByteCount byteInLen
= from
.Length() ;
643 ByteCount byteBufferLen
= byteInLen
*2 ;
644 wxWCharBuffer
result( from
.Length() ) ;
645 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
646 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
647 result
.data()[byteOutLen
/2] = 0 ;
652 wxString
wxMacMakeStringFromCString( const char * from
, int len
)
654 OSStatus status
= noErr
;
656 wxChar
* buf
= result
.GetWriteBuf( len
) ;
658 ByteCount byteOutLen
;
659 ByteCount byteInLen
= len
;
660 ByteCount byteBufferLen
= len
*2 ;
662 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
663 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
665 if ( !wxApp::s_macDefaultEncodingIsPC
)
666 memcpy( buf
, from
, len
) ;
669 ByteCount byteOutLen
;
670 ByteCount byteInLen
= len
;
671 ByteCount byteBufferLen
= byteInLen
;
673 status
= TECConvertText(s_TECPlatformToNativeC
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
674 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
678 result
.UngetWriteBuf() ;
682 wxString
wxMacMakeStringFromCString( const char * from
)
684 return wxMacMakeStringFromCString( from
, strlen(from
) ) ;
687 wxCharBuffer
wxMacStringToCString( const wxString
&from
)
690 OSStatus status
= noErr
;
691 ByteCount byteOutLen
;
692 ByteCount byteInLen
= from
.Length() * 2 ;
693 ByteCount byteBufferLen
= from
.Length() ;
694 wxCharBuffer
result( from
.Length() ) ;
695 status
= TECConvertText(s_TECUnicodeToNativeC
, (ConstTextPtr
)from
.wc_str() , byteInLen
, &byteInLen
,
696 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
699 if ( !wxApp::s_macDefaultEncodingIsPC
)
700 return wxCharBuffer( from
.c_str() ) ;
703 wxCharBuffer
result( from
.Length() ) ;
704 OSStatus status
= noErr
;
705 ByteCount byteOutLen
;
706 ByteCount byteInLen
= from
.Length() ;
707 ByteCount byteBufferLen
= byteInLen
;
709 status
= TECConvertText(s_TECNativeCToPlatform
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
710 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
716 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
718 wxCharBuffer buf
= wxMacStringToCString( from
) ;
719 int len
= strlen(buf
) ;
724 memcpy( (char*) &to
[1] , buf
, len
) ;
727 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
729 return wxMacMakeStringFromCString( (char*) &from
[1] , from
[0] ) ;
733 // CFStringRefs (Carbon only)
737 // converts this string into a carbon foundation string with optional pc 2 mac encoding
738 void wxMacCFStringHolder::Assign( const wxString
&str
)
741 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
742 (const unsigned short*)str
.wc_str(), str
.Len() );
744 m_cfs
= CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
745 wxApp::s_macDefaultEncodingIsPC
?
746 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
751 wxString
wxMacCFStringHolder::AsString()
754 Size len
= CFStringGetLength( m_cfs
) ;
755 wxChar
* buf
= result
.GetWriteBuf( len
) ;
757 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , len
) , (UniChar
*) buf
) ;
759 CFStringGetCString( m_cfs
, buf
, len
+1 , wxApp::s_macDefaultEncodingIsPC
?
760 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
763 result
.UngetWriteBuf() ;
769 wxString
wxMacMakeMacStringFromPC( const wxChar
* p
)
772 int len
= wxStrlen ( p
) ;
775 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
776 wxMacConvertFromPC( p
, ptr
, len
) ;
778 result
.UngetWriteBuf( len
) ;
783 wxString
wxMacMakePCStringFromMac( const wxChar
* p
)
786 int len
= wxStrlen ( p
) ;
789 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
790 wxMacConvertToPC( p
, ptr
, len
) ;
792 result
.UngetWriteBuf( len
) ;
797 wxString
wxMacMakeStringFromMacString( const wxChar
* from
, bool mac2pcEncoding
)
801 return wxMacMakePCStringFromMac( from
) ;
805 return wxString( from
) ;
813 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
, bool mac2pcEncoding
)
815 // this is safe since a pascal string can never be larger than 256 bytes
817 CopyPascalStringToC( from
, s
) ;
820 return wxMacMakePCStringFromMac( s
) ;
824 return wxString( s
) ;
828 void wxMacStringToPascal( const wxChar
* from
, StringPtr to
, bool pc2macEncoding
)
832 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
836 CopyCStringToPascal( from
, to
) ;
842 #endif //TARGET_CARBON
844 // ----------------------------------------------------------------------------
846 // ----------------------------------------------------------------------------
848 #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
850 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
852 #ifndef __MetroNubUtils__
853 #include "MetroNubUtils.h"
860 #if TARGET_API_MAC_CARBON
862 #include <CodeFragments.h>
865 CallUniversalProc(UniversalProcPtr theProcPtr
, ProcInfoType procInfo
, ...);
867 ProcPtr gCallUniversalProc_Proc
= NULL
;
871 static MetroNubUserEntryBlock
* gMetroNubEntry
= NULL
;
873 static long fRunOnce
= false;
875 /* ---------------------------------------------------------------------------
877 --------------------------------------------------------------------------- */
879 Boolean
IsMetroNubInstalled()
886 gMetroNubEntry
= NULL
;
888 if (Gestalt(gestaltSystemVersion
, &value
) == noErr
&& value
< 0x1000)
890 /* look for MetroNub's Gestalt selector */
891 if (Gestalt(kMetroNubUserSignature
, &result
) == noErr
)
894 #if TARGET_API_MAC_CARBON
895 if (gCallUniversalProc_Proc
== NULL
)
897 CFragConnectionID connectionID
;
900 ProcPtr symbolAddress
;
902 CFragSymbolClass symbolClass
;
904 symbolAddress
= NULL
;
905 err
= GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch
, kFindCFrag
,
906 &connectionID
, &mainAddress
, errorString
);
910 gCallUniversalProc_Proc
= NULL
;
914 err
= FindSymbol(connectionID
, "\pCallUniversalProc",
915 (Ptr
*) &gCallUniversalProc_Proc
, &symbolClass
);
919 gCallUniversalProc_Proc
= NULL
;
926 MetroNubUserEntryBlock
* block
= (MetroNubUserEntryBlock
*)result
;
928 /* make sure the version of the API is compatible */
929 if (block
->apiLowVersion
<= kMetroNubUserAPIVersion
&&
930 kMetroNubUserAPIVersion
<= block
->apiHiVersion
)
931 gMetroNubEntry
= block
; /* success! */
940 #if TARGET_API_MAC_CARBON
941 return (gMetroNubEntry
!= NULL
&& gCallUniversalProc_Proc
!= NULL
);
943 return (gMetroNubEntry
!= NULL
);
947 /* ---------------------------------------------------------------------------
948 IsMWDebuggerRunning [v1 API]
949 --------------------------------------------------------------------------- */
951 Boolean
IsMWDebuggerRunning()
953 if (IsMetroNubInstalled())
954 return CallIsDebuggerRunningProc(gMetroNubEntry
->isDebuggerRunning
);
959 /* ---------------------------------------------------------------------------
960 AmIBeingMWDebugged [v1 API]
961 --------------------------------------------------------------------------- */
963 Boolean
AmIBeingMWDebugged()
965 if (IsMetroNubInstalled())
966 return CallAmIBeingDebuggedProc(gMetroNubEntry
->amIBeingDebugged
);
971 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
973 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
978 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
983 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)