1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     Various utilities 
   8 // Copyright:   (c) AUTHOR 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 // Note: this is done in utilscmn.cpp now. 
  14 // #pragma implementation 
  15 // #pragma implementation "utils.h" 
  21 #include "wx/mac/uma.h" 
  31 #  include "morefile.h" 
  32 #  include "moreextr.h" 
  36 // defined in unix/utilsunx.cpp for Mac OS X 
  38 // get full hostname (with domain name if possible) 
  39 bool wxGetFullHostName(wxChar 
*buf
, int maxSize
) 
  41     return wxGetHostName(buf
, maxSize
); 
  44 // Get hostname only (without domain name) 
  45 bool wxGetHostName(char *buf
, int maxSize
) 
  47         // Gets Chooser name of user by examining a System resource. 
  49         const short kComputerNameID 
= -16413; 
  51         short oldResFile 
= CurResFile() ; 
  53         StringHandle chooserName 
= (StringHandle
)::GetString(kComputerNameID
); 
  54         UseResFile(oldResFile
); 
  56         if (chooserName 
&& *chooserName
) 
  58           int length 
= (*chooserName
)[0] ; 
  59           if ( length 
+ 1 > maxSize 
) 
  61             length 
= maxSize 
- 1 ; 
  63           strncpy( buf 
, (char*) &(*chooserName
)[1] , length 
) ; 
  72 // Get user ID e.g. jacs 
  73 bool wxGetUserId(char *buf
, int maxSize
) 
  75   return wxGetUserName( buf 
, maxSize 
) ; 
  78 const wxChar
* wxGetHomeDir(wxString 
*pstr
) 
  80         *pstr 
= wxMacFindFolder(  (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder 
) ; 
  81         return pstr
->c_str() ; 
  84 // Get user name e.g. AUTHOR 
  85 bool wxGetUserName(char *buf
, int maxSize
) 
  87         // Gets Chooser name of user by examining a System resource. 
  89         const short kChooserNameID 
= -16096; 
  91         short oldResFile 
= CurResFile() ; 
  93         StringHandle chooserName 
= (StringHandle
)::GetString(kChooserNameID
); 
  94         UseResFile(oldResFile
); 
  96         if (chooserName 
&& *chooserName
) 
  98           int length 
= (*chooserName
)[0] ; 
  99           if ( length 
+ 1 > maxSize 
) 
 101             length 
= maxSize 
- 1 ; 
 103           strncpy( buf 
, (char*) &(*chooserName
)[1] , length 
) ; 
 112 int wxKill(long pid
, wxSignal sig
) 
 119 // Execute a program in an Interactive Shell 
 121 bool wxShell(const wxString
& command
) 
 127 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) 
 128 long wxGetFreeMemory() 
 133 void wxUsleep(unsigned long milliseconds
) 
 135                 clock_t start 
= clock() ; 
 139                 } while( clock() - start 
< milliseconds 
/ CLOCKS_PER_SEC 
) ; 
 142 void wxSleep(int nSecs
) 
 144     wxUsleep(1000*nSecs
); 
 147 // Consume all events until no more left 
 152 // Output a debug message, in a system dependent fashion. 
 153 void wxDebugMsg(const char *fmt 
...) 
 156   static char buffer
[512]; 
 158   if (!wxTheApp
->GetWantDebugOutput()) 
 163   vsprintf(buffer
,fmt
,ap
) ; 
 164   strcat(buffer
,";g") ; 
 166   DebugStr((unsigned char*) buffer
) ; 
 171 // Non-fatal error: pop up message box and (possibly) continue 
 172 void wxError(const wxString
& msg
, const wxString
& title
) 
 174   wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
); 
 175   if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO 
) 
 179 // Fatal error: pop up message box and abort 
 180 void wxFatalError(const wxString
& msg
, const wxString
& title
) 
 182   wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
); 
 183   wxMessageBox(wxBuffer
); 
 186 #endif // !__DARWIN__ 
 194 int wxGetOsVersion(int *majorVsn
, int *minorVsn
) 
 197   Gestalt(gestaltSystemVersion
, &theSystem
) ; 
 198   *minorVsn 
= (theSystem 
& 0xFF ) ; 
 199   *majorVsn 
= (theSystem 
>> 8 ) ; // are there x-platform conventions ? 
 203 // Reading and writing resources (eg WIN.INI, .Xdefaults) 
 205 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
) 
 211 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
) 
 214   sprintf(buf
, "%.4f", value
); 
 215   return wxWriteResource(section
, entry
, buf
, file
); 
 218 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
) 
 221   sprintf(buf
, "%ld", value
); 
 222   return wxWriteResource(section
, entry
, buf
, file
); 
 225 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
) 
 228   sprintf(buf
, "%d", value
); 
 229   return wxWriteResource(section
, entry
, buf
, file
); 
 232 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
) 
 238 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
) 
 241   bool succ 
= wxGetResource(section
, entry
, (char **)&s
, file
); 
 244     *value 
= (float)strtod(s
, NULL
); 
 251 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
) 
 254   bool succ 
= wxGetResource(section
, entry
, (char **)&s
, file
); 
 257     *value 
= strtol(s
, NULL
, 10); 
 264 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
) 
 267   bool succ 
= wxGetResource(section
, entry
, (char **)&s
, file
); 
 270     *value 
= (int)strtol(s
, NULL
, 10); 
 276 #endif // wxUSE_RESOURCES 
 278 int wxBusyCursorCount 
= 0; 
 279 extern CursHandle       gMacCurrentCursor 
; 
 280 CursHandle                      gMacStoredActiveCursor 
= NULL 
; 
 282 // Set the cursor to the busy cursor for all windows 
 283 void wxBeginBusyCursor(wxCursor 
*cursor
) 
 285   wxBusyCursorCount 
++; 
 286   if (wxBusyCursorCount 
== 1) 
 288         gMacStoredActiveCursor 
= gMacCurrentCursor 
; 
 289                 ::SetCursor( *::GetCursor( watchCursor 
) ) ; 
 297 // Restore cursor to normal 
 298 void wxEndBusyCursor() 
 300   if (wxBusyCursorCount 
== 0) 
 303   wxBusyCursorCount 
--; 
 304   if (wxBusyCursorCount 
== 0) 
 306     if ( gMacStoredActiveCursor 
) 
 307         ::SetCursor( *gMacStoredActiveCursor 
) ; 
 311         ::SetCursor( GetQDGlobalsArrow( &MacArrow 
) ) ; 
 313         gMacStoredActiveCursor 
= NULL 
; 
 317 // TRUE if we're between the above two calls 
 320   return (wxBusyCursorCount 
> 0); 
 324 wxString 
wxMacFindFolder( short                                         vol
, 
 326                                                                  Boolean                                createFolder
) 
 332         if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
) 
 335                 if ( FSMakeFSSpec( vRefNum 
, dirID 
, "\p" , &file 
) == noErr 
) 
 337                         strDir 
= wxMacFSSpec2MacFilename( &file 
) + ":" ; 
 345 char *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 
); 
 433     *width 
= screenBits
.bounds
.right 
- screenBits
.bounds
.left  
; 
 434     *height 
= screenBits
.bounds
.bottom 
- screenBits
.bounds
.top 
; 
 437         GetThemeMenuBarHeight( &mheight 
) ; 
 440      *height 
-= LMGetMBarHeight() ; 
 444 void wxDisplaySizeMM(int *width
, int *height
) 
 446    wxDisplaySize(width
, height
); 
 449 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
) 
 451     // This is supposed to return desktop dimensions minus any window 
 452     // manager panels, menus, taskbars, etc.  If there is a way to do that 
 453     // for this platform please fix this function, otherwise it defaults 
 454     // to the entire desktop. 
 457     wxDisplaySize(width
, height
); 
 460 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
) 
 462     return wxGenericFindWindowAtPoint(pt
);