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
, wxKillError
*rc
)
120 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
)
122 // TODO : under classic there is no environement support, under X yes
126 // set the env var name to the given value, return TRUE on success
127 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
)
129 // TODO : under classic there is no environement support, under X yes
134 // Execute a program in an Interactive Shell
136 bool wxShell(const wxString
& command
)
142 // Shutdown or reboot the PC
143 bool wxShutdown(wxShutdownFlags wFlags
)
149 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
150 long wxGetFreeMemory()
155 void wxUsleep(unsigned long milliseconds
)
157 clock_t start
= clock() ;
161 } while( clock() - start
< milliseconds
/ CLOCKS_PER_SEC
) ;
164 void wxSleep(int nSecs
)
166 wxUsleep(1000*nSecs
);
169 // Consume all events until no more left
174 #if WXWIN_COMPATIBILITY_2_2
176 // Output a debug message, in a system dependent fashion.
177 void wxDebugMsg(const char *fmt
...)
180 static char buffer
[512];
182 if (!wxTheApp
->GetWantDebugOutput())
187 vsprintf(buffer
,fmt
,ap
) ;
188 strcat(buffer
,";g") ;
190 DebugStr((unsigned char*) buffer
) ;
195 // Non-fatal error: pop up message box and (possibly) continue
196 void wxError(const wxString
& msg
, const wxString
& title
)
198 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
199 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
203 // Fatal error: pop up message box and abort
204 void wxFatalError(const wxString
& msg
, const wxString
& title
)
206 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
207 wxMessageBox(wxBuffer
);
211 #endif // WXWIN_COMPATIBILITY_2_2
213 #endif // !__DARWIN__
221 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
225 // are there x-platform conventions ?
227 Gestalt(gestaltSystemVersion
, &theSystem
) ;
228 if (minorVsn
!= NULL
) {
229 *minorVsn
= (theSystem
& 0xFF ) ;
231 if (majorVsn
!= NULL
) {
232 *majorVsn
= (theSystem
>> 8 ) ;
241 // Reading and writing resources (eg WIN.INI, .Xdefaults)
243 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
249 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
252 sprintf(buf
, "%.4f", value
);
253 return wxWriteResource(section
, entry
, buf
, file
);
256 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
259 sprintf(buf
, "%ld", value
);
260 return wxWriteResource(section
, entry
, buf
, file
);
263 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
266 sprintf(buf
, "%d", value
);
267 return wxWriteResource(section
, entry
, buf
, file
);
270 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
276 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
279 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
282 *value
= (float)strtod(s
, NULL
);
289 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
292 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
295 *value
= strtol(s
, NULL
, 10);
302 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
305 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
308 *value
= (int)strtol(s
, NULL
, 10);
314 #endif // wxUSE_RESOURCES
316 int wxBusyCursorCount
= 0;
317 extern CursHandle gMacCurrentCursor
;
318 CursHandle gMacStoredActiveCursor
= NULL
;
320 // Set the cursor to the busy cursor for all windows
321 void wxBeginBusyCursor(wxCursor
*cursor
)
323 wxBusyCursorCount
++;
324 if (wxBusyCursorCount
== 1)
326 gMacStoredActiveCursor
= gMacCurrentCursor
;
327 ::SetCursor( *::GetCursor( watchCursor
) ) ;
335 // Restore cursor to normal
336 void wxEndBusyCursor()
338 if (wxBusyCursorCount
== 0)
341 wxBusyCursorCount
--;
342 if (wxBusyCursorCount
== 0)
344 if ( gMacStoredActiveCursor
)
345 ::SetCursor( *gMacStoredActiveCursor
) ;
349 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
351 gMacStoredActiveCursor
= NULL
;
355 // TRUE if we're between the above two calls
358 return (wxBusyCursorCount
> 0);
361 wxString
wxMacFindFolder( short vol
,
363 Boolean createFolder
)
369 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
372 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
374 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
381 char *wxGetUserHome (const wxString
& user
)
387 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
397 int pos
= p
.Find(':') ;
398 if ( pos
!= wxNOT_FOUND
) {
407 wxMacStringToPascal( p
, volumeName
) ;
408 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
409 if ( err
== noErr
) {
411 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
414 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
418 return err
== noErr
;
422 // Check whether this window wants to process messages, e.g. Stop button
423 // in long calculations.
424 bool wxCheckForInterrupt(wxWindow
*wnd
)
430 void wxGetMousePosition( int* x
, int* y
)
435 LocalToGlobal( &pt
) ;
440 // Return TRUE if we have a colour display
441 bool wxColourDisplay()
446 // Returns depth of screen
450 SetRect(&globRect
, -32760, -32760, 32760, 32760);
451 GDHandle theMaxDevice
;
454 theMaxDevice
= GetMaxDevice(&globRect
);
455 if (theMaxDevice
!= nil
)
456 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
461 // Get size of display
462 void wxDisplaySize(int *width
, int *height
)
465 GetQDGlobalsScreenBits( &screenBits
);
467 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
468 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
471 void wxDisplaySizeMM(int *width
, int *height
)
473 wxDisplaySize(width
, height
);
474 // on mac 72 is fixed (at least now ;-)
475 float cvPt2Mm
= 25.4 / 72;
476 *width
= int( *width
* cvPt2Mm
);
477 *height
= int( *height
* cvPt2Mm
);
480 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
483 GetQDGlobalsScreenBits( &screenBits
);
488 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
489 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
493 GetThemeMenuBarHeight( &mheight
) ;
495 mheight
= LMGetMBarHeight() ;
502 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
504 return wxGenericFindWindowAtPoint(pt
);
507 wxString
wxGetOsDescription()
509 #ifdef WXWIN_OS_DESCRIPTION
510 // use configure generated description if available
511 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
513 return "MacOS" ; //TODO:define further