]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
replaced wxGetOsVersion() with wxGUIAppTraits::GetOSVersion()
[wxWidgets.git] / src / mac / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
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/apptrait.h"
21 #include "wx/mac/uma.h"
22
23 #include <ctype.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29
30 #ifdef __DARWIN__
31 # include "MoreFilesX.h"
32 #else
33 # include "MoreFiles.h"
34 # include "MoreFilesExtras.h"
35 #endif
36
37 #ifndef __DARWIN__
38 #include <Threads.h>
39 #include <Sound.h>
40 #endif
41
42 #include "ATSUnicode.h"
43 #include "TextCommon.h"
44 #include "TextEncodingConverter.h"
45
46 #if defined(__WXMAC__)
47 #include "wx/mac/private.h" // includes mac headers
48 #endif
49
50 #if defined(__MWERKS__) && wxUSE_UNICODE
51 #include <wtime.h>
52 #endif
53
54
55 #ifndef __DARWIN__
56 // defined in unix/utilsunx.cpp for Mac OS X
57
58 // get full hostname (with domain name if possible)
59 bool wxGetFullHostName(wxChar *buf, int maxSize)
60 {
61 return wxGetHostName(buf, maxSize);
62 }
63
64 // Get hostname only (without domain name)
65 bool wxGetHostName(wxChar *buf, int maxSize)
66 {
67 // Gets Chooser name of user by examining a System resource.
68
69 const short kComputerNameID = -16413;
70
71 short oldResFile = CurResFile() ;
72 UseResFile(0);
73 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
74 UseResFile(oldResFile);
75
76 if (chooserName && *chooserName)
77 {
78 HLock( (Handle) chooserName ) ;
79 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
80 HUnlock( (Handle) chooserName ) ;
81 ReleaseResource( (Handle) chooserName ) ;
82 wxStrncpy( buf , name , maxSize - 1 ) ;
83 }
84 else
85 buf[0] = 0 ;
86
87 return TRUE;
88 }
89
90 // Get user ID e.g. jacs
91 bool wxGetUserId(wxChar *buf, int maxSize)
92 {
93 return wxGetUserName( buf , maxSize ) ;
94 }
95
96 const wxChar* wxGetHomeDir(wxString *pstr)
97 {
98 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
99 return pstr->c_str() ;
100 }
101
102 // Get user name e.g. Stefan Csomor
103 bool wxGetUserName(wxChar *buf, int maxSize)
104 {
105 // Gets Chooser name of user by examining a System resource.
106
107 const short kChooserNameID = -16096;
108
109 short oldResFile = CurResFile() ;
110 UseResFile(0);
111 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
112 UseResFile(oldResFile);
113
114 if (chooserName && *chooserName)
115 {
116 HLock( (Handle) chooserName ) ;
117 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
118 HUnlock( (Handle) chooserName ) ;
119 ReleaseResource( (Handle) chooserName ) ;
120 wxStrncpy( buf , name , maxSize - 1 ) ;
121 }
122 else
123 buf[0] = 0 ;
124
125 return TRUE;
126 }
127
128 int wxKill(long pid, wxSignal sig , wxKillError *rc )
129 {
130 // TODO
131 return 0;
132 }
133
134 WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
135 {
136 // TODO : under classic there is no environement support, under X yes
137 return false ;
138 }
139
140 // set the env var name to the given value, return TRUE on success
141 WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
142 {
143 // TODO : under classic there is no environement support, under X yes
144 return false ;
145 }
146
147 //
148 // Execute a program in an Interactive Shell
149 //
150 bool wxShell(const wxString& command)
151 {
152 // TODO
153 return FALSE;
154 }
155
156 // Shutdown or reboot the PC
157 bool wxShutdown(wxShutdownFlags wFlags)
158 {
159 // TODO
160 return FALSE;
161 }
162
163 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
164 long wxGetFreeMemory()
165 {
166 return FreeMem() ;
167 }
168
169 void wxUsleep(unsigned long milliseconds)
170 {
171 clock_t start = clock() ;
172 do
173 {
174 YieldToAnyThread() ;
175 } while( clock() - start < milliseconds / 1000.0 * CLOCKS_PER_SEC ) ;
176 }
177
178 void wxSleep(int nSecs)
179 {
180 wxUsleep(1000*nSecs);
181 }
182
183 // Consume all events until no more left
184 void wxFlushEvents()
185 {
186 }
187
188 #endif // !__DARWIN__
189
190 // Emit a beeeeeep
191 void wxBell()
192 {
193 SysBeep(30);
194 }
195
196 int wxGUIAppTraits::GetOSVersion(int *majorVsn, int *minorVsn)
197 {
198 long theSystem ;
199
200 // are there x-platform conventions ?
201
202 Gestalt(gestaltSystemVersion, &theSystem) ;
203 if (minorVsn != NULL) {
204 *minorVsn = (theSystem & 0xFF ) ;
205 }
206 if (majorVsn != NULL) {
207 *majorVsn = (theSystem >> 8 ) ;
208 }
209 #ifdef __DARWIN__
210 return wxMAC_DARWIN;
211 #else
212 return wxMAC;
213 #endif
214 }
215
216 // Reading and writing resources (eg WIN.INI, .Xdefaults)
217 #if wxUSE_RESOURCES
218 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
219 {
220 // TODO
221 return FALSE;
222 }
223
224 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
225 {
226 wxString buf;
227 buf.Printf(wxT("%.4f"), value);
228
229 return wxWriteResource(section, entry, buf, file);
230 }
231
232 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
233 {
234 wxString buf;
235 buf.Printf(wxT("%ld"), value);
236
237 return wxWriteResource(section, entry, buf, file);
238 }
239
240 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
241 {
242 wxString buf;
243 buf.Printf(wxT("%d"), value);
244
245 return wxWriteResource(section, entry, buf, file);
246 }
247
248 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
249 {
250 // TODO
251 return FALSE;
252 }
253
254 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
255 {
256 char *s = NULL;
257 bool succ = wxGetResource(section, entry, (char **)&s, file);
258 if (succ)
259 {
260 *value = (float)strtod(s, NULL);
261 delete[] s;
262 return TRUE;
263 }
264 else return FALSE;
265 }
266
267 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
268 {
269 char *s = NULL;
270 bool succ = wxGetResource(section, entry, (char **)&s, file);
271 if (succ)
272 {
273 *value = strtol(s, NULL, 10);
274 delete[] s;
275 return TRUE;
276 }
277 else return FALSE;
278 }
279
280 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
281 {
282 char *s = NULL;
283 bool succ = wxGetResource(section, entry, (char **)&s, file);
284 if (succ)
285 {
286 *value = (int)strtol(s, NULL, 10);
287 delete[] s;
288 return TRUE;
289 }
290 else return FALSE;
291 }
292 #endif // wxUSE_RESOURCES
293
294 int gs_wxBusyCursorCount = 0;
295 extern wxCursor gMacCurrentCursor ;
296 wxCursor gMacStoredActiveCursor ;
297
298 // Set the cursor to the busy cursor for all windows
299 void wxBeginBusyCursor(wxCursor *cursor)
300 {
301 if (gs_wxBusyCursorCount++ == 0)
302 {
303 gMacStoredActiveCursor = gMacCurrentCursor ;
304 cursor->MacInstall() ;
305 }
306 //else: nothing to do, already set
307 }
308
309 // Restore cursor to normal
310 void wxEndBusyCursor()
311 {
312 wxCHECK_RET( gs_wxBusyCursorCount > 0,
313 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
314
315 if (--gs_wxBusyCursorCount == 0)
316 {
317 gMacStoredActiveCursor.MacInstall() ;
318 gMacStoredActiveCursor = wxNullCursor ;
319 }
320 }
321
322 // TRUE if we're between the above two calls
323 bool wxIsBusy()
324 {
325 return (gs_wxBusyCursorCount > 0);
326 }
327
328 wxString wxMacFindFolder( short vol,
329 OSType folderType,
330 Boolean createFolder)
331 {
332 short vRefNum ;
333 long dirID ;
334 wxString strDir ;
335
336 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
337 {
338 FSSpec file ;
339 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
340 {
341 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
342 }
343 }
344 return strDir ;
345 }
346
347 #ifndef __DARWIN__
348 wxChar *wxGetUserHome (const wxString& user)
349 {
350 // TODO
351 return NULL;
352 }
353
354 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
355 {
356 if ( path.empty() )
357 return FALSE;
358
359 wxString p = path ;
360 if (p[0] == ':' ) {
361 p = wxGetCwd() + p ;
362 }
363
364 int pos = p.Find(':') ;
365 if ( pos != wxNOT_FOUND ) {
366 p = p.Mid(1,pos) ;
367 }
368
369 p = p + wxT(":") ;
370
371 Str255 volumeName ;
372 XVolumeParam pb ;
373
374 wxMacStringToPascal( p , volumeName ) ;
375 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
376 if ( err == noErr ) {
377 if ( pTotal ) {
378 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
379 }
380 if ( pFree ) {
381 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
382 }
383 }
384
385 return err == noErr ;
386 }
387 #endif
388
389 // Check whether this window wants to process messages, e.g. Stop button
390 // in long calculations.
391 bool wxCheckForInterrupt(wxWindow *wnd)
392 {
393 // TODO
394 return FALSE;
395 }
396
397 void wxGetMousePosition( int* x, int* y )
398 {
399 Point pt ;
400
401 GetMouse( &pt ) ;
402 LocalToGlobal( &pt ) ;
403 *x = pt.h ;
404 *y = pt.v ;
405 };
406
407 // Return TRUE if we have a colour display
408 bool wxColourDisplay()
409 {
410 return TRUE;
411 }
412
413 // Returns depth of screen
414 int wxDisplayDepth()
415 {
416 Rect globRect ;
417 SetRect(&globRect, -32760, -32760, 32760, 32760);
418 GDHandle theMaxDevice;
419
420 int theDepth = 8;
421 theMaxDevice = GetMaxDevice(&globRect);
422 if (theMaxDevice != nil)
423 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
424
425 return theDepth ;
426 }
427
428 // Get size of display
429 void wxDisplaySize(int *width, int *height)
430 {
431 BitMap screenBits;
432 GetQDGlobalsScreenBits( &screenBits );
433
434 if (width != NULL) {
435 *width = screenBits.bounds.right - screenBits.bounds.left ;
436 }
437 if (height != NULL) {
438 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
439 }
440 }
441
442 void wxDisplaySizeMM(int *width, int *height)
443 {
444 wxDisplaySize(width, height);
445 // on mac 72 is fixed (at least now ;-)
446 float cvPt2Mm = 25.4 / 72;
447
448 if (width != NULL) {
449 *width = int( *width * cvPt2Mm );
450 }
451 if (height != NULL) {
452 *height = int( *height * cvPt2Mm );
453 }
454 }
455
456 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
457 {
458 BitMap screenBits;
459 GetQDGlobalsScreenBits( &screenBits );
460
461 if (x) *x = 0;
462 if (y) *y = 0;
463
464 if (width != NULL) {
465 *width = screenBits.bounds.right - screenBits.bounds.left ;
466 }
467 if (height != NULL) {
468 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
469 }
470
471 SInt16 mheight ;
472 #if TARGET_CARBON
473 GetThemeMenuBarHeight( &mheight ) ;
474 #else
475 mheight = LMGetMBarHeight() ;
476 #endif
477 if (height != NULL) {
478 *height -= mheight ;
479 }
480 if (y)
481 *y = mheight ;
482 }
483
484 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
485 {
486 return wxGenericFindWindowAtPoint(pt);
487 }
488
489 wxString wxGetOsDescription()
490 {
491 #ifdef WXWIN_OS_DESCRIPTION
492 // use configure generated description if available
493 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
494 #else
495 return wxT("MacOS") ; //TODO:define further
496 #endif
497 }
498
499 //---------------------------------------------------------------------------
500 // wxMac Specific utility functions
501 //---------------------------------------------------------------------------
502
503 char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
504 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
505 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
506 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
507 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
508 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
509
510 char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
511 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
512 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
513 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
514 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
515 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
516
517 void wxMacConvertFromPC( const char *from , char *to , int len )
518 {
519 char *c ;
520 if ( from == to )
521 {
522 for( int i = 0 ; i < len ; ++ i )
523 {
524 c = strchr( StringANSI , *from ) ;
525 if ( c != NULL )
526 {
527 *to = StringMac[ c - StringANSI] ;
528 }
529 ++to ;
530 ++from ;
531 }
532 }
533 else
534 {
535 for( int i = 0 ; i < len ; ++ i )
536 {
537 c = strchr( StringANSI , *from ) ;
538 if ( c != NULL )
539 {
540 *to = StringMac[ c - StringANSI] ;
541 }
542 else
543 {
544 *to = *from ;
545 }
546 ++to ;
547 ++from ;
548 }
549 }
550 }
551
552 void wxMacConvertToPC( const char *from , char *to , int len )
553 {
554 char *c ;
555 if ( from == to )
556 {
557 for( int i = 0 ; i < len ; ++ i )
558 {
559 c = strchr( StringMac , *from ) ;
560 if ( c != NULL )
561 {
562 *to = StringANSI[ c - StringMac] ;
563 }
564 ++to ;
565 ++from ;
566 }
567 }
568 else
569 {
570 for( int i = 0 ; i < len ; ++ i )
571 {
572 c = strchr( StringMac , *from ) ;
573 if ( c != NULL )
574 {
575 *to = StringANSI[ c - StringMac] ;
576 }
577 else
578 {
579 *to = *from ;
580 }
581 ++to ;
582 ++from ;
583 }
584 }
585 }
586
587 TECObjectRef s_TECNativeCToUnicode = NULL ;
588 TECObjectRef s_TECUnicodeToNativeC = NULL ;
589 TECObjectRef s_TECPlatformToNativeC = NULL ;
590 TECObjectRef s_TECNativeCToPlatform = NULL ;
591 void wxMacSetupConverters()
592 {
593 // if we assume errors are happening here we need low level debugging
594 // since the high level assert will use the encoders that are not yet
595 // setup...
596 const int kEncoding = wxApp::s_macDefaultEncodingIsPC
597 ? (int)kTextEncodingWindowsLatin1
598 : (int)kTextEncodingMacRoman;
599
600 OSStatus status = noErr ;
601 status = TECCreateConverter(&s_TECNativeCToUnicode,
602 kEncoding,
603 kTextEncodingUnicodeDefault);
604
605
606 status = TECCreateConverter(&s_TECUnicodeToNativeC,
607 kTextEncodingUnicodeDefault,
608 kEncoding);
609
610 if ( wxApp::s_macDefaultEncodingIsPC )
611 {
612 status = TECCreateConverter(&s_TECPlatformToNativeC,
613 kTextEncodingMacRoman,
614 kTextEncodingWindowsLatin1);
615
616
617 status = TECCreateConverter(&s_TECNativeCToPlatform,
618 kTextEncodingWindowsLatin1,
619 kTextEncodingMacRoman);
620 }
621 }
622
623 void wxMacCleanupConverters()
624 {
625 OSStatus status = noErr ;
626 status = TECDisposeConverter(s_TECNativeCToUnicode);
627
628 status = TECDisposeConverter(s_TECUnicodeToNativeC);
629
630 status = TECDisposeConverter(s_TECPlatformToNativeC);
631
632 status = TECDisposeConverter(s_TECNativeCToPlatform);
633 }
634
635 wxWCharBuffer wxMacStringToWString( const wxString &from )
636 {
637 #if wxUSE_UNICODE
638 wxWCharBuffer result( from.wc_str() ) ;
639 #else
640 OSStatus status = noErr ;
641 ByteCount byteOutLen ;
642 ByteCount byteInLen = from.Length() ;
643 ByteCount byteBufferLen = byteInLen *2 ;
644 wxWCharBuffer result( from.Length() ) ;
645 status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from.c_str() , byteInLen, &byteInLen,
646 (TextPtr)result.data(), byteBufferLen, &byteOutLen);
647 result.data()[byteOutLen/2] = 0 ;
648 #endif
649 return result ;
650 }
651
652 wxString wxMacMakeStringFromCString( const char * from , int len )
653 {
654 OSStatus status = noErr ;
655 wxString result ;
656 wxChar* buf = result.GetWriteBuf( len ) ;
657 #if wxUSE_UNICODE
658 ByteCount byteOutLen ;
659 ByteCount byteInLen = len ;
660 ByteCount byteBufferLen = len *2 ;
661
662 status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from , byteInLen, &byteInLen,
663 (TextPtr)buf, byteBufferLen, &byteOutLen);
664 #else
665 if ( !wxApp::s_macDefaultEncodingIsPC )
666 memcpy( buf , from , len ) ;
667 else
668 {
669 ByteCount byteOutLen ;
670 ByteCount byteInLen = len ;
671 ByteCount byteBufferLen = byteInLen ;
672
673 status = TECConvertText(s_TECPlatformToNativeC, (ConstTextPtr)from , byteInLen, &byteInLen,
674 (TextPtr)buf, byteBufferLen, &byteOutLen);
675 }
676 #endif
677 buf[len] = 0 ;
678 result.UngetWriteBuf() ;
679 return result ;
680 }
681
682 wxString wxMacMakeStringFromCString( const char * from )
683 {
684 return wxMacMakeStringFromCString( from , strlen(from) ) ;
685 }
686
687 wxCharBuffer wxMacStringToCString( const wxString &from )
688 {
689 #if wxUSE_UNICODE
690 OSStatus status = noErr ;
691 ByteCount byteOutLen ;
692 ByteCount byteInLen = from.Length() * 2 ;
693 ByteCount byteBufferLen = from.Length() ;
694 wxCharBuffer result( from.Length() ) ;
695 status = TECConvertText(s_TECUnicodeToNativeC , (ConstTextPtr)from.wc_str() , byteInLen, &byteInLen,
696 (TextPtr)result.data(), byteBufferLen, &byteOutLen);
697 return result ;
698 #else
699 if ( !wxApp::s_macDefaultEncodingIsPC )
700 return wxCharBuffer( from.c_str() ) ;
701 else
702 {
703 wxCharBuffer result( from.Length() ) ;
704 OSStatus status = noErr ;
705 ByteCount byteOutLen ;
706 ByteCount byteInLen = from.Length() ;
707 ByteCount byteBufferLen = byteInLen ;
708
709 status = TECConvertText(s_TECNativeCToPlatform, (ConstTextPtr)from.c_str() , byteInLen, &byteInLen,
710 (TextPtr)result.data(), byteBufferLen, &byteOutLen);
711 return result ;
712 }
713 #endif
714 }
715
716 void wxMacStringToPascal( const wxString&from , StringPtr to )
717 {
718 wxCharBuffer buf = wxMacStringToCString( from ) ;
719 int len = strlen(buf) ;
720
721 if ( len > 255 )
722 len = 255 ;
723 to[0] = len ;
724 memcpy( (char*) &to[1] , buf , len ) ;
725 }
726
727 wxString wxMacMakeStringFromPascal( ConstStringPtr from )
728 {
729 return wxMacMakeStringFromCString( (char*) &from[1] , from[0] ) ;
730 }
731
732 //
733 // CFStringRefs (Carbon only)
734 //
735
736 #if TARGET_CARBON
737 // converts this string into a carbon foundation string with optional pc 2 mac encoding
738 void wxMacCFStringHolder::Assign( const wxString &str )
739 {
740 #if wxUSE_UNICODE
741 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault,
742 (const unsigned short*)str.wc_str(), str.Len() );
743 #else
744 m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
745 wxApp::s_macDefaultEncodingIsPC ?
746 kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ;
747 #endif
748 m_release = true ;
749 }
750
751 wxString wxMacCFStringHolder::AsString()
752 {
753 wxString result ;
754 Size len = CFStringGetLength( m_cfs ) ;
755 wxChar* buf = result.GetWriteBuf( len ) ;
756 #if wxUSE_UNICODE
757 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , len ) , (UniChar*) buf ) ;
758 #else
759 CFStringGetCString( m_cfs , buf , len+1 , wxApp::s_macDefaultEncodingIsPC ?
760 kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ;
761 #endif
762 buf[len] = 0 ;
763 result.UngetWriteBuf() ;
764 return result ;
765 }
766
767 #if 0
768
769 wxString wxMacMakeMacStringFromPC( const wxChar * p )
770 {
771 wxString result ;
772 int len = wxStrlen ( p ) ;
773 if ( len > 0 )
774 {
775 wxChar* ptr = result.GetWriteBuf(len) ;
776 wxMacConvertFromPC( p , ptr , len ) ;
777 ptr[len] = 0 ;
778 result.UngetWriteBuf( len ) ;
779 }
780 return result ;
781 }
782
783 wxString wxMacMakePCStringFromMac( const wxChar * p )
784 {
785 wxString result ;
786 int len = wxStrlen ( p ) ;
787 if ( len > 0 )
788 {
789 wxChar* ptr = result.GetWriteBuf(len) ;
790 wxMacConvertToPC( p , ptr , len ) ;
791 ptr[len] = 0 ;
792 result.UngetWriteBuf( len ) ;
793 }
794 return result ;
795 }
796
797 wxString wxMacMakeStringFromMacString( const wxChar* from , bool mac2pcEncoding )
798 {
799 if (mac2pcEncoding)
800 {
801 return wxMacMakePCStringFromMac( from ) ;
802 }
803 else
804 {
805 return wxString( from ) ;
806 }
807 }
808
809 //
810 // Pascal Strings
811 //
812
813 wxString wxMacMakeStringFromPascal( ConstStringPtr from , bool mac2pcEncoding )
814 {
815 // this is safe since a pascal string can never be larger than 256 bytes
816 char s[256] ;
817 CopyPascalStringToC( from , s ) ;
818 if (mac2pcEncoding)
819 {
820 return wxMacMakePCStringFromMac( s ) ;
821 }
822 else
823 {
824 return wxString( s ) ;
825 }
826 }
827
828 void wxMacStringToPascal( const wxChar * from , StringPtr to , bool pc2macEncoding )
829 {
830 if (pc2macEncoding)
831 {
832 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
833 }
834 else
835 {
836 CopyCStringToPascal( from , to ) ;
837 }
838 }
839 #endif
840
841
842 #endif //TARGET_CARBON
843
844 // ----------------------------------------------------------------------------
845 // debugging support
846 // ----------------------------------------------------------------------------
847
848 #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
849
850 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
851
852 #ifndef __MetroNubUtils__
853 #include "MetroNubUtils.h"
854 #endif
855
856 #ifndef __GESTALT__
857 #include <Gestalt.h>
858 #endif
859
860 #if TARGET_API_MAC_CARBON
861
862 #include <CodeFragments.h>
863
864 EXTERN_API_C( long )
865 CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
866
867 ProcPtr gCallUniversalProc_Proc = NULL;
868
869 #endif
870
871 static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
872
873 static long fRunOnce = false;
874
875 /* ---------------------------------------------------------------------------
876 IsMetroNubInstalled
877 --------------------------------------------------------------------------- */
878
879 Boolean IsMetroNubInstalled()
880 {
881 if (!fRunOnce)
882 {
883 long result, value;
884
885 fRunOnce = true;
886 gMetroNubEntry = NULL;
887
888 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
889 {
890 /* look for MetroNub's Gestalt selector */
891 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
892 {
893
894 #if TARGET_API_MAC_CARBON
895 if (gCallUniversalProc_Proc == NULL)
896 {
897 CFragConnectionID connectionID;
898 Ptr mainAddress;
899 Str255 errorString;
900 ProcPtr symbolAddress;
901 OSErr err;
902 CFragSymbolClass symbolClass;
903
904 symbolAddress = NULL;
905 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
906 &connectionID, &mainAddress, errorString);
907
908 if (err != noErr)
909 {
910 gCallUniversalProc_Proc = NULL;
911 goto end;
912 }
913
914 err = FindSymbol(connectionID, "\pCallUniversalProc",
915 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
916
917 if (err != noErr)
918 {
919 gCallUniversalProc_Proc = NULL;
920 goto end;
921 }
922 }
923 #endif
924
925 {
926 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
927
928 /* make sure the version of the API is compatible */
929 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
930 kMetroNubUserAPIVersion <= block->apiHiVersion)
931 gMetroNubEntry = block; /* success! */
932 }
933
934 }
935 }
936 }
937
938 end:
939
940 #if TARGET_API_MAC_CARBON
941 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
942 #else
943 return (gMetroNubEntry != NULL);
944 #endif
945 }
946
947 /* ---------------------------------------------------------------------------
948 IsMWDebuggerRunning [v1 API]
949 --------------------------------------------------------------------------- */
950
951 Boolean IsMWDebuggerRunning()
952 {
953 if (IsMetroNubInstalled())
954 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
955 else
956 return false;
957 }
958
959 /* ---------------------------------------------------------------------------
960 AmIBeingMWDebugged [v1 API]
961 --------------------------------------------------------------------------- */
962
963 Boolean AmIBeingMWDebugged()
964 {
965 if (IsMetroNubInstalled())
966 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
967 else
968 return false;
969 }
970
971 extern bool WXDLLEXPORT wxIsDebuggerRunning()
972 {
973 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
974 }
975
976 #else
977
978 extern bool WXDLLEXPORT wxIsDebuggerRunning()
979 {
980 return false;
981 }
982
983 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
984