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