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"
33 // defined in unix/utilsunx.cpp for Mac OS X
35 // get full hostname (with domain name if possible)
36 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
38 return wxGetHostName(buf
, maxSize
);
41 // Get hostname only (without domain name)
42 bool wxGetHostName(char *buf
, int maxSize
)
44 // Gets Chooser name of user by examining a System resource.
46 const short kComputerNameID
= -16413;
48 short oldResFile
= CurResFile() ;
50 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
51 UseResFile(oldResFile
);
53 if (chooserName
&& *chooserName
)
55 int length
= (*chooserName
)[0] ;
56 if ( length
+ 1 > maxSize
)
58 length
= maxSize
- 1 ;
60 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
69 // Get user ID e.g. jacs
70 bool wxGetUserId(char *buf
, int maxSize
)
72 return wxGetUserName( buf
, maxSize
) ;
75 const wxChar
* wxGetHomeDir(wxString
*pstr
)
77 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
78 return pstr
->c_str() ;
81 // Get user name e.g. AUTHOR
82 bool wxGetUserName(char *buf
, int maxSize
)
84 // Gets Chooser name of user by examining a System resource.
86 const short kChooserNameID
= -16096;
88 short oldResFile
= CurResFile() ;
90 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
91 UseResFile(oldResFile
);
93 if (chooserName
&& *chooserName
)
95 int length
= (*chooserName
)[0] ;
96 if ( length
+ 1 > maxSize
)
98 length
= maxSize
- 1 ;
100 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
109 int wxKill(long pid
, wxSignal sig
)
116 // Execute a program in an Interactive Shell
118 bool wxShell(const wxString
& command
)
124 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
125 long wxGetFreeMemory()
130 void wxUsleep(unsigned long milliseconds
)
132 clock_t start
= clock() ;
136 } while( clock() - start
< milliseconds
/ CLOCKS_PER_SEC
) ;
139 void wxSleep(int nSecs
)
141 wxUsleep(1000*nSecs
);
144 // Consume all events until no more left
149 // Output a debug message, in a system dependent fashion.
150 void wxDebugMsg(const char *fmt
...)
153 static char buffer
[512];
155 if (!wxTheApp
->GetWantDebugOutput())
160 vsprintf(buffer
,fmt
,ap
) ;
161 strcat(buffer
,";g") ;
163 DebugStr((unsigned char*) buffer
) ;
168 // Non-fatal error: pop up message box and (possibly) continue
169 void wxError(const wxString
& msg
, const wxString
& title
)
171 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
172 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
176 // Fatal error: pop up message box and abort
177 void wxFatalError(const wxString
& msg
, const wxString
& title
)
179 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
180 wxMessageBox(wxBuffer
);
183 #endif // !__DARWIN__
191 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
194 Gestalt(gestaltSystemVersion
, &theSystem
) ;
195 *minorVsn
= (theSystem
& 0xFF ) ;
196 *majorVsn
= (theSystem
>> 8 ) ; // are there x-platform conventions ?
200 // Reading and writing resources (eg WIN.INI, .Xdefaults)
202 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
208 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
211 sprintf(buf
, "%.4f", value
);
212 return wxWriteResource(section
, entry
, buf
, file
);
215 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
218 sprintf(buf
, "%ld", value
);
219 return wxWriteResource(section
, entry
, buf
, file
);
222 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
225 sprintf(buf
, "%d", value
);
226 return wxWriteResource(section
, entry
, buf
, file
);
229 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
235 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
238 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
241 *value
= (float)strtod(s
, NULL
);
248 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
251 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
254 *value
= strtol(s
, NULL
, 10);
261 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
264 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
267 *value
= (int)strtol(s
, NULL
, 10);
273 #endif // wxUSE_RESOURCES
275 int wxBusyCursorCount
= 0;
276 extern CursHandle gMacCurrentCursor
;
277 CursHandle gMacStoredActiveCursor
= NULL
;
279 // Set the cursor to the busy cursor for all windows
280 void wxBeginBusyCursor(wxCursor
*cursor
)
282 wxBusyCursorCount
++;
283 if (wxBusyCursorCount
== 1)
285 gMacStoredActiveCursor
= gMacCurrentCursor
;
286 ::SetCursor( *::GetCursor( watchCursor
) ) ;
294 // Restore cursor to normal
295 void wxEndBusyCursor()
297 if (wxBusyCursorCount
== 0)
300 wxBusyCursorCount
--;
301 if (wxBusyCursorCount
== 0)
303 if ( gMacStoredActiveCursor
)
304 ::SetCursor( *gMacStoredActiveCursor
) ;
308 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
310 gMacStoredActiveCursor
= NULL
;
314 // TRUE if we're between the above two calls
317 return (wxBusyCursorCount
> 0);
320 wxString
wxMacFindFolder( short vol
,
322 Boolean createFolder
)
328 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
331 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
333 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
340 char *wxGetUserHome (const wxString
& user
)
346 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
356 int pos
= p
.Find(':') ;
357 if ( pos
!= wxNOT_FOUND
) {
366 wxMacStringToPascal( p
, volumeName
) ;
367 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
368 if ( err
== noErr
) {
370 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
373 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
377 return err
== noErr
;
381 // Check whether this window wants to process messages, e.g. Stop button
382 // in long calculations.
383 bool wxCheckForInterrupt(wxWindow
*wnd
)
389 void wxGetMousePosition( int* x
, int* y
)
394 LocalToGlobal( &pt
) ;
399 // Return TRUE if we have a colour display
400 bool wxColourDisplay()
405 // Returns depth of screen
409 SetRect(&globRect
, -32760, -32760, 32760, 32760);
410 GDHandle theMaxDevice
;
413 theMaxDevice
= GetMaxDevice(&globRect
);
414 if (theMaxDevice
!= nil
)
415 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
420 // Get size of display
421 void wxDisplaySize(int *width
, int *height
)
424 GetQDGlobalsScreenBits( &screenBits
);
426 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
427 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
430 GetThemeMenuBarHeight( &mheight
) ;
433 *height
-= LMGetMBarHeight() ;
437 void wxDisplaySizeMM(int *width
, int *height
)
439 wxDisplaySize(width
, height
);
442 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
444 // This is supposed to return desktop dimensions minus any window
445 // manager panels, menus, taskbars, etc. If there is a way to do that
446 // for this platform please fix this function, otherwise it defaults
447 // to the entire desktop.
450 wxDisplaySize(width
, height
);
453 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
455 return wxGenericFindWindowAtPoint(pt
);