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