]> git.saurik.com Git - wxWidgets.git/blame - src/mac/utils.cpp
test wxExecute() in the sample
[wxWidgets.git] / src / mac / utils.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose: Various utilities
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13// Note: this is done in utilscmn.cpp now.
e9576ca5
SC
14// #pragma implementation "utils.h"
15#endif
16
17#include "wx/setup.h"
18#include "wx/utils.h"
19#include "wx/app.h"
2f1ae414 20#include "wx/mac/uma.h"
e9576ca5
SC
21
22#include <ctype.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdarg.h>
28
da2b4b7a
GD
29#include "MoreFiles.h"
30#include "MoreFilesExtras.h"
518af45b 31
66a09d47
SC
32#ifndef __DARWIN__
33#include <Threads.h>
34#include <Sound.h>
35#endif
36
f5c6eb5c 37#ifndef __DARWIN__
03e11df5
GD
38// defined in unix/utilsunx.cpp for Mac OS X
39
2f1ae414
SC
40// get full hostname (with domain name if possible)
41bool wxGetFullHostName(wxChar *buf, int maxSize)
42{
43 return wxGetHostName(buf, maxSize);
44}
45
46// Get hostname only (without domain name)
e9576ca5
SC
47bool wxGetHostName(char *buf, int maxSize)
48{
0a67a93b
SC
49 // Gets Chooser name of user by examining a System resource.
50
51 const short kComputerNameID = -16413;
52
53 short oldResFile = CurResFile() ;
54 UseResFile(0);
55 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
56 UseResFile(oldResFile);
57
58 if (chooserName && *chooserName)
59 {
60 int length = (*chooserName)[0] ;
61 if ( length + 1 > maxSize )
62 {
63 length = maxSize - 1 ;
64 }
65 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
66 buf[length] = 0 ;
67 }
68 else
69 buf[0] = 0 ;
70
71 return TRUE;
e9576ca5
SC
72}
73
74// Get user ID e.g. jacs
75bool wxGetUserId(char *buf, int maxSize)
76{
0a67a93b 77 return wxGetUserName( buf , maxSize ) ;
e9576ca5
SC
78}
79
5b781a67
SC
80const wxChar* wxGetHomeDir(wxString *pstr)
81{
82 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
83 return pstr->c_str() ;
84}
85
e9576ca5
SC
86// Get user name e.g. AUTHOR
87bool wxGetUserName(char *buf, int maxSize)
88{
0a67a93b
SC
89 // Gets Chooser name of user by examining a System resource.
90
91 const short kChooserNameID = -16096;
92
93 short oldResFile = CurResFile() ;
94 UseResFile(0);
95 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
96 UseResFile(oldResFile);
97
98 if (chooserName && *chooserName)
99 {
100 int length = (*chooserName)[0] ;
101 if ( length + 1 > maxSize )
102 {
103 length = maxSize - 1 ;
104 }
105 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
106 buf[length] = 0 ;
107 }
108 else
109 buf[0] = 0 ;
110
111 return TRUE;
e9576ca5
SC
112}
113
5dbb17e2 114int wxKill(long pid, wxSignal sig , wxKillError *rc )
e9576ca5
SC
115{
116 // TODO
117 return 0;
118}
119
5dbb17e2
SC
120WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
121{
122 // TODO : under classic there is no environement support, under X yes
123 return false ;
124}
125
126// set the env var name to the given value, return TRUE on success
127WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
128{
129 // TODO : under classic there is no environement support, under X yes
130 return false ;
131}
132
e9576ca5
SC
133//
134// Execute a program in an Interactive Shell
135//
136bool wxShell(const wxString& command)
137{
138 // TODO
139 return FALSE;
140}
141
142// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
143long wxGetFreeMemory()
144{
0a67a93b
SC
145 return FreeMem() ;
146}
147
148void wxUsleep(unsigned long milliseconds)
149{
e7e1b01e
GD
150 clock_t start = clock() ;
151 do
152 {
153 YieldToAnyThread() ;
154 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
e9576ca5
SC
155}
156
157void wxSleep(int nSecs)
158{
0a67a93b 159 wxUsleep(1000*nSecs);
e9576ca5
SC
160}
161
162// Consume all events until no more left
163void wxFlushEvents()
164{
165}
166
73deed44
VZ
167#if WXWIN_COMPATIBILITY_2_2
168
e9576ca5
SC
169// Output a debug message, in a system dependent fashion.
170void wxDebugMsg(const char *fmt ...)
171{
172 va_list ap;
173 static char buffer[512];
174
175 if (!wxTheApp->GetWantDebugOutput())
176 return ;
177
178 va_start(ap, fmt);
179
0a67a93b
SC
180 vsprintf(buffer,fmt,ap) ;
181 strcat(buffer,";g") ;
9ff647cf
SC
182 c2pstr(buffer) ;
183 DebugStr((unsigned char*) buffer) ;
e9576ca5
SC
184
185 va_end(ap);
186}
187
188// Non-fatal error: pop up message box and (possibly) continue
189void wxError(const wxString& msg, const wxString& title)
190{
0a67a93b
SC
191 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
192 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
e9576ca5
SC
193 wxExit();
194}
195
196// Fatal error: pop up message box and abort
197void wxFatalError(const wxString& msg, const wxString& title)
198{
0a67a93b
SC
199 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
200 wxMessageBox(wxBuffer);
201 wxExit();
e9576ca5 202}
73deed44
VZ
203
204#endif // WXWIN_COMPATIBILITY_2_2
205
f5c6eb5c 206#endif // !__DARWIN__
e9576ca5
SC
207
208// Emit a beeeeeep
209void wxBell()
210{
0a67a93b 211 SysBeep(30);
e9576ca5
SC
212}
213
214int wxGetOsVersion(int *majorVsn, int *minorVsn)
215{
ff8fda36
GD
216 long theSystem ;
217
218 // are there x-platform conventions ?
219
220 Gestalt(gestaltSystemVersion, &theSystem) ;
221 if (minorVsn != NULL) {
222 *minorVsn = (theSystem & 0xFF ) ;
223 }
224 if (majorVsn != NULL) {
225 *majorVsn = (theSystem >> 8 ) ;
226 }
227#ifdef __DARWIN__
228 return wxMAC_DARWIN;
229#else
230 return wxMAC;
231#endif
e9576ca5
SC
232}
233
234// Reading and writing resources (eg WIN.INI, .Xdefaults)
235#if wxUSE_RESOURCES
236bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
237{
238 // TODO
239 return FALSE;
240}
241
242bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
243{
244 char buf[50];
245 sprintf(buf, "%.4f", value);
246 return wxWriteResource(section, entry, buf, file);
247}
248
249bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
250{
251 char buf[50];
252 sprintf(buf, "%ld", value);
253 return wxWriteResource(section, entry, buf, file);
254}
255
256bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
257{
258 char buf[50];
259 sprintf(buf, "%d", value);
260 return wxWriteResource(section, entry, buf, file);
261}
262
263bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
264{
265 // TODO
266 return FALSE;
267}
268
269bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
270{
271 char *s = NULL;
272 bool succ = wxGetResource(section, entry, (char **)&s, file);
273 if (succ)
274 {
275 *value = (float)strtod(s, NULL);
276 delete[] s;
277 return TRUE;
278 }
279 else return FALSE;
280}
281
282bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
283{
284 char *s = NULL;
285 bool succ = wxGetResource(section, entry, (char **)&s, file);
286 if (succ)
287 {
288 *value = strtol(s, NULL, 10);
289 delete[] s;
290 return TRUE;
291 }
292 else return FALSE;
293}
294
295bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
296{
297 char *s = NULL;
298 bool succ = wxGetResource(section, entry, (char **)&s, file);
299 if (succ)
300 {
301 *value = (int)strtol(s, NULL, 10);
ec5d7799 302 delete[] s;
e9576ca5
SC
303 return TRUE;
304 }
305 else return FALSE;
306}
307#endif // wxUSE_RESOURCES
308
519cb848
SC
309int wxBusyCursorCount = 0;
310extern CursHandle gMacCurrentCursor ;
311CursHandle gMacStoredActiveCursor = NULL ;
e9576ca5
SC
312
313// Set the cursor to the busy cursor for all windows
314void wxBeginBusyCursor(wxCursor *cursor)
315{
316 wxBusyCursorCount ++;
317 if (wxBusyCursorCount == 1)
318 {
519cb848
SC
319 gMacStoredActiveCursor = gMacCurrentCursor ;
320 ::SetCursor( *::GetCursor( watchCursor ) ) ;
e9576ca5
SC
321 }
322 else
323 {
324 // TODO
325 }
326}
327
328// Restore cursor to normal
329void wxEndBusyCursor()
330{
331 if (wxBusyCursorCount == 0)
332 return;
ec5d7799 333
e9576ca5
SC
334 wxBusyCursorCount --;
335 if (wxBusyCursorCount == 0)
336 {
519cb848
SC
337 if ( gMacStoredActiveCursor )
338 ::SetCursor( *gMacStoredActiveCursor ) ;
339 else
2f1ae414
SC
340 {
341 Cursor MacArrow ;
342 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
343 }
519cb848 344 gMacStoredActiveCursor = NULL ;
e9576ca5
SC
345 }
346}
347
348// TRUE if we're between the above two calls
349bool wxIsBusy()
350{
351 return (wxBusyCursorCount > 0);
ec5d7799 352}
e9576ca5 353
e7e1b01e
GD
354wxString wxMacFindFolder( short vol,
355 OSType folderType,
356 Boolean createFolder)
2f1ae414
SC
357{
358 short vRefNum ;
359 long dirID ;
360 wxString strDir ;
ec5d7799 361
2f1ae414
SC
362 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
363 {
364 FSSpec file ;
365 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
366 {
e7e1b01e 367 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
2f1ae414
SC
368 }
369 }
370 return strDir ;
371}
372
f5c6eb5c 373#ifndef __DARWIN__
e9576ca5
SC
374char *wxGetUserHome (const wxString& user)
375{
376 // TODO
377 return NULL;
378}
379
518af45b
SC
380bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
381{
382 if ( path.empty() )
383 return FALSE;
384
385 wxString p = path ;
386 if (p[0] == ':' ) {
387 p = wxGetCwd() + p ;
388 }
389
390 int pos = p.Find(':') ;
391 if ( pos != wxNOT_FOUND ) {
392 p = p.Mid(1,pos) ;
393 }
394
395 p = p + ":" ;
396
397 Str255 volumeName ;
398 XVolumeParam pb ;
399
400 wxMacStringToPascal( p , volumeName ) ;
401 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
402 if ( err == noErr ) {
403 if ( pTotal ) {
404 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
405 }
406 if ( pFree ) {
407 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
408 }
409 }
410
411 return err == noErr ;
412}
e7e1b01e 413#endif
518af45b 414
e9576ca5
SC
415// Check whether this window wants to process messages, e.g. Stop button
416// in long calculations.
417bool wxCheckForInterrupt(wxWindow *wnd)
418{
419 // TODO
420 return FALSE;
421}
422
423void wxGetMousePosition( int* x, int* y )
424{
519cb848 425 Point pt ;
ec5d7799 426
519cb848
SC
427 GetMouse( &pt ) ;
428 LocalToGlobal( &pt ) ;
429 *x = pt.h ;
430 *y = pt.v ;
e9576ca5
SC
431};
432
433// Return TRUE if we have a colour display
434bool wxColourDisplay()
435{
e9576ca5
SC
436 return TRUE;
437}
438
439// Returns depth of screen
440int wxDisplayDepth()
441{
ec5d7799 442 Rect globRect ;
2f1ae414
SC
443 SetRect(&globRect, -32760, -32760, 32760, 32760);
444 GDHandle theMaxDevice;
445
446 int theDepth = 8;
447 theMaxDevice = GetMaxDevice(&globRect);
448 if (theMaxDevice != nil)
449 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 450
2f1ae414 451 return theDepth ;
e9576ca5
SC
452}
453
454// Get size of display
455void wxDisplaySize(int *width, int *height)
456{
2f1ae414
SC
457 BitMap screenBits;
458 GetQDGlobalsScreenBits( &screenBits );
459
460 *width = screenBits.bounds.right - screenBits.bounds.left ;
ec5d7799 461 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
03e11df5 462#if TARGET_CARBON
2f1ae414
SC
463 SInt16 mheight ;
464 GetThemeMenuBarHeight( &mheight ) ;
465 *height -= mheight ;
466#else
467 *height -= LMGetMBarHeight() ;
03e11df5 468#endif
e9576ca5
SC
469}
470
5fde6fcc
GD
471void wxDisplaySizeMM(int *width, int *height)
472{
473 wxDisplaySize(width, height);
474}
475
ec5d7799
RD
476void wxClientDisplayRect(int *x, int *y, int *width, int *height)
477{
478 // This is supposed to return desktop dimensions minus any window
479 // manager panels, menus, taskbars, etc. If there is a way to do that
480 // for this platform please fix this function, otherwise it defaults
481 // to the entire desktop.
482 if (x) *x = 0;
483 if (y) *y = 0;
484 wxDisplaySize(width, height);
485}
486
57591e0e
JS
487wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
488{
489 return wxGenericFindWindowAtPoint(pt);
490}
5dbb17e2
SC
491
492wxString wxGetOsDescription()
493{
6e73695c
GD
494#ifdef WXWIN_OS_DESCRIPTION
495 // use configure generated description if available
496 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
497#else
498 return "MacOS" ; //TODO:define further
499#endif
500}
501