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"
42 // defined in unix/utilsunx.cpp for Mac OS X
44 // get full hostname (with domain name if possible)
45 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
47 return wxGetHostName(buf
, maxSize
);
50 // Get hostname only (without domain name)
51 bool wxGetHostName(char *buf
, int maxSize
)
53 // Gets Chooser name of user by examining a System resource.
55 const short kComputerNameID
= -16413;
57 short oldResFile
= CurResFile() ;
59 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
60 UseResFile(oldResFile
);
62 if (chooserName
&& *chooserName
)
64 int length
= (*chooserName
)[0] ;
65 if ( length
+ 1 > maxSize
)
67 length
= maxSize
- 1 ;
69 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
78 // Get user ID e.g. jacs
79 bool wxGetUserId(char *buf
, int maxSize
)
81 return wxGetUserName( buf
, maxSize
) ;
84 const wxChar
* wxGetHomeDir(wxString
*pstr
)
86 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
87 return pstr
->c_str() ;
90 // Get user name e.g. Stefan Csomor
91 bool wxGetUserName(char *buf
, int maxSize
)
93 // Gets Chooser name of user by examining a System resource.
95 const short kChooserNameID
= -16096;
97 short oldResFile
= CurResFile() ;
99 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
100 UseResFile(oldResFile
);
102 if (chooserName
&& *chooserName
)
104 int length
= (*chooserName
)[0] ;
105 if ( length
+ 1 > maxSize
)
107 length
= maxSize
- 1 ;
109 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
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 sprintf(buf
, "%.4f", value
);
255 return wxWriteResource(section
, entry
, buf
, file
);
258 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
261 sprintf(buf
, "%ld", value
);
262 return wxWriteResource(section
, entry
, buf
, file
);
265 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
268 sprintf(buf
, "%d", value
);
269 return wxWriteResource(section
, entry
, buf
, file
);
272 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
278 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
281 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
284 *value
= (float)strtod(s
, NULL
);
291 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
294 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
297 *value
= strtol(s
, NULL
, 10);
304 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
307 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
310 *value
= (int)strtol(s
, NULL
, 10);
316 #endif // wxUSE_RESOURCES
318 int gs_wxBusyCursorCount
= 0;
319 extern wxCursor gMacCurrentCursor
;
320 wxCursor gMacStoredActiveCursor
;
322 // Set the cursor to the busy cursor for all windows
323 void wxBeginBusyCursor(wxCursor
*cursor
)
325 if (gs_wxBusyCursorCount
++ == 0)
327 gMacStoredActiveCursor
= gMacCurrentCursor
;
328 cursor
->MacInstall() ;
330 //else: nothing to do, already set
333 // Restore cursor to normal
334 void wxEndBusyCursor()
336 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
337 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
339 if (--gs_wxBusyCursorCount
== 0)
341 gMacStoredActiveCursor
.MacInstall() ;
342 gMacStoredActiveCursor
= wxNullCursor
;
346 // TRUE if we're between the above two calls
349 return (gs_wxBusyCursorCount
> 0);
352 wxString
wxMacFindFolder( short vol
,
354 Boolean createFolder
)
360 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
363 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
365 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
372 char *wxGetUserHome (const wxString
& user
)
378 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
388 int pos
= p
.Find(':') ;
389 if ( pos
!= wxNOT_FOUND
) {
398 wxMacStringToPascal( p
, volumeName
) ;
399 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
400 if ( err
== noErr
) {
402 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
405 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
409 return err
== noErr
;
413 // Check whether this window wants to process messages, e.g. Stop button
414 // in long calculations.
415 bool wxCheckForInterrupt(wxWindow
*wnd
)
421 void wxGetMousePosition( int* x
, int* y
)
426 LocalToGlobal( &pt
) ;
431 // Return TRUE if we have a colour display
432 bool wxColourDisplay()
437 // Returns depth of screen
441 SetRect(&globRect
, -32760, -32760, 32760, 32760);
442 GDHandle theMaxDevice
;
445 theMaxDevice
= GetMaxDevice(&globRect
);
446 if (theMaxDevice
!= nil
)
447 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
452 // Get size of display
453 void wxDisplaySize(int *width
, int *height
)
456 GetQDGlobalsScreenBits( &screenBits
);
458 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
459 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
462 void wxDisplaySizeMM(int *width
, int *height
)
464 wxDisplaySize(width
, height
);
465 // on mac 72 is fixed (at least now ;-)
466 float cvPt2Mm
= 25.4 / 72;
467 *width
= int( *width
* cvPt2Mm
);
468 *height
= int( *height
* cvPt2Mm
);
471 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
474 GetQDGlobalsScreenBits( &screenBits
);
479 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
480 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
484 GetThemeMenuBarHeight( &mheight
) ;
486 mheight
= LMGetMBarHeight() ;
493 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
495 return wxGenericFindWindowAtPoint(pt
);
498 wxString
wxGetOsDescription()
500 #ifdef WXWIN_OS_DESCRIPTION
501 // use configure generated description if available
502 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
504 return "MacOS" ; //TODO:define further
508 //---------------------------------------------------------------------------
509 // wxMac Specific utility functions
510 //---------------------------------------------------------------------------
512 char StringMac
[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
513 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
514 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
515 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
516 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
517 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
519 char StringANSI
[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
520 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
521 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
522 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
523 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
524 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
526 void wxMacConvertFromPC( const char *from
, char *to
, int len
)
531 for( int i
= 0 ; i
< len
; ++ i
)
533 c
= strchr( StringANSI
, *from
) ;
536 *to
= StringMac
[ c
- StringANSI
] ;
544 for( int i
= 0 ; i
< len
; ++ i
)
546 c
= strchr( StringANSI
, *from
) ;
549 *to
= StringMac
[ c
- StringANSI
] ;
561 void wxMacConvertToPC( const char *from
, char *to
, int len
)
566 for( int i
= 0 ; i
< len
; ++ i
)
568 c
= strchr( StringMac
, *from
) ;
571 *to
= StringANSI
[ c
- StringMac
] ;
579 for( int i
= 0 ; i
< len
; ++ i
)
581 c
= strchr( StringMac
, *from
) ;
584 *to
= StringANSI
[ c
- StringMac
] ;
596 wxString
wxMacMakeMacStringFromPC( const char * p
)
599 int len
= strlen ( p
) ;
602 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
603 wxMacConvertFromPC( p
, ptr
, len
) ;
605 result
.UngetWriteBuf( len
) ;
610 wxString
wxMacMakePCStringFromMac( const char * p
)
613 int len
= strlen ( p
) ;
616 wxChar
* ptr
= result
.GetWriteBuf(len
) ;
617 wxMacConvertToPC( p
, ptr
, len
) ;
619 result
.UngetWriteBuf( len
) ;
624 wxString
wxMacMakeStringFromMacString( const char* from
, bool mac2pcEncoding
)
628 return wxMacMakePCStringFromMac( from
) ;
632 return wxString( from
) ;
640 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
, bool mac2pcEncoding
)
642 // this is safe since a pascal string can never be larger than 256 bytes
644 CopyPascalStringToC( from
, s
) ;
647 return wxMacMakePCStringFromMac( s
) ;
651 return wxString( s
) ;
655 void wxMacStringToPascal( const char * from
, StringPtr to
, bool pc2macEncoding
)
659 CopyCStringToPascal( wxMacMakeMacStringFromPC( from
) , to
) ;
663 CopyCStringToPascal( from
, to
) ;
668 // CFStringRefs (Carbon only)
672 // converts this string into a carbon foundation string with optional pc 2 mac encoding
673 CFStringRef
wxMacCreateCFString( const wxString
&str
, bool pc2macEncoding
)
675 return CFStringCreateWithCString( kCFAllocatorSystemDefault
, str
.c_str() ,
677 kCFStringEncodingWindowsLatin1
: CFStringGetSystemEncoding() ) ;
680 #endif //TARGET_CARBON