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