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"
31 // defined in unix/utilsunx.cpp for Mac OS X
33 // get full hostname (with domain name if possible)
34 bool wxGetFullHostName(wxChar
*buf
, int maxSize
)
36 return wxGetHostName(buf
, maxSize
);
39 // Get hostname only (without domain name)
40 bool wxGetHostName(char *buf
, int maxSize
)
42 // Gets Chooser name of user by examining a System resource.
44 const short kComputerNameID
= -16413;
46 short oldResFile
= CurResFile() ;
48 StringHandle chooserName
= (StringHandle
)::GetString(kComputerNameID
);
49 UseResFile(oldResFile
);
51 if (chooserName
&& *chooserName
)
53 int length
= (*chooserName
)[0] ;
54 if ( length
+ 1 > maxSize
)
56 length
= maxSize
- 1 ;
58 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
67 // Get user ID e.g. jacs
68 bool wxGetUserId(char *buf
, int maxSize
)
70 return wxGetUserName( buf
, maxSize
) ;
73 const wxChar
* wxGetHomeDir(wxString
*pstr
)
75 *pstr
= wxMacFindFolder( (short) kOnSystemDisk
, kPreferencesFolderType
, kDontCreateFolder
) ;
76 return pstr
->c_str() ;
79 // Get user name e.g. AUTHOR
80 bool wxGetUserName(char *buf
, int maxSize
)
82 // Gets Chooser name of user by examining a System resource.
84 const short kChooserNameID
= -16096;
86 short oldResFile
= CurResFile() ;
88 StringHandle chooserName
= (StringHandle
)::GetString(kChooserNameID
);
89 UseResFile(oldResFile
);
91 if (chooserName
&& *chooserName
)
93 int length
= (*chooserName
)[0] ;
94 if ( length
+ 1 > maxSize
)
96 length
= maxSize
- 1 ;
98 strncpy( buf
, (char*) &(*chooserName
)[1] , length
) ;
107 int wxKill(long pid
, int sig
)
114 // Execute a program in an Interactive Shell
116 bool wxShell(const wxString
& command
)
122 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
123 long wxGetFreeMemory()
128 void wxUsleep(unsigned long milliseconds
)
130 clock_t start
= clock() ;
134 } while( clock() - start
< milliseconds
/ CLOCKS_PER_SEC
) ;
137 void wxSleep(int nSecs
)
139 wxUsleep(1000*nSecs
);
142 // Consume all events until no more left
147 // Output a debug message, in a system dependent fashion.
148 void wxDebugMsg(const char *fmt
...)
151 static char buffer
[512];
153 if (!wxTheApp
->GetWantDebugOutput())
158 vsprintf(buffer
,fmt
,ap
) ;
159 strcat(buffer
,";g") ;
161 DebugStr((unsigned char*) buffer
) ;
166 // Non-fatal error: pop up message box and (possibly) continue
167 void wxError(const wxString
& msg
, const wxString
& title
)
169 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
170 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
174 // Fatal error: pop up message box and abort
175 void wxFatalError(const wxString
& msg
, const wxString
& title
)
177 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
178 wxMessageBox(wxBuffer
);
189 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
192 Gestalt(gestaltSystemVersion
, &theSystem
) ;
193 *minorVsn
= (theSystem
& 0xFF ) ;
194 *majorVsn
= (theSystem
>> 8 ) ; // are there x-platform conventions ?
198 // Reading and writing resources (eg WIN.INI, .Xdefaults)
200 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
206 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
209 sprintf(buf
, "%.4f", value
);
210 return wxWriteResource(section
, entry
, buf
, file
);
213 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
216 sprintf(buf
, "%ld", value
);
217 return wxWriteResource(section
, entry
, buf
, file
);
220 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
223 sprintf(buf
, "%d", value
);
224 return wxWriteResource(section
, entry
, buf
, file
);
227 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
233 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
236 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
239 *value
= (float)strtod(s
, NULL
);
246 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
249 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
252 *value
= strtol(s
, NULL
, 10);
259 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
262 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
265 *value
= (int)strtol(s
, NULL
, 10);
271 #endif // wxUSE_RESOURCES
273 int wxBusyCursorCount
= 0;
274 extern CursHandle gMacCurrentCursor
;
275 CursHandle gMacStoredActiveCursor
= NULL
;
277 // Set the cursor to the busy cursor for all windows
278 void wxBeginBusyCursor(wxCursor
*cursor
)
280 wxBusyCursorCount
++;
281 if (wxBusyCursorCount
== 1)
283 gMacStoredActiveCursor
= gMacCurrentCursor
;
284 ::SetCursor( *::GetCursor( watchCursor
) ) ;
292 // Restore cursor to normal
293 void wxEndBusyCursor()
295 if (wxBusyCursorCount
== 0)
298 wxBusyCursorCount
--;
299 if (wxBusyCursorCount
== 0)
301 if ( gMacStoredActiveCursor
)
302 ::SetCursor( *gMacStoredActiveCursor
) ;
306 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
308 gMacStoredActiveCursor
= NULL
;
312 // TRUE if we're between the above two calls
315 return (wxBusyCursorCount
> 0);
319 wxString
wxMacFindFolder( short vol
,
321 Boolean createFolder
)
327 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
330 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
332 strDir
= wxMacFSSpec2MacFilename( &file
) + ":" ;
340 char *wxGetUserHome (const wxString
& user
)
347 // Check whether this window wants to process messages, e.g. Stop button
348 // in long calculations.
349 bool wxCheckForInterrupt(wxWindow
*wnd
)
355 void wxGetMousePosition( int* x
, int* y
)
360 LocalToGlobal( &pt
) ;
365 // Return TRUE if we have a colour display
366 bool wxColourDisplay()
371 // Returns depth of screen
375 SetRect(&globRect
, -32760, -32760, 32760, 32760);
376 GDHandle theMaxDevice
;
379 theMaxDevice
= GetMaxDevice(&globRect
);
380 if (theMaxDevice
!= nil
)
381 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
386 // Get size of display
387 void wxDisplaySize(int *width
, int *height
)
390 GetQDGlobalsScreenBits( &screenBits
);
392 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
393 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
396 GetThemeMenuBarHeight( &mheight
) ;
399 *height
-= LMGetMBarHeight() ;
403 void wxDisplaySizeMM(int *width
, int *height
)
405 wxDisplaySize(width
, height
);
408 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
410 // This is supposed to return desktop dimensions minus any window
411 // manager panels, menus, taskbars, etc. If there is a way to do that
412 // for this platform please fix this function, otherwise it defaults
413 // to the entire desktop.
416 wxDisplaySize(width
, height
);
419 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
421 return wxGenericFindWindowAtPoint(pt
);