]> git.saurik.com Git - wxWidgets.git/blame - src/mac/utils.cpp
multilib mode
[wxWidgets.git] / src / mac / utils.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose: Various utilities
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e40298d5 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13// Note: this is done in utilscmn.cpp now.
e9576ca5
SC
14// #pragma implementation "utils.h"
15#endif
16
17#include "wx/setup.h"
18#include "wx/utils.h"
19#include "wx/app.h"
2bf2d09e 20#include "wx/apptrait.h"
b6ed2b86
VZ
21
22#if wxUSE_GUI
23 #include "wx/mac/uma.h"
24#endif
e9576ca5
SC
25
26#include <ctype.h>
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdarg.h>
32
2d4e4f80
GD
33#ifdef __DARWIN__
34# include "MoreFilesX.h"
35#else
36# include "MoreFiles.h"
37# include "MoreFilesExtras.h"
38#endif
518af45b 39
66a09d47
SC
40#ifndef __DARWIN__
41#include <Threads.h>
42#include <Sound.h>
43#endif
44
427ff662
SC
45#include "ATSUnicode.h"
46#include "TextCommon.h"
47#include "TextEncodingConverter.h"
48
a434b43f
VZ
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
b6ed2b86 57#if wxUSE_BASE
a434b43f 58
f5c6eb5c 59#ifndef __DARWIN__
03e11df5
GD
60// defined in unix/utilsunx.cpp for Mac OS X
61
2f1ae414
SC
62// get full hostname (with domain name if possible)
63bool wxGetFullHostName(wxChar *buf, int maxSize)
64{
65 return wxGetHostName(buf, maxSize);
66}
67
68// Get hostname only (without domain name)
427ff662 69bool wxGetHostName(wxChar *buf, int maxSize)
e9576ca5 70{
e40298d5
JS
71 // Gets Chooser name of user by examining a System resource.
72
73 const short kComputerNameID = -16413;
5be55d56 74
e40298d5
JS
75 short oldResFile = CurResFile() ;
76 UseResFile(0);
77 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
78 UseResFile(oldResFile);
79
80 if (chooserName && *chooserName)
81 {
427ff662
SC
82 HLock( (Handle) chooserName ) ;
83 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
84 HUnlock( (Handle) chooserName ) ;
85 ReleaseResource( (Handle) chooserName ) ;
86 wxStrncpy( buf , name , maxSize - 1 ) ;
e40298d5
JS
87 }
88 else
89 buf[0] = 0 ;
0a67a93b
SC
90
91 return TRUE;
e9576ca5
SC
92}
93
94// Get user ID e.g. jacs
427ff662 95bool wxGetUserId(wxChar *buf, int maxSize)
e9576ca5 96{
0a67a93b 97 return wxGetUserName( buf , maxSize ) ;
e9576ca5
SC
98}
99
5b781a67
SC
100const wxChar* wxGetHomeDir(wxString *pstr)
101{
e40298d5
JS
102 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
103 return pstr->c_str() ;
5b781a67
SC
104}
105
a31a5f85 106// Get user name e.g. Stefan Csomor
427ff662 107bool wxGetUserName(wxChar *buf, int maxSize)
e9576ca5 108{
e40298d5
JS
109 // Gets Chooser name of user by examining a System resource.
110
111 const short kChooserNameID = -16096;
5be55d56 112
e40298d5
JS
113 short oldResFile = CurResFile() ;
114 UseResFile(0);
115 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
116 UseResFile(oldResFile);
117
118 if (chooserName && *chooserName)
119 {
427ff662
SC
120 HLock( (Handle) chooserName ) ;
121 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
122 HUnlock( (Handle) chooserName ) ;
123 ReleaseResource( (Handle) chooserName ) ;
124 wxStrncpy( buf , name , maxSize - 1 ) ;
e40298d5
JS
125 }
126 else
127 buf[0] = 0 ;
0a67a93b
SC
128
129 return TRUE;
e9576ca5
SC
130}
131
5dbb17e2 132int wxKill(long pid, wxSignal sig , wxKillError *rc )
e9576ca5
SC
133{
134 // TODO
135 return 0;
136}
137
5dbb17e2
SC
138WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
139{
e40298d5
JS
140 // TODO : under classic there is no environement support, under X yes
141 return false ;
5dbb17e2
SC
142}
143
144// set the env var name to the given value, return TRUE on success
145WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
146{
e40298d5
JS
147 // TODO : under classic there is no environement support, under X yes
148 return false ;
5dbb17e2
SC
149}
150
e9576ca5
SC
151//
152// Execute a program in an Interactive Shell
153//
154bool wxShell(const wxString& command)
155{
156 // TODO
157 return FALSE;
158}
159
5be55d56 160// Shutdown or reboot the PC
f6ba47d9
VZ
161bool wxShutdown(wxShutdownFlags wFlags)
162{
163 // TODO
164 return FALSE;
165}
166
e9576ca5
SC
167// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
168long wxGetFreeMemory()
169{
0a67a93b
SC
170 return FreeMem() ;
171}
172
173void wxUsleep(unsigned long milliseconds)
174{
e7e1b01e 175 clock_t start = clock() ;
5be55d56 176 do
e7e1b01e 177 {
e40298d5 178 YieldToAnyThread() ;
2a616188 179 } while( clock() - start < milliseconds / 1000.0 * CLOCKS_PER_SEC ) ;
e9576ca5
SC
180}
181
182void wxSleep(int nSecs)
183{
0a67a93b 184 wxUsleep(1000*nSecs);
e9576ca5
SC
185}
186
187// Consume all events until no more left
188void wxFlushEvents()
189{
190}
191
f5c6eb5c 192#endif // !__DARWIN__
e9576ca5
SC
193
194// Emit a beeeeeep
195void wxBell()
196{
0a67a93b 197 SysBeep(30);
e9576ca5
SC
198}
199
29c99ad3
VZ
200// our OS version is the same in non GUI and GUI cases
201static int DoGetOSVersion(int *majorVsn, int *minorVsn)
e9576ca5 202{
ff8fda36 203 long theSystem ;
5be55d56 204
ff8fda36 205 // are there x-platform conventions ?
5be55d56 206
ff8fda36
GD
207 Gestalt(gestaltSystemVersion, &theSystem) ;
208 if (minorVsn != NULL) {
e40298d5 209 *minorVsn = (theSystem & 0xFF ) ;
ff8fda36
GD
210 }
211 if (majorVsn != NULL) {
e40298d5 212 *majorVsn = (theSystem >> 8 ) ;
ff8fda36
GD
213 }
214#ifdef __DARWIN__
215 return wxMAC_DARWIN;
216#else
217 return wxMAC;
218#endif
e9576ca5
SC
219}
220
29c99ad3
VZ
221int wxConsoleAppTraits::GetOSVersion(int *majorVsn, int *minorVsn)
222{
223 return DoGetOSVersion(majorVsn, minorVsn);
224}
225
b6ed2b86
VZ
226#endif // wxUSE_BASE
227
228#if wxUSE_GUI
536732e4 229
29c99ad3
VZ
230int wxGUIAppTraits::GetOSVersion(int *majorVsn, int *minorVsn)
231{
232 return DoGetOSVersion(majorVsn, minorVsn);
233}
234
e9576ca5
SC
235// Reading and writing resources (eg WIN.INI, .Xdefaults)
236#if wxUSE_RESOURCES
237bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
238{
239 // TODO
240 return FALSE;
241}
242
243bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
244{
427ff662
SC
245 wxString buf;
246 buf.Printf(wxT("%.4f"), value);
247
e40298d5 248 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
249}
250
251bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
252{
427ff662
SC
253 wxString buf;
254 buf.Printf(wxT("%ld"), value);
255
e40298d5 256 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
257}
258
259bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
260{
427ff662
SC
261 wxString buf;
262 buf.Printf(wxT("%d"), value);
263
e40298d5 264 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
265}
266
267bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
268{
269 // TODO
270 return FALSE;
271}
272
273bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
274{
e40298d5
JS
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;
e9576ca5
SC
284}
285
286bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
287{
e40298d5
JS
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;
e9576ca5
SC
297}
298
299bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
300{
e40298d5
JS
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;
e9576ca5
SC
310}
311#endif // wxUSE_RESOURCES
312
6b57b49a 313int gs_wxBusyCursorCount = 0;
e40298d5
JS
314extern wxCursor gMacCurrentCursor ;
315wxCursor gMacStoredActiveCursor ;
e9576ca5
SC
316
317// Set the cursor to the busy cursor for all windows
318void wxBeginBusyCursor(wxCursor *cursor)
319{
e40298d5
JS
320 if (gs_wxBusyCursorCount++ == 0)
321 {
322 gMacStoredActiveCursor = gMacCurrentCursor ;
323 cursor->MacInstall() ;
324 }
325 //else: nothing to do, already set
e9576ca5
SC
326}
327
328// Restore cursor to normal
329void wxEndBusyCursor()
330{
6b57b49a 331 wxCHECK_RET( gs_wxBusyCursorCount > 0,
e40298d5 332 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
5be55d56 333
e40298d5
JS
334 if (--gs_wxBusyCursorCount == 0)
335 {
336 gMacStoredActiveCursor.MacInstall() ;
337 gMacStoredActiveCursor = wxNullCursor ;
338 }
e9576ca5
SC
339}
340
341// TRUE if we're between the above two calls
342bool wxIsBusy()
343{
e40298d5 344 return (gs_wxBusyCursorCount > 0);
ec5d7799 345}
e9576ca5 346
e7e1b01e 347wxString wxMacFindFolder( short vol,
e40298d5
JS
348 OSType folderType,
349 Boolean createFolder)
2f1ae414 350{
2d4e4f80
GD
351 short vRefNum ;
352 long dirID ;
353 wxString strDir ;
5be55d56 354
2d4e4f80
GD
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 ;
2f1ae414
SC
364}
365
e9576ca5
SC
366// Check whether this window wants to process messages, e.g. Stop button
367// in long calculations.
368bool wxCheckForInterrupt(wxWindow *wnd)
369{
370 // TODO
371 return FALSE;
372}
373
374void wxGetMousePosition( int* x, int* y )
375{
519cb848 376 Point pt ;
ec5d7799 377
519cb848
SC
378 GetMouse( &pt ) ;
379 LocalToGlobal( &pt ) ;
380 *x = pt.h ;
381 *y = pt.v ;
e9576ca5
SC
382};
383
384// Return TRUE if we have a colour display
385bool wxColourDisplay()
386{
e9576ca5
SC
387 return TRUE;
388}
389
390// Returns depth of screen
391int wxDisplayDepth()
392{
e40298d5
JS
393 Rect globRect ;
394 SetRect(&globRect, -32760, -32760, 32760, 32760);
395 GDHandle theMaxDevice;
2f1ae414 396
e40298d5
JS
397 int theDepth = 8;
398 theMaxDevice = GetMaxDevice(&globRect);
399 if (theMaxDevice != nil)
400 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 401
e40298d5 402 return theDepth ;
e9576ca5
SC
403}
404
405// Get size of display
406void wxDisplaySize(int *width, int *height)
407{
e40298d5
JS
408 BitMap screenBits;
409 GetQDGlobalsScreenBits( &screenBits );
5be55d56
VZ
410
411 if (width != NULL) {
e8ca7105
GD
412 *width = screenBits.bounds.right - screenBits.bounds.left ;
413 }
5be55d56 414 if (height != NULL) {
e8ca7105
GD
415 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
416 }
e9576ca5
SC
417}
418
5fde6fcc
GD
419void wxDisplaySizeMM(int *width, int *height)
420{
5b028d57
SC
421 wxDisplaySize(width, height);
422 // on mac 72 is fixed (at least now ;-)
423 float cvPt2Mm = 25.4 / 72;
e8ca7105 424
5be55d56 425 if (width != NULL) {
e8ca7105
GD
426 *width = int( *width * cvPt2Mm );
427 }
5be55d56 428 if (height != NULL) {
e8ca7105
GD
429 *height = int( *height * cvPt2Mm );
430 }
5fde6fcc
GD
431}
432
ec5d7799
RD
433void wxClientDisplayRect(int *x, int *y, int *width, int *height)
434{
2d4e4f80
GD
435 BitMap screenBits;
436 GetQDGlobalsScreenBits( &screenBits );
7cfebe05 437
ec5d7799
RD
438 if (x) *x = 0;
439 if (y) *y = 0;
7cfebe05 440
5be55d56 441 if (width != NULL) {
e8ca7105
GD
442 *width = screenBits.bounds.right - screenBits.bounds.left ;
443 }
5be55d56 444 if (height != NULL) {
e8ca7105
GD
445 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
446 }
7cfebe05 447
2d4e4f80
GD
448 SInt16 mheight ;
449#if TARGET_CARBON
450 GetThemeMenuBarHeight( &mheight ) ;
451#else
7cfebe05 452 mheight = LMGetMBarHeight() ;
2d4e4f80 453#endif
5be55d56 454 if (height != NULL) {
e8ca7105
GD
455 *height -= mheight ;
456 }
457 if (y)
2d4e4f80 458 *y = mheight ;
ec5d7799
RD
459}
460
57591e0e
JS
461wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
462{
463 return wxGenericFindWindowAtPoint(pt);
464}
5dbb17e2 465
b6ed2b86
VZ
466#endif // wxUSE_GUI
467
468#if wxUSE_BASE
469
5dbb17e2
SC
470wxString wxGetOsDescription()
471{
6e73695c
GD
472#ifdef WXWIN_OS_DESCRIPTION
473 // use configure generated description if available
474 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
475#else
427ff662 476 return wxT("MacOS") ; //TODO:define further
6e73695c
GD
477#endif
478}
479
b6ed2b86
VZ
480#ifndef __DARWIN__
481wxChar *wxGetUserHome (const wxString& user)
482{
483 // TODO
484 return NULL;
485}
486
487bool 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
3d963f81
SC
526//---------------------------------------------------------------------------
527// wxMac Specific utility functions
528//---------------------------------------------------------------------------
529
4c200e8d
SC
530char 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
537char 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
544void 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
579void 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
427ff662
SC
614TECObjectRef s_TECNativeCToUnicode = NULL ;
615TECObjectRef s_TECUnicodeToNativeC = NULL ;
c0ef107e
SC
616TECObjectRef s_TECPlatformToNativeC = NULL ;
617TECObjectRef s_TECNativeCToPlatform = NULL ;
5be55d56 618void wxMacSetupConverters()
4c200e8d 619{
5be55d56
VZ
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
427ff662 627 OSStatus status = noErr ;
5be55d56
VZ
628 status = TECCreateConverter(&s_TECNativeCToUnicode,
629 kEncoding,
630 kTextEncodingUnicodeDefault);
427ff662
SC
631
632
5be55d56
VZ
633 status = TECCreateConverter(&s_TECUnicodeToNativeC,
634 kTextEncodingUnicodeDefault,
635 kEncoding);
427ff662 636
c0ef107e 637 if ( wxApp::s_macDefaultEncodingIsPC )
427ff662 638 {
5be55d56
VZ
639 status = TECCreateConverter(&s_TECPlatformToNativeC,
640 kTextEncodingMacRoman,
641 kTextEncodingWindowsLatin1);
642
427ff662 643
5be55d56
VZ
644 status = TECCreateConverter(&s_TECNativeCToPlatform,
645 kTextEncodingWindowsLatin1,
646 kTextEncodingMacRoman);
427ff662
SC
647 }
648}
649
650void wxMacCleanupConverters()
651{
652 OSStatus status = noErr ;
653 status = TECDisposeConverter(s_TECNativeCToUnicode);
654
655 status = TECDisposeConverter(s_TECUnicodeToNativeC);
656
c0ef107e 657 status = TECDisposeConverter(s_TECPlatformToNativeC);
427ff662 658
c0ef107e 659 status = TECDisposeConverter(s_TECNativeCToPlatform);
427ff662
SC
660}
661
5be55d56 662wxWCharBuffer wxMacStringToWString( const wxString &from )
427ff662
SC
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
5be55d56 679wxString wxMacMakeStringFromCString( const char * from , int len )
427ff662
SC
680{
681 OSStatus status = noErr ;
e40298d5 682 wxString result ;
427ff662
SC
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
c0ef107e 692 if ( !wxApp::s_macDefaultEncodingIsPC )
427ff662
SC
693 memcpy( buf , from , len ) ;
694 else
695 {
427ff662
SC
696 ByteCount byteOutLen ;
697 ByteCount byteInLen = len ;
698 ByteCount byteBufferLen = byteInLen ;
699
c0ef107e 700 status = TECConvertText(s_TECPlatformToNativeC, (ConstTextPtr)from , byteInLen, &byteInLen,
427ff662
SC
701 (TextPtr)buf, byteBufferLen, &byteOutLen);
702 }
703#endif
704 buf[len] = 0 ;
705 result.UngetWriteBuf() ;
706 return result ;
707}
708
709wxString wxMacMakeStringFromCString( const char * from )
710{
711 return wxMacMakeStringFromCString( from , strlen(from) ) ;
712}
713
5be55d56 714wxCharBuffer wxMacStringToCString( const wxString &from )
427ff662
SC
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
c0ef107e 726 if ( !wxApp::s_macDefaultEncodingIsPC )
427ff662
SC
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
c0ef107e 736 status = TECConvertText(s_TECNativeCToPlatform, (ConstTextPtr)from.c_str() , byteInLen, &byteInLen,
427ff662
SC
737 (TextPtr)result.data(), byteBufferLen, &byteOutLen);
738 return result ;
739 }
740#endif
741}
742
5be55d56 743void wxMacStringToPascal( const wxString&from , StringPtr to )
427ff662
SC
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
5be55d56 754wxString wxMacMakeStringFromPascal( ConstStringPtr from )
427ff662
SC
755{
756 return wxMacMakeStringFromCString( (char*) &from[1] , from[0] ) ;
757}
758
5be55d56 759//
427ff662
SC
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
5be55d56 765void wxMacCFStringHolder::Assign( const wxString &str )
427ff662
SC
766{
767#if wxUSE_UNICODE
5be55d56 768 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault,
427ff662
SC
769 (const unsigned short*)str.wc_str(), str.Len() );
770#else
771 m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
5be55d56 772 wxApp::s_macDefaultEncodingIsPC ?
427ff662
SC
773 kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ;
774#endif
775 m_release = true ;
776}
777
5be55d56 778wxString wxMacCFStringHolder::AsString()
427ff662
SC
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
5be55d56 786 CFStringGetCString( m_cfs , buf , len+1 , wxApp::s_macDefaultEncodingIsPC ?
427ff662
SC
787 kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ;
788#endif
789 buf[len] = 0 ;
790 result.UngetWriteBuf() ;
791 return result ;
792}
793
794#if 0
795
796wxString wxMacMakeMacStringFromPC( const wxChar * p )
797{
798 wxString result ;
799 int len = wxStrlen ( p ) ;
e40298d5
JS
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 }
4c200e8d
SC
807 return result ;
808}
809
427ff662 810wxString wxMacMakePCStringFromMac( const wxChar * p )
4c200e8d 811{
e40298d5 812 wxString result ;
427ff662 813 int len = wxStrlen ( p ) ;
e40298d5
JS
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 }
4c200e8d
SC
821 return result ;
822}
823
427ff662 824wxString wxMacMakeStringFromMacString( const wxChar* from , bool mac2pcEncoding )
4c200e8d
SC
825{
826 if (mac2pcEncoding)
827 {
828 return wxMacMakePCStringFromMac( from ) ;
829 }
830 else
831 {
832 return wxString( from ) ;
833 }
834}
835
5be55d56 836//
4c200e8d
SC
837// Pascal Strings
838//
839
840wxString wxMacMakeStringFromPascal( ConstStringPtr from , bool mac2pcEncoding )
841{
e40298d5
JS
842 // this is safe since a pascal string can never be larger than 256 bytes
843 char s[256] ;
844 CopyPascalStringToC( from , s ) ;
4c200e8d
SC
845 if (mac2pcEncoding)
846 {
847 return wxMacMakePCStringFromMac( s ) ;
848 }
849 else
850 {
851 return wxString( s ) ;
852 }
853}
854
427ff662 855void wxMacStringToPascal( const wxChar * from , StringPtr to , bool pc2macEncoding )
4c200e8d
SC
856{
857 if (pc2macEncoding)
858 {
859 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
860 }
861 else
862 {
863 CopyCStringToPascal( from , to ) ;
864 }
865}
427ff662 866#endif
4c200e8d 867
3d963f81
SC
868
869#endif //TARGET_CARBON
870
a434b43f
VZ
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
536732e4 891 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
a434b43f
VZ
892
893 ProcPtr gCallUniversalProc_Proc = NULL;
894
895#endif
896
897static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
898
899static long fRunOnce = false;
900
901/* ---------------------------------------------------------------------------
902 IsMetroNubInstalled
903 --------------------------------------------------------------------------- */
904
905Boolean 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
964end:
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
977Boolean IsMWDebuggerRunning()
978{
979 if (IsMetroNubInstalled())
980 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
981 else
982 return false;
983}
984
985/* ---------------------------------------------------------------------------
986 AmIBeingMWDebugged [v1 API]
987 --------------------------------------------------------------------------- */
988
989Boolean AmIBeingMWDebugged()
990{
991 if (IsMetroNubInstalled())
992 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
993 else
994 return false;
995}
996
997extern bool WXDLLEXPORT wxIsDebuggerRunning()
998{
999 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
1000}
1001
1002#else
1003
1004extern bool WXDLLEXPORT wxIsDebuggerRunning()
1005{
1006 return false;
1007}
1008
1009#endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
1010
b6ed2b86
VZ
1011#endif // wxUSE_GUI
1012