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