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