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