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