]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/utils.cpp
added missing include for compilation with Apple Developer Tools
[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
518af45b
SC
30#include "morefile.h"
31#include "moreextr.h"
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
f5c6eb5c 321#ifndef __DARWIN__
2f1ae414
SC
322wxString wxMacFindFolder( short vol,
323 OSType folderType,
324 Boolean createFolder)
325{
326 short vRefNum ;
327 long dirID ;
328 wxString strDir ;
ec5d7799 329
2f1ae414
SC
330 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
331 {
332 FSSpec file ;
333 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
334 {
bedaf53e 335 strDir = wxMacFSSpec2MacFilename( &file ) + ":" ;
2f1ae414
SC
336 }
337 }
338 return strDir ;
339}
5fde6fcc 340#endif
2f1ae414 341
f5c6eb5c 342#ifndef __DARWIN__
e9576ca5
SC
343char *wxGetUserHome (const wxString& user)
344{
345 // TODO
346 return NULL;
347}
03e11df5 348#endif
e9576ca5 349
518af45b
SC
350bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
351{
352 if ( path.empty() )
353 return FALSE;
354
355 wxString p = path ;
356 if (p[0] == ':' ) {
357 p = wxGetCwd() + p ;
358 }
359
360 int pos = p.Find(':') ;
361 if ( pos != wxNOT_FOUND ) {
362 p = p.Mid(1,pos) ;
363 }
364
365 p = p + ":" ;
366
367 Str255 volumeName ;
368 XVolumeParam pb ;
369
370 wxMacStringToPascal( p , volumeName ) ;
371 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
372 if ( err == noErr ) {
373 if ( pTotal ) {
374 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
375 }
376 if ( pFree ) {
377 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
378 }
379 }
380
381 return err == noErr ;
382}
383
e9576ca5
SC
384// Check whether this window wants to process messages, e.g. Stop button
385// in long calculations.
386bool wxCheckForInterrupt(wxWindow *wnd)
387{
388 // TODO
389 return FALSE;
390}
391
392void wxGetMousePosition( int* x, int* y )
393{
519cb848 394 Point pt ;
ec5d7799 395
519cb848
SC
396 GetMouse( &pt ) ;
397 LocalToGlobal( &pt ) ;
398 *x = pt.h ;
399 *y = pt.v ;
e9576ca5
SC
400};
401
402// Return TRUE if we have a colour display
403bool wxColourDisplay()
404{
e9576ca5
SC
405 return TRUE;
406}
407
408// Returns depth of screen
409int wxDisplayDepth()
410{
ec5d7799 411 Rect globRect ;
2f1ae414
SC
412 SetRect(&globRect, -32760, -32760, 32760, 32760);
413 GDHandle theMaxDevice;
414
415 int theDepth = 8;
416 theMaxDevice = GetMaxDevice(&globRect);
417 if (theMaxDevice != nil)
418 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 419
2f1ae414 420 return theDepth ;
e9576ca5
SC
421}
422
423// Get size of display
424void wxDisplaySize(int *width, int *height)
425{
2f1ae414
SC
426 BitMap screenBits;
427 GetQDGlobalsScreenBits( &screenBits );
428
429 *width = screenBits.bounds.right - screenBits.bounds.left ;
ec5d7799 430 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
03e11df5 431#if TARGET_CARBON
2f1ae414
SC
432 SInt16 mheight ;
433 GetThemeMenuBarHeight( &mheight ) ;
434 *height -= mheight ;
435#else
436 *height -= LMGetMBarHeight() ;
03e11df5 437#endif
e9576ca5
SC
438}
439
5fde6fcc
GD
440void wxDisplaySizeMM(int *width, int *height)
441{
442 wxDisplaySize(width, height);
443}
444
ec5d7799
RD
445void wxClientDisplayRect(int *x, int *y, int *width, int *height)
446{
447 // This is supposed to return desktop dimensions minus any window
448 // manager panels, menus, taskbars, etc. If there is a way to do that
449 // for this platform please fix this function, otherwise it defaults
450 // to the entire desktop.
451 if (x) *x = 0;
452 if (y) *y = 0;
453 wxDisplaySize(width, height);
454}
455
57591e0e
JS
456wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
457{
458 return wxGenericFindWindowAtPoint(pt);
459}