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