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