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
)
195 // are there x-platform conventions ?
197 Gestalt(gestaltSystemVersion
, &theSystem
) ;
198 if (minorVsn
!= NULL
) {
199 *minorVsn
= (theSystem
& 0xFF ) ;
201 if (majorVsn
!= NULL
) {
202 *majorVsn
= (theSystem
>> 8 ) ;
211 // Reading and writing resources (eg WIN.INI, .Xdefaults)
213 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
219 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
222 sprintf(buf
, "%.4f", value
);
223 return wxWriteResource(section
, entry
, buf
, file
);
226 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
229 sprintf(buf
, "%ld", value
);
230 return wxWriteResource(section
, entry
, buf
, file
);
233 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
236 sprintf(buf
, "%d", value
);
237 return wxWriteResource(section
, entry
, buf
, file
);
240 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
246 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
249 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
252 *value
= (float)strtod(s
, NULL
);
259 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
262 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
265 *value
= strtol(s
, NULL
, 10);
272 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
275 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
278 *value
= (int)strtol(s
, NULL
, 10);
284 #endif // wxUSE_RESOURCES
286 int wxBusyCursorCount
= 0;
287 extern CursHandle gMacCurrentCursor
;
288 CursHandle gMacStoredActiveCursor
= NULL
;
290 // Set the cursor to the busy cursor for all windows
291 void wxBeginBusyCursor(wxCursor
*cursor
)
293 wxBusyCursorCount
++;
294 if (wxBusyCursorCount
== 1)
296 gMacStoredActiveCursor
= gMacCurrentCursor
;
297 ::SetCursor( *::GetCursor( watchCursor
) ) ;
305 // Restore cursor to normal
306 void wxEndBusyCursor()
308 if (wxBusyCursorCount
== 0)
311 wxBusyCursorCount
--;
312 if (wxBusyCursorCount
== 0)
314 if ( gMacStoredActiveCursor
)
315 ::SetCursor( *gMacStoredActiveCursor
) ;
319 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
321 gMacStoredActiveCursor
= NULL
;
325 // TRUE if we're between the above two calls
328 return (wxBusyCursorCount
> 0);
331 wxString
wxMacFindFolder( short vol
,
333 Boolean createFolder
)
339 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
342 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
344 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
351 char *wxGetUserHome (const wxString
& user
)
357 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
367 int pos
= p
.Find(':') ;
368 if ( pos
!= wxNOT_FOUND
) {
377 wxMacStringToPascal( p
, volumeName
) ;
378 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
379 if ( err
== noErr
) {
381 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
384 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
388 return err
== noErr
;
392 // Check whether this window wants to process messages, e.g. Stop button
393 // in long calculations.
394 bool wxCheckForInterrupt(wxWindow
*wnd
)
400 void wxGetMousePosition( int* x
, int* y
)
405 LocalToGlobal( &pt
) ;
410 // Return TRUE if we have a colour display
411 bool wxColourDisplay()
416 // Returns depth of screen
420 SetRect(&globRect
, -32760, -32760, 32760, 32760);
421 GDHandle theMaxDevice
;
424 theMaxDevice
= GetMaxDevice(&globRect
);
425 if (theMaxDevice
!= nil
)
426 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
431 // Get size of display
432 void wxDisplaySize(int *width
, int *height
)
435 GetQDGlobalsScreenBits( &screenBits
);
437 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
438 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
441 GetThemeMenuBarHeight( &mheight
) ;
444 *height
-= LMGetMBarHeight() ;
448 void wxDisplaySizeMM(int *width
, int *height
)
450 wxDisplaySize(width
, height
);
453 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
455 // This is supposed to return desktop dimensions minus any window
456 // manager panels, menus, taskbars, etc. If there is a way to do that
457 // for this platform please fix this function, otherwise it defaults
458 // to the entire desktop.
461 wxDisplaySize(width
, height
);
464 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
466 return wxGenericFindWindowAtPoint(pt
);