]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/utils.cpp
changed wxSystemSettings::HasFrameDecorations to more general GetCapability(index)
[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{
0a67a93b
SC
193 long theSystem ;
194 Gestalt(gestaltSystemVersion, &theSystem) ;
195 *minorVsn = (theSystem & 0xFF ) ;
196 *majorVsn = (theSystem >> 8 ) ; // are there x-platform conventions ?
197 return wxMACINTOSH;
e9576ca5
SC
198}
199
200// Reading and writing resources (eg WIN.INI, .Xdefaults)
201#if wxUSE_RESOURCES
202bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
203{
204 // TODO
205 return FALSE;
206}
207
208bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
209{
210 char buf[50];
211 sprintf(buf, "%.4f", value);
212 return wxWriteResource(section, entry, buf, file);
213}
214
215bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
216{
217 char buf[50];
218 sprintf(buf, "%ld", value);
219 return wxWriteResource(section, entry, buf, file);
220}
221
222bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
223{
224 char buf[50];
225 sprintf(buf, "%d", value);
226 return wxWriteResource(section, entry, buf, file);
227}
228
229bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
230{
231 // TODO
232 return FALSE;
233}
234
235bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
236{
237 char *s = NULL;
238 bool succ = wxGetResource(section, entry, (char **)&s, file);
239 if (succ)
240 {
241 *value = (float)strtod(s, NULL);
242 delete[] s;
243 return TRUE;
244 }
245 else return FALSE;
246}
247
248bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
249{
250 char *s = NULL;
251 bool succ = wxGetResource(section, entry, (char **)&s, file);
252 if (succ)
253 {
254 *value = strtol(s, NULL, 10);
255 delete[] s;
256 return TRUE;
257 }
258 else return FALSE;
259}
260
261bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
262{
263 char *s = NULL;
264 bool succ = wxGetResource(section, entry, (char **)&s, file);
265 if (succ)
266 {
267 *value = (int)strtol(s, NULL, 10);
ec5d7799 268 delete[] s;
e9576ca5
SC
269 return TRUE;
270 }
271 else return FALSE;
272}
273#endif // wxUSE_RESOURCES
274
519cb848
SC
275int wxBusyCursorCount = 0;
276extern CursHandle gMacCurrentCursor ;
277CursHandle gMacStoredActiveCursor = NULL ;
e9576ca5
SC
278
279// Set the cursor to the busy cursor for all windows
280void wxBeginBusyCursor(wxCursor *cursor)
281{
282 wxBusyCursorCount ++;
283 if (wxBusyCursorCount == 1)
284 {
519cb848
SC
285 gMacStoredActiveCursor = gMacCurrentCursor ;
286 ::SetCursor( *::GetCursor( watchCursor ) ) ;
e9576ca5
SC
287 }
288 else
289 {
290 // TODO
291 }
292}
293
294// Restore cursor to normal
295void wxEndBusyCursor()
296{
297 if (wxBusyCursorCount == 0)
298 return;
ec5d7799 299
e9576ca5
SC
300 wxBusyCursorCount --;
301 if (wxBusyCursorCount == 0)
302 {
519cb848
SC
303 if ( gMacStoredActiveCursor )
304 ::SetCursor( *gMacStoredActiveCursor ) ;
305 else
2f1ae414
SC
306 {
307 Cursor MacArrow ;
308 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
309 }
519cb848 310 gMacStoredActiveCursor = NULL ;
e9576ca5
SC
311 }
312}
313
314// TRUE if we're between the above two calls
315bool wxIsBusy()
316{
317 return (wxBusyCursorCount > 0);
ec5d7799 318}
e9576ca5 319
e7e1b01e
GD
320wxString wxMacFindFolder( short vol,
321 OSType folderType,
322 Boolean createFolder)
2f1ae414
SC
323{
324 short vRefNum ;
325 long dirID ;
326 wxString strDir ;
ec5d7799 327
2f1ae414
SC
328 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
329 {
330 FSSpec file ;
331 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
332 {
e7e1b01e 333 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
2f1ae414
SC
334 }
335 }
336 return strDir ;
337}
338
f5c6eb5c 339#ifndef __DARWIN__
e9576ca5
SC
340char *wxGetUserHome (const wxString& user)
341{
342 // TODO
343 return NULL;
344}
345
518af45b
SC
346bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
347{
348 if ( path.empty() )
349 return FALSE;
350
351 wxString p = path ;
352 if (p[0] == ':' ) {
353 p = wxGetCwd() + p ;
354 }
355
356 int pos = p.Find(':') ;
357 if ( pos != wxNOT_FOUND ) {
358 p = p.Mid(1,pos) ;
359 }
360
361 p = p + ":" ;
362
363 Str255 volumeName ;
364 XVolumeParam pb ;
365
366 wxMacStringToPascal( p , volumeName ) ;
367 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
368 if ( err == noErr ) {
369 if ( pTotal ) {
370 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
371 }
372 if ( pFree ) {
373 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
374 }
375 }
376
377 return err == noErr ;
378}
e7e1b01e 379#endif
518af45b 380
e9576ca5
SC
381// Check whether this window wants to process messages, e.g. Stop button
382// in long calculations.
383bool wxCheckForInterrupt(wxWindow *wnd)
384{
385 // TODO
386 return FALSE;
387}
388
389void wxGetMousePosition( int* x, int* y )
390{
519cb848 391 Point pt ;
ec5d7799 392
519cb848
SC
393 GetMouse( &pt ) ;
394 LocalToGlobal( &pt ) ;
395 *x = pt.h ;
396 *y = pt.v ;
e9576ca5
SC
397};
398
399// Return TRUE if we have a colour display
400bool wxColourDisplay()
401{
e9576ca5
SC
402 return TRUE;
403}
404
405// Returns depth of screen
406int wxDisplayDepth()
407{
ec5d7799 408 Rect globRect ;
2f1ae414
SC
409 SetRect(&globRect, -32760, -32760, 32760, 32760);
410 GDHandle theMaxDevice;
411
412 int theDepth = 8;
413 theMaxDevice = GetMaxDevice(&globRect);
414 if (theMaxDevice != nil)
415 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 416
2f1ae414 417 return theDepth ;
e9576ca5
SC
418}
419
420// Get size of display
421void wxDisplaySize(int *width, int *height)
422{
2f1ae414
SC
423 BitMap screenBits;
424 GetQDGlobalsScreenBits( &screenBits );
425
426 *width = screenBits.bounds.right - screenBits.bounds.left ;
ec5d7799 427 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
03e11df5 428#if TARGET_CARBON
2f1ae414
SC
429 SInt16 mheight ;
430 GetThemeMenuBarHeight( &mheight ) ;
431 *height -= mheight ;
432#else
433 *height -= LMGetMBarHeight() ;
03e11df5 434#endif
e9576ca5
SC
435}
436
5fde6fcc
GD
437void wxDisplaySizeMM(int *width, int *height)
438{
439 wxDisplaySize(width, height);
440}
441
ec5d7799
RD
442void wxClientDisplayRect(int *x, int *y, int *width, int *height)
443{
444 // This is supposed to return desktop dimensions minus any window
445 // manager panels, menus, taskbars, etc. If there is a way to do that
446 // for this platform please fix this function, otherwise it defaults
447 // to the entire desktop.
448 if (x) *x = 0;
449 if (y) *y = 0;
450 wxDisplaySize(width, height);
451}
452
57591e0e
JS
453wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
454{
455 return wxGenericFindWindowAtPoint(pt);
456}