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/mac/uma.h"
30 # include "MoreFilesX.h"
32 # include "MoreFiles.h"
33 # include "MoreFilesExtras.h"
41 #include "ATSUnicode.h"
42 #include "TextCommon.h"
43 #include "TextEncodingConverter.h"
45 #if defined(__WXMAC__)
46 #include "wx/mac/private.h" // includes mac headers
49 #if defined(__MWERKS__) && wxUSE_UNICODE
55 // defined in unix/utilsunx.cpp for Mac OS X
57 // get full hostname (with domain name if possible)
58 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
60 return wxGetHostName(buf
, maxSize
);
63 // Get hostname only (without domain name)
64 bool wxGetHostName(wxChar
*buf
, int maxSize
)
66 // Gets Chooser name of user by examining a System resource.
68 const short kComputerNameID
= -16413;
70 short oldResFile
= CurResFile() ;
72 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
73 UseResFile(oldResFile
);
75 if (chooserName
&& *chooserName
)
77 HLock( (Handle
) chooserName
) ;
78 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
79 HUnlock( (Handle
) chooserName
) ;
80 ReleaseResource( (Handle
) chooserName
) ;
81 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
89 // Get user ID e.g. jacs
90 bool wxGetUserId(wxChar
*buf
, int maxSize
)
92 return wxGetUserName( buf
, maxSize
) ;
95 const wxChar
* wxGetHomeDir(wxString
*pstr
)
97 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
98 return pstr
->c_str() ;
101 // Get user name e.g. Stefan Csomor
102 bool wxGetUserName(wxChar
*buf
, int maxSize
)
104 // Gets Chooser name of user by examining a System resource.
106 const short kChooserNameID
= -16096;
108 short oldResFile
= CurResFile() ;
110 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
111 UseResFile(oldResFile
);
113 if (chooserName
&& *chooserName
)
115 HLock( (Handle
) chooserName
) ;
116 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
117 HUnlock( (Handle
) chooserName
) ;
118 ReleaseResource( (Handle
) chooserName
) ;
119 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
127 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
133 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
135 // TODO : under classic there is no environement support, under X yes
139 // set the env var name to the given value, return TRUE on success
140 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
142 // TODO : under classic there is no environement support, under X yes
147 // Execute a program in an Interactive Shell
149 bool wxShell(const wxString
& command
)
155 // Shutdown or reboot the PC
156 bool wxShutdown(wxShutdownFlags wFlags
)
162 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
163 long wxGetFreeMemory()
168 void wxUsleep(unsigned long milliseconds
)
170 clock_t start
= clock() ;
174 } while( clock() - start
< milliseconds
/ 1000.0 * CLOCKS_PER_SEC
) ;
177 void wxSleep(int nSecs
)
179 wxUsleep(1000*nSecs
);
182 // Consume all events until no more left
187 #endif // !__DARWIN__
195 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
199 // are there x-platform conventions ?
201 Gestalt(gestaltSystemVersion
, &theSystem
) ;
202 if (minorVsn
!= NULL
) {
203 *minorVsn
= (theSystem
& 0xFF ) ;
205 if (majorVsn
!= NULL
) {
206 *majorVsn
= (theSystem
>> 8 ) ;
215 // Reading and writing resources (eg WIN.INI, .Xdefaults)
217 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
223 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
226 buf
.Printf(wxT("%.4f"), value
);
228 return wxWriteResource(section
, entry
, buf
, file
);
231 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
234 buf
.Printf(wxT("%ld"), value
);
236 return wxWriteResource(section
, entry
, buf
, file
);
239 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
242 buf
.Printf(wxT("%d"), value
);
244 return wxWriteResource(section
, entry
, buf
, file
);
247 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
253 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
256 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
259 *value
= (float)strtod(s
, NULL
);
266 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
269 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
272 *value
= strtol(s
, NULL
, 10);
279 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
282 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
285 *value
= (int)strtol(s
, NULL
, 10);
291 #endif // wxUSE_RESOURCES
293 int gs_wxBusyCursorCount
= 0;
294 extern wxCursor gMacCurrentCursor
;
295 wxCursor gMacStoredActiveCursor
;
297 // Set the cursor to the busy cursor for all windows
298 void wxBeginBusyCursor(wxCursor
*cursor
)
300 if (gs_wxBusyCursorCount
++ == 0)
302 gMacStoredActiveCursor
= gMacCurrentCursor
;
303 cursor
->MacInstall() ;
305 //else: nothing to do, already set
308 // Restore cursor to normal
309 void wxEndBusyCursor()
311 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
312 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
314 if (--gs_wxBusyCursorCount
== 0)
316 gMacStoredActiveCursor
.MacInstall() ;
317 gMacStoredActiveCursor
= wxNullCursor
;
321 // TRUE if we're between the above two calls
324 return (gs_wxBusyCursorCount
> 0);
327 wxString
wxMacFindFolder( short vol
,
329 Boolean createFolder
)
335 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
338 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
340 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
347 wxChar
*wxGetUserHome (const wxString
& user
)
353 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
363 int pos
= p
.Find(':') ;
364 if ( pos
!= wxNOT_FOUND
) {
373 wxMacStringToPascal( p
, volumeName
) ;
374 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
375 if ( err
== noErr
) {
377 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
380 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
384 return err
== noErr
;
388 // Check whether this window wants to process messages, e.g. Stop button
389 // in long calculations.
390 bool wxCheckForInterrupt(wxWindow
*wnd
)
396 void wxGetMousePosition( int* x
, int* y
)
401 LocalToGlobal( &pt
) ;
406 // Return TRUE if we have a colour display
407 bool wxColourDisplay()
412 // Returns depth of screen
416 SetRect(&globRect
, -32760, -32760, 32760, 32760);
417 GDHandle theMaxDevice
;
420 theMaxDevice
= GetMaxDevice(&globRect
);
421 if (theMaxDevice
!= nil
)
422 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
427 // Get size of display
428 void wxDisplaySize(int *width
, int *height
)
431 GetQDGlobalsScreenBits( &screenBits
);
434 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
436 if (height
!= NULL
) {
437 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
441 void wxDisplaySizeMM(int *width
, int *height
)
443 wxDisplaySize(width
, height
);
444 // on mac 72 is fixed (at least now ;-)
445 float cvPt2Mm
= 25.4 / 72;
448 *width
= int( *width
* cvPt2Mm
);
450 if (height
!= NULL
) {
451 *height
= int( *height
* cvPt2Mm
);
455 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
458 GetQDGlobalsScreenBits( &screenBits
);
464 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
466 if (height
!= NULL
) {
467 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
472 GetThemeMenuBarHeight( &mheight
) ;
474 mheight
= LMGetMBarHeight() ;
476 if (height
!= NULL
) {
483 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
485 return wxGenericFindWindowAtPoint(pt
);
488 wxString
wxGetOsDescription()
490 #ifdef WXWIN_OS_DESCRIPTION
491 // use configure generated description if available
492 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
494 return wxT("MacOS") ; //TODO:define further
498 //---------------------------------------------------------------------------
499 // wxMac Specific utility functions
500 //---------------------------------------------------------------------------
502 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
503 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
504 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
505 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
506 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
507 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
509 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
510 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
511 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
512 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
513 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
514 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
516 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
521 for( int i
= 0 ; i
< len
; ++ i
)
523 c
= strchr( StringANSI
, *from
) ;
526 *to
= StringMac
[ c
- StringANSI
] ;
534 for( int i
= 0 ; i
< len
; ++ i
)
536 c
= strchr( StringANSI
, *from
) ;
539 *to
= StringMac
[ c
- StringANSI
] ;
551 void wxMacConvertToPC( const char *from
, char *to
, int len
)
556 for( int i
= 0 ; i
< len
; ++ i
)
558 c
= strchr( StringMac
, *from
) ;
561 *to
= StringANSI
[ c
- StringMac
] ;
569 for( int i
= 0 ; i
< len
; ++ i
)
571 c
= strchr( StringMac
, *from
) ;
574 *to
= StringANSI
[ c
- StringMac
] ;
586 TECObjectRef s_TECNativeCToUnicode
= NULL
;
587 TECObjectRef s_TECUnicodeToNativeC
= NULL
;
588 TECObjectRef s_TECPlatformToNativeC
= NULL
;
589 TECObjectRef s_TECNativeCToPlatform
= NULL
;
590 void wxMacSetupConverters()
592 // if we assume errors are happening here we need low level debugging
593 // since the high level assert will use the encoders that are not yet
595 const int kEncoding
= wxApp::s_macDefaultEncodingIsPC
596 ? (int)kTextEncodingWindowsLatin1
597 : (int)kTextEncodingMacRoman
;
599 OSStatus status
= noErr
;
600 status
= TECCreateConverter(&s_TECNativeCToUnicode
,
602 kTextEncodingUnicodeDefault
);
605 status
= TECCreateConverter(&s_TECUnicodeToNativeC
,
606 kTextEncodingUnicodeDefault
,
609 if ( wxApp::s_macDefaultEncodingIsPC
)
611 status
= TECCreateConverter(&s_TECPlatformToNativeC
,
612 kTextEncodingMacRoman
,
613 kTextEncodingWindowsLatin1
);
616 status
= TECCreateConverter(&s_TECNativeCToPlatform
,
617 kTextEncodingWindowsLatin1
,
618 kTextEncodingMacRoman
);
622 void wxMacCleanupConverters()
624 OSStatus status
= noErr
;
625 status
= TECDisposeConverter(s_TECNativeCToUnicode
);
627 status
= TECDisposeConverter(s_TECUnicodeToNativeC
);
629 status
= TECDisposeConverter(s_TECPlatformToNativeC
);
631 status
= TECDisposeConverter(s_TECNativeCToPlatform
);
634 wxWCharBuffer
wxMacStringToWString( const wxString
&from
)
637 wxWCharBuffer
result( from
.wc_str() ) ;
639 OSStatus status
= noErr
;
640 ByteCount byteOutLen
;
641 ByteCount byteInLen
= from
.Length() ;
642 ByteCount byteBufferLen
= byteInLen
*2 ;
643 wxWCharBuffer
result( from
.Length() ) ;
644 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
645 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
646 result
.data()[byteOutLen
/2] = 0 ;
651 wxString
wxMacMakeStringFromCString( const char * from
, int len
)
653 OSStatus status
= noErr
;
655 wxChar
* buf
= result
.GetWriteBuf( len
) ;
657 ByteCount byteOutLen
;
658 ByteCount byteInLen
= len
;
659 ByteCount byteBufferLen
= len
*2 ;
661 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
662 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
664 if ( !wxApp::s_macDefaultEncodingIsPC
)
665 memcpy( buf
, from
, len
) ;
668 ByteCount byteOutLen
;
669 ByteCount byteInLen
= len
;
670 ByteCount byteBufferLen
= byteInLen
;
672 status
= TECConvertText(s_TECPlatformToNativeC
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
673 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
677 result
.UngetWriteBuf() ;
681 wxString
wxMacMakeStringFromCString( const char * from
)
683 return wxMacMakeStringFromCString( from
, strlen(from
) ) ;
686 wxCharBuffer
wxMacStringToCString( const wxString
&from
)
689 OSStatus status
= noErr
;
690 ByteCount byteOutLen
;
691 ByteCount byteInLen
= from
.Length() * 2 ;
692 ByteCount byteBufferLen
= from
.Length() ;
693 wxCharBuffer
result( from
.Length() ) ;
694 status
= TECConvertText(s_TECUnicodeToNativeC
, (ConstTextPtr
)from
.wc_str() , byteInLen
, &byteInLen
,
695 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
698 if ( !wxApp::s_macDefaultEncodingIsPC
)
699 return wxCharBuffer( from
.c_str() ) ;
702 wxCharBuffer
result( from
.Length() ) ;
703 OSStatus status
= noErr
;
704 ByteCount byteOutLen
;
705 ByteCount byteInLen
= from
.Length() ;
706 ByteCount byteBufferLen
= byteInLen
;
708 status
= TECConvertText(s_TECNativeCToPlatform
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
709 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
715 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
717 wxCharBuffer buf
= wxMacStringToCString( from
) ;
718 int len
= strlen(buf
) ;
723 memcpy( (char*) &to
[1] , buf
, len
) ;
726 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
728 return wxMacMakeStringFromCString( (char*) &from
[1] , from
[0] ) ;
732 // CFStringRefs (Carbon only)
736 // converts this string into a carbon foundation string with optional pc 2 mac encoding
737 void wxMacCFStringHolder::Assign( const wxString
&str
)
740 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
741 (const unsigned short*)str
.wc_str(), str
.Len() );
743 m_cfs
= CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
744 wxApp::s_macDefaultEncodingIsPC
?
745 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
750 wxString
wxMacCFStringHolder::AsString()
753 Size len
= CFStringGetLength( m_cfs
) ;
754 wxChar
* buf
= result
.GetWriteBuf( len
) ;
756 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , len
) , (UniChar
*) buf
) ;
758 CFStringGetCString( m_cfs
, buf
, len
+1 , wxApp::s_macDefaultEncodingIsPC
?
759 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
762 result
.UngetWriteBuf() ;
768 wxString
wxMacMakeMacStringFromPC( const wxChar
* p
)
771 int len
= wxStrlen ( p
) ;
774 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
775 wxMacConvertFromPC( p
, ptr
, len
) ;
777 result
.UngetWriteBuf( len
) ;
782 wxString
wxMacMakePCStringFromMac( const wxChar
* p
)
785 int len
= wxStrlen ( p
) ;
788 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
789 wxMacConvertToPC( p
, ptr
, len
) ;
791 result
.UngetWriteBuf( len
) ;
796 wxString
wxMacMakeStringFromMacString( const wxChar
* from
, bool mac2pcEncoding
)
800 return wxMacMakePCStringFromMac( from
) ;
804 return wxString( from
) ;
812 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
, bool mac2pcEncoding
)
814 // this is safe since a pascal string can never be larger than 256 bytes
816 CopyPascalStringToC( from
, s
) ;
819 return wxMacMakePCStringFromMac( s
) ;
823 return wxString( s
) ;
827 void wxMacStringToPascal( const wxChar
* from
, StringPtr to
, bool pc2macEncoding
)
831 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
835 CopyCStringToPascal( from
, to
) ;
841 #endif //TARGET_CARBON
843 // ----------------------------------------------------------------------------
845 // ----------------------------------------------------------------------------
847 #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
849 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
851 #ifndef __MetroNubUtils__
852 #include "MetroNubUtils.h"
859 #if TARGET_API_MAC_CARBON
861 #include <CodeFragments.h>
864 CallUniversalProc(UniversalProcPtr theProcPtr
, ProcInfoType procInfo
, ...);
866 ProcPtr gCallUniversalProc_Proc
= NULL
;
870 static MetroNubUserEntryBlock
* gMetroNubEntry
= NULL
;
872 static long fRunOnce
= false;
874 /* ---------------------------------------------------------------------------
876 --------------------------------------------------------------------------- */
878 Boolean
IsMetroNubInstalled()
885 gMetroNubEntry
= NULL
;
887 if (Gestalt(gestaltSystemVersion
, &value
) == noErr
&& value
< 0x1000)
889 /* look for MetroNub's Gestalt selector */
890 if (Gestalt(kMetroNubUserSignature
, &result
) == noErr
)
893 #if TARGET_API_MAC_CARBON
894 if (gCallUniversalProc_Proc
== NULL
)
896 CFragConnectionID connectionID
;
899 ProcPtr symbolAddress
;
901 CFragSymbolClass symbolClass
;
903 symbolAddress
= NULL
;
904 err
= GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch
, kFindCFrag
,
905 &connectionID
, &mainAddress
, errorString
);
909 gCallUniversalProc_Proc
= NULL
;
913 err
= FindSymbol(connectionID
, "\pCallUniversalProc",
914 (Ptr
*) &gCallUniversalProc_Proc
, &symbolClass
);
918 gCallUniversalProc_Proc
= NULL
;
925 MetroNubUserEntryBlock
* block
= (MetroNubUserEntryBlock
*)result
;
927 /* make sure the version of the API is compatible */
928 if (block
->apiLowVersion
<= kMetroNubUserAPIVersion
&&
929 kMetroNubUserAPIVersion
<= block
->apiHiVersion
)
930 gMetroNubEntry
= block
; /* success! */
939 #if TARGET_API_MAC_CARBON
940 return (gMetroNubEntry
!= NULL
&& gCallUniversalProc_Proc
!= NULL
);
942 return (gMetroNubEntry
!= NULL
);
946 /* ---------------------------------------------------------------------------
947 IsMWDebuggerRunning [v1 API]
948 --------------------------------------------------------------------------- */
950 Boolean
IsMWDebuggerRunning()
952 if (IsMetroNubInstalled())
953 return CallIsDebuggerRunningProc(gMetroNubEntry
->isDebuggerRunning
);
958 /* ---------------------------------------------------------------------------
959 AmIBeingMWDebugged [v1 API]
960 --------------------------------------------------------------------------- */
962 Boolean
AmIBeingMWDebugged()
964 if (IsMetroNubInstalled())
965 return CallAmIBeingDebuggedProc(gMetroNubEntry
->amIBeingDebugged
);
970 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
972 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
977 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
982 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)