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