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 "utils.h"
20 #include "wx/mac/uma.h"
29 #include "MoreFiles.h"
30 #include "MoreFilesExtras.h"
38 // defined in unix/utilsunx.cpp for Mac OS X
40 // get full hostname (with domain name if possible)
41 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
43 return wxGetHostName(buf
, maxSize
);
46 // Get hostname only (without domain name)
47 bool wxGetHostName(char *buf
, int maxSize
)
49 // Gets Chooser name of user by examining a System resource.
51 const short kComputerNameID
= -16413;
53 short oldResFile
= CurResFile() ;
55 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
56 UseResFile(oldResFile
);
58 if (chooserName
&& *chooserName
)
60 int length
= (*chooserName
)[0] ;
61 if ( length
+ 1 > maxSize
)
63 length
= maxSize
- 1 ;
65 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
74 // Get user ID e.g. jacs
75 bool wxGetUserId(char *buf
, int maxSize
)
77 return wxGetUserName( buf
, maxSize
) ;
80 const wxChar
* wxGetHomeDir(wxString
*pstr
)
82 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
83 return pstr
->c_str() ;
86 // Get user name e.g. AUTHOR
87 bool wxGetUserName(char *buf
, int maxSize
)
89 // Gets Chooser name of user by examining a System resource.
91 const short kChooserNameID
= -16096;
93 short oldResFile
= CurResFile() ;
95 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
96 UseResFile(oldResFile
);
98 if (chooserName
&& *chooserName
)
100 int length
= (*chooserName
)[0] ;
101 if ( length
+ 1 > maxSize
)
103 length
= maxSize
- 1 ;
105 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
114 int wxKill(long pid
, wxSignal sig
)
121 // Execute a program in an Interactive Shell
123 bool wxShell(const wxString
& command
)
129 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
130 long wxGetFreeMemory()
135 void wxUsleep(unsigned long milliseconds
)
137 clock_t start
= clock() ;
141 } while( clock() - start
< milliseconds
/ CLOCKS_PER_SEC
) ;
144 void wxSleep(int nSecs
)
146 wxUsleep(1000*nSecs
);
149 // Consume all events until no more left
154 // Output a debug message, in a system dependent fashion.
155 void wxDebugMsg(const char *fmt
...)
158 static char buffer
[512];
160 if (!wxTheApp
->GetWantDebugOutput())
165 vsprintf(buffer
,fmt
,ap
) ;
166 strcat(buffer
,";g") ;
168 DebugStr((unsigned char*) buffer
) ;
173 // Non-fatal error: pop up message box and (possibly) continue
174 void wxError(const wxString
& msg
, const wxString
& title
)
176 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
177 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
181 // Fatal error: pop up message box and abort
182 void wxFatalError(const wxString
& msg
, const wxString
& title
)
184 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
185 wxMessageBox(wxBuffer
);
188 #endif // !__DARWIN__
196 int wxGetOsVersion(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 sprintf(buf
, "%.4f", value
);
228 return wxWriteResource(section
, entry
, buf
, file
);
231 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
234 sprintf(buf
, "%ld", value
);
235 return wxWriteResource(section
, entry
, buf
, file
);
238 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
241 sprintf(buf
, "%d", value
);
242 return wxWriteResource(section
, entry
, buf
, file
);
245 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
251 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
254 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
257 *value
= (float)strtod(s
, NULL
);
264 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
267 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
270 *value
= strtol(s
, NULL
, 10);
277 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
280 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
283 *value
= (int)strtol(s
, NULL
, 10);
289 #endif // wxUSE_RESOURCES
291 int wxBusyCursorCount
= 0;
292 extern CursHandle gMacCurrentCursor
;
293 CursHandle gMacStoredActiveCursor
= NULL
;
295 // Set the cursor to the busy cursor for all windows
296 void wxBeginBusyCursor(wxCursor
*cursor
)
298 wxBusyCursorCount
++;
299 if (wxBusyCursorCount
== 1)
301 gMacStoredActiveCursor
= gMacCurrentCursor
;
302 ::SetCursor( *::GetCursor( watchCursor
) ) ;
310 // Restore cursor to normal
311 void wxEndBusyCursor()
313 if (wxBusyCursorCount
== 0)
316 wxBusyCursorCount
--;
317 if (wxBusyCursorCount
== 0)
319 if ( gMacStoredActiveCursor
)
320 ::SetCursor( *gMacStoredActiveCursor
) ;
324 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
326 gMacStoredActiveCursor
= NULL
;
330 // TRUE if we're between the above two calls
333 return (wxBusyCursorCount
> 0);
336 wxString
wxMacFindFolder( short vol
,
338 Boolean createFolder
)
344 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
347 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
349 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
356 char *wxGetUserHome (const wxString
& user
)
362 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
372 int pos
= p
.Find(':') ;
373 if ( pos
!= wxNOT_FOUND
) {
382 wxMacStringToPascal( p
, volumeName
) ;
383 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
384 if ( err
== noErr
) {
386 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
389 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
393 return err
== noErr
;
397 // Check whether this window wants to process messages, e.g. Stop button
398 // in long calculations.
399 bool wxCheckForInterrupt(wxWindow
*wnd
)
405 void wxGetMousePosition( int* x
, int* y
)
410 LocalToGlobal( &pt
) ;
415 // Return TRUE if we have a colour display
416 bool wxColourDisplay()
421 // Returns depth of screen
425 SetRect(&globRect
, -32760, -32760, 32760, 32760);
426 GDHandle theMaxDevice
;
429 theMaxDevice
= GetMaxDevice(&globRect
);
430 if (theMaxDevice
!= nil
)
431 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
436 // Get size of display
437 void wxDisplaySize(int *width
, int *height
)
440 GetQDGlobalsScreenBits( &screenBits
);
442 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
443 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
446 GetThemeMenuBarHeight( &mheight
) ;
449 *height
-= LMGetMBarHeight() ;
453 void wxDisplaySizeMM(int *width
, int *height
)
455 wxDisplaySize(width
, height
);
458 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
460 // This is supposed to return desktop dimensions minus any window
461 // manager panels, menus, taskbars, etc. If there is a way to do that
462 // for this platform please fix this function, otherwise it defaults
463 // to the entire desktop.
466 wxDisplaySize(width
, height
);
469 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
471 return wxGenericFindWindowAtPoint(pt
);