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