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