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