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