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