]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
Added missing test in wxObject::Ref() for already
[wxWidgets.git] / src / mac / utils.cpp
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.
14 // #pragma implementation "utils.h"
15 #endif
16
17 #include "wx/setup.h"
18 #include "wx/utils.h"
19 #include "wx/app.h"
20 #include "wx/mac/uma.h"
21
22 #include <ctype.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28
29 #include "MoreFiles.h"
30 #include "MoreFilesExtras.h"
31
32 #ifndef __DARWIN__
33 #include <Threads.h>
34 #include <Sound.h>
35 #endif
36
37 #ifndef __DARWIN__
38 // defined in unix/utilsunx.cpp for Mac OS X
39
40 // get full hostname (with domain name if possible)
41 bool wxGetFullHostName(wxChar *buf, int maxSize)
42 {
43 return wxGetHostName(buf, maxSize);
44 }
45
46 // Get hostname only (without domain name)
47 bool wxGetHostName(char *buf, int maxSize)
48 {
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;
72 }
73
74 // Get user ID e.g. jacs
75 bool wxGetUserId(char *buf, int maxSize)
76 {
77 return wxGetUserName( buf , maxSize ) ;
78 }
79
80 const wxChar* wxGetHomeDir(wxString *pstr)
81 {
82 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
83 return pstr->c_str() ;
84 }
85
86 // Get user name e.g. AUTHOR
87 bool wxGetUserName(char *buf, int maxSize)
88 {
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;
112 }
113
114 int wxKill(long pid, wxSignal sig , wxKillError *rc )
115 {
116 // TODO
117 return 0;
118 }
119
120 WXDLLEXPORT 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
127 WXDLLEXPORT 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
133 //
134 // Execute a program in an Interactive Shell
135 //
136 bool 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)
143 long wxGetFreeMemory()
144 {
145 return FreeMem() ;
146 }
147
148 void wxUsleep(unsigned long milliseconds)
149 {
150 clock_t start = clock() ;
151 do
152 {
153 YieldToAnyThread() ;
154 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
155 }
156
157 void wxSleep(int nSecs)
158 {
159 wxUsleep(1000*nSecs);
160 }
161
162 // Consume all events until no more left
163 void wxFlushEvents()
164 {
165 }
166
167 // Output a debug message, in a system dependent fashion.
168 void 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
178 vsprintf(buffer,fmt,ap) ;
179 strcat(buffer,";g") ;
180 c2pstr(buffer) ;
181 DebugStr((unsigned char*) buffer) ;
182
183 va_end(ap);
184 }
185
186 // Non-fatal error: pop up message box and (possibly) continue
187 void wxError(const wxString& msg, const wxString& title)
188 {
189 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
190 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
191 wxExit();
192 }
193
194 // Fatal error: pop up message box and abort
195 void wxFatalError(const wxString& msg, const wxString& title)
196 {
197 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
198 wxMessageBox(wxBuffer);
199 wxExit();
200 }
201 #endif // !__DARWIN__
202
203 // Emit a beeeeeep
204 void wxBell()
205 {
206 SysBeep(30);
207 }
208
209 int wxGetOsVersion(int *majorVsn, int *minorVsn)
210 {
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
227 }
228
229 // Reading and writing resources (eg WIN.INI, .Xdefaults)
230 #if wxUSE_RESOURCES
231 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
232 {
233 // TODO
234 return FALSE;
235 }
236
237 bool 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
244 bool 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
251 bool 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
258 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
259 {
260 // TODO
261 return FALSE;
262 }
263
264 bool 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
277 bool 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
290 bool 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);
297 delete[] s;
298 return TRUE;
299 }
300 else return FALSE;
301 }
302 #endif // wxUSE_RESOURCES
303
304 int wxBusyCursorCount = 0;
305 extern CursHandle gMacCurrentCursor ;
306 CursHandle gMacStoredActiveCursor = NULL ;
307
308 // Set the cursor to the busy cursor for all windows
309 void wxBeginBusyCursor(wxCursor *cursor)
310 {
311 wxBusyCursorCount ++;
312 if (wxBusyCursorCount == 1)
313 {
314 gMacStoredActiveCursor = gMacCurrentCursor ;
315 ::SetCursor( *::GetCursor( watchCursor ) ) ;
316 }
317 else
318 {
319 // TODO
320 }
321 }
322
323 // Restore cursor to normal
324 void wxEndBusyCursor()
325 {
326 if (wxBusyCursorCount == 0)
327 return;
328
329 wxBusyCursorCount --;
330 if (wxBusyCursorCount == 0)
331 {
332 if ( gMacStoredActiveCursor )
333 ::SetCursor( *gMacStoredActiveCursor ) ;
334 else
335 {
336 Cursor MacArrow ;
337 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
338 }
339 gMacStoredActiveCursor = NULL ;
340 }
341 }
342
343 // TRUE if we're between the above two calls
344 bool wxIsBusy()
345 {
346 return (wxBusyCursorCount > 0);
347 }
348
349 wxString wxMacFindFolder( short vol,
350 OSType folderType,
351 Boolean createFolder)
352 {
353 short vRefNum ;
354 long dirID ;
355 wxString strDir ;
356
357 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
358 {
359 FSSpec file ;
360 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
361 {
362 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
363 }
364 }
365 return strDir ;
366 }
367
368 #ifndef __DARWIN__
369 char *wxGetUserHome (const wxString& user)
370 {
371 // TODO
372 return NULL;
373 }
374
375 bool 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 }
408 #endif
409
410 // Check whether this window wants to process messages, e.g. Stop button
411 // in long calculations.
412 bool wxCheckForInterrupt(wxWindow *wnd)
413 {
414 // TODO
415 return FALSE;
416 }
417
418 void wxGetMousePosition( int* x, int* y )
419 {
420 Point pt ;
421
422 GetMouse( &pt ) ;
423 LocalToGlobal( &pt ) ;
424 *x = pt.h ;
425 *y = pt.v ;
426 };
427
428 // Return TRUE if we have a colour display
429 bool wxColourDisplay()
430 {
431 return TRUE;
432 }
433
434 // Returns depth of screen
435 int wxDisplayDepth()
436 {
437 Rect globRect ;
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;
445
446 return theDepth ;
447 }
448
449 // Get size of display
450 void wxDisplaySize(int *width, int *height)
451 {
452 BitMap screenBits;
453 GetQDGlobalsScreenBits( &screenBits );
454
455 *width = screenBits.bounds.right - screenBits.bounds.left ;
456 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
457 #if TARGET_CARBON
458 SInt16 mheight ;
459 GetThemeMenuBarHeight( &mheight ) ;
460 *height -= mheight ;
461 #else
462 *height -= LMGetMBarHeight() ;
463 #endif
464 }
465
466 void wxDisplaySizeMM(int *width, int *height)
467 {
468 wxDisplaySize(width, height);
469 }
470
471 void 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
482 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
483 {
484 return wxGenericFindWindowAtPoint(pt);
485 }
486
487 wxString wxGetOsDescription()
488 {
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