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