]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/utils.cpp
applied patch 923858 (fixes crash in zlib streams)
[wxWidgets.git] / src / mac / carbon / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 // Note: this is done in utilscmn.cpp now.
14 // #pragma implementation "utils.h"
15 #endif
16
17 #include "wx/setup.h"
18 #include "wx/utils.h"
19 #include "wx/app.h"
20 #include "wx/apptrait.h"
21
22 #if wxUSE_GUI
23 #include "wx/mac/uma.h"
24 #include "wx/font.h"
25 #else
26 #include "wx/intl.h"
27 #endif
28
29 #include <ctype.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35
36 #ifdef __DARWIN__
37 # include "MoreFilesX.h"
38 #else
39 # include "MoreFiles.h"
40 # include "MoreFilesExtras.h"
41 #endif
42
43 #ifndef __DARWIN__
44 #include <Threads.h>
45 #include <Sound.h>
46 #endif
47
48 #include <ATSUnicode.h>
49 #include <TextCommon.h>
50 #include <TextEncodingConverter.h>
51
52 #include "wx/mac/private.h" // includes mac headers
53
54 #if defined(__MWERKS__) && wxUSE_UNICODE
55 #include <wtime.h>
56 #endif
57
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
63 static 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
83 #if wxUSE_BASE
84
85 #ifndef __DARWIN__
86 // defined in unix/utilsunx.cpp for Mac OS X
87
88 // get full hostname (with domain name if possible)
89 bool wxGetFullHostName(wxChar *buf, int maxSize)
90 {
91 return wxGetHostName(buf, maxSize);
92 }
93
94 // Get hostname only (without domain name)
95 bool wxGetHostName(wxChar *buf, int maxSize)
96 {
97 // Gets Chooser name of user by examining a System resource.
98
99 const short kComputerNameID = -16413;
100
101 short oldResFile = CurResFile() ;
102 UseResFile(0);
103 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
104 UseResFile(oldResFile);
105
106 if (chooserName && *chooserName)
107 {
108 HLock( (Handle) chooserName ) ;
109 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
110 HUnlock( (Handle) chooserName ) ;
111 ReleaseResource( (Handle) chooserName ) ;
112 wxStrncpy( buf , name , maxSize - 1 ) ;
113 }
114 else
115 buf[0] = 0 ;
116
117 return TRUE;
118 }
119
120 // Get user ID e.g. jacs
121 bool wxGetUserId(wxChar *buf, int maxSize)
122 {
123 return wxGetUserName( buf , maxSize ) ;
124 }
125
126 const wxChar* wxGetHomeDir(wxString *pstr)
127 {
128 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
129 return pstr->c_str() ;
130 }
131
132 // Get user name e.g. Stefan Csomor
133 bool wxGetUserName(wxChar *buf, int maxSize)
134 {
135 // Gets Chooser name of user by examining a System resource.
136
137 const short kChooserNameID = -16096;
138
139 short oldResFile = CurResFile() ;
140 UseResFile(0);
141 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
142 UseResFile(oldResFile);
143
144 if (chooserName && *chooserName)
145 {
146 HLock( (Handle) chooserName ) ;
147 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
148 HUnlock( (Handle) chooserName ) ;
149 ReleaseResource( (Handle) chooserName ) ;
150 wxStrncpy( buf , name , maxSize - 1 ) ;
151 }
152 else
153 buf[0] = 0 ;
154
155 return TRUE;
156 }
157
158 int wxKill(long pid, wxSignal sig , wxKillError *rc )
159 {
160 // TODO
161 return 0;
162 }
163
164 WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
165 {
166 // TODO : under classic there is no environement support, under X yes
167 return false ;
168 }
169
170 // set the env var name to the given value, return TRUE on success
171 WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
172 {
173 // TODO : under classic there is no environement support, under X yes
174 return false ;
175 }
176
177 //
178 // Execute a program in an Interactive Shell
179 //
180 bool wxShell(const wxString& command)
181 {
182 // TODO
183 return FALSE;
184 }
185
186 // Shutdown or reboot the PC
187 bool wxShutdown(wxShutdownFlags wFlags)
188 {
189 // TODO
190 return FALSE;
191 }
192
193 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
194 long wxGetFreeMemory()
195 {
196 return FreeMem() ;
197 }
198
199 void wxUsleep(unsigned long milliseconds)
200 {
201 clock_t start = clock() ;
202 do
203 {
204 YieldToAnyThread() ;
205 } while( clock() - start < milliseconds / 1000.0 * CLOCKS_PER_SEC ) ;
206 }
207
208 void wxSleep(int nSecs)
209 {
210 wxUsleep(1000*nSecs);
211 }
212
213 // Consume all events until no more left
214 void wxFlushEvents()
215 {
216 }
217
218 #endif // !__DARWIN__
219
220 // Emit a beeeeeep
221 void wxBell()
222 {
223 SysBeep(30);
224 }
225
226 wxToolkitInfo& wxConsoleAppTraits::GetToolkitInfo()
227 {
228 static wxToolkitInfo info;
229 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
230 info.name = _T("wxBase");
231 return info;
232 }
233
234 #endif // wxUSE_BASE
235
236 #if wxUSE_GUI
237
238 wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
239 {
240 static wxToolkitInfo info;
241 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
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
248 return info;
249 }
250
251 // Reading and writing resources (eg WIN.INI, .Xdefaults)
252 #if wxUSE_RESOURCES
253 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
254 {
255 // TODO
256 return FALSE;
257 }
258
259 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
260 {
261 wxString buf;
262 buf.Printf(wxT("%.4f"), value);
263
264 return wxWriteResource(section, entry, buf, file);
265 }
266
267 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
268 {
269 wxString buf;
270 buf.Printf(wxT("%ld"), value);
271
272 return wxWriteResource(section, entry, buf, file);
273 }
274
275 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
276 {
277 wxString buf;
278 buf.Printf(wxT("%d"), value);
279
280 return wxWriteResource(section, entry, buf, file);
281 }
282
283 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
284 {
285 // TODO
286 return FALSE;
287 }
288
289 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
290 {
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;
300 }
301
302 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
303 {
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;
313 }
314
315 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
316 {
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;
326 }
327 #endif // wxUSE_RESOURCES
328
329 int gs_wxBusyCursorCount = 0;
330 extern wxCursor gMacCurrentCursor ;
331 wxCursor gMacStoredActiveCursor ;
332
333 // Set the cursor to the busy cursor for all windows
334 void wxBeginBusyCursor(wxCursor *cursor)
335 {
336 if (gs_wxBusyCursorCount++ == 0)
337 {
338 gMacStoredActiveCursor = gMacCurrentCursor ;
339 cursor->MacInstall() ;
340 }
341 //else: nothing to do, already set
342 }
343
344 // Restore cursor to normal
345 void wxEndBusyCursor()
346 {
347 wxCHECK_RET( gs_wxBusyCursorCount > 0,
348 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
349
350 if (--gs_wxBusyCursorCount == 0)
351 {
352 gMacStoredActiveCursor.MacInstall() ;
353 gMacStoredActiveCursor = wxNullCursor ;
354 }
355 }
356
357 // TRUE if we're between the above two calls
358 bool wxIsBusy()
359 {
360 return (gs_wxBusyCursorCount > 0);
361 }
362
363 #endif // wxUSE_GUI
364
365 #if wxUSE_BASE
366
367 wxString wxMacFindFolder( short vol,
368 OSType folderType,
369 Boolean createFolder)
370 {
371 short vRefNum ;
372 long dirID ;
373 wxString strDir ;
374
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 ;
384 }
385
386 #endif // wxUSE_BASE
387
388 #if wxUSE_GUI
389
390 // Check whether this window wants to process messages, e.g. Stop button
391 // in long calculations.
392 bool wxCheckForInterrupt(wxWindow *wnd)
393 {
394 // TODO
395 return FALSE;
396 }
397
398 void wxGetMousePosition( int* x, int* y )
399 {
400 Point pt ;
401
402 GetMouse( &pt ) ;
403 LocalToGlobal( &pt ) ;
404 *x = pt.h ;
405 *y = pt.v ;
406 };
407
408 // Return TRUE if we have a colour display
409 bool wxColourDisplay()
410 {
411 return TRUE;
412 }
413
414 // Returns depth of screen
415 int wxDisplayDepth()
416 {
417 Rect globRect ;
418 SetRect(&globRect, -32760, -32760, 32760, 32760);
419 GDHandle theMaxDevice;
420
421 int theDepth = 8;
422 theMaxDevice = GetMaxDevice(&globRect);
423 if (theMaxDevice != nil)
424 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
425
426 return theDepth ;
427 }
428
429 // Get size of display
430 void wxDisplaySize(int *width, int *height)
431 {
432 BitMap screenBits;
433 GetQDGlobalsScreenBits( &screenBits );
434
435 if (width != NULL) {
436 *width = screenBits.bounds.right - screenBits.bounds.left ;
437 }
438 if (height != NULL) {
439 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
440 }
441 }
442
443 void wxDisplaySizeMM(int *width, int *height)
444 {
445 wxDisplaySize(width, height);
446 // on mac 72 is fixed (at least now ;-)
447 float cvPt2Mm = 25.4 / 72;
448
449 if (width != NULL) {
450 *width = int( *width * cvPt2Mm );
451 }
452 if (height != NULL) {
453 *height = int( *height * cvPt2Mm );
454 }
455 }
456
457 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
458 {
459 Rect r ;
460 GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ;
461 if ( x )
462 *x = r.left ;
463 if ( y )
464 *y = r.top ;
465 if ( width )
466 *width = r.right - r.left ;
467 if ( height )
468 *height = r.bottom - r.top ;
469 }
470
471 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
472 {
473 return wxGenericFindWindowAtPoint(pt);
474 }
475
476 #endif // wxUSE_GUI
477
478 #if wxUSE_BASE
479
480 wxString wxGetOsDescription()
481 {
482 #ifdef WXWIN_OS_DESCRIPTION
483 // use configure generated description if available
484 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION) + wxString(wxT(")"));
485 #else
486 return wxT("MacOS") ; //TODO:define further
487 #endif
488 }
489
490 #ifndef __DARWIN__
491 wxChar *wxGetUserHome (const wxString& user)
492 {
493 // TODO
494 return NULL;
495 }
496
497 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
498 {
499 if ( path.empty() )
500 return FALSE;
501
502 wxString p = path ;
503 if (p[0u] == ':' ) {
504 p = wxGetCwd() + p ;
505 }
506
507 int pos = p.Find(':') ;
508 if ( pos != wxNOT_FOUND ) {
509 p = p.Mid(1,pos) ;
510 }
511
512 p = p + wxT(":") ;
513
514 Str255 volumeName ;
515 XVolumeParam pb ;
516
517 wxMacStringToPascal( p , volumeName ) ;
518 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
519 if ( err == noErr ) {
520 if ( pTotal ) {
521 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
522 }
523 if ( pFree ) {
524 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
525 }
526 }
527
528 return err == noErr ;
529 }
530 #endif // !__DARWIN__
531
532 //---------------------------------------------------------------------------
533 // wxMac Specific utility functions
534 //---------------------------------------------------------------------------
535
536 void wxMacStringToPascal( const wxString&from , StringPtr to )
537 {
538 wxCharBuffer buf = from.mb_str( wxConvLocal ) ;
539 int len = strlen(buf) ;
540
541 if ( len > 255 )
542 len = 255 ;
543 to[0] = len ;
544 memcpy( (char*) &to[1] , buf , len ) ;
545 }
546
547 wxString wxMacMakeStringFromPascal( ConstStringPtr from )
548 {
549 return wxString( (char*) &from[1] , wxConvLocal , from[0] ) ;
550 }
551
552
553 wxUint32 wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding)
554 {
555 TextEncodingBase enc = 0 ;
556 if ( encoding == wxFONTENCODING_DEFAULT )
557 {
558 #if wxUSE_GUI
559 encoding = wxFont::GetDefaultEncoding() ;
560 #else
561 encoding = wxLocale::GetSystemEncoding() ;
562 #endif
563 }
564
565 switch( encoding)
566 {
567 case wxFONTENCODING_ISO8859_1 :
568 enc = kTextEncodingISOLatin1 ;
569 break ;
570 case wxFONTENCODING_ISO8859_2 :
571 enc = kTextEncodingISOLatin2;
572 break ;
573 case wxFONTENCODING_ISO8859_3 :
574 enc = kTextEncodingISOLatin3 ;
575 break ;
576 case wxFONTENCODING_ISO8859_4 :
577 enc = kTextEncodingISOLatin4;
578 break ;
579 case wxFONTENCODING_ISO8859_5 :
580 enc = kTextEncodingISOLatinCyrillic;
581 break ;
582 case wxFONTENCODING_ISO8859_6 :
583 enc = kTextEncodingISOLatinArabic;
584 break ;
585 case wxFONTENCODING_ISO8859_7 :
586 enc = kTextEncodingISOLatinGreek;
587 break ;
588 case wxFONTENCODING_ISO8859_8 :
589 enc = kTextEncodingISOLatinHebrew;
590 break ;
591 case wxFONTENCODING_ISO8859_9 :
592 enc = kTextEncodingISOLatin5;
593 break ;
594 case wxFONTENCODING_ISO8859_10 :
595 enc = kTextEncodingISOLatin6;
596 break ;
597 case wxFONTENCODING_ISO8859_13 :
598 enc = kTextEncodingISOLatin7;
599 break ;
600 case wxFONTENCODING_ISO8859_14 :
601 enc = kTextEncodingISOLatin8;
602 break ;
603 case wxFONTENCODING_ISO8859_15 :
604 enc = kTextEncodingISOLatin9;
605 break ;
606
607 case wxFONTENCODING_KOI8 :
608 enc = kTextEncodingKOI8_R;
609 break ;
610 case wxFONTENCODING_ALTERNATIVE : // MS-DOS CP866
611 enc = kTextEncodingDOSRussian;
612 break ;
613 /*
614 case wxFONTENCODING_BULGARIAN :
615 enc = ;
616 break ;
617 */
618 case wxFONTENCODING_CP437 :
619 enc =kTextEncodingDOSLatinUS ;
620 break ;
621 case wxFONTENCODING_CP850 :
622 enc = kTextEncodingDOSLatin1;
623 break ;
624 case wxFONTENCODING_CP852 :
625 enc = kTextEncodingDOSLatin2;
626 break ;
627 case wxFONTENCODING_CP855 :
628 enc = kTextEncodingDOSCyrillic;
629 break ;
630 case wxFONTENCODING_CP866 :
631 enc =kTextEncodingDOSRussian ;
632 break ;
633 case wxFONTENCODING_CP874 :
634 enc = kTextEncodingDOSThai;
635 break ;
636 case wxFONTENCODING_CP932 :
637 enc = kTextEncodingDOSJapanese;
638 break ;
639 case wxFONTENCODING_CP936 :
640 enc =kTextEncodingDOSChineseSimplif ;
641 break ;
642 case wxFONTENCODING_CP949 :
643 enc = kTextEncodingDOSKorean;
644 break ;
645 case wxFONTENCODING_CP950 :
646 enc = kTextEncodingDOSChineseTrad;
647 break ;
648
649 case wxFONTENCODING_CP1250 :
650 enc = kTextEncodingWindowsLatin2;
651 break ;
652 case wxFONTENCODING_CP1251 :
653 enc =kTextEncodingWindowsCyrillic ;
654 break ;
655 case wxFONTENCODING_CP1252 :
656 enc =kTextEncodingWindowsLatin1 ;
657 break ;
658 case wxFONTENCODING_CP1253 :
659 enc = kTextEncodingWindowsGreek;
660 break ;
661 case wxFONTENCODING_CP1254 :
662 enc = kTextEncodingWindowsLatin5;
663 break ;
664 case wxFONTENCODING_CP1255 :
665 enc =kTextEncodingWindowsHebrew ;
666 break ;
667 case wxFONTENCODING_CP1256 :
668 enc =kTextEncodingWindowsArabic ;
669 break ;
670 case wxFONTENCODING_CP1257 :
671 enc = kTextEncodingWindowsBalticRim;
672 break ;
673
674 case wxFONTENCODING_UTF7 :
675 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
676 break ;
677 case wxFONTENCODING_UTF8 :
678 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
679 break ;
680 case wxFONTENCODING_EUC_JP :
681 enc = kTextEncodingEUC_JP;
682 break ;
683 case wxFONTENCODING_UTF16BE :
684 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
685 break ;
686 case wxFONTENCODING_UTF16LE :
687 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
688 break ;
689 case wxFONTENCODING_UTF32BE :
690 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
691 break ;
692 case wxFONTENCODING_UTF32LE :
693 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
694 break ;
695
696 case wxFONTENCODING_MACROMAN :
697 enc = kTextEncodingMacRoman ;
698 break ;
699 case wxFONTENCODING_MACJAPANESE :
700 enc = kTextEncodingMacJapanese ;
701 break ;
702 case wxFONTENCODING_MACCHINESETRAD :
703 enc = kTextEncodingMacChineseTrad ;
704 break ;
705 case wxFONTENCODING_MACKOREAN :
706 enc = kTextEncodingMacKorean ;
707 break ;
708 case wxFONTENCODING_MACARABIC :
709 enc = kTextEncodingMacArabic ;
710 break ;
711 case wxFONTENCODING_MACHEBREW :
712 enc = kTextEncodingMacHebrew ;
713 break ;
714 case wxFONTENCODING_MACGREEK :
715 enc = kTextEncodingMacGreek ;
716 break ;
717 case wxFONTENCODING_MACCYRILLIC :
718 enc = kTextEncodingMacCyrillic ;
719 break ;
720 case wxFONTENCODING_MACDEVANAGARI :
721 enc = kTextEncodingMacDevanagari ;
722 break ;
723 case wxFONTENCODING_MACGURMUKHI :
724 enc = kTextEncodingMacGurmukhi ;
725 break ;
726 case wxFONTENCODING_MACGUJARATI :
727 enc = kTextEncodingMacGujarati ;
728 break ;
729 case wxFONTENCODING_MACORIYA :
730 enc = kTextEncodingMacOriya ;
731 break ;
732 case wxFONTENCODING_MACBENGALI :
733 enc = kTextEncodingMacBengali ;
734 break ;
735 case wxFONTENCODING_MACTAMIL :
736 enc = kTextEncodingMacTamil ;
737 break ;
738 case wxFONTENCODING_MACTELUGU :
739 enc = kTextEncodingMacTelugu ;
740 break ;
741 case wxFONTENCODING_MACKANNADA :
742 enc = kTextEncodingMacKannada ;
743 break ;
744 case wxFONTENCODING_MACMALAJALAM :
745 enc = kTextEncodingMacMalayalam ;
746 break ;
747 case wxFONTENCODING_MACSINHALESE :
748 enc = kTextEncodingMacSinhalese ;
749 break ;
750 case wxFONTENCODING_MACBURMESE :
751 enc = kTextEncodingMacBurmese ;
752 break ;
753 case wxFONTENCODING_MACKHMER :
754 enc = kTextEncodingMacKhmer ;
755 break ;
756 case wxFONTENCODING_MACTHAI :
757 enc = kTextEncodingMacThai ;
758 break ;
759 case wxFONTENCODING_MACLAOTIAN :
760 enc = kTextEncodingMacLaotian ;
761 break ;
762 case wxFONTENCODING_MACGEORGIAN :
763 enc = kTextEncodingMacGeorgian ;
764 break ;
765 case wxFONTENCODING_MACARMENIAN :
766 enc = kTextEncodingMacArmenian ;
767 break ;
768 case wxFONTENCODING_MACCHINESESIMP :
769 enc = kTextEncodingMacChineseSimp ;
770 break ;
771 case wxFONTENCODING_MACTIBETAN :
772 enc = kTextEncodingMacTibetan ;
773 break ;
774 case wxFONTENCODING_MACMONGOLIAN :
775 enc = kTextEncodingMacMongolian ;
776 break ;
777 case wxFONTENCODING_MACETHIOPIC :
778 enc = kTextEncodingMacEthiopic ;
779 break ;
780 case wxFONTENCODING_MACCENTRALEUR :
781 enc = kTextEncodingMacCentralEurRoman ;
782 break ;
783 case wxFONTENCODING_MACVIATNAMESE :
784 enc = kTextEncodingMacVietnamese ;
785 break ;
786 case wxFONTENCODING_MACARABICEXT :
787 enc = kTextEncodingMacExtArabic ;
788 break ;
789 case wxFONTENCODING_MACSYMBOL :
790 enc = kTextEncodingMacSymbol ;
791 break ;
792 case wxFONTENCODING_MACDINGBATS :
793 enc = kTextEncodingMacDingbats ;
794 break ;
795 case wxFONTENCODING_MACTURKISH :
796 enc = kTextEncodingMacTurkish ;
797 break ;
798 case wxFONTENCODING_MACCROATIAN :
799 enc = kTextEncodingMacCroatian ;
800 break ;
801 case wxFONTENCODING_MACICELANDIC :
802 enc = kTextEncodingMacIcelandic ;
803 break ;
804 case wxFONTENCODING_MACROMANIAN :
805 enc = kTextEncodingMacRomanian ;
806 break ;
807 case wxFONTENCODING_MACCELTIC :
808 enc = kTextEncodingMacCeltic ;
809 break ;
810 case wxFONTENCODING_MACGAELIC :
811 enc = kTextEncodingMacGaelic ;
812 break ;
813 case wxFONTENCODING_MACKEYBOARD :
814 enc = kTextEncodingMacKeyboardGlyphs ;
815 break ;
816 default :
817 // to make gcc happy
818 break ;
819 } ;
820 return enc ;
821 }
822
823 wxFontEncoding wxMacGetFontEncFromSystemEnc(wxUint32 encoding)
824 {
825 wxFontEncoding enc = wxFONTENCODING_DEFAULT ;
826
827 switch( encoding)
828 {
829 case kTextEncodingISOLatin1 :
830 enc = wxFONTENCODING_ISO8859_1 ;
831 break ;
832 case kTextEncodingISOLatin2 :
833 enc = wxFONTENCODING_ISO8859_2;
834 break ;
835 case kTextEncodingISOLatin3 :
836 enc = wxFONTENCODING_ISO8859_3 ;
837 break ;
838 case kTextEncodingISOLatin4 :
839 enc = wxFONTENCODING_ISO8859_4;
840 break ;
841 case kTextEncodingISOLatinCyrillic :
842 enc = wxFONTENCODING_ISO8859_5;
843 break ;
844 case kTextEncodingISOLatinArabic :
845 enc = wxFONTENCODING_ISO8859_6;
846 break ;
847 case kTextEncodingISOLatinGreek :
848 enc = wxFONTENCODING_ISO8859_7;
849 break ;
850 case kTextEncodingISOLatinHebrew :
851 enc = wxFONTENCODING_ISO8859_8;
852 break ;
853 case kTextEncodingISOLatin5 :
854 enc = wxFONTENCODING_ISO8859_9;
855 break ;
856 case kTextEncodingISOLatin6 :
857 enc = wxFONTENCODING_ISO8859_10;
858 break ;
859 case kTextEncodingISOLatin7 :
860 enc = wxFONTENCODING_ISO8859_13;
861 break ;
862 case kTextEncodingISOLatin8 :
863 enc = wxFONTENCODING_ISO8859_14;
864 break ;
865 case kTextEncodingISOLatin9 :
866 enc =wxFONTENCODING_ISO8859_15 ;
867 break ;
868
869 case kTextEncodingKOI8_R :
870 enc = wxFONTENCODING_KOI8;
871 break ;
872 /*
873 case :
874 enc = wxFONTENCODING_BULGARIAN;
875 break ;
876 */
877 case kTextEncodingDOSLatinUS :
878 enc = wxFONTENCODING_CP437;
879 break ;
880 case kTextEncodingDOSLatin1 :
881 enc = wxFONTENCODING_CP850;
882 break ;
883 case kTextEncodingDOSLatin2 :
884 enc =wxFONTENCODING_CP852 ;
885 break ;
886 case kTextEncodingDOSCyrillic :
887 enc = wxFONTENCODING_CP855;
888 break ;
889 case kTextEncodingDOSRussian :
890 enc = wxFONTENCODING_CP866;
891 break ;
892 case kTextEncodingDOSThai :
893 enc =wxFONTENCODING_CP874 ;
894 break ;
895 case kTextEncodingDOSJapanese :
896 enc = wxFONTENCODING_CP932;
897 break ;
898 case kTextEncodingDOSChineseSimplif :
899 enc = wxFONTENCODING_CP936;
900 break ;
901 case kTextEncodingDOSKorean :
902 enc = wxFONTENCODING_CP949;
903 break ;
904 case kTextEncodingDOSChineseTrad :
905 enc = wxFONTENCODING_CP950;
906 break ;
907
908 case kTextEncodingWindowsLatin2 :
909 enc = wxFONTENCODING_CP1250;
910 break ;
911 case kTextEncodingWindowsCyrillic :
912 enc = wxFONTENCODING_CP1251;
913 break ;
914 case kTextEncodingWindowsLatin1 :
915 enc = wxFONTENCODING_CP1252;
916 break ;
917 case kTextEncodingWindowsGreek :
918 enc = wxFONTENCODING_CP1253;
919 break ;
920 case kTextEncodingWindowsLatin5 :
921 enc = wxFONTENCODING_CP1254;
922 break ;
923 case kTextEncodingWindowsHebrew :
924 enc = wxFONTENCODING_CP1255;
925 break ;
926 case kTextEncodingWindowsArabic :
927 enc = wxFONTENCODING_CP1256;
928 break ;
929 case kTextEncodingWindowsBalticRim :
930 enc =wxFONTENCODING_CP1257 ;
931 break ;
932 case kTextEncodingEUC_JP :
933 enc = wxFONTENCODING_EUC_JP;
934 break ;
935 /*
936 case wxFONTENCODING_UTF7 :
937 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
938 break ;
939 case wxFONTENCODING_UTF8 :
940 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
941 break ;
942 case wxFONTENCODING_UTF16BE :
943 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
944 break ;
945 case wxFONTENCODING_UTF16LE :
946 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
947 break ;
948 case wxFONTENCODING_UTF32BE :
949 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
950 break ;
951 case wxFONTENCODING_UTF32LE :
952 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
953 break ;
954 */
955 case kTextEncodingMacRoman :
956 enc = wxFONTENCODING_MACROMAN ;
957 break ;
958 case kTextEncodingMacJapanese :
959 enc = wxFONTENCODING_MACJAPANESE ;
960 break ;
961 case kTextEncodingMacChineseTrad :
962 enc = wxFONTENCODING_MACCHINESETRAD ;
963 break ;
964 case kTextEncodingMacKorean :
965 enc = wxFONTENCODING_MACKOREAN ;
966 break ;
967 case kTextEncodingMacArabic :
968 enc =wxFONTENCODING_MACARABIC ;
969 break ;
970 case kTextEncodingMacHebrew :
971 enc = wxFONTENCODING_MACHEBREW ;
972 break ;
973 case kTextEncodingMacGreek :
974 enc = wxFONTENCODING_MACGREEK ;
975 break ;
976 case kTextEncodingMacCyrillic :
977 enc = wxFONTENCODING_MACCYRILLIC ;
978 break ;
979 case kTextEncodingMacDevanagari :
980 enc = wxFONTENCODING_MACDEVANAGARI ;
981 break ;
982 case kTextEncodingMacGurmukhi :
983 enc = wxFONTENCODING_MACGURMUKHI ;
984 break ;
985 case kTextEncodingMacGujarati :
986 enc = wxFONTENCODING_MACGUJARATI ;
987 break ;
988 case kTextEncodingMacOriya :
989 enc =wxFONTENCODING_MACORIYA ;
990 break ;
991 case kTextEncodingMacBengali :
992 enc =wxFONTENCODING_MACBENGALI ;
993 break ;
994 case kTextEncodingMacTamil :
995 enc = wxFONTENCODING_MACTAMIL ;
996 break ;
997 case kTextEncodingMacTelugu :
998 enc = wxFONTENCODING_MACTELUGU ;
999 break ;
1000 case kTextEncodingMacKannada :
1001 enc = wxFONTENCODING_MACKANNADA ;
1002 break ;
1003 case kTextEncodingMacMalayalam :
1004 enc = wxFONTENCODING_MACMALAJALAM ;
1005 break ;
1006 case kTextEncodingMacSinhalese :
1007 enc = wxFONTENCODING_MACSINHALESE ;
1008 break ;
1009 case kTextEncodingMacBurmese :
1010 enc = wxFONTENCODING_MACBURMESE ;
1011 break ;
1012 case kTextEncodingMacKhmer :
1013 enc = wxFONTENCODING_MACKHMER ;
1014 break ;
1015 case kTextEncodingMacThai :
1016 enc = wxFONTENCODING_MACTHAI ;
1017 break ;
1018 case kTextEncodingMacLaotian :
1019 enc = wxFONTENCODING_MACLAOTIAN ;
1020 break ;
1021 case kTextEncodingMacGeorgian :
1022 enc = wxFONTENCODING_MACGEORGIAN ;
1023 break ;
1024 case kTextEncodingMacArmenian :
1025 enc = wxFONTENCODING_MACARMENIAN ;
1026 break ;
1027 case kTextEncodingMacChineseSimp :
1028 enc = wxFONTENCODING_MACCHINESESIMP ;
1029 break ;
1030 case kTextEncodingMacTibetan :
1031 enc = wxFONTENCODING_MACTIBETAN ;
1032 break ;
1033 case kTextEncodingMacMongolian :
1034 enc = wxFONTENCODING_MACMONGOLIAN ;
1035 break ;
1036 case kTextEncodingMacEthiopic :
1037 enc = wxFONTENCODING_MACETHIOPIC ;
1038 break ;
1039 case kTextEncodingMacCentralEurRoman:
1040 enc = wxFONTENCODING_MACCENTRALEUR ;
1041 break ;
1042 case kTextEncodingMacVietnamese:
1043 enc = wxFONTENCODING_MACVIATNAMESE ;
1044 break ;
1045 case kTextEncodingMacExtArabic :
1046 enc = wxFONTENCODING_MACARABICEXT ;
1047 break ;
1048 case kTextEncodingMacSymbol :
1049 enc = wxFONTENCODING_MACSYMBOL ;
1050 break ;
1051 case kTextEncodingMacDingbats :
1052 enc = wxFONTENCODING_MACDINGBATS ;
1053 break ;
1054 case kTextEncodingMacTurkish :
1055 enc = wxFONTENCODING_MACTURKISH ;
1056 break ;
1057 case kTextEncodingMacCroatian :
1058 enc = wxFONTENCODING_MACCROATIAN ;
1059 break ;
1060 case kTextEncodingMacIcelandic :
1061 enc = wxFONTENCODING_MACICELANDIC ;
1062 break ;
1063 case kTextEncodingMacRomanian :
1064 enc = wxFONTENCODING_MACROMANIAN ;
1065 break ;
1066 case kTextEncodingMacCeltic :
1067 enc = wxFONTENCODING_MACCELTIC ;
1068 break ;
1069 case kTextEncodingMacGaelic :
1070 enc = wxFONTENCODING_MACGAELIC ;
1071 break ;
1072 case kTextEncodingMacKeyboardGlyphs :
1073 enc = wxFONTENCODING_MACKEYBOARD ;
1074 break ;
1075 } ;
1076 return enc ;
1077 }
1078
1079 #endif // wxUSE_BASE
1080
1081 #if wxUSE_GUI
1082
1083
1084 //
1085 // CFStringRefs (Carbon only)
1086 //
1087
1088 #if TARGET_CARBON
1089
1090 // converts this string into a carbon foundation string with optional pc 2 mac encoding
1091 void wxMacCFStringHolder::Assign( const wxString &st , wxFontEncoding encoding )
1092 {
1093 Release() ;
1094
1095 wxString str = st ;
1096 wxMacConvertNewlines13To10( &str ) ;
1097 #if wxUSE_UNICODE
1098 #if SIZEOF_WCHAR_T == 2
1099 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault,
1100 (UniChar*)str.wc_str() , str.Len() );
1101 #else
1102 wxMBConvUTF16BE converter ;
1103 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1104 UniChar *unibuf = new UniChar[ unicharlen / sizeof(UniChar) + 1 ] ;
1105 converter.WC2MB( (char*)unibuf , str.wc_str() , unicharlen ) ;
1106 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault ,
1107 unibuf , unicharlen / sizeof(UniChar) ) ;
1108 delete[] unibuf ;
1109 #endif
1110 #else // not wxUSE_UNICODE
1111 m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
1112 wxMacGetSystemEncFromFontEnc( encoding ) ) ;
1113 #endif
1114 m_release = true ;
1115 }
1116
1117 wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding)
1118 {
1119 Size cflen = CFStringGetLength( m_cfs ) ;
1120 size_t noChars ;
1121 wxChar* buf = NULL ;
1122
1123 #if wxUSE_UNICODE
1124 #if SIZEOF_WCHAR_T == 2
1125 buf = new wxChar[ cflen + 1 ] ;
1126 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) buf ) ;
1127 noChars = cflen ;
1128 #else
1129 UniChar* unibuf = new UniChar[ cflen + 1 ] ;
1130 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) unibuf ) ;
1131 unibuf[cflen] = 0 ;
1132 wxMBConvUTF16BE converter ;
1133 noChars = converter.MB2WC( NULL , (const char*)unibuf , 0 ) ;
1134 buf = new wxChar[ noChars + 1 ] ;
1135 converter.MB2WC( buf , (const char*)unibuf , noChars ) ;
1136 delete[] unibuf ;
1137 #endif
1138 #else
1139 CFIndex cStrLen ;
1140 CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) ,
1141 '?' , false , NULL , 0 , &cStrLen ) ;
1142 buf = new wxChar[ cStrLen + 1 ] ;
1143 CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) ,
1144 '?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ;
1145 noChars = cStrLen ;
1146 #endif
1147
1148 buf[noChars] = 0 ;
1149 wxMacConvertNewlines10To13( buf ) ;
1150 wxString result(buf) ;
1151 delete[] buf ;
1152 return result ;
1153 }
1154
1155 #endif //TARGET_CARBON
1156
1157 void wxMacConvertNewlines13To10( char * data )
1158 {
1159 char * buf = data ;
1160 while( (buf=strchr(buf,0x0d)) != NULL )
1161 {
1162 *buf = 0x0a ;
1163 buf++ ;
1164 }
1165 }
1166
1167 void wxMacConvertNewlines10To13( char * data )
1168 {
1169 char * buf = data ;
1170 while( (buf=strchr(buf,0x0a)) != NULL )
1171 {
1172 *buf = 0x0d ;
1173 buf++ ;
1174 }
1175 }
1176
1177 void wxMacConvertNewlines13To10( wxString * data )
1178 {
1179 size_t len = data->Length() ;
1180
1181 if ( len == 0 || wxStrchr(data->c_str(),0x0d)==NULL)
1182 return ;
1183
1184 wxString temp(*data) ;
1185 wxStringBuffer buf(*data,len ) ;
1186 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
1187
1188 wxMacConvertNewlines13To10( buf ) ;
1189 }
1190
1191 void wxMacConvertNewlines10To13( wxString * data )
1192 {
1193 size_t len = data->Length() ;
1194
1195 if ( data->Length() == 0 || wxStrchr(data->c_str(),0x0a)==NULL)
1196 return ;
1197
1198 wxString temp(*data) ;
1199 wxStringBuffer buf(*data,len ) ;
1200 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
1201 wxMacConvertNewlines10To13( buf ) ;
1202 }
1203
1204
1205 #if wxUSE_UNICODE
1206 void wxMacConvertNewlines13To10( wxChar * data )
1207 {
1208 wxChar * buf = data ;
1209 while( (buf=wxStrchr(buf,0x0d)) != NULL )
1210 {
1211 *buf = 0x0a ;
1212 buf++ ;
1213 }
1214 }
1215
1216 void wxMacConvertNewlines10To13( wxChar * data )
1217 {
1218 wxChar * buf = data ;
1219 while( (buf=wxStrchr(buf,0x0a)) != NULL )
1220 {
1221 *buf = 0x0d ;
1222 buf++ ;
1223 }
1224 }
1225 #endif
1226
1227 // ----------------------------------------------------------------------------
1228 // Carbon Event Support
1229 // ----------------------------------------------------------------------------
1230
1231
1232 OSStatus wxMacCarbonEvent::GetParameter(EventParamName inName, EventParamType inDesiredType, UInt32 inBufferSize, void * outData)
1233 {
1234 return ::GetEventParameter( m_eventRef , inName , inDesiredType , NULL , inBufferSize , NULL , outData ) ;
1235 }
1236
1237 OSStatus wxMacCarbonEvent::SetParameter(EventParamName inName, EventParamType inType, UInt32 inBufferSize, void * inData)
1238 {
1239 return ::SetEventParameter( m_eventRef , inName , inType , inBufferSize , inData ) ;
1240 }
1241
1242 // ----------------------------------------------------------------------------
1243 // debugging support
1244 // ----------------------------------------------------------------------------
1245
1246 #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
1247
1248 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
1249
1250 #ifndef __MetroNubUtils__
1251 #include "MetroNubUtils.h"
1252 #endif
1253
1254 #ifndef __GESTALT__
1255 #include <Gestalt.h>
1256 #endif
1257
1258 #if TARGET_API_MAC_CARBON
1259
1260 #include <CodeFragments.h>
1261
1262 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
1263
1264 ProcPtr gCallUniversalProc_Proc = NULL;
1265
1266 #endif
1267
1268 static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
1269
1270 static long fRunOnce = false;
1271
1272 /* ---------------------------------------------------------------------------
1273 IsMetroNubInstalled
1274 --------------------------------------------------------------------------- */
1275
1276 Boolean IsMetroNubInstalled()
1277 {
1278 if (!fRunOnce)
1279 {
1280 long result, value;
1281
1282 fRunOnce = true;
1283 gMetroNubEntry = NULL;
1284
1285 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
1286 {
1287 /* look for MetroNub's Gestalt selector */
1288 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
1289 {
1290
1291 #if TARGET_API_MAC_CARBON
1292 if (gCallUniversalProc_Proc == NULL)
1293 {
1294 CFragConnectionID connectionID;
1295 Ptr mainAddress;
1296 Str255 errorString;
1297 ProcPtr symbolAddress;
1298 OSErr err;
1299 CFragSymbolClass symbolClass;
1300
1301 symbolAddress = NULL;
1302 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
1303 &connectionID, &mainAddress, errorString);
1304
1305 if (err != noErr)
1306 {
1307 gCallUniversalProc_Proc = NULL;
1308 goto end;
1309 }
1310
1311 err = FindSymbol(connectionID, "\pCallUniversalProc",
1312 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
1313
1314 if (err != noErr)
1315 {
1316 gCallUniversalProc_Proc = NULL;
1317 goto end;
1318 }
1319 }
1320 #endif
1321
1322 {
1323 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
1324
1325 /* make sure the version of the API is compatible */
1326 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
1327 kMetroNubUserAPIVersion <= block->apiHiVersion)
1328 gMetroNubEntry = block; /* success! */
1329 }
1330
1331 }
1332 }
1333 }
1334
1335 end:
1336
1337 #if TARGET_API_MAC_CARBON
1338 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
1339 #else
1340 return (gMetroNubEntry != NULL);
1341 #endif
1342 }
1343
1344 /* ---------------------------------------------------------------------------
1345 IsMWDebuggerRunning [v1 API]
1346 --------------------------------------------------------------------------- */
1347
1348 Boolean IsMWDebuggerRunning()
1349 {
1350 if (IsMetroNubInstalled())
1351 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
1352 else
1353 return false;
1354 }
1355
1356 /* ---------------------------------------------------------------------------
1357 AmIBeingMWDebugged [v1 API]
1358 --------------------------------------------------------------------------- */
1359
1360 Boolean AmIBeingMWDebugged()
1361 {
1362 if (IsMetroNubInstalled())
1363 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
1364 else
1365 return false;
1366 }
1367
1368 extern bool WXDLLEXPORT wxIsDebuggerRunning()
1369 {
1370 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
1371 }
1372
1373 #else
1374
1375 extern bool WXDLLEXPORT wxIsDebuggerRunning()
1376 {
1377 return false;
1378 }
1379
1380 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
1381
1382 #endif // wxUSE_GUI
1383