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 // Output a debug message, in a system dependent fashion.
168 void wxDebugMsg(const char *fmt
...)
171 static char buffer
[512];
173 if (!wxTheApp
->GetWantDebugOutput())
178 vsprintf(buffer
,fmt
,ap
) ;
179 strcat(buffer
,";g") ;
181 DebugStr((unsigned char*) buffer
) ;
186 // Non-fatal error: pop up message box and (possibly) continue
187 void wxError(const wxString
& msg
, const wxString
& title
)
189 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
190 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
194 // Fatal error: pop up message box and abort
195 void wxFatalError(const wxString
& msg
, const wxString
& title
)
197 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
198 wxMessageBox(wxBuffer
);
201 #endif // !__DARWIN__
209 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
213 // are there x-platform conventions ?
215 Gestalt(gestaltSystemVersion
, &theSystem
) ;
216 if (minorVsn
!= NULL
) {
217 *minorVsn
= (theSystem
& 0xFF ) ;
219 if (majorVsn
!= NULL
) {
220 *majorVsn
= (theSystem
>> 8 ) ;
229 // Reading and writing resources (eg WIN.INI, .Xdefaults)
231 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
237 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
240 sprintf(buf
, "%.4f", value
);
241 return wxWriteResource(section
, entry
, buf
, file
);
244 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
247 sprintf(buf
, "%ld", value
);
248 return wxWriteResource(section
, entry
, buf
, file
);
251 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
254 sprintf(buf
, "%d", value
);
255 return wxWriteResource(section
, entry
, buf
, file
);
258 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
264 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
267 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
270 *value
= (float)strtod(s
, NULL
);
277 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
280 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
283 *value
= strtol(s
, NULL
, 10);
290 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
293 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
296 *value
= (int)strtol(s
, NULL
, 10);
302 #endif // wxUSE_RESOURCES
304 int wxBusyCursorCount
= 0;
305 extern CursHandle gMacCurrentCursor
;
306 CursHandle gMacStoredActiveCursor
= NULL
;
308 // Set the cursor to the busy cursor for all windows
309 void wxBeginBusyCursor(wxCursor
*cursor
)
311 wxBusyCursorCount
++;
312 if (wxBusyCursorCount
== 1)
314 gMacStoredActiveCursor
= gMacCurrentCursor
;
315 ::SetCursor( *::GetCursor( watchCursor
) ) ;
323 // Restore cursor to normal
324 void wxEndBusyCursor()
326 if (wxBusyCursorCount
== 0)
329 wxBusyCursorCount
--;
330 if (wxBusyCursorCount
== 0)
332 if ( gMacStoredActiveCursor
)
333 ::SetCursor( *gMacStoredActiveCursor
) ;
337 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
339 gMacStoredActiveCursor
= NULL
;
343 // TRUE if we're between the above two calls
346 return (wxBusyCursorCount
> 0);
349 wxString
wxMacFindFolder( short vol
,
351 Boolean createFolder
)
357 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
360 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
362 strDir
= wxMacFSSpec2MacFilename( &file
) + wxFILE_SEP_PATH
;
369 char *wxGetUserHome (const wxString
& user
)
375 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
385 int pos
= p
.Find(':') ;
386 if ( pos
!= wxNOT_FOUND
) {
395 wxMacStringToPascal( p
, volumeName
) ;
396 OSErr err
= XGetVolumeInfoNoName( volumeName
, 0 , &pb
) ;
397 if ( err
== noErr
) {
399 (*pTotal
) = wxLongLong( pb
.ioVTotalBytes
) ;
402 (*pFree
) = wxLongLong( pb
.ioVFreeBytes
) ;
406 return err
== noErr
;
410 // Check whether this window wants to process messages, e.g. Stop button
411 // in long calculations.
412 bool wxCheckForInterrupt(wxWindow
*wnd
)
418 void wxGetMousePosition( int* x
, int* y
)
423 LocalToGlobal( &pt
) ;
428 // Return TRUE if we have a colour display
429 bool wxColourDisplay()
434 // Returns depth of screen
438 SetRect(&globRect
, -32760, -32760, 32760, 32760);
439 GDHandle theMaxDevice
;
442 theMaxDevice
= GetMaxDevice(&globRect
);
443 if (theMaxDevice
!= nil
)
444 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
449 // Get size of display
450 void wxDisplaySize(int *width
, int *height
)
453 GetQDGlobalsScreenBits( &screenBits
);
455 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
456 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
459 GetThemeMenuBarHeight( &mheight
) ;
462 *height
-= LMGetMBarHeight() ;
466 void wxDisplaySizeMM(int *width
, int *height
)
468 wxDisplaySize(width
, height
);
471 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
473 // This is supposed to return desktop dimensions minus any window
474 // manager panels, menus, taskbars, etc. If there is a way to do that
475 // for this platform please fix this function, otherwise it defaults
476 // to the entire desktop.
479 wxDisplaySize(width
, height
);
482 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
484 return wxGenericFindWindowAtPoint(pt
);
487 wxString
wxGetOsDescription()
489 #ifdef WXWIN_OS_DESCRIPTION
490 // use configure generated description if available
491 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION
+ wxString(")");
493 return "MacOS" ; //TODO:define further