]> git.saurik.com Git - wxWidgets.git/blame - src/mac/utils.cpp
don't cache the result of IsAlwaysConnected() and don't call IsOnline() unnecessarily...
[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
f6ba47d9
VZ
142// Shutdown or reboot the PC
143bool wxShutdown(wxShutdownFlags wFlags)
144{
145 // TODO
146 return FALSE;
147}
148
e9576ca5
SC
149// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
150long wxGetFreeMemory()
151{
0a67a93b
SC
152 return FreeMem() ;
153}
154
155void wxUsleep(unsigned long milliseconds)
156{
e7e1b01e
GD
157 clock_t start = clock() ;
158 do
159 {
160 YieldToAnyThread() ;
161 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
e9576ca5
SC
162}
163
164void wxSleep(int nSecs)
165{
0a67a93b 166 wxUsleep(1000*nSecs);
e9576ca5
SC
167}
168
169// Consume all events until no more left
170void wxFlushEvents()
171{
172}
173
73deed44
VZ
174#if WXWIN_COMPATIBILITY_2_2
175
e9576ca5
SC
176// Output a debug message, in a system dependent fashion.
177void wxDebugMsg(const char *fmt ...)
178{
179 va_list ap;
180 static char buffer[512];
181
182 if (!wxTheApp->GetWantDebugOutput())
183 return ;
184
185 va_start(ap, fmt);
186
0a67a93b
SC
187 vsprintf(buffer,fmt,ap) ;
188 strcat(buffer,";g") ;
9ff647cf
SC
189 c2pstr(buffer) ;
190 DebugStr((unsigned char*) buffer) ;
e9576ca5
SC
191
192 va_end(ap);
193}
194
195// Non-fatal error: pop up message box and (possibly) continue
196void wxError(const wxString& msg, const wxString& title)
197{
0a67a93b
SC
198 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
199 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
e9576ca5
SC
200 wxExit();
201}
202
203// Fatal error: pop up message box and abort
204void wxFatalError(const wxString& msg, const wxString& title)
205{
0a67a93b
SC
206 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
207 wxMessageBox(wxBuffer);
208 wxExit();
e9576ca5 209}
73deed44
VZ
210
211#endif // WXWIN_COMPATIBILITY_2_2
212
f5c6eb5c 213#endif // !__DARWIN__
e9576ca5
SC
214
215// Emit a beeeeeep
216void wxBell()
217{
0a67a93b 218 SysBeep(30);
e9576ca5
SC
219}
220
221int wxGetOsVersion(int *majorVsn, int *minorVsn)
222{
ff8fda36
GD
223 long theSystem ;
224
225 // are there x-platform conventions ?
226
227 Gestalt(gestaltSystemVersion, &theSystem) ;
228 if (minorVsn != NULL) {
229 *minorVsn = (theSystem & 0xFF ) ;
230 }
231 if (majorVsn != NULL) {
232 *majorVsn = (theSystem >> 8 ) ;
233 }
234#ifdef __DARWIN__
235 return wxMAC_DARWIN;
236#else
237 return wxMAC;
238#endif
e9576ca5
SC
239}
240
241// Reading and writing resources (eg WIN.INI, .Xdefaults)
242#if wxUSE_RESOURCES
243bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
244{
245 // TODO
246 return FALSE;
247}
248
249bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
250{
251 char buf[50];
252 sprintf(buf, "%.4f", value);
253 return wxWriteResource(section, entry, buf, file);
254}
255
256bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
257{
258 char buf[50];
259 sprintf(buf, "%ld", value);
260 return wxWriteResource(section, entry, buf, file);
261}
262
263bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
264{
265 char buf[50];
266 sprintf(buf, "%d", value);
267 return wxWriteResource(section, entry, buf, file);
268}
269
270bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
271{
272 // TODO
273 return FALSE;
274}
275
276bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
277{
278 char *s = NULL;
279 bool succ = wxGetResource(section, entry, (char **)&s, file);
280 if (succ)
281 {
282 *value = (float)strtod(s, NULL);
283 delete[] s;
284 return TRUE;
285 }
286 else return FALSE;
287}
288
289bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
290{
291 char *s = NULL;
292 bool succ = wxGetResource(section, entry, (char **)&s, file);
293 if (succ)
294 {
295 *value = strtol(s, NULL, 10);
296 delete[] s;
297 return TRUE;
298 }
299 else return FALSE;
300}
301
302bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
303{
304 char *s = NULL;
305 bool succ = wxGetResource(section, entry, (char **)&s, file);
306 if (succ)
307 {
308 *value = (int)strtol(s, NULL, 10);
ec5d7799 309 delete[] s;
e9576ca5
SC
310 return TRUE;
311 }
312 else return FALSE;
313}
314#endif // wxUSE_RESOURCES
315
519cb848
SC
316int wxBusyCursorCount = 0;
317extern CursHandle gMacCurrentCursor ;
318CursHandle gMacStoredActiveCursor = NULL ;
e9576ca5
SC
319
320// Set the cursor to the busy cursor for all windows
321void wxBeginBusyCursor(wxCursor *cursor)
322{
323 wxBusyCursorCount ++;
324 if (wxBusyCursorCount == 1)
325 {
519cb848
SC
326 gMacStoredActiveCursor = gMacCurrentCursor ;
327 ::SetCursor( *::GetCursor( watchCursor ) ) ;
e9576ca5
SC
328 }
329 else
330 {
331 // TODO
332 }
333}
334
335// Restore cursor to normal
336void wxEndBusyCursor()
337{
338 if (wxBusyCursorCount == 0)
339 return;
ec5d7799 340
e9576ca5
SC
341 wxBusyCursorCount --;
342 if (wxBusyCursorCount == 0)
343 {
519cb848
SC
344 if ( gMacStoredActiveCursor )
345 ::SetCursor( *gMacStoredActiveCursor ) ;
346 else
2f1ae414
SC
347 {
348 Cursor MacArrow ;
349 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
350 }
519cb848 351 gMacStoredActiveCursor = NULL ;
e9576ca5
SC
352 }
353}
354
355// TRUE if we're between the above two calls
356bool wxIsBusy()
357{
358 return (wxBusyCursorCount > 0);
ec5d7799 359}
e9576ca5 360
e7e1b01e
GD
361wxString wxMacFindFolder( short vol,
362 OSType folderType,
363 Boolean createFolder)
2f1ae414
SC
364{
365 short vRefNum ;
366 long dirID ;
367 wxString strDir ;
ec5d7799 368
2f1ae414
SC
369 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
370 {
371 FSSpec file ;
372 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
373 {
e7e1b01e 374 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
2f1ae414
SC
375 }
376 }
377 return strDir ;
378}
379
f5c6eb5c 380#ifndef __DARWIN__
e9576ca5
SC
381char *wxGetUserHome (const wxString& user)
382{
383 // TODO
384 return NULL;
385}
386
518af45b
SC
387bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
388{
389 if ( path.empty() )
390 return FALSE;
391
392 wxString p = path ;
393 if (p[0] == ':' ) {
394 p = wxGetCwd() + p ;
395 }
396
397 int pos = p.Find(':') ;
398 if ( pos != wxNOT_FOUND ) {
399 p = p.Mid(1,pos) ;
400 }
401
402 p = p + ":" ;
403
404 Str255 volumeName ;
405 XVolumeParam pb ;
406
407 wxMacStringToPascal( p , volumeName ) ;
408 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
409 if ( err == noErr ) {
410 if ( pTotal ) {
411 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
412 }
413 if ( pFree ) {
414 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
415 }
416 }
417
418 return err == noErr ;
419}
e7e1b01e 420#endif
518af45b 421
e9576ca5
SC
422// Check whether this window wants to process messages, e.g. Stop button
423// in long calculations.
424bool wxCheckForInterrupt(wxWindow *wnd)
425{
426 // TODO
427 return FALSE;
428}
429
430void wxGetMousePosition( int* x, int* y )
431{
519cb848 432 Point pt ;
ec5d7799 433
519cb848
SC
434 GetMouse( &pt ) ;
435 LocalToGlobal( &pt ) ;
436 *x = pt.h ;
437 *y = pt.v ;
e9576ca5
SC
438};
439
440// Return TRUE if we have a colour display
441bool wxColourDisplay()
442{
e9576ca5
SC
443 return TRUE;
444}
445
446// Returns depth of screen
447int wxDisplayDepth()
448{
ec5d7799 449 Rect globRect ;
2f1ae414
SC
450 SetRect(&globRect, -32760, -32760, 32760, 32760);
451 GDHandle theMaxDevice;
452
453 int theDepth = 8;
454 theMaxDevice = GetMaxDevice(&globRect);
455 if (theMaxDevice != nil)
456 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 457
2f1ae414 458 return theDepth ;
e9576ca5
SC
459}
460
461// Get size of display
462void wxDisplaySize(int *width, int *height)
463{
7cfebe05 464 wxClientDisplayRect( NULL , NULL , width , height ) ;
e9576ca5
SC
465}
466
5fde6fcc
GD
467void wxDisplaySizeMM(int *width, int *height)
468{
469 wxDisplaySize(width, height);
7cfebe05
SC
470 // on mac 72 is fixed (at least now ;-)
471 *width *= 25.4 / 72 ;
472 *height *= 25.4 / 72 ;
5fde6fcc
GD
473}
474
ec5d7799
RD
475void wxClientDisplayRect(int *x, int *y, int *width, int *height)
476{
7cfebe05
SC
477 BitMap screenBits;
478 GetQDGlobalsScreenBits( &screenBits );
479
ec5d7799
RD
480 if (x) *x = 0;
481 if (y) *y = 0;
7cfebe05
SC
482
483 *width = screenBits.bounds.right - screenBits.bounds.left ;
484 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
485
486 SInt16 mheight ;
487 #if TARGET_CARBON
488 GetThemeMenuBarHeight( &mheight ) ;
489 #else
490 mheight = LMGetMBarHeight() ;
491 #endif
492 *height -= mheight ;
493 if ( y )
494 *y = mheight ;
ec5d7799
RD
495}
496
57591e0e
JS
497wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
498{
499 return wxGenericFindWindowAtPoint(pt);
500}
5dbb17e2
SC
501
502wxString wxGetOsDescription()
503{
6e73695c
GD
504#ifdef WXWIN_OS_DESCRIPTION
505 // use configure generated description if available
506 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
507#else
508 return "MacOS" ; //TODO:define further
509#endif
510}
511