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