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") ;
165 // Non-fatal error: pop up message box and (possibly) continue
166 void wxError(const wxString
& msg
, const wxString
& title
)
168 wxSprintf(wxBuffer
, wxT("%s\nContinue?"), WXSTRINGCAST msg
);
169 if (wxMessageBox(wxBuffer
, title
, wxYES_NO
) == wxID_NO
)
173 // Fatal error: pop up message box and abort
174 void wxFatalError(const wxString
& msg
, const wxString
& title
)
176 wxSprintf(wxBuffer
, wxT("%s: %s"), WXSTRINGCAST title
, WXSTRINGCAST msg
);
177 wxMessageBox(wxBuffer
);
188 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
191 Gestalt(gestaltSystemVersion
, &theSystem
) ;
192 *minorVsn
= (theSystem
& 0xFF ) ;
193 *majorVsn
= (theSystem
>> 8 ) ; // are there x-platform conventions ?
197 // Reading and writing resources (eg WIN.INI, .Xdefaults)
199 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
205 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
208 sprintf(buf
, "%.4f", value
);
209 return wxWriteResource(section
, entry
, buf
, file
);
212 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
215 sprintf(buf
, "%ld", value
);
216 return wxWriteResource(section
, entry
, buf
, file
);
219 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
222 sprintf(buf
, "%d", value
);
223 return wxWriteResource(section
, entry
, buf
, file
);
226 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
232 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
235 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
238 *value
= (float)strtod(s
, NULL
);
245 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
248 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
251 *value
= strtol(s
, NULL
, 10);
258 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
261 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
264 *value
= (int)strtol(s
, NULL
, 10);
270 #endif // wxUSE_RESOURCES
272 int wxBusyCursorCount
= 0;
273 extern CursHandle gMacCurrentCursor
;
274 CursHandle gMacStoredActiveCursor
= NULL
;
276 // Set the cursor to the busy cursor for all windows
277 void wxBeginBusyCursor(wxCursor
*cursor
)
279 wxBusyCursorCount
++;
280 if (wxBusyCursorCount
== 1)
282 gMacStoredActiveCursor
= gMacCurrentCursor
;
283 ::SetCursor( *::GetCursor( watchCursor
) ) ;
291 // Restore cursor to normal
292 void wxEndBusyCursor()
294 if (wxBusyCursorCount
== 0)
297 wxBusyCursorCount
--;
298 if (wxBusyCursorCount
== 0)
300 if ( gMacStoredActiveCursor
)
301 ::SetCursor( *gMacStoredActiveCursor
) ;
305 ::SetCursor( GetQDGlobalsArrow( &MacArrow
) ) ;
307 gMacStoredActiveCursor
= NULL
;
311 // TRUE if we're between the above two calls
314 return (wxBusyCursorCount
> 0);
318 wxString
wxMacFindFolder( short vol
,
320 Boolean createFolder
)
326 if ( FindFolder( vol
, folderType
, createFolder
, &vRefNum
, &dirID
) == noErr
)
329 if ( FSMakeFSSpec( vRefNum
, dirID
, "\p" , &file
) == noErr
)
331 strDir
= wxMacFSSpec2UnixFilename( &file
) + "/" ;
339 char *wxGetUserHome (const wxString
& user
)
346 // Check whether this window wants to process messages, e.g. Stop button
347 // in long calculations.
348 bool wxCheckForInterrupt(wxWindow
*wnd
)
354 void wxGetMousePosition( int* x
, int* y
)
359 LocalToGlobal( &pt
) ;
364 // Return TRUE if we have a colour display
365 bool wxColourDisplay()
370 // Returns depth of screen
374 SetRect(&globRect
, -32760, -32760, 32760, 32760);
375 GDHandle theMaxDevice
;
378 theMaxDevice
= GetMaxDevice(&globRect
);
379 if (theMaxDevice
!= nil
)
380 theDepth
= (**(**theMaxDevice
).gdPMap
).pixelSize
;
385 // Get size of display
386 void wxDisplaySize(int *width
, int *height
)
389 GetQDGlobalsScreenBits( &screenBits
);
391 *width
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
392 *height
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
395 GetThemeMenuBarHeight( &mheight
) ;
398 *height
-= LMGetMBarHeight() ;
402 void wxDisplaySizeMM(int *width
, int *height
)
404 wxDisplaySize(width
, height
);
407 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
409 // This is supposed to return desktop dimensions minus any window
410 // manager panels, menus, taskbars, etc. If there is a way to do that
411 // for this platform please fix this function, otherwise it defaults
412 // to the entire desktop.
415 wxDisplaySize(width
, height
);
418 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
420 return wxGenericFindWindowAtPoint(pt
);