]> git.saurik.com Git - wxWidgets.git/blame - src/mac/utils.cpp
corrections for buffers
[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"
fe929141
SC
24 #include "wx/font.h"
25#else
26 #include "wx/intl.h"
b6ed2b86 27#endif
e9576ca5
SC
28
29#include <ctype.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <stdarg.h>
35
2d4e4f80
GD
36#ifdef __DARWIN__
37# include "MoreFilesX.h"
38#else
39# include "MoreFiles.h"
40# include "MoreFilesExtras.h"
41#endif
518af45b 42
66a09d47
SC
43#ifndef __DARWIN__
44#include <Threads.h>
45#include <Sound.h>
46#endif
47
427ff662
SC
48#include "ATSUnicode.h"
49#include "TextCommon.h"
50#include "TextEncodingConverter.h"
51
8aa701ed 52#include "wx/mac/private.h" // includes mac headers
a434b43f
VZ
53
54#if defined(__MWERKS__) && wxUSE_UNICODE
55 #include <wtime.h>
56#endif
57
659b3e8b
VZ
58// ---------------------------------------------------------------------------
59// code used in both base and GUI compilation
60// ---------------------------------------------------------------------------
61
62// our OS version is the same in non GUI and GUI cases
63static int DoGetOSVersion(int *majorVsn, int *minorVsn)
64{
65 long theSystem ;
66
67 // are there x-platform conventions ?
68
69 Gestalt(gestaltSystemVersion, &theSystem) ;
70 if (minorVsn != NULL) {
71 *minorVsn = (theSystem & 0xFF ) ;
72 }
73 if (majorVsn != NULL) {
74 *majorVsn = (theSystem >> 8 ) ;
75 }
76#ifdef __DARWIN__
77 return wxMAC_DARWIN;
78#else
79 return wxMAC;
80#endif
81}
82
b6ed2b86 83#if wxUSE_BASE
a434b43f 84
f5c6eb5c 85#ifndef __DARWIN__
03e11df5
GD
86// defined in unix/utilsunx.cpp for Mac OS X
87
2f1ae414
SC
88// get full hostname (with domain name if possible)
89bool wxGetFullHostName(wxChar *buf, int maxSize)
90{
91 return wxGetHostName(buf, maxSize);
92}
93
94// Get hostname only (without domain name)
427ff662 95bool wxGetHostName(wxChar *buf, int maxSize)
e9576ca5 96{
e40298d5
JS
97 // Gets Chooser name of user by examining a System resource.
98
99 const short kComputerNameID = -16413;
5be55d56 100
e40298d5
JS
101 short oldResFile = CurResFile() ;
102 UseResFile(0);
103 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
104 UseResFile(oldResFile);
105
106 if (chooserName && *chooserName)
107 {
427ff662
SC
108 HLock( (Handle) chooserName ) ;
109 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
110 HUnlock( (Handle) chooserName ) ;
111 ReleaseResource( (Handle) chooserName ) ;
112 wxStrncpy( buf , name , maxSize - 1 ) ;
e40298d5
JS
113 }
114 else
115 buf[0] = 0 ;
0a67a93b
SC
116
117 return TRUE;
e9576ca5
SC
118}
119
120// Get user ID e.g. jacs
427ff662 121bool wxGetUserId(wxChar *buf, int maxSize)
e9576ca5 122{
0a67a93b 123 return wxGetUserName( buf , maxSize ) ;
e9576ca5
SC
124}
125
5b781a67
SC
126const wxChar* wxGetHomeDir(wxString *pstr)
127{
e40298d5
JS
128 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
129 return pstr->c_str() ;
5b781a67
SC
130}
131
a31a5f85 132// Get user name e.g. Stefan Csomor
427ff662 133bool wxGetUserName(wxChar *buf, int maxSize)
e9576ca5 134{
e40298d5
JS
135 // Gets Chooser name of user by examining a System resource.
136
137 const short kChooserNameID = -16096;
5be55d56 138
e40298d5
JS
139 short oldResFile = CurResFile() ;
140 UseResFile(0);
141 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
142 UseResFile(oldResFile);
143
144 if (chooserName && *chooserName)
145 {
427ff662
SC
146 HLock( (Handle) chooserName ) ;
147 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
148 HUnlock( (Handle) chooserName ) ;
149 ReleaseResource( (Handle) chooserName ) ;
150 wxStrncpy( buf , name , maxSize - 1 ) ;
e40298d5
JS
151 }
152 else
153 buf[0] = 0 ;
0a67a93b
SC
154
155 return TRUE;
e9576ca5
SC
156}
157
5dbb17e2 158int wxKill(long pid, wxSignal sig , wxKillError *rc )
e9576ca5
SC
159{
160 // TODO
161 return 0;
162}
163
5dbb17e2
SC
164WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
165{
e40298d5
JS
166 // TODO : under classic there is no environement support, under X yes
167 return false ;
5dbb17e2
SC
168}
169
170// set the env var name to the given value, return TRUE on success
171WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
172{
e40298d5
JS
173 // TODO : under classic there is no environement support, under X yes
174 return false ;
5dbb17e2
SC
175}
176
e9576ca5
SC
177//
178// Execute a program in an Interactive Shell
179//
180bool wxShell(const wxString& command)
181{
182 // TODO
183 return FALSE;
184}
185
5be55d56 186// Shutdown or reboot the PC
f6ba47d9
VZ
187bool wxShutdown(wxShutdownFlags wFlags)
188{
189 // TODO
190 return FALSE;
191}
192
e9576ca5
SC
193// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
194long wxGetFreeMemory()
195{
0a67a93b
SC
196 return FreeMem() ;
197}
198
199void wxUsleep(unsigned long milliseconds)
200{
e7e1b01e 201 clock_t start = clock() ;
5be55d56 202 do
e7e1b01e 203 {
e40298d5 204 YieldToAnyThread() ;
2a616188 205 } while( clock() - start < milliseconds / 1000.0 * CLOCKS_PER_SEC ) ;
e9576ca5
SC
206}
207
208void wxSleep(int nSecs)
209{
0a67a93b 210 wxUsleep(1000*nSecs);
e9576ca5
SC
211}
212
213// Consume all events until no more left
214void wxFlushEvents()
215{
216}
217
f5c6eb5c 218#endif // !__DARWIN__
e9576ca5
SC
219
220// Emit a beeeeeep
221void wxBell()
222{
0a67a93b 223 SysBeep(30);
e9576ca5
SC
224}
225
324899f6 226wxToolkitInfo& wxConsoleAppTraits::GetToolkitInfo()
29c99ad3 227{
a8eaaeb2 228 static wxToolkitInfo info;
66b6b57c 229 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
a8eaaeb2 230 info.name = _T("wxBase");
324899f6 231 return info;
29c99ad3
VZ
232}
233
b6ed2b86
VZ
234#endif // wxUSE_BASE
235
236#if wxUSE_GUI
536732e4 237
324899f6 238wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
29c99ad3 239{
a8eaaeb2 240 static wxToolkitInfo info;
66b6b57c 241 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
a8eaaeb2
VS
242 info.shortName = _T("mac");
243 info.name = _T("wxMac");
244#ifdef __WXUNIVERSAL__
245 info.shortName << _T("univ");
246 info.name << _T("/wxUniversal");
247#endif
324899f6 248 return info;
29c99ad3
VZ
249}
250
e9576ca5
SC
251// Reading and writing resources (eg WIN.INI, .Xdefaults)
252#if wxUSE_RESOURCES
253bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
254{
255 // TODO
256 return FALSE;
257}
258
259bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
260{
427ff662
SC
261 wxString buf;
262 buf.Printf(wxT("%.4f"), value);
263
e40298d5 264 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
265}
266
267bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
268{
427ff662
SC
269 wxString buf;
270 buf.Printf(wxT("%ld"), value);
271
e40298d5 272 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
273}
274
275bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
276{
427ff662
SC
277 wxString buf;
278 buf.Printf(wxT("%d"), value);
279
e40298d5 280 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
281}
282
283bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
284{
285 // TODO
286 return FALSE;
287}
288
289bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
290{
e40298d5
JS
291 char *s = NULL;
292 bool succ = wxGetResource(section, entry, (char **)&s, file);
293 if (succ)
294 {
295 *value = (float)strtod(s, NULL);
296 delete[] s;
297 return TRUE;
298 }
299 else return FALSE;
e9576ca5
SC
300}
301
302bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
303{
e40298d5
JS
304 char *s = NULL;
305 bool succ = wxGetResource(section, entry, (char **)&s, file);
306 if (succ)
307 {
308 *value = strtol(s, NULL, 10);
309 delete[] s;
310 return TRUE;
311 }
312 else return FALSE;
e9576ca5
SC
313}
314
315bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
316{
e40298d5
JS
317 char *s = NULL;
318 bool succ = wxGetResource(section, entry, (char **)&s, file);
319 if (succ)
320 {
321 *value = (int)strtol(s, NULL, 10);
322 delete[] s;
323 return TRUE;
324 }
325 else return FALSE;
e9576ca5
SC
326}
327#endif // wxUSE_RESOURCES
328
6b57b49a 329int gs_wxBusyCursorCount = 0;
e40298d5
JS
330extern wxCursor gMacCurrentCursor ;
331wxCursor gMacStoredActiveCursor ;
e9576ca5
SC
332
333// Set the cursor to the busy cursor for all windows
334void wxBeginBusyCursor(wxCursor *cursor)
335{
e40298d5
JS
336 if (gs_wxBusyCursorCount++ == 0)
337 {
338 gMacStoredActiveCursor = gMacCurrentCursor ;
339 cursor->MacInstall() ;
340 }
341 //else: nothing to do, already set
e9576ca5
SC
342}
343
344// Restore cursor to normal
345void wxEndBusyCursor()
346{
6b57b49a 347 wxCHECK_RET( gs_wxBusyCursorCount > 0,
e40298d5 348 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
5be55d56 349
e40298d5
JS
350 if (--gs_wxBusyCursorCount == 0)
351 {
352 gMacStoredActiveCursor.MacInstall() ;
353 gMacStoredActiveCursor = wxNullCursor ;
354 }
e9576ca5
SC
355}
356
357// TRUE if we're between the above two calls
358bool wxIsBusy()
359{
e40298d5 360 return (gs_wxBusyCursorCount > 0);
ec5d7799 361}
e9576ca5 362
2dbc444a
RD
363#endif // wxUSE_GUI
364
365#if wxUSE_BASE
366
e7e1b01e 367wxString wxMacFindFolder( short vol,
e40298d5
JS
368 OSType folderType,
369 Boolean createFolder)
2f1ae414 370{
2d4e4f80
GD
371 short vRefNum ;
372 long dirID ;
373 wxString strDir ;
5be55d56 374
2d4e4f80
GD
375 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
376 {
377 FSSpec file ;
378 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
379 {
380 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
381 }
382 }
383 return strDir ;
2f1ae414
SC
384}
385
2dbc444a
RD
386#endif // wxUSE_BASE
387
388#if wxUSE_GUI
389
e9576ca5
SC
390// Check whether this window wants to process messages, e.g. Stop button
391// in long calculations.
392bool wxCheckForInterrupt(wxWindow *wnd)
393{
394 // TODO
395 return FALSE;
396}
397
398void wxGetMousePosition( int* x, int* y )
399{
519cb848 400 Point pt ;
ec5d7799 401
519cb848
SC
402 GetMouse( &pt ) ;
403 LocalToGlobal( &pt ) ;
404 *x = pt.h ;
405 *y = pt.v ;
e9576ca5
SC
406};
407
408// Return TRUE if we have a colour display
409bool wxColourDisplay()
410{
e9576ca5
SC
411 return TRUE;
412}
413
414// Returns depth of screen
415int wxDisplayDepth()
416{
e40298d5
JS
417 Rect globRect ;
418 SetRect(&globRect, -32760, -32760, 32760, 32760);
419 GDHandle theMaxDevice;
2f1ae414 420
e40298d5
JS
421 int theDepth = 8;
422 theMaxDevice = GetMaxDevice(&globRect);
423 if (theMaxDevice != nil)
424 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 425
e40298d5 426 return theDepth ;
e9576ca5
SC
427}
428
429// Get size of display
430void wxDisplaySize(int *width, int *height)
431{
e40298d5
JS
432 BitMap screenBits;
433 GetQDGlobalsScreenBits( &screenBits );
5be55d56
VZ
434
435 if (width != NULL) {
e8ca7105
GD
436 *width = screenBits.bounds.right - screenBits.bounds.left ;
437 }
5be55d56 438 if (height != NULL) {
e8ca7105
GD
439 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
440 }
e9576ca5
SC
441}
442
5fde6fcc
GD
443void wxDisplaySizeMM(int *width, int *height)
444{
5b028d57
SC
445 wxDisplaySize(width, height);
446 // on mac 72 is fixed (at least now ;-)
447 float cvPt2Mm = 25.4 / 72;
e8ca7105 448
5be55d56 449 if (width != NULL) {
e8ca7105
GD
450 *width = int( *width * cvPt2Mm );
451 }
5be55d56 452 if (height != NULL) {
e8ca7105
GD
453 *height = int( *height * cvPt2Mm );
454 }
5fde6fcc
GD
455}
456
ec5d7799
RD
457void wxClientDisplayRect(int *x, int *y, int *width, int *height)
458{
2d4e4f80
GD
459 BitMap screenBits;
460 GetQDGlobalsScreenBits( &screenBits );
7cfebe05 461
ec5d7799
RD
462 if (x) *x = 0;
463 if (y) *y = 0;
7cfebe05 464
5be55d56 465 if (width != NULL) {
e8ca7105
GD
466 *width = screenBits.bounds.right - screenBits.bounds.left ;
467 }
5be55d56 468 if (height != NULL) {
e8ca7105
GD
469 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
470 }
7cfebe05 471
2d4e4f80
GD
472 SInt16 mheight ;
473#if TARGET_CARBON
474 GetThemeMenuBarHeight( &mheight ) ;
475#else
7cfebe05 476 mheight = LMGetMBarHeight() ;
2d4e4f80 477#endif
5be55d56 478 if (height != NULL) {
e8ca7105
GD
479 *height -= mheight ;
480 }
481 if (y)
2d4e4f80 482 *y = mheight ;
ec5d7799
RD
483}
484
57591e0e
JS
485wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
486{
487 return wxGenericFindWindowAtPoint(pt);
488}
5dbb17e2 489
b6ed2b86
VZ
490#endif // wxUSE_GUI
491
492#if wxUSE_BASE
493
5dbb17e2
SC
494wxString wxGetOsDescription()
495{
6e73695c
GD
496#ifdef WXWIN_OS_DESCRIPTION
497 // use configure generated description if available
5f3f0f17 498 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION) + wxString(wxT(")"));
6e73695c 499#else
427ff662 500 return wxT("MacOS") ; //TODO:define further
6e73695c
GD
501#endif
502}
503
b6ed2b86
VZ
504#ifndef __DARWIN__
505wxChar *wxGetUserHome (const wxString& user)
506{
507 // TODO
508 return NULL;
509}
510
511bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
512{
513 if ( path.empty() )
514 return FALSE;
515
516 wxString p = path ;
71111c40 517 if (p[0u] == ':' ) {
b6ed2b86
VZ
518 p = wxGetCwd() + p ;
519 }
520
521 int pos = p.Find(':') ;
522 if ( pos != wxNOT_FOUND ) {
523 p = p.Mid(1,pos) ;
524 }
525
526 p = p + wxT(":") ;
527
528 Str255 volumeName ;
529 XVolumeParam pb ;
530
531 wxMacStringToPascal( p , volumeName ) ;
532 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
533 if ( err == noErr ) {
534 if ( pTotal ) {
535 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
536 }
537 if ( pFree ) {
538 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
539 }
540 }
541
542 return err == noErr ;
543}
544#endif // !__DARWIN__
545
3d963f81
SC
546//---------------------------------------------------------------------------
547// wxMac Specific utility functions
548//---------------------------------------------------------------------------
549
939fba6c
SC
550#if 0
551
4c200e8d
SC
552char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
553 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
554 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
555 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
556 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
557 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
558
559char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
560 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
561 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
562 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
563 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
564 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
565
566void wxMacConvertFromPC( const char *from , char *to , int len )
567{
568 char *c ;
569 if ( from == to )
570 {
571 for( int i = 0 ; i < len ; ++ i )
572 {
573 c = strchr( StringANSI , *from ) ;
574 if ( c != NULL )
575 {
576 *to = StringMac[ c - StringANSI] ;
577 }
578 ++to ;
579 ++from ;
580 }
581 }
582 else
583 {
584 for( int i = 0 ; i < len ; ++ i )
585 {
586 c = strchr( StringANSI , *from ) ;
587 if ( c != NULL )
588 {
589 *to = StringMac[ c - StringANSI] ;
590 }
591 else
592 {
593 *to = *from ;
594 }
595 ++to ;
596 ++from ;
597 }
598 }
599}
600
601void wxMacConvertToPC( const char *from , char *to , int len )
602{
603 char *c ;
604 if ( from == to )
605 {
606 for( int i = 0 ; i < len ; ++ i )
607 {
608 c = strchr( StringMac , *from ) ;
609 if ( c != NULL )
610 {
611 *to = StringANSI[ c - StringMac] ;
612 }
613 ++to ;
614 ++from ;
615 }
616 }
617 else
618 {
619 for( int i = 0 ; i < len ; ++ i )
620 {
621 c = strchr( StringMac , *from ) ;
622 if ( c != NULL )
623 {
624 *to = StringANSI[ c - StringMac] ;
625 }
626 else
627 {
628 *to = *from ;
629 }
630 ++to ;
631 ++from ;
632 }
633 }
634}
635
427ff662
SC
636TECObjectRef s_TECNativeCToUnicode = NULL ;
637TECObjectRef s_TECUnicodeToNativeC = NULL ;
939fba6c 638
5be55d56 639void wxMacSetupConverters()
4c200e8d 640{
5be55d56
VZ
641 // if we assume errors are happening here we need low level debugging
642 // since the high level assert will use the encoders that are not yet
643 // setup...
939fba6c
SC
644#if TARGET_CARBON
645 const TextEncodingBase kEncoding = CFStringGetSystemEncoding();
646#else
647 const TextEncodingBase kEncoding = kTextEncodingMacRoman;
648#endif
427ff662 649 OSStatus status = noErr ;
5be55d56
VZ
650 status = TECCreateConverter(&s_TECNativeCToUnicode,
651 kEncoding,
652 kTextEncodingUnicodeDefault);
427ff662 653
5be55d56
VZ
654 status = TECCreateConverter(&s_TECUnicodeToNativeC,
655 kTextEncodingUnicodeDefault,
656 kEncoding);
71111c40
SC
657
658#if (wxUSE_UNICODE == 1) && (SIZEOF_WCHAR_T == 4)
659 TextEncoding kUnicode32 = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
660
661 status = TECCreateConverter(&s_TECUnicode16To32,
662 kTextEncodingUnicodeDefault,
663 kUnicode32);
664 status = TECCreateConverter(&s_TECUnicode32To16,
665 kUnicode32,
666 kTextEncodingUnicodeDefault);
667#endif
427ff662
SC
668}
669
670void wxMacCleanupConverters()
671{
672 OSStatus status = noErr ;
673 status = TECDisposeConverter(s_TECNativeCToUnicode);
427ff662 674 status = TECDisposeConverter(s_TECUnicodeToNativeC);
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() ;
71111c40 685 ByteCount byteBufferLen = byteInLen * SIZEOF_WCHAR_T ;
427ff662
SC
686 wxWCharBuffer result( from.Length() ) ;
687 status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from.c_str() , byteInLen, &byteInLen,
688 (TextPtr)result.data(), byteBufferLen, &byteOutLen);
71111c40 689 result.data()[byteOutLen/SIZEOF_WCHAR_T] = 0 ;
427ff662
SC
690#endif
691 return result ;
692}
693
2dbc444a 694
5be55d56 695wxString wxMacMakeStringFromCString( const char * from , int len )
427ff662
SC
696{
697 OSStatus status = noErr ;
e40298d5 698 wxString result ;
427ff662
SC
699 wxChar* buf = result.GetWriteBuf( len ) ;
700#if wxUSE_UNICODE
701 ByteCount byteOutLen ;
702 ByteCount byteInLen = len ;
71111c40 703 ByteCount byteBufferLen = len * SIZEOF_WCHAR_T;
427ff662
SC
704
705 status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from , byteInLen, &byteInLen,
706 (TextPtr)buf, byteBufferLen, &byteOutLen);
707#else
939fba6c 708 memcpy( buf , from , len ) ;
427ff662
SC
709#endif
710 buf[len] = 0 ;
711 result.UngetWriteBuf() ;
712 return result ;
713}
714
715wxString wxMacMakeStringFromCString( const char * from )
716{
717 return wxMacMakeStringFromCString( from , strlen(from) ) ;
718}
719
5be55d56 720wxCharBuffer wxMacStringToCString( const wxString &from )
427ff662
SC
721{
722#if wxUSE_UNICODE
723 OSStatus status = noErr ;
724 ByteCount byteOutLen ;
71111c40 725 ByteCount byteInLen = from.Length() * SIZEOF_WCHAR_T ;
427ff662
SC
726 ByteCount byteBufferLen = from.Length() ;
727 wxCharBuffer result( from.Length() ) ;
728 status = TECConvertText(s_TECUnicodeToNativeC , (ConstTextPtr)from.wc_str() , byteInLen, &byteInLen,
729 (TextPtr)result.data(), byteBufferLen, &byteOutLen);
730 return result ;
731#else
939fba6c 732 return wxCharBuffer( from.c_str() ) ;
427ff662
SC
733#endif
734}
939fba6c 735#endif
427ff662 736
5be55d56 737void wxMacStringToPascal( const wxString&from , StringPtr to )
427ff662 738{
939fba6c 739 wxCharBuffer buf = from.mb_str( wxConvLocal ) ;
427ff662
SC
740 int len = strlen(buf) ;
741
742 if ( len > 255 )
743 len = 255 ;
744 to[0] = len ;
745 memcpy( (char*) &to[1] , buf , len ) ;
746}
747
5be55d56 748wxString wxMacMakeStringFromPascal( ConstStringPtr from )
427ff662 749{
939fba6c 750 return wxString( (char*) &from[1] , wxConvLocal , from[0] ) ;
427ff662
SC
751}
752
e50d5284
RD
753
754wxUint32 wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding)
755{
756 TextEncodingBase enc = 0 ;
fe929141
SC
757 if ( encoding == wxFONTENCODING_DEFAULT )
758 {
759#if wxUSE_GUI
760 encoding = wxFont::GetDefaultEncoding() ;
761#else
762 encoding = wxLocale::GetSystemEncoding() ;
763#endif
764 }
e50d5284
RD
765
766 switch( encoding)
767 {
768 case wxFONTENCODING_ISO8859_1 :
769 enc = kTextEncodingISOLatin1 ;
770 break ;
771 case wxFONTENCODING_ISO8859_2 :
772 enc = kTextEncodingISOLatin2;
773 break ;
774 case wxFONTENCODING_ISO8859_3 :
775 enc = kTextEncodingISOLatin3 ;
776 break ;
777 case wxFONTENCODING_ISO8859_4 :
778 enc = kTextEncodingISOLatin4;
779 break ;
780 case wxFONTENCODING_ISO8859_5 :
781 enc = kTextEncodingISOLatinCyrillic;
782 break ;
783 case wxFONTENCODING_ISO8859_6 :
784 enc = kTextEncodingISOLatinArabic;
785 break ;
786 case wxFONTENCODING_ISO8859_7 :
787 enc = kTextEncodingISOLatinGreek;
788 break ;
789 case wxFONTENCODING_ISO8859_8 :
790 enc = kTextEncodingISOLatinHebrew;
791 break ;
792 case wxFONTENCODING_ISO8859_9 :
793 enc = kTextEncodingISOLatin5;
794 break ;
795 case wxFONTENCODING_ISO8859_10 :
796 enc = kTextEncodingISOLatin6;
797 break ;
798 case wxFONTENCODING_ISO8859_13 :
799 enc = kTextEncodingISOLatin7;
800 break ;
801 case wxFONTENCODING_ISO8859_14 :
802 enc = kTextEncodingISOLatin8;
803 break ;
804 case wxFONTENCODING_ISO8859_15 :
805 enc = kTextEncodingISOLatin9;
806 break ;
807
808 case wxFONTENCODING_KOI8 :
809 enc = kTextEncodingKOI8_R;
810 break ;
811 case wxFONTENCODING_ALTERNATIVE : // MS-DOS CP866
812 enc = kTextEncodingDOSRussian;
813 break ;
814/*
815 case wxFONTENCODING_BULGARIAN :
816 enc = ;
817 break ;
818*/
819 case wxFONTENCODING_CP437 :
820 enc =kTextEncodingDOSLatinUS ;
821 break ;
822 case wxFONTENCODING_CP850 :
823 enc = kTextEncodingDOSLatin1;
824 break ;
825 case wxFONTENCODING_CP852 :
826 enc = kTextEncodingDOSLatin2;
827 break ;
828 case wxFONTENCODING_CP855 :
829 enc = kTextEncodingDOSCyrillic;
830 break ;
831 case wxFONTENCODING_CP866 :
832 enc =kTextEncodingDOSRussian ;
833 break ;
834 case wxFONTENCODING_CP874 :
835 enc = kTextEncodingDOSThai;
836 break ;
837 case wxFONTENCODING_CP932 :
838 enc = kTextEncodingDOSJapanese;
839 break ;
840 case wxFONTENCODING_CP936 :
841 enc =kTextEncodingDOSChineseSimplif ;
842 break ;
843 case wxFONTENCODING_CP949 :
844 enc = kTextEncodingDOSKorean;
845 break ;
846 case wxFONTENCODING_CP950 :
847 enc = kTextEncodingDOSChineseTrad;
848 break ;
849
850 case wxFONTENCODING_CP1250 :
851 enc = kTextEncodingWindowsLatin2;
852 break ;
853 case wxFONTENCODING_CP1251 :
854 enc =kTextEncodingWindowsCyrillic ;
855 break ;
856 case wxFONTENCODING_CP1252 :
857 enc =kTextEncodingWindowsLatin1 ;
858 break ;
859 case wxFONTENCODING_CP1253 :
860 enc = kTextEncodingWindowsGreek;
861 break ;
862 case wxFONTENCODING_CP1254 :
863 enc = kTextEncodingWindowsLatin5;
864 break ;
865 case wxFONTENCODING_CP1255 :
866 enc =kTextEncodingWindowsHebrew ;
867 break ;
868 case wxFONTENCODING_CP1256 :
869 enc =kTextEncodingWindowsArabic ;
870 break ;
871 case wxFONTENCODING_CP1257 :
872 enc = kTextEncodingWindowsBalticRim;
873 break ;
874
875 case wxFONTENCODING_UTF7 :
876 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
877 break ;
878 case wxFONTENCODING_UTF8 :
879 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
880 break ;
881 case wxFONTENCODING_EUC_JP :
882 enc = kTextEncodingEUC_JP;
883 break ;
884 case wxFONTENCODING_UTF16BE :
885 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
886 break ;
887 case wxFONTENCODING_UTF16LE :
888 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
889 break ;
890 case wxFONTENCODING_UTF32BE :
891 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
892 break ;
893 case wxFONTENCODING_UTF32LE :
894 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
895 break ;
896
897 case wxFONTENCODING_MACROMAN :
898 enc = kTextEncodingMacRoman ;
899 break ;
900 case wxFONTENCODING_MACJAPANESE :
901 enc = kTextEncodingMacJapanese ;
902 break ;
903 case wxFONTENCODING_MACCHINESETRAD :
904 enc = kTextEncodingMacChineseTrad ;
905 break ;
906 case wxFONTENCODING_MACKOREAN :
907 enc = kTextEncodingMacKorean ;
908 break ;
909 case wxFONTENCODING_MACARABIC :
910 enc = kTextEncodingMacArabic ;
911 break ;
912 case wxFONTENCODING_MACHEBREW :
913 enc = kTextEncodingMacHebrew ;
914 break ;
915 case wxFONTENCODING_MACGREEK :
916 enc = kTextEncodingMacGreek ;
917 break ;
918 case wxFONTENCODING_MACCYRILLIC :
919 enc = kTextEncodingMacCyrillic ;
920 break ;
921 case wxFONTENCODING_MACDEVANAGARI :
922 enc = kTextEncodingMacDevanagari ;
923 break ;
924 case wxFONTENCODING_MACGURMUKHI :
925 enc = kTextEncodingMacGurmukhi ;
926 break ;
927 case wxFONTENCODING_MACGUJARATI :
928 enc = kTextEncodingMacGujarati ;
929 break ;
930 case wxFONTENCODING_MACORIYA :
931 enc = kTextEncodingMacOriya ;
932 break ;
933 case wxFONTENCODING_MACBENGALI :
934 enc = kTextEncodingMacBengali ;
935 break ;
936 case wxFONTENCODING_MACTAMIL :
937 enc = kTextEncodingMacTamil ;
938 break ;
939 case wxFONTENCODING_MACTELUGU :
940 enc = kTextEncodingMacTelugu ;
941 break ;
942 case wxFONTENCODING_MACKANNADA :
943 enc = kTextEncodingMacKannada ;
944 break ;
945 case wxFONTENCODING_MACMALAJALAM :
946 enc = kTextEncodingMacMalayalam ;
947 break ;
948 case wxFONTENCODING_MACSINHALESE :
949 enc = kTextEncodingMacSinhalese ;
950 break ;
951 case wxFONTENCODING_MACBURMESE :
952 enc = kTextEncodingMacBurmese ;
953 break ;
954 case wxFONTENCODING_MACKHMER :
955 enc = kTextEncodingMacKhmer ;
956 break ;
957 case wxFONTENCODING_MACTHAI :
958 enc = kTextEncodingMacThai ;
959 break ;
960 case wxFONTENCODING_MACLAOTIAN :
961 enc = kTextEncodingMacLaotian ;
962 break ;
963 case wxFONTENCODING_MACGEORGIAN :
964 enc = kTextEncodingMacGeorgian ;
965 break ;
966 case wxFONTENCODING_MACARMENIAN :
967 enc = kTextEncodingMacArmenian ;
968 break ;
969 case wxFONTENCODING_MACCHINESESIMP :
970 enc = kTextEncodingMacChineseSimp ;
971 break ;
972 case wxFONTENCODING_MACTIBETAN :
973 enc = kTextEncodingMacTibetan ;
974 break ;
975 case wxFONTENCODING_MACMONGOLIAN :
976 enc = kTextEncodingMacMongolian ;
977 break ;
978 case wxFONTENCODING_MACETHIOPIC :
979 enc = kTextEncodingMacEthiopic ;
980 break ;
981 case wxFONTENCODING_MACCENTRALEUR :
982 enc = kTextEncodingMacCentralEurRoman ;
983 break ;
984 case wxFONTENCODING_MACVIATNAMESE :
985 enc = kTextEncodingMacVietnamese ;
986 break ;
987 case wxFONTENCODING_MACARABICEXT :
988 enc = kTextEncodingMacExtArabic ;
989 break ;
990 case wxFONTENCODING_MACSYMBOL :
991 enc = kTextEncodingMacSymbol ;
992 break ;
993 case wxFONTENCODING_MACDINGBATS :
994 enc = kTextEncodingMacDingbats ;
995 break ;
996 case wxFONTENCODING_MACTURKISH :
997 enc = kTextEncodingMacTurkish ;
998 break ;
999 case wxFONTENCODING_MACCROATIAN :
1000 enc = kTextEncodingMacCroatian ;
1001 break ;
1002 case wxFONTENCODING_MACICELANDIC :
1003 enc = kTextEncodingMacIcelandic ;
1004 break ;
1005 case wxFONTENCODING_MACROMANIAN :
1006 enc = kTextEncodingMacRomanian ;
1007 break ;
1008 case wxFONTENCODING_MACCELTIC :
1009 enc = kTextEncodingMacCeltic ;
1010 break ;
1011 case wxFONTENCODING_MACGAELIC :
1012 enc = kTextEncodingMacGaelic ;
1013 break ;
1014 case wxFONTENCODING_MACKEYBOARD :
1015 enc = kTextEncodingMacKeyboardGlyphs ;
1016 break ;
1017 } ;
1018 return enc ;
1019}
1020
1021wxFontEncoding wxMacGetFontEncFromSystemEnc(wxUint32 encoding)
1022{
1023 wxFontEncoding enc = wxFONTENCODING_DEFAULT ;
1024
1025 switch( encoding)
1026 {
1027 case kTextEncodingISOLatin1 :
1028 enc = wxFONTENCODING_ISO8859_1 ;
1029 break ;
1030 case kTextEncodingISOLatin2 :
1031 enc = wxFONTENCODING_ISO8859_2;
1032 break ;
1033 case kTextEncodingISOLatin3 :
1034 enc = wxFONTENCODING_ISO8859_3 ;
1035 break ;
1036 case kTextEncodingISOLatin4 :
1037 enc = wxFONTENCODING_ISO8859_4;
1038 break ;
1039 case kTextEncodingISOLatinCyrillic :
1040 enc = wxFONTENCODING_ISO8859_5;
1041 break ;
1042 case kTextEncodingISOLatinArabic :
1043 enc = wxFONTENCODING_ISO8859_6;
1044 break ;
1045 case kTextEncodingISOLatinGreek :
1046 enc = wxFONTENCODING_ISO8859_7;
1047 break ;
1048 case kTextEncodingISOLatinHebrew :
1049 enc = wxFONTENCODING_ISO8859_8;
1050 break ;
1051 case kTextEncodingISOLatin5 :
1052 enc = wxFONTENCODING_ISO8859_9;
1053 break ;
1054 case kTextEncodingISOLatin6 :
1055 enc = wxFONTENCODING_ISO8859_10;
1056 break ;
1057 case kTextEncodingISOLatin7 :
1058 enc = wxFONTENCODING_ISO8859_13;
1059 break ;
1060 case kTextEncodingISOLatin8 :
1061 enc = wxFONTENCODING_ISO8859_14;
1062 break ;
1063 case kTextEncodingISOLatin9 :
1064 enc =wxFONTENCODING_ISO8859_15 ;
1065 break ;
1066
1067 case kTextEncodingKOI8_R :
1068 enc = wxFONTENCODING_KOI8;
1069 break ;
1070/*
1071 case :
1072 enc = wxFONTENCODING_BULGARIAN;
1073 break ;
1074*/
fe929141 1075 case kTextEncodingDOSLatinUS :
e50d5284
RD
1076 enc = wxFONTENCODING_CP437;
1077 break ;
1078 case kTextEncodingDOSLatin1 :
1079 enc = wxFONTENCODING_CP850;
1080 break ;
1081 case kTextEncodingDOSLatin2 :
1082 enc =wxFONTENCODING_CP852 ;
1083 break ;
1084 case kTextEncodingDOSCyrillic :
1085 enc = wxFONTENCODING_CP855;
1086 break ;
1087 case kTextEncodingDOSRussian :
1088 enc = wxFONTENCODING_CP866;
1089 break ;
1090 case kTextEncodingDOSThai :
1091 enc =wxFONTENCODING_CP874 ;
1092 break ;
1093 case kTextEncodingDOSJapanese :
1094 enc = wxFONTENCODING_CP932;
1095 break ;
1096 case kTextEncodingDOSChineseSimplif :
1097 enc = wxFONTENCODING_CP936;
1098 break ;
1099 case kTextEncodingDOSKorean :
1100 enc = wxFONTENCODING_CP949;
1101 break ;
1102 case kTextEncodingDOSChineseTrad :
1103 enc = wxFONTENCODING_CP950;
1104 break ;
1105
1106 case kTextEncodingWindowsLatin2 :
1107 enc = wxFONTENCODING_CP1250;
1108 break ;
1109 case kTextEncodingWindowsCyrillic :
1110 enc = wxFONTENCODING_CP1251;
1111 break ;
1112 case kTextEncodingWindowsLatin1 :
1113 enc = wxFONTENCODING_CP1252;
1114 break ;
1115 case kTextEncodingWindowsGreek :
1116 enc = wxFONTENCODING_CP1253;
1117 break ;
1118 case kTextEncodingWindowsLatin5 :
1119 enc = wxFONTENCODING_CP1254;
1120 break ;
1121 case kTextEncodingWindowsHebrew :
1122 enc = wxFONTENCODING_CP1255;
1123 break ;
1124 case kTextEncodingWindowsArabic :
1125 enc = wxFONTENCODING_CP1256;
1126 break ;
1127 case kTextEncodingWindowsBalticRim :
1128 enc =wxFONTENCODING_CP1257 ;
1129 break ;
1130 case kTextEncodingEUC_JP :
1131 enc = wxFONTENCODING_EUC_JP;
1132 break ;
1133 /*
1134 case wxFONTENCODING_UTF7 :
1135 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
1136 break ;
1137 case wxFONTENCODING_UTF8 :
1138 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
1139 break ;
1140 case wxFONTENCODING_UTF16BE :
1141 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
1142 break ;
1143 case wxFONTENCODING_UTF16LE :
1144 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
1145 break ;
1146 case wxFONTENCODING_UTF32BE :
1147 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
1148 break ;
1149 case wxFONTENCODING_UTF32LE :
1150 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
1151 break ;
1152 */
1153 case kTextEncodingMacRoman :
1154 enc = wxFONTENCODING_MACROMAN ;
1155 break ;
1156 case kTextEncodingMacJapanese :
1157 enc = wxFONTENCODING_MACJAPANESE ;
1158 break ;
1159 case kTextEncodingMacChineseTrad :
1160 enc = wxFONTENCODING_MACCHINESETRAD ;
1161 break ;
1162 case kTextEncodingMacKorean :
1163 enc = wxFONTENCODING_MACKOREAN ;
1164 break ;
1165 case kTextEncodingMacArabic :
1166 enc =wxFONTENCODING_MACARABIC ;
1167 break ;
1168 case kTextEncodingMacHebrew :
1169 enc = wxFONTENCODING_MACHEBREW ;
1170 break ;
1171 case kTextEncodingMacGreek :
1172 enc = wxFONTENCODING_MACGREEK ;
1173 break ;
1174 case kTextEncodingMacCyrillic :
1175 enc = wxFONTENCODING_MACCYRILLIC ;
1176 break ;
1177 case kTextEncodingMacDevanagari :
1178 enc = wxFONTENCODING_MACDEVANAGARI ;
1179 break ;
1180 case kTextEncodingMacGurmukhi :
1181 enc = wxFONTENCODING_MACGURMUKHI ;
1182 break ;
1183 case kTextEncodingMacGujarati :
1184 enc = wxFONTENCODING_MACGUJARATI ;
1185 break ;
1186 case kTextEncodingMacOriya :
1187 enc =wxFONTENCODING_MACORIYA ;
1188 break ;
1189 case kTextEncodingMacBengali :
1190 enc =wxFONTENCODING_MACBENGALI ;
1191 break ;
1192 case kTextEncodingMacTamil :
1193 enc = wxFONTENCODING_MACTAMIL ;
1194 break ;
1195 case kTextEncodingMacTelugu :
1196 enc = wxFONTENCODING_MACTELUGU ;
1197 break ;
1198 case kTextEncodingMacKannada :
1199 enc = wxFONTENCODING_MACKANNADA ;
1200 break ;
1201 case kTextEncodingMacMalayalam :
1202 enc = wxFONTENCODING_MACMALAJALAM ;
1203 break ;
1204 case kTextEncodingMacSinhalese :
1205 enc = wxFONTENCODING_MACSINHALESE ;
1206 break ;
1207 case kTextEncodingMacBurmese :
1208 enc = wxFONTENCODING_MACBURMESE ;
1209 break ;
1210 case kTextEncodingMacKhmer :
1211 enc = wxFONTENCODING_MACKHMER ;
1212 break ;
1213 case kTextEncodingMacThai :
1214 enc = wxFONTENCODING_MACTHAI ;
1215 break ;
1216 case kTextEncodingMacLaotian :
1217 enc = wxFONTENCODING_MACLAOTIAN ;
1218 break ;
1219 case kTextEncodingMacGeorgian :
1220 enc = wxFONTENCODING_MACGEORGIAN ;
1221 break ;
1222 case kTextEncodingMacArmenian :
1223 enc = wxFONTENCODING_MACARMENIAN ;
1224 break ;
1225 case kTextEncodingMacChineseSimp :
1226 enc = wxFONTENCODING_MACCHINESESIMP ;
1227 break ;
1228 case kTextEncodingMacTibetan :
1229 enc = wxFONTENCODING_MACTIBETAN ;
1230 break ;
1231 case kTextEncodingMacMongolian :
1232 enc = wxFONTENCODING_MACMONGOLIAN ;
1233 break ;
1234 case kTextEncodingMacEthiopic :
1235 enc = wxFONTENCODING_MACETHIOPIC ;
1236 break ;
1237 case kTextEncodingMacCentralEurRoman:
1238 enc = wxFONTENCODING_MACCENTRALEUR ;
1239 break ;
1240 case kTextEncodingMacVietnamese:
1241 enc = wxFONTENCODING_MACVIATNAMESE ;
1242 break ;
1243 case kTextEncodingMacExtArabic :
1244 enc = wxFONTENCODING_MACARABICEXT ;
1245 break ;
1246 case kTextEncodingMacSymbol :
1247 enc = wxFONTENCODING_MACSYMBOL ;
1248 break ;
1249 case kTextEncodingMacDingbats :
1250 enc = wxFONTENCODING_MACDINGBATS ;
1251 break ;
1252 case kTextEncodingMacTurkish :
1253 enc = wxFONTENCODING_MACTURKISH ;
1254 break ;
1255 case kTextEncodingMacCroatian :
1256 enc = wxFONTENCODING_MACCROATIAN ;
1257 break ;
1258 case kTextEncodingMacIcelandic :
1259 enc = wxFONTENCODING_MACICELANDIC ;
1260 break ;
1261 case kTextEncodingMacRomanian :
1262 enc = wxFONTENCODING_MACROMANIAN ;
1263 break ;
1264 case kTextEncodingMacCeltic :
1265 enc = wxFONTENCODING_MACCELTIC ;
1266 break ;
1267 case kTextEncodingMacGaelic :
1268 enc = wxFONTENCODING_MACGAELIC ;
1269 break ;
1270 case kTextEncodingMacKeyboardGlyphs :
1271 enc = wxFONTENCODING_MACKEYBOARD ;
1272 break ;
1273 } ;
1274 return enc ;
1275}
1276
2dbc444a
RD
1277#endif // wxUSE_BASE
1278
1279#if wxUSE_GUI
1280
1281
5be55d56 1282//
427ff662
SC
1283// CFStringRefs (Carbon only)
1284//
1285
1286#if TARGET_CARBON
71111c40
SC
1287
1288#if (wxUSE_UNICODE == 1) && (SIZEOF_WCHAR_T == 4)
1289
1290TECObjectRef s_TECUnicode32To16 = NULL ;
1291TECObjectRef s_TECUnicode16To32 = NULL ;
1292
1293class wxMacUnicodeConverters
1294{
1295public :
1296 wxMacUnicodeConverters() ;
1297 ~wxMacUnicodeConverters() ;
1298} ;
1299
1300wxMacUnicodeConverters guard ;
1301
1302wxMacUnicodeConverters::wxMacUnicodeConverters()
1303{
1304 OSStatus status = noErr ;
fe929141
SC
1305 TextEncoding kUnicode32 = CreateTextEncoding(kTextEncodingUnicodeDefault,kTextEncodingDefaultVariant,kUnicode32BitFormat) ;
1306 TextEncoding kUnicode16 = CreateTextEncoding(kTextEncodingUnicodeDefault,kTextEncodingDefaultVariant,kUnicode16BitFormat) ;
71111c40
SC
1307
1308 status = TECCreateConverter(&s_TECUnicode16To32,
1309 kUnicode16,
1310 kUnicode32);
1311 status = TECCreateConverter(&s_TECUnicode32To16,
1312 kUnicode32,
1313 kUnicode16);
1314}
1315
1316wxMacUnicodeConverters::~wxMacUnicodeConverters()
1317{
1318 OSStatus status = noErr ;
1319 status = TECDisposeConverter(s_TECUnicode32To16);
1320 status = TECDisposeConverter(s_TECUnicode16To32);
1321}
1322#endif
427ff662 1323// converts this string into a carbon foundation string with optional pc 2 mac encoding
fe929141 1324void wxMacCFStringHolder::Assign( const wxString &st , wxFontEncoding encoding )
427ff662 1325{
fe929141 1326 Release() ;
71111c40 1327 wxString str = st ;
8aa701ed 1328 wxMacConvertNewlines13To10( &str ) ;
427ff662 1329#if wxUSE_UNICODE
fe929141 1330 size_t len = str.Len() ;
71111c40
SC
1331 UniChar *unibuf ;
1332#if SIZEOF_WCHAR_T == 2
1333 unibuf = (UniChar*)str.wc_str() ;
1334#else
1335 OSStatus status = noErr ;
1336 ByteCount byteOutLen ;
1337 ByteCount byteInLen = len * SIZEOF_WCHAR_T ;
1338 ByteCount byteBufferLen = len * sizeof( UniChar ) ;
1339 unibuf = (UniChar*) malloc(byteBufferLen) ;
fe929141
SC
1340 for( int i = 0 ; i < len ; ++ i )
1341 unibuf[i] = (UniChar) str[i] ;
1342 /*
71111c40
SC
1343 status = TECConvertText( s_TECUnicode32To16 , (ConstTextPtr)str.wc_str() , byteInLen, &byteInLen,
1344 (TextPtr)unibuf, byteBufferLen, &byteOutLen);
fe929141 1345 */
71111c40 1346#endif
5be55d56 1347 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault,
71111c40
SC
1348 unibuf , len );
1349#if SIZEOF_WCHAR_T == 2
1350 // as long as UniChar is the same as wchar_t nothing to do here
427ff662 1351#else
71111c40
SC
1352 free( unibuf ) ;
1353#endif
1354
1355#else // not wxUSE_UNICODE
427ff662 1356 m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
fe929141 1357 wxMacGetSystemEncFromFontEnc( encoding ) ) ;
427ff662
SC
1358#endif
1359 m_release = true ;
1360}
1361
fe929141 1362wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding)
427ff662
SC
1363{
1364 wxString result ;
1365 Size len = CFStringGetLength( m_cfs ) ;
1366 wxChar* buf = result.GetWriteBuf( len ) ;
1367#if wxUSE_UNICODE
fe929141
SC
1368 UniChar *unibuf ;
1369#if SIZEOF_WCHAR_T == 2
1370 unibuf = (UniChar*) buf ;
1371#else
1372 unibuf = malloc( len * sizeof( UniChar ) ) ;
1373#endif
1374 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , len ) , (UniChar*) unibuf ) ;
1375#if SIZEOF_WCHAR_T == 2
1376 // as long as UniChar is the same as wchar_t nothing to do here
1377#else
1378 for( int i = 0 ; i < len ; ++ i )
1379 buf[i] = (wchar_t) unibuf[i] ;
1380 free( unibuf ) ;
1381#endif
427ff662 1382#else
fe929141 1383 CFStringGetCString( m_cfs , buf , len+1 , wxMacGetSystemEncFromFontEnc( encoding ) ) ;
427ff662
SC
1384#endif
1385 buf[len] = 0 ;
8aa701ed 1386 wxMacConvertNewlines10To13( buf ) ;
427ff662
SC
1387 result.UngetWriteBuf() ;
1388 return result ;
1389}
1390
3d963f81
SC
1391#endif //TARGET_CARBON
1392
8aa701ed
SC
1393void wxMacConvertNewlines13To10( char * data )
1394{
1395 char * buf = data ;
1396 while( (buf=strchr(buf,0x0d)) != NULL )
1397 {
1398 *buf = 0x0a ;
1399 buf++ ;
1400 }
1401}
1402
1403void wxMacConvertNewlines10To13( char * data )
1404{
1405 char * buf = data ;
1406 while( (buf=strchr(buf,0x0a)) != NULL )
1407 {
1408 *buf = 0x0d ;
1409 buf++ ;
1410 }
1411}
1412
1413void wxMacConvertNewlines13To10( wxString * data )
1414{
be6628ce
SC
1415 size_t len = data->Length() ;
1416
1417 if ( len == 0 || wxStrchr(data->c_str(),0x0d)==NULL)
8aa701ed
SC
1418 return ;
1419
be6628ce
SC
1420 wxString temp(*data) ;
1421 wxStringBuffer buf(*data,len ) ;
1422 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
1423
1424 wxMacConvertNewlines13To10( buf ) ;
8aa701ed
SC
1425}
1426
1427void wxMacConvertNewlines10To13( wxString * data )
1428{
be6628ce
SC
1429 size_t len = data->Length() ;
1430
1431 if ( data->Length() == 0 || wxStrchr(data->c_str(),0x0a)==NULL)
8aa701ed 1432 return ;
be6628ce
SC
1433
1434 wxString temp(*data) ;
1435 wxStringBuffer buf(*data,len ) ;
1436 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
1437 wxMacConvertNewlines10To13( buf ) ;
8aa701ed
SC
1438}
1439
1440
1441#if wxUSE_UNICODE
1442void wxMacConvertNewlines13To10( wxChar * data )
1443{
1444 wxChar * buf = data ;
1445 while( (buf=wxStrchr(buf,0x0d)) != NULL )
1446 {
1447 *buf = 0x0a ;
1448 buf++ ;
1449 }
1450}
1451
1452void wxMacConvertNewlines10To13( wxChar * data )
1453{
1454 wxChar * buf = data ;
1455 while( (buf=wxStrchr(buf,0x0a)) != NULL )
1456 {
1457 *buf = 0x0d ;
1458 buf++ ;
1459 }
1460}
1461#endif
1462
a434b43f
VZ
1463// ----------------------------------------------------------------------------
1464// debugging support
1465// ----------------------------------------------------------------------------
1466
1467#if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
1468
1469// MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
1470
1471#ifndef __MetroNubUtils__
1472#include "MetroNubUtils.h"
1473#endif
1474
1475#ifndef __GESTALT__
1476#include <Gestalt.h>
1477#endif
1478
1479#if TARGET_API_MAC_CARBON
1480
1481 #include <CodeFragments.h>
1482
536732e4 1483 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
a434b43f
VZ
1484
1485 ProcPtr gCallUniversalProc_Proc = NULL;
1486
1487#endif
1488
1489static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
1490
1491static long fRunOnce = false;
1492
1493/* ---------------------------------------------------------------------------
1494 IsMetroNubInstalled
1495 --------------------------------------------------------------------------- */
1496
1497Boolean IsMetroNubInstalled()
1498{
1499 if (!fRunOnce)
1500 {
1501 long result, value;
1502
1503 fRunOnce = true;
1504 gMetroNubEntry = NULL;
1505
1506 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
1507 {
1508 /* look for MetroNub's Gestalt selector */
1509 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
1510 {
1511
1512 #if TARGET_API_MAC_CARBON
1513 if (gCallUniversalProc_Proc == NULL)
1514 {
1515 CFragConnectionID connectionID;
1516 Ptr mainAddress;
1517 Str255 errorString;
1518 ProcPtr symbolAddress;
1519 OSErr err;
1520 CFragSymbolClass symbolClass;
1521
1522 symbolAddress = NULL;
1523 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
1524 &connectionID, &mainAddress, errorString);
1525
1526 if (err != noErr)
1527 {
1528 gCallUniversalProc_Proc = NULL;
1529 goto end;
1530 }
1531
1532 err = FindSymbol(connectionID, "\pCallUniversalProc",
1533 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
1534
1535 if (err != noErr)
1536 {
1537 gCallUniversalProc_Proc = NULL;
1538 goto end;
1539 }
1540 }
1541 #endif
1542
1543 {
1544 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
1545
1546 /* make sure the version of the API is compatible */
1547 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
1548 kMetroNubUserAPIVersion <= block->apiHiVersion)
1549 gMetroNubEntry = block; /* success! */
1550 }
1551
1552 }
1553 }
1554 }
1555
1556end:
1557
1558#if TARGET_API_MAC_CARBON
1559 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
1560#else
1561 return (gMetroNubEntry != NULL);
1562#endif
1563}
1564
1565/* ---------------------------------------------------------------------------
1566 IsMWDebuggerRunning [v1 API]
1567 --------------------------------------------------------------------------- */
1568
1569Boolean IsMWDebuggerRunning()
1570{
1571 if (IsMetroNubInstalled())
1572 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
1573 else
1574 return false;
1575}
1576
1577/* ---------------------------------------------------------------------------
1578 AmIBeingMWDebugged [v1 API]
1579 --------------------------------------------------------------------------- */
1580
1581Boolean AmIBeingMWDebugged()
1582{
1583 if (IsMetroNubInstalled())
1584 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
1585 else
1586 return false;
1587}
1588
1589extern bool WXDLLEXPORT wxIsDebuggerRunning()
1590{
1591 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
1592}
1593
1594#else
1595
1596extern bool WXDLLEXPORT wxIsDebuggerRunning()
1597{
1598 return false;
1599}
1600
1601#endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
1602
b6ed2b86
VZ
1603#endif // wxUSE_GUI
1604