]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
try to create smaller executables
[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 #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 if (wxMessageBox(wxString::Format(wxT("%s\nContinue?"),msg), title, wxYES_NO) == wxID_NO )
203 wxExit();
204 }
205
206 // Fatal error: pop up message box and abort
207 void wxFatalError(const wxString& msg, const wxString& title)
208 {
209 wxMessageBox(wxString::Format(wxT("%s: %s"),title,msg));
210 wxExit();
211 }
212
213 #endif // WXWIN_COMPATIBILITY_2_2
214
215 #endif // !__DARWIN__
216
217 // Emit a beeeeeep
218 void wxBell()
219 {
220 SysBeep(30);
221 }
222
223 int wxGetOsVersion(int *majorVsn, int *minorVsn)
224 {
225 long theSystem ;
226
227 // are there x-platform conventions ?
228
229 Gestalt(gestaltSystemVersion, &theSystem) ;
230 if (minorVsn != NULL) {
231 *minorVsn = (theSystem & 0xFF ) ;
232 }
233 if (majorVsn != NULL) {
234 *majorVsn = (theSystem >> 8 ) ;
235 }
236 #ifdef __DARWIN__
237 return wxMAC_DARWIN;
238 #else
239 return wxMAC;
240 #endif
241 }
242
243 // Reading and writing resources (eg WIN.INI, .Xdefaults)
244 #if wxUSE_RESOURCES
245 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
246 {
247 // TODO
248 return FALSE;
249 }
250
251 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
252 {
253 char buf[50];
254 sprintf(buf, "%.4f", value);
255 return wxWriteResource(section, entry, buf, file);
256 }
257
258 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
259 {
260 char buf[50];
261 sprintf(buf, "%ld", value);
262 return wxWriteResource(section, entry, buf, file);
263 }
264
265 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
266 {
267 char buf[50];
268 sprintf(buf, "%d", value);
269 return wxWriteResource(section, entry, buf, file);
270 }
271
272 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
273 {
274 // TODO
275 return FALSE;
276 }
277
278 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
279 {
280 char *s = NULL;
281 bool succ = wxGetResource(section, entry, (char **)&s, file);
282 if (succ)
283 {
284 *value = (float)strtod(s, NULL);
285 delete[] s;
286 return TRUE;
287 }
288 else return FALSE;
289 }
290
291 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
292 {
293 char *s = NULL;
294 bool succ = wxGetResource(section, entry, (char **)&s, file);
295 if (succ)
296 {
297 *value = strtol(s, NULL, 10);
298 delete[] s;
299 return TRUE;
300 }
301 else return FALSE;
302 }
303
304 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
305 {
306 char *s = NULL;
307 bool succ = wxGetResource(section, entry, (char **)&s, file);
308 if (succ)
309 {
310 *value = (int)strtol(s, NULL, 10);
311 delete[] s;
312 return TRUE;
313 }
314 else return FALSE;
315 }
316 #endif // wxUSE_RESOURCES
317
318 int gs_wxBusyCursorCount = 0;
319 extern wxCursor gMacCurrentCursor ;
320 wxCursor gMacStoredActiveCursor ;
321
322 // Set the cursor to the busy cursor for all windows
323 void wxBeginBusyCursor(wxCursor *cursor)
324 {
325 if (gs_wxBusyCursorCount++ == 0)
326 {
327 gMacStoredActiveCursor = gMacCurrentCursor ;
328 cursor->MacInstall() ;
329 }
330 //else: nothing to do, already set
331 }
332
333 // Restore cursor to normal
334 void wxEndBusyCursor()
335 {
336 wxCHECK_RET( gs_wxBusyCursorCount > 0,
337 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
338
339 if (--gs_wxBusyCursorCount == 0)
340 {
341 gMacStoredActiveCursor.MacInstall() ;
342 gMacStoredActiveCursor = wxNullCursor ;
343 }
344 }
345
346 // TRUE if we're between the above two calls
347 bool wxIsBusy()
348 {
349 return (gs_wxBusyCursorCount > 0);
350 }
351
352 wxString wxMacFindFolder( short vol,
353 OSType folderType,
354 Boolean createFolder)
355 {
356 short vRefNum ;
357 long dirID ;
358 wxString strDir ;
359
360 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
361 {
362 FSSpec file ;
363 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
364 {
365 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
366 }
367 }
368 return strDir ;
369 }
370
371 #ifndef __DARWIN__
372 char *wxGetUserHome (const wxString& user)
373 {
374 // TODO
375 return NULL;
376 }
377
378 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
379 {
380 if ( path.empty() )
381 return FALSE;
382
383 wxString p = path ;
384 if (p[0] == ':' ) {
385 p = wxGetCwd() + p ;
386 }
387
388 int pos = p.Find(':') ;
389 if ( pos != wxNOT_FOUND ) {
390 p = p.Mid(1,pos) ;
391 }
392
393 p = p + ":" ;
394
395 Str255 volumeName ;
396 XVolumeParam pb ;
397
398 wxMacStringToPascal( p , volumeName ) ;
399 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
400 if ( err == noErr ) {
401 if ( pTotal ) {
402 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
403 }
404 if ( pFree ) {
405 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
406 }
407 }
408
409 return err == noErr ;
410 }
411 #endif
412
413 // Check whether this window wants to process messages, e.g. Stop button
414 // in long calculations.
415 bool wxCheckForInterrupt(wxWindow *wnd)
416 {
417 // TODO
418 return FALSE;
419 }
420
421 void wxGetMousePosition( int* x, int* y )
422 {
423 Point pt ;
424
425 GetMouse( &pt ) ;
426 LocalToGlobal( &pt ) ;
427 *x = pt.h ;
428 *y = pt.v ;
429 };
430
431 // Return TRUE if we have a colour display
432 bool wxColourDisplay()
433 {
434 return TRUE;
435 }
436
437 // Returns depth of screen
438 int wxDisplayDepth()
439 {
440 Rect globRect ;
441 SetRect(&globRect, -32760, -32760, 32760, 32760);
442 GDHandle theMaxDevice;
443
444 int theDepth = 8;
445 theMaxDevice = GetMaxDevice(&globRect);
446 if (theMaxDevice != nil)
447 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
448
449 return theDepth ;
450 }
451
452 // Get size of display
453 void wxDisplaySize(int *width, int *height)
454 {
455 BitMap screenBits;
456 GetQDGlobalsScreenBits( &screenBits );
457
458 *width = screenBits.bounds.right - screenBits.bounds.left ;
459 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
460 }
461
462 void wxDisplaySizeMM(int *width, int *height)
463 {
464 wxDisplaySize(width, height);
465 // on mac 72 is fixed (at least now ;-)
466 float cvPt2Mm = 25.4 / 72;
467 *width = int( *width * cvPt2Mm );
468 *height = int( *height * cvPt2Mm );
469 }
470
471 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
472 {
473 BitMap screenBits;
474 GetQDGlobalsScreenBits( &screenBits );
475
476 if (x) *x = 0;
477 if (y) *y = 0;
478
479 *width = screenBits.bounds.right - screenBits.bounds.left ;
480 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
481
482 SInt16 mheight ;
483 #if TARGET_CARBON
484 GetThemeMenuBarHeight( &mheight ) ;
485 #else
486 mheight = LMGetMBarHeight() ;
487 #endif
488 *height -= mheight ;
489 if ( y )
490 *y = mheight ;
491 }
492
493 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
494 {
495 return wxGenericFindWindowAtPoint(pt);
496 }
497
498 wxString wxGetOsDescription()
499 {
500 #ifdef WXWIN_OS_DESCRIPTION
501 // use configure generated description if available
502 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
503 #else
504 return "MacOS" ; //TODO:define further
505 #endif
506 }
507
508 //---------------------------------------------------------------------------
509 // wxMac Specific utility functions
510 //---------------------------------------------------------------------------
511
512 char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
513 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
514 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
515 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
516 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
517 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
518
519 char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
520 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
521 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
522 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
523 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
524 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
525
526 void wxMacConvertFromPC( const char *from , char *to , int len )
527 {
528 char *c ;
529 if ( from == to )
530 {
531 for( int i = 0 ; i < len ; ++ i )
532 {
533 c = strchr( StringANSI , *from ) ;
534 if ( c != NULL )
535 {
536 *to = StringMac[ c - StringANSI] ;
537 }
538 ++to ;
539 ++from ;
540 }
541 }
542 else
543 {
544 for( int i = 0 ; i < len ; ++ i )
545 {
546 c = strchr( StringANSI , *from ) ;
547 if ( c != NULL )
548 {
549 *to = StringMac[ c - StringANSI] ;
550 }
551 else
552 {
553 *to = *from ;
554 }
555 ++to ;
556 ++from ;
557 }
558 }
559 }
560
561 void wxMacConvertToPC( const char *from , char *to , int len )
562 {
563 char *c ;
564 if ( from == to )
565 {
566 for( int i = 0 ; i < len ; ++ i )
567 {
568 c = strchr( StringMac , *from ) ;
569 if ( c != NULL )
570 {
571 *to = StringANSI[ c - StringMac] ;
572 }
573 ++to ;
574 ++from ;
575 }
576 }
577 else
578 {
579 for( int i = 0 ; i < len ; ++ i )
580 {
581 c = strchr( StringMac , *from ) ;
582 if ( c != NULL )
583 {
584 *to = StringANSI[ c - StringMac] ;
585 }
586 else
587 {
588 *to = *from ;
589 }
590 ++to ;
591 ++from ;
592 }
593 }
594 }
595
596 wxString wxMacMakeMacStringFromPC( const char * p )
597 {
598 wxString result ;
599 int len = strlen ( p ) ;
600 if ( len > 0 )
601 {
602 wxChar* ptr = result.GetWriteBuf(len) ;
603 wxMacConvertFromPC( p , ptr , len ) ;
604 ptr[len] = 0 ;
605 result.UngetWriteBuf( len ) ;
606 }
607 return result ;
608 }
609
610 wxString wxMacMakePCStringFromMac( const char * p )
611 {
612 wxString result ;
613 int len = strlen ( p ) ;
614 if ( len > 0 )
615 {
616 wxChar* ptr = result.GetWriteBuf(len) ;
617 wxMacConvertToPC( p , ptr , len ) ;
618 ptr[len] = 0 ;
619 result.UngetWriteBuf( len ) ;
620 }
621 return result ;
622 }
623
624 wxString wxMacMakeStringFromMacString( const char* from , bool mac2pcEncoding )
625 {
626 if (mac2pcEncoding)
627 {
628 return wxMacMakePCStringFromMac( from ) ;
629 }
630 else
631 {
632 return wxString( from ) ;
633 }
634 }
635
636 //
637 // Pascal Strings
638 //
639
640 wxString wxMacMakeStringFromPascal( ConstStringPtr from , bool mac2pcEncoding )
641 {
642 // this is safe since a pascal string can never be larger than 256 bytes
643 char s[256] ;
644 CopyPascalStringToC( from , s ) ;
645 if (mac2pcEncoding)
646 {
647 return wxMacMakePCStringFromMac( s ) ;
648 }
649 else
650 {
651 return wxString( s ) ;
652 }
653 }
654
655 void wxMacStringToPascal( const char * from , StringPtr to , bool pc2macEncoding )
656 {
657 if (pc2macEncoding)
658 {
659 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
660 }
661 else
662 {
663 CopyCStringToPascal( from , to ) ;
664 }
665 }
666
667 //
668 // CFStringRefs (Carbon only)
669 //
670
671 #if TARGET_CARBON
672 // converts this string into a carbon foundation string with optional pc 2 mac encoding
673 CFStringRef wxMacCreateCFString( const wxString &str , bool pc2macEncoding )
674 {
675 return CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
676 pc2macEncoding ?
677 kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ;
678 }
679
680 #endif //TARGET_CARBON
681