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
15 // #pragma implementation "utils.h"
21 #include "wx/mac/uma.h"
30 #include "MoreFiles.h"
31 #include "MoreFilesExtras.h"
34 // defined in unix/utilsunx.cpp for Mac OS X
36 // get full hostname (with domain name if possible)
37 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
39 return wxGetHostName(buf
, maxSize
);
42 // Get hostname only (without domain name)
43 bool wxGetHostName(char *buf
, int maxSize
)
45 // Gets Chooser name of user by examining a System resource.
47 const short kComputerNameID
= -16413;
49 short oldResFile
= CurResFile() ;
51 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
52 UseResFile(oldResFile
);
54 if (chooserName
&& *chooserName
)
56 int length
= (*chooserName
)[0] ;
57 if ( length
+ 1 > maxSize
)
59 length
= maxSize
- 1 ;
61 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
70 // Get user ID e.g. jacs
71 bool wxGetUserId(char *buf
, int maxSize
)
73 return wxGetUserName( buf
, maxSize
) ;
76 const wxChar
* wxGetHomeDir(wxString
*pstr
)
78 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
79 return pstr
->c_str() ;
82 // Get user name e.g. AUTHOR
83 bool wxGetUserName(char *buf
, int maxSize
)
85 // Gets Chooser name of user by examining a System resource.
87 const short kChooserNameID
= -16096;
89 short oldResFile
= CurResFile() ;
91 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
92 UseResFile(oldResFile
);
94 if (chooserName
&& *chooserName
)
96 int length
= (*chooserName
)[0] ;
97 if ( length
+ 1 > maxSize
)
99 length
= maxSize
- 1 ;
101 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
110 int wxKill(long pid
, wxSignal sig
)
117 // Execute a program in an Interactive Shell
119 bool wxShell(const wxString
& command
)
125 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
126 long wxGetFreeMemory()
131 void wxUsleep(unsigned long milliseconds
)
133 clock_t start
= clock() ;
137 } while( clock() - start
< milliseconds
/ CLOCKS_PER_SEC
) ;
140 void wxSleep(int nSecs
)
142 wxUsleep(1000*nSecs
);
145 // Consume all events until no more left
150 // Output a debug message, in a system dependent fashion.
151 void wxDebugMsg(const char *fmt
...)
154 static char buffer
[512];
156 if (!wxTheApp
->GetWantDebugOutput())
161 vsprintf(buffer
,fmt
,ap
) ;
162 strcat(buffer
,";g") ;
164 DebugStr((unsigned char*) buffer
) ;
169 // Non-fatal error: pop up message box and (possibly) continue
170 void wxError(const wxString
& msg
, const wxString
& title
)
172 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
173 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
177 // Fatal error: pop up message box and abort
178 void wxFatalError(const wxString
& msg
, const wxString
& title
)
180 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
181 wxMessageBox(wxBuffer
);
184 #endif // !__DARWIN__
192 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
195 Gestalt(gestaltSystemVersion
, &theSystem
) ;
196 *minorVsn
= (theSystem
& 0xFF ) ;
197 *majorVsn
= (theSystem
>> 8 ) ; // are there x-platform conventions ?
201 // Reading and writing resources (eg WIN.INI, .Xdefaults)
203 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
209 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
212 sprintf(buf
, "%.4f", value
);
213 return wxWriteResource(section
, entry
, buf
, file
);
216 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
219 sprintf(buf
, "%ld", value
);
220 return wxWriteResource(section
, entry
, buf
, file
);
223 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
226 sprintf(buf
, "%d", value
);
227 return wxWriteResource(section
, entry
, buf
, file
);
230 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
236 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
239 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
242 *value
= (float)strtod(s
, NULL
);
249 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
252 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
255 *value
= strtol(s
, NULL
, 10);
262 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
265 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
268 *value
= (int)strtol(s
, NULL
, 10);
274 #endif // wxUSE_RESOURCES
276 int wxBusyCursorCount
= 0;
277 extern CursHandle gMacCurrentCursor
;
278 CursHandle gMacStoredActiveCursor
= NULL
;
280 // Set the cursor to the busy cursor for all windows
281 void wxBeginBusyCursor(wxCursor
*cursor
)
283 wxBusyCursorCount
++;
284 if (wxBusyCursorCount
== 1)
286 gMacStoredActiveCursor
= gMacCurrentCursor
;
287 ::SetCursor( *::GetCursor( watchCursor
) ) ;
295 // Restore cursor to normal
296 void wxEndBusyCursor()
298 if (wxBusyCursorCount
== 0)
301 wxBusyCursorCount
--;
302 if (wxBusyCursorCount
== 0)
304 if ( gMacStoredActiveCursor
)
305 ::SetCursor( *gMacStoredActiveCursor
) ;
309 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
311 gMacStoredActiveCursor
= NULL
;
315 // TRUE if we're between the above two calls
318 return (wxBusyCursorCount
> 0);
321 wxString
wxMacFindFolder( short vol
,
323 Boolean createFolder
)
329 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
332 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
334 strDir
= wxMacFSSpec2MacFilename( &file
) + ":" ;
341 char *wxGetUserHome (const wxString
& user
)
348 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
358 int pos
= p
.Find(':') ;
359 if ( pos
!= wxNOT_FOUND
) {
368 wxMacStringToPascal( p
, volumeName
) ;
369 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
370 if ( err
== noErr
) {
372 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
375 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
379 return err
== noErr
;
382 // Check whether this window wants to process messages, e.g. Stop button
383 // in long calculations.
384 bool wxCheckForInterrupt(wxWindow
*wnd
)
390 void wxGetMousePosition( int* x
, int* y
)
395 LocalToGlobal( &pt
) ;
400 // Return TRUE if we have a colour display
401 bool wxColourDisplay()
406 // Returns depth of screen
410 SetRect(&globRect
, -32760, -32760, 32760, 32760);
411 GDHandle theMaxDevice
;
414 theMaxDevice
= GetMaxDevice(&globRect
);
415 if (theMaxDevice
!= nil
)
416 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
421 // Get size of display
422 void wxDisplaySize(int *width
, int *height
)
425 GetQDGlobalsScreenBits( &screenBits
);
427 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
428 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
431 GetThemeMenuBarHeight( &mheight
) ;
434 *height
-= LMGetMBarHeight() ;
438 void wxDisplaySizeMM(int *width
, int *height
)
440 wxDisplaySize(width
, height
);
443 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
445 // This is supposed to return desktop dimensions minus any window
446 // manager panels, menus, taskbars, etc. If there is a way to do that
447 // for this platform please fix this function, otherwise it defaults
448 // to the entire desktop.
451 wxDisplaySize(width
, height
);
454 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
456 return wxGenericFindWindowAtPoint(pt
);