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"
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. AUTHOR
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
/ 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 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
203 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
207 // Fatal error: pop up message box and abort
208 void wxFatalError(const wxString
& msg
, const wxString
& title
)
210 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
211 wxMessageBox(wxBuffer
);
215 #endif // WXWIN_COMPATIBILITY_2_2
217 #endif // !__DARWIN__
225 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
229 // are there x-platform conventions ?
231 Gestalt(gestaltSystemVersion
, &theSystem
) ;
232 if (minorVsn
!= NULL
) {
233 *minorVsn
= (theSystem
& 0xFF ) ;
235 if (majorVsn
!= NULL
) {
236 *majorVsn
= (theSystem
>> 8 ) ;
245 // Reading and writing resources (eg WIN.INI, .Xdefaults)
247 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
253 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
256 sprintf(buf
, "%.4f", value
);
257 return wxWriteResource(section
, entry
, buf
, file
);
260 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
263 sprintf(buf
, "%ld", value
);
264 return wxWriteResource(section
, entry
, buf
, file
);
267 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
270 sprintf(buf
, "%d", value
);
271 return wxWriteResource(section
, entry
, buf
, file
);
274 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
280 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
283 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
286 *value
= (float)strtod(s
, NULL
);
293 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
296 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
299 *value
= strtol(s
, NULL
, 10);
306 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
309 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
312 *value
= (int)strtol(s
, NULL
, 10);
318 #endif // wxUSE_RESOURCES
320 int wxBusyCursorCount
= 0;
321 extern CursHandle gMacCurrentCursor
;
322 CursHandle gMacStoredActiveCursor
= NULL
;
324 // Set the cursor to the busy cursor for all windows
325 void wxBeginBusyCursor(wxCursor
*cursor
)
327 wxBusyCursorCount
++;
328 if (wxBusyCursorCount
== 1)
330 gMacStoredActiveCursor
= gMacCurrentCursor
;
331 ::SetCursor( *::GetCursor( watchCursor
) ) ;
339 // Restore cursor to normal
340 void wxEndBusyCursor()
342 if (wxBusyCursorCount
== 0)
345 wxBusyCursorCount
--;
346 if (wxBusyCursorCount
== 0)
348 if ( gMacStoredActiveCursor
)
349 ::SetCursor( *gMacStoredActiveCursor
) ;
353 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
355 gMacStoredActiveCursor
= NULL
;
359 // TRUE if we're between the above two calls
362 return (wxBusyCursorCount
> 0);
365 wxString
wxMacFindFolder( short vol
,
367 Boolean createFolder
)
373 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
376 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
378 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
385 char *wxGetUserHome (const wxString
& user
)
391 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
401 int pos
= p
.Find(':') ;
402 if ( pos
!= wxNOT_FOUND
) {
411 wxMacStringToPascal( p
, volumeName
) ;
412 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
413 if ( err
== noErr
) {
415 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
418 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
422 return err
== noErr
;
426 // Check whether this window wants to process messages, e.g. Stop button
427 // in long calculations.
428 bool wxCheckForInterrupt(wxWindow
*wnd
)
434 void wxGetMousePosition( int* x
, int* y
)
439 LocalToGlobal( &pt
) ;
444 // Return TRUE if we have a colour display
445 bool wxColourDisplay()
450 // Returns depth of screen
454 SetRect(&globRect
, -32760, -32760, 32760, 32760);
455 GDHandle theMaxDevice
;
458 theMaxDevice
= GetMaxDevice(&globRect
);
459 if (theMaxDevice
!= nil
)
460 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
465 // Get size of display
466 void wxDisplaySize(int *width
, int *height
)
469 GetQDGlobalsScreenBits( &screenBits
);
471 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
472 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
475 void wxDisplaySizeMM(int *width
, int *height
)
477 wxDisplaySize(width
, height
);
478 // on mac 72 is fixed (at least now ;-)
479 float cvPt2Mm
= 25.4 / 72;
480 *width
= int( *width
* cvPt2Mm
);
481 *height
= int( *height
* cvPt2Mm
);
484 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
487 GetQDGlobalsScreenBits( &screenBits
);
492 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
493 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
497 GetThemeMenuBarHeight( &mheight
) ;
499 mheight
= LMGetMBarHeight() ;
506 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
508 return wxGenericFindWindowAtPoint(pt
);
511 wxString
wxGetOsDescription()
513 #ifdef WXWIN_OS_DESCRIPTION
514 // use configure generated description if available
515 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
517 return "MacOS" ; //TODO:define further