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"
46 // defined in unix/utilsunx.cpp for Mac OS X
48 // get full hostname (with domain name if possible)
49 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
51 return wxGetHostName(buf
, maxSize
);
54 // Get hostname only (without domain name)
55 bool wxGetHostName(wxChar
*buf
, int maxSize
)
57 // Gets Chooser name of user by examining a System resource.
59 const short kComputerNameID
= -16413;
61 short oldResFile
= CurResFile() ;
63 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
64 UseResFile(oldResFile
);
66 if (chooserName
&& *chooserName
)
68 HLock( (Handle
) chooserName
) ;
69 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
70 HUnlock( (Handle
) chooserName
) ;
71 ReleaseResource( (Handle
) chooserName
) ;
72 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
80 // Get user ID e.g. jacs
81 bool wxGetUserId(wxChar
*buf
, int maxSize
)
83 return wxGetUserName( buf
, maxSize
) ;
86 const wxChar
* wxGetHomeDir(wxString
*pstr
)
88 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
89 return pstr
->c_str() ;
92 // Get user name e.g. Stefan Csomor
93 bool wxGetUserName(wxChar
*buf
, int maxSize
)
95 // Gets Chooser name of user by examining a System resource.
97 const short kChooserNameID
= -16096;
99 short oldResFile
= CurResFile() ;
101 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
102 UseResFile(oldResFile
);
104 if (chooserName
&& *chooserName
)
106 HLock( (Handle
) chooserName
) ;
107 wxString name
= wxMacMakeStringFromPascal( *chooserName
) ;
108 HUnlock( (Handle
) chooserName
) ;
109 ReleaseResource( (Handle
) chooserName
) ;
110 wxStrncpy( buf
, name
, maxSize
- 1 ) ;
118 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
124 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
126 // TODO : under classic there is no environement support, under X yes
130 // set the env var name to the given value, return TRUE on success
131 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
133 // TODO : under classic there is no environement support, under X yes
138 // Execute a program in an Interactive Shell
140 bool wxShell(const wxString
& command
)
146 // Shutdown or reboot the PC
147 bool wxShutdown(wxShutdownFlags wFlags
)
153 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
154 long wxGetFreeMemory()
159 void wxUsleep(unsigned long milliseconds
)
161 clock_t start
= clock() ;
165 } while( clock() - start
< milliseconds
/ 1000.0 * CLOCKS_PER_SEC
) ;
168 void wxSleep(int nSecs
)
170 wxUsleep(1000*nSecs
);
173 // Consume all events until no more left
178 #if WXWIN_COMPATIBILITY_2_2
180 // Output a debug message, in a system dependent fashion.
181 void wxDebugMsg(const char *fmt
...)
184 static char buffer
[512];
186 if (!wxTheApp
->GetWantDebugOutput())
191 vsprintf(buffer
,fmt
,ap
) ;
192 strcat(buffer
,";g") ;
194 DebugStr((unsigned char*) buffer
) ;
199 // Non-fatal error: pop up message box and (possibly) continue
200 void wxError(const wxString
& msg
, const wxString
& title
)
202 if (wxMessageBox(wxString::Format(wxT("%s\nContinue?"),msg
), title
, wxYES_NO
) == wxID_NO
)
206 // Fatal error: pop up message box and abort
207 void wxFatalError(const wxString
& msg
, const wxString
& title
)
209 wxMessageBox(wxString::Format(wxT("%s: %s"),title
,msg
));
213 #endif // WXWIN_COMPATIBILITY_2_2
215 #endif // !__DARWIN__
223 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
227 // are there x-platform conventions ?
229 Gestalt(gestaltSystemVersion
, &theSystem
) ;
230 if (minorVsn
!= NULL
) {
231 *minorVsn
= (theSystem
& 0xFF ) ;
233 if (majorVsn
!= NULL
) {
234 *majorVsn
= (theSystem
>> 8 ) ;
243 // Reading and writing resources (eg WIN.INI, .Xdefaults)
245 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
251 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
254 buf
.Printf(wxT("%.4f"), value
);
256 return wxWriteResource(section
, entry
, buf
, file
);
259 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
262 buf
.Printf(wxT("%ld"), value
);
264 return wxWriteResource(section
, entry
, buf
, file
);
267 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
270 buf
.Printf(wxT("%d"), value
);
272 return wxWriteResource(section
, entry
, buf
, file
);
275 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
281 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
284 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
287 *value
= (float)strtod(s
, NULL
);
294 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
297 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
300 *value
= strtol(s
, NULL
, 10);
307 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
310 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
313 *value
= (int)strtol(s
, NULL
, 10);
319 #endif // wxUSE_RESOURCES
321 int gs_wxBusyCursorCount
= 0;
322 extern wxCursor gMacCurrentCursor
;
323 wxCursor gMacStoredActiveCursor
;
325 // Set the cursor to the busy cursor for all windows
326 void wxBeginBusyCursor(wxCursor
*cursor
)
328 if (gs_wxBusyCursorCount
++ == 0)
330 gMacStoredActiveCursor
= gMacCurrentCursor
;
331 cursor
->MacInstall() ;
333 //else: nothing to do, already set
336 // Restore cursor to normal
337 void wxEndBusyCursor()
339 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
340 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
342 if (--gs_wxBusyCursorCount
== 0)
344 gMacStoredActiveCursor
.MacInstall() ;
345 gMacStoredActiveCursor
= wxNullCursor
;
349 // TRUE if we're between the above two calls
352 return (gs_wxBusyCursorCount
> 0);
355 wxString
wxMacFindFolder( short vol
,
357 Boolean createFolder
)
363 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
366 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
368 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
375 wxChar
*wxGetUserHome (const wxString
& user
)
381 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
391 int pos
= p
.Find(':') ;
392 if ( pos
!= wxNOT_FOUND
) {
401 wxMacStringToPascal( p
, volumeName
) ;
402 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
403 if ( err
== noErr
) {
405 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
408 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
412 return err
== noErr
;
416 // Check whether this window wants to process messages, e.g. Stop button
417 // in long calculations.
418 bool wxCheckForInterrupt(wxWindow
*wnd
)
424 void wxGetMousePosition( int* x
, int* y
)
429 LocalToGlobal( &pt
) ;
434 // Return TRUE if we have a colour display
435 bool wxColourDisplay()
440 // Returns depth of screen
444 SetRect(&globRect
, -32760, -32760, 32760, 32760);
445 GDHandle theMaxDevice
;
448 theMaxDevice
= GetMaxDevice(&globRect
);
449 if (theMaxDevice
!= nil
)
450 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
455 // Get size of display
456 void wxDisplaySize(int *width
, int *height
)
459 GetQDGlobalsScreenBits( &screenBits
);
462 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
464 if (height
!= NULL
) {
465 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
469 void wxDisplaySizeMM(int *width
, int *height
)
471 wxDisplaySize(width
, height
);
472 // on mac 72 is fixed (at least now ;-)
473 float cvPt2Mm
= 25.4 / 72;
476 *width
= int( *width
* cvPt2Mm
);
478 if (height
!= NULL
) {
479 *height
= int( *height
* cvPt2Mm
);
483 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
486 GetQDGlobalsScreenBits( &screenBits
);
492 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
494 if (height
!= NULL
) {
495 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
500 GetThemeMenuBarHeight( &mheight
) ;
502 mheight
= LMGetMBarHeight() ;
504 if (height
!= NULL
) {
511 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
513 return wxGenericFindWindowAtPoint(pt
);
516 wxString
wxGetOsDescription()
518 #ifdef WXWIN_OS_DESCRIPTION
519 // use configure generated description if available
520 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
522 return wxT("MacOS") ; //TODO:define further
526 //---------------------------------------------------------------------------
527 // wxMac Specific utility functions
528 //---------------------------------------------------------------------------
530 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
531 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
532 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
533 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
534 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
535 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
537 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
538 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
539 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
540 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
541 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
542 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
544 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
549 for( int i
= 0 ; i
< len
; ++ i
)
551 c
= strchr( StringANSI
, *from
) ;
554 *to
= StringMac
[ c
- StringANSI
] ;
562 for( int i
= 0 ; i
< len
; ++ i
)
564 c
= strchr( StringANSI
, *from
) ;
567 *to
= StringMac
[ c
- StringANSI
] ;
579 void wxMacConvertToPC( const char *from
, char *to
, int len
)
584 for( int i
= 0 ; i
< len
; ++ i
)
586 c
= strchr( StringMac
, *from
) ;
589 *to
= StringANSI
[ c
- StringMac
] ;
597 for( int i
= 0 ; i
< len
; ++ i
)
599 c
= strchr( StringMac
, *from
) ;
602 *to
= StringANSI
[ c
- StringMac
] ;
614 TECObjectRef s_TECNativeCToUnicode
= NULL
;
615 TECObjectRef s_TECUnicodeToNativeC
= NULL
;
616 TECObjectRef s_TECPlatformToNativeC
= NULL
;
617 TECObjectRef s_TECNativeCToPlatform
= NULL
;
618 void wxMacSetupConverters()
620 // if we assume errors are happening here we need low level debugging since the high level assert will use the encoders that
621 // are not yet setup...
623 OSStatus status
= noErr
;
624 status
= TECCreateConverter(&s_TECNativeCToUnicode
,
625 wxApp::s_macDefaultEncodingIsPC
? kTextEncodingWindowsLatin1
: kTextEncodingMacRoman
, kTextEncodingUnicodeDefault
);
628 status
= TECCreateConverter(&s_TECUnicodeToNativeC
,
629 kTextEncodingUnicodeDefault
, wxApp::s_macDefaultEncodingIsPC
? kTextEncodingWindowsLatin1
: kTextEncodingMacRoman
);
631 if ( wxApp::s_macDefaultEncodingIsPC
)
633 status
= TECCreateConverter(&s_TECPlatformToNativeC
,
634 kTextEncodingMacRoman
, wxApp::s_macDefaultEncodingIsPC
? kTextEncodingWindowsLatin1
: kTextEncodingMacRoman
);
637 status
= TECCreateConverter(&s_TECNativeCToPlatform
,
638 wxApp::s_macDefaultEncodingIsPC
? kTextEncodingWindowsLatin1
: kTextEncodingMacRoman
, kTextEncodingMacRoman
);
642 void wxMacCleanupConverters()
644 OSStatus status
= noErr
;
645 status
= TECDisposeConverter(s_TECNativeCToUnicode
);
647 status
= TECDisposeConverter(s_TECUnicodeToNativeC
);
649 status
= TECDisposeConverter(s_TECPlatformToNativeC
);
651 status
= TECDisposeConverter(s_TECNativeCToPlatform
);
654 wxWCharBuffer
wxMacStringToWString( const wxString
&from
)
657 wxWCharBuffer
result( from
.wc_str() ) ;
659 OSStatus status
= noErr
;
660 ByteCount byteOutLen
;
661 ByteCount byteInLen
= from
.Length() ;
662 ByteCount byteBufferLen
= byteInLen
*2 ;
663 wxWCharBuffer
result( from
.Length() ) ;
664 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
665 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
666 result
.data()[byteOutLen
/2] = 0 ;
671 wxString
wxMacMakeStringFromCString( const char * from
, int len
)
673 OSStatus status
= noErr
;
675 wxChar
* buf
= result
.GetWriteBuf( len
) ;
677 ByteCount byteOutLen
;
678 ByteCount byteInLen
= len
;
679 ByteCount byteBufferLen
= len
*2 ;
681 status
= TECConvertText(s_TECNativeCToUnicode
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
682 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
684 if ( !wxApp::s_macDefaultEncodingIsPC
)
685 memcpy( buf
, from
, len
) ;
688 OSStatus status
= noErr
;
689 ByteCount byteOutLen
;
690 ByteCount byteInLen
= len
;
691 ByteCount byteBufferLen
= byteInLen
;
693 status
= TECConvertText(s_TECPlatformToNativeC
, (ConstTextPtr
)from
, byteInLen
, &byteInLen
,
694 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
698 result
.UngetWriteBuf() ;
702 wxString
wxMacMakeStringFromCString( const char * from
)
704 return wxMacMakeStringFromCString( from
, strlen(from
) ) ;
707 wxCharBuffer
wxMacStringToCString( const wxString
&from
)
710 OSStatus status
= noErr
;
711 ByteCount byteOutLen
;
712 ByteCount byteInLen
= from
.Length() * 2 ;
713 ByteCount byteBufferLen
= from
.Length() ;
714 wxCharBuffer
result( from
.Length() ) ;
715 status
= TECConvertText(s_TECUnicodeToNativeC
, (ConstTextPtr
)from
.wc_str() , byteInLen
, &byteInLen
,
716 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
719 if ( !wxApp::s_macDefaultEncodingIsPC
)
720 return wxCharBuffer( from
.c_str() ) ;
723 wxCharBuffer
result( from
.Length() ) ;
724 OSStatus status
= noErr
;
725 ByteCount byteOutLen
;
726 ByteCount byteInLen
= from
.Length() ;
727 ByteCount byteBufferLen
= byteInLen
;
729 status
= TECConvertText(s_TECNativeCToPlatform
, (ConstTextPtr
)from
.c_str() , byteInLen
, &byteInLen
,
730 (TextPtr
)result
.data(), byteBufferLen
, &byteOutLen
);
736 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
738 wxCharBuffer buf
= wxMacStringToCString( from
) ;
739 int len
= strlen(buf
) ;
744 memcpy( (char*) &to
[1] , buf
, len
) ;
747 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
749 return wxMacMakeStringFromCString( (char*) &from
[1] , from
[0] ) ;
753 // CFStringRefs (Carbon only)
757 // converts this string into a carbon foundation string with optional pc 2 mac encoding
758 void wxMacCFStringHolder::Assign( const wxString
&str
)
761 m_cfs
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
762 (const unsigned short*)str
.wc_str(), str
.Len() );
764 m_cfs
= CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
765 wxApp::s_macDefaultEncodingIsPC
?
766 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
771 wxString
wxMacCFStringHolder::AsString()
774 Size len
= CFStringGetLength( m_cfs
) ;
775 wxChar
* buf
= result
.GetWriteBuf( len
) ;
777 CFStringGetCharacters( m_cfs
, CFRangeMake( 0 , len
) , (UniChar
*) buf
) ;
779 CFStringGetCString( m_cfs
, buf
, len
+1 , wxApp::s_macDefaultEncodingIsPC
?
780 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
783 result
.UngetWriteBuf() ;
789 wxString
wxMacMakeMacStringFromPC( const wxChar
* p
)
792 int len
= wxStrlen ( p
) ;
795 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
796 wxMacConvertFromPC( p
, ptr
, len
) ;
798 result
.UngetWriteBuf( len
) ;
803 wxString
wxMacMakePCStringFromMac( const wxChar
* p
)
806 int len
= wxStrlen ( p
) ;
809 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
810 wxMacConvertToPC( p
, ptr
, len
) ;
812 result
.UngetWriteBuf( len
) ;
817 wxString
wxMacMakeStringFromMacString( const wxChar
* from
, bool mac2pcEncoding
)
821 return wxMacMakePCStringFromMac( from
) ;
825 return wxString( from
) ;
833 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
, bool mac2pcEncoding
)
835 // this is safe since a pascal string can never be larger than 256 bytes
837 CopyPascalStringToC( from
, s
) ;
840 return wxMacMakePCStringFromMac( s
) ;
844 return wxString( s
) ;
848 void wxMacStringToPascal( const wxChar
* from
, StringPtr to
, bool pc2macEncoding
)
852 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
856 CopyCStringToPascal( from
, to
) ;
862 #endif //TARGET_CARBON