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 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
143 long wxGetFreeMemory()
148 void wxUsleep(unsigned long milliseconds
)
150 clock_t start
= clock() ;
154 } while( clock() - start
< milliseconds
/ CLOCKS_PER_SEC
) ;
157 void wxSleep(int nSecs
)
159 wxUsleep(1000*nSecs
);
162 // Consume all events until no more left
167 #if WXWIN_COMPATIBILITY_2_2
169 // Output a debug message, in a system dependent fashion.
170 void wxDebugMsg(const char *fmt
...)
173 static char buffer
[512];
175 if (!wxTheApp
->GetWantDebugOutput())
180 vsprintf(buffer
,fmt
,ap
) ;
181 strcat(buffer
,";g") ;
183 DebugStr((unsigned char*) buffer
) ;
188 // Non-fatal error: pop up message box and (possibly) continue
189 void wxError(const wxString
& msg
, const wxString
& title
)
191 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
192 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
196 // Fatal error: pop up message box and abort
197 void wxFatalError(const wxString
& msg
, const wxString
& title
)
199 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
200 wxMessageBox(wxBuffer
);
204 #endif // WXWIN_COMPATIBILITY_2_2
206 #endif // !__DARWIN__
214 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
218 // are there x-platform conventions ?
220 Gestalt(gestaltSystemVersion
, &theSystem
) ;
221 if (minorVsn
!= NULL
) {
222 *minorVsn
= (theSystem
& 0xFF ) ;
224 if (majorVsn
!= NULL
) {
225 *majorVsn
= (theSystem
>> 8 ) ;
234 // Reading and writing resources (eg WIN.INI, .Xdefaults)
236 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
242 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
245 sprintf(buf
, "%.4f", value
);
246 return wxWriteResource(section
, entry
, buf
, file
);
249 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
252 sprintf(buf
, "%ld", value
);
253 return wxWriteResource(section
, entry
, buf
, file
);
256 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
259 sprintf(buf
, "%d", value
);
260 return wxWriteResource(section
, entry
, buf
, file
);
263 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
269 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
272 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
275 *value
= (float)strtod(s
, NULL
);
282 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
285 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
288 *value
= strtol(s
, NULL
, 10);
295 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
298 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
301 *value
= (int)strtol(s
, NULL
, 10);
307 #endif // wxUSE_RESOURCES
309 int wxBusyCursorCount
= 0;
310 extern CursHandle gMacCurrentCursor
;
311 CursHandle gMacStoredActiveCursor
= NULL
;
313 // Set the cursor to the busy cursor for all windows
314 void wxBeginBusyCursor(wxCursor
*cursor
)
316 wxBusyCursorCount
++;
317 if (wxBusyCursorCount
== 1)
319 gMacStoredActiveCursor
= gMacCurrentCursor
;
320 ::SetCursor( *::GetCursor( watchCursor
) ) ;
328 // Restore cursor to normal
329 void wxEndBusyCursor()
331 if (wxBusyCursorCount
== 0)
334 wxBusyCursorCount
--;
335 if (wxBusyCursorCount
== 0)
337 if ( gMacStoredActiveCursor
)
338 ::SetCursor( *gMacStoredActiveCursor
) ;
342 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
344 gMacStoredActiveCursor
= NULL
;
348 // TRUE if we're between the above two calls
351 return (wxBusyCursorCount
> 0);
354 wxString
wxMacFindFolder( short vol
,
356 Boolean createFolder
)
362 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
365 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
367 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
374 char *wxGetUserHome (const wxString
& user
)
380 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
390 int pos
= p
.Find(':') ;
391 if ( pos
!= wxNOT_FOUND
) {
400 wxMacStringToPascal( p
, volumeName
) ;
401 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
402 if ( err
== noErr
) {
404 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
407 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
411 return err
== noErr
;
415 // Check whether this window wants to process messages, e.g. Stop button
416 // in long calculations.
417 bool wxCheckForInterrupt(wxWindow
*wnd
)
423 void wxGetMousePosition( int* x
, int* y
)
428 LocalToGlobal( &pt
) ;
433 // Return TRUE if we have a colour display
434 bool wxColourDisplay()
439 // Returns depth of screen
443 SetRect(&globRect
, -32760, -32760, 32760, 32760);
444 GDHandle theMaxDevice
;
447 theMaxDevice
= GetMaxDevice(&globRect
);
448 if (theMaxDevice
!= nil
)
449 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
454 // Get size of display
455 void wxDisplaySize(int *width
, int *height
)
458 GetQDGlobalsScreenBits( &screenBits
);
460 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
461 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
464 GetThemeMenuBarHeight( &mheight
) ;
467 *height
-= LMGetMBarHeight() ;
471 void wxDisplaySizeMM(int *width
, int *height
)
473 wxDisplaySize(width
, height
);
476 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
478 // This is supposed to return desktop dimensions minus any window
479 // manager panels, menus, taskbars, etc. If there is a way to do that
480 // for this platform please fix this function, otherwise it defaults
481 // to the entire desktop.
484 wxDisplaySize(width
, height
);
487 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
489 return wxGenericFindWindowAtPoint(pt
);
492 wxString
wxGetOsDescription()
494 #ifdef WXWIN_OS_DESCRIPTION
495 // use configure generated description if available
496 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
498 return "MacOS" ; //TODO:define further