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