]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
added wxShutdown (patch 547443)
[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 // Shutdown or reboot the PC
143 bool wxShutdown(wxShutdownFlags wFlags)
144 {
145 // TODO
146 return FALSE;
147 }
148
149 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
150 long wxGetFreeMemory()
151 {
152 return FreeMem() ;
153 }
154
155 void wxUsleep(unsigned long milliseconds)
156 {
157 clock_t start = clock() ;
158 do
159 {
160 YieldToAnyThread() ;
161 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
162 }
163
164 void wxSleep(int nSecs)
165 {
166 wxUsleep(1000*nSecs);
167 }
168
169 // Consume all events until no more left
170 void wxFlushEvents()
171 {
172 }
173
174 #if WXWIN_COMPATIBILITY_2_2
175
176 // Output a debug message, in a system dependent fashion.
177 void 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
187 vsprintf(buffer,fmt,ap) ;
188 strcat(buffer,";g") ;
189 c2pstr(buffer) ;
190 DebugStr((unsigned char*) buffer) ;
191
192 va_end(ap);
193 }
194
195 // Non-fatal error: pop up message box and (possibly) continue
196 void wxError(const wxString& msg, const wxString& title)
197 {
198 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
199 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
200 wxExit();
201 }
202
203 // Fatal error: pop up message box and abort
204 void wxFatalError(const wxString& msg, const wxString& title)
205 {
206 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
207 wxMessageBox(wxBuffer);
208 wxExit();
209 }
210
211 #endif // WXWIN_COMPATIBILITY_2_2
212
213 #endif // !__DARWIN__
214
215 // Emit a beeeeeep
216 void wxBell()
217 {
218 SysBeep(30);
219 }
220
221 int wxGetOsVersion(int *majorVsn, int *minorVsn)
222 {
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
239 }
240
241 // Reading and writing resources (eg WIN.INI, .Xdefaults)
242 #if wxUSE_RESOURCES
243 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
244 {
245 // TODO
246 return FALSE;
247 }
248
249 bool 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
256 bool 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
263 bool 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
270 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
271 {
272 // TODO
273 return FALSE;
274 }
275
276 bool 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
289 bool 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
302 bool 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);
309 delete[] s;
310 return TRUE;
311 }
312 else return FALSE;
313 }
314 #endif // wxUSE_RESOURCES
315
316 int wxBusyCursorCount = 0;
317 extern CursHandle gMacCurrentCursor ;
318 CursHandle gMacStoredActiveCursor = NULL ;
319
320 // Set the cursor to the busy cursor for all windows
321 void wxBeginBusyCursor(wxCursor *cursor)
322 {
323 wxBusyCursorCount ++;
324 if (wxBusyCursorCount == 1)
325 {
326 gMacStoredActiveCursor = gMacCurrentCursor ;
327 ::SetCursor( *::GetCursor( watchCursor ) ) ;
328 }
329 else
330 {
331 // TODO
332 }
333 }
334
335 // Restore cursor to normal
336 void wxEndBusyCursor()
337 {
338 if (wxBusyCursorCount == 0)
339 return;
340
341 wxBusyCursorCount --;
342 if (wxBusyCursorCount == 0)
343 {
344 if ( gMacStoredActiveCursor )
345 ::SetCursor( *gMacStoredActiveCursor ) ;
346 else
347 {
348 Cursor MacArrow ;
349 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
350 }
351 gMacStoredActiveCursor = NULL ;
352 }
353 }
354
355 // TRUE if we're between the above two calls
356 bool wxIsBusy()
357 {
358 return (wxBusyCursorCount > 0);
359 }
360
361 wxString wxMacFindFolder( short vol,
362 OSType folderType,
363 Boolean createFolder)
364 {
365 short vRefNum ;
366 long dirID ;
367 wxString strDir ;
368
369 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
370 {
371 FSSpec file ;
372 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
373 {
374 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
375 }
376 }
377 return strDir ;
378 }
379
380 #ifndef __DARWIN__
381 char *wxGetUserHome (const wxString& user)
382 {
383 // TODO
384 return NULL;
385 }
386
387 bool 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 }
420 #endif
421
422 // Check whether this window wants to process messages, e.g. Stop button
423 // in long calculations.
424 bool wxCheckForInterrupt(wxWindow *wnd)
425 {
426 // TODO
427 return FALSE;
428 }
429
430 void wxGetMousePosition( int* x, int* y )
431 {
432 Point pt ;
433
434 GetMouse( &pt ) ;
435 LocalToGlobal( &pt ) ;
436 *x = pt.h ;
437 *y = pt.v ;
438 };
439
440 // Return TRUE if we have a colour display
441 bool wxColourDisplay()
442 {
443 return TRUE;
444 }
445
446 // Returns depth of screen
447 int wxDisplayDepth()
448 {
449 Rect globRect ;
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;
457
458 return theDepth ;
459 }
460
461 // Get size of display
462 void wxDisplaySize(int *width, int *height)
463 {
464 BitMap screenBits;
465 GetQDGlobalsScreenBits( &screenBits );
466
467 *width = screenBits.bounds.right - screenBits.bounds.left ;
468 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
469 #if TARGET_CARBON
470 SInt16 mheight ;
471 GetThemeMenuBarHeight( &mheight ) ;
472 *height -= mheight ;
473 #else
474 *height -= LMGetMBarHeight() ;
475 #endif
476 }
477
478 void wxDisplaySizeMM(int *width, int *height)
479 {
480 wxDisplaySize(width, height);
481 }
482
483 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
484 {
485 // This is supposed to return desktop dimensions minus any window
486 // manager panels, menus, taskbars, etc. If there is a way to do that
487 // for this platform please fix this function, otherwise it defaults
488 // to the entire desktop.
489 if (x) *x = 0;
490 if (y) *y = 0;
491 wxDisplaySize(width, height);
492 }
493
494 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
495 {
496 return wxGenericFindWindowAtPoint(pt);
497 }
498
499 wxString wxGetOsDescription()
500 {
501 #ifdef WXWIN_OS_DESCRIPTION
502 // use configure generated description if available
503 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
504 #else
505 return "MacOS" ; //TODO:define further
506 #endif
507 }
508