]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
Added accelerator handling for buttons and infrastructure to do the same for
[wxWidgets.git] / src / mac / 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 BitMap screenBits;
460 GetQDGlobalsScreenBits( &screenBits );
461
462 if (x) *x = 0;
463 if (y) *y = 0;
464
465 if (width != NULL) {
466 *width = screenBits.bounds.right - screenBits.bounds.left ;
467 }
468 if (height != NULL) {
469 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
470 }
471
472 SInt16 mheight ;
473 #if TARGET_CARBON
474 GetThemeMenuBarHeight( &mheight ) ;
475 #else
476 mheight = LMGetMBarHeight() ;
477 #endif
478 if (height != NULL) {
479 *height -= mheight ;
480 }
481 if (y)
482 *y = mheight ;
483 }
484
485 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
486 {
487 return wxGenericFindWindowAtPoint(pt);
488 }
489
490 #endif // wxUSE_GUI
491
492 #if wxUSE_BASE
493
494 wxString wxGetOsDescription()
495 {
496 #ifdef WXWIN_OS_DESCRIPTION
497 // use configure generated description if available
498 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION) + wxString(wxT(")"));
499 #else
500 return wxT("MacOS") ; //TODO:define further
501 #endif
502 }
503
504 #ifndef __DARWIN__
505 wxChar *wxGetUserHome (const wxString& user)
506 {
507 // TODO
508 return NULL;
509 }
510
511 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
512 {
513 if ( path.empty() )
514 return FALSE;
515
516 wxString p = path ;
517 if (p[0u] == ':' ) {
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
546 //---------------------------------------------------------------------------
547 // wxMac Specific utility functions
548 //---------------------------------------------------------------------------
549
550 void wxMacStringToPascal( const wxString&from , StringPtr to )
551 {
552 wxCharBuffer buf = from.mb_str( wxConvLocal ) ;
553 int len = strlen(buf) ;
554
555 if ( len > 255 )
556 len = 255 ;
557 to[0] = len ;
558 memcpy( (char*) &to[1] , buf , len ) ;
559 }
560
561 wxString wxMacMakeStringFromPascal( ConstStringPtr from )
562 {
563 return wxString( (char*) &from[1] , wxConvLocal , from[0] ) ;
564 }
565
566
567 wxUint32 wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding)
568 {
569 TextEncodingBase enc = 0 ;
570 if ( encoding == wxFONTENCODING_DEFAULT )
571 {
572 #if wxUSE_GUI
573 encoding = wxFont::GetDefaultEncoding() ;
574 #else
575 encoding = wxLocale::GetSystemEncoding() ;
576 #endif
577 }
578
579 switch( encoding)
580 {
581 case wxFONTENCODING_ISO8859_1 :
582 enc = kTextEncodingISOLatin1 ;
583 break ;
584 case wxFONTENCODING_ISO8859_2 :
585 enc = kTextEncodingISOLatin2;
586 break ;
587 case wxFONTENCODING_ISO8859_3 :
588 enc = kTextEncodingISOLatin3 ;
589 break ;
590 case wxFONTENCODING_ISO8859_4 :
591 enc = kTextEncodingISOLatin4;
592 break ;
593 case wxFONTENCODING_ISO8859_5 :
594 enc = kTextEncodingISOLatinCyrillic;
595 break ;
596 case wxFONTENCODING_ISO8859_6 :
597 enc = kTextEncodingISOLatinArabic;
598 break ;
599 case wxFONTENCODING_ISO8859_7 :
600 enc = kTextEncodingISOLatinGreek;
601 break ;
602 case wxFONTENCODING_ISO8859_8 :
603 enc = kTextEncodingISOLatinHebrew;
604 break ;
605 case wxFONTENCODING_ISO8859_9 :
606 enc = kTextEncodingISOLatin5;
607 break ;
608 case wxFONTENCODING_ISO8859_10 :
609 enc = kTextEncodingISOLatin6;
610 break ;
611 case wxFONTENCODING_ISO8859_13 :
612 enc = kTextEncodingISOLatin7;
613 break ;
614 case wxFONTENCODING_ISO8859_14 :
615 enc = kTextEncodingISOLatin8;
616 break ;
617 case wxFONTENCODING_ISO8859_15 :
618 enc = kTextEncodingISOLatin9;
619 break ;
620
621 case wxFONTENCODING_KOI8 :
622 enc = kTextEncodingKOI8_R;
623 break ;
624 case wxFONTENCODING_ALTERNATIVE : // MS-DOS CP866
625 enc = kTextEncodingDOSRussian;
626 break ;
627 /*
628 case wxFONTENCODING_BULGARIAN :
629 enc = ;
630 break ;
631 */
632 case wxFONTENCODING_CP437 :
633 enc =kTextEncodingDOSLatinUS ;
634 break ;
635 case wxFONTENCODING_CP850 :
636 enc = kTextEncodingDOSLatin1;
637 break ;
638 case wxFONTENCODING_CP852 :
639 enc = kTextEncodingDOSLatin2;
640 break ;
641 case wxFONTENCODING_CP855 :
642 enc = kTextEncodingDOSCyrillic;
643 break ;
644 case wxFONTENCODING_CP866 :
645 enc =kTextEncodingDOSRussian ;
646 break ;
647 case wxFONTENCODING_CP874 :
648 enc = kTextEncodingDOSThai;
649 break ;
650 case wxFONTENCODING_CP932 :
651 enc = kTextEncodingDOSJapanese;
652 break ;
653 case wxFONTENCODING_CP936 :
654 enc =kTextEncodingDOSChineseSimplif ;
655 break ;
656 case wxFONTENCODING_CP949 :
657 enc = kTextEncodingDOSKorean;
658 break ;
659 case wxFONTENCODING_CP950 :
660 enc = kTextEncodingDOSChineseTrad;
661 break ;
662
663 case wxFONTENCODING_CP1250 :
664 enc = kTextEncodingWindowsLatin2;
665 break ;
666 case wxFONTENCODING_CP1251 :
667 enc =kTextEncodingWindowsCyrillic ;
668 break ;
669 case wxFONTENCODING_CP1252 :
670 enc =kTextEncodingWindowsLatin1 ;
671 break ;
672 case wxFONTENCODING_CP1253 :
673 enc = kTextEncodingWindowsGreek;
674 break ;
675 case wxFONTENCODING_CP1254 :
676 enc = kTextEncodingWindowsLatin5;
677 break ;
678 case wxFONTENCODING_CP1255 :
679 enc =kTextEncodingWindowsHebrew ;
680 break ;
681 case wxFONTENCODING_CP1256 :
682 enc =kTextEncodingWindowsArabic ;
683 break ;
684 case wxFONTENCODING_CP1257 :
685 enc = kTextEncodingWindowsBalticRim;
686 break ;
687
688 case wxFONTENCODING_UTF7 :
689 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
690 break ;
691 case wxFONTENCODING_UTF8 :
692 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
693 break ;
694 case wxFONTENCODING_EUC_JP :
695 enc = kTextEncodingEUC_JP;
696 break ;
697 case wxFONTENCODING_UTF16BE :
698 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
699 break ;
700 case wxFONTENCODING_UTF16LE :
701 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
702 break ;
703 case wxFONTENCODING_UTF32BE :
704 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
705 break ;
706 case wxFONTENCODING_UTF32LE :
707 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
708 break ;
709
710 case wxFONTENCODING_MACROMAN :
711 enc = kTextEncodingMacRoman ;
712 break ;
713 case wxFONTENCODING_MACJAPANESE :
714 enc = kTextEncodingMacJapanese ;
715 break ;
716 case wxFONTENCODING_MACCHINESETRAD :
717 enc = kTextEncodingMacChineseTrad ;
718 break ;
719 case wxFONTENCODING_MACKOREAN :
720 enc = kTextEncodingMacKorean ;
721 break ;
722 case wxFONTENCODING_MACARABIC :
723 enc = kTextEncodingMacArabic ;
724 break ;
725 case wxFONTENCODING_MACHEBREW :
726 enc = kTextEncodingMacHebrew ;
727 break ;
728 case wxFONTENCODING_MACGREEK :
729 enc = kTextEncodingMacGreek ;
730 break ;
731 case wxFONTENCODING_MACCYRILLIC :
732 enc = kTextEncodingMacCyrillic ;
733 break ;
734 case wxFONTENCODING_MACDEVANAGARI :
735 enc = kTextEncodingMacDevanagari ;
736 break ;
737 case wxFONTENCODING_MACGURMUKHI :
738 enc = kTextEncodingMacGurmukhi ;
739 break ;
740 case wxFONTENCODING_MACGUJARATI :
741 enc = kTextEncodingMacGujarati ;
742 break ;
743 case wxFONTENCODING_MACORIYA :
744 enc = kTextEncodingMacOriya ;
745 break ;
746 case wxFONTENCODING_MACBENGALI :
747 enc = kTextEncodingMacBengali ;
748 break ;
749 case wxFONTENCODING_MACTAMIL :
750 enc = kTextEncodingMacTamil ;
751 break ;
752 case wxFONTENCODING_MACTELUGU :
753 enc = kTextEncodingMacTelugu ;
754 break ;
755 case wxFONTENCODING_MACKANNADA :
756 enc = kTextEncodingMacKannada ;
757 break ;
758 case wxFONTENCODING_MACMALAJALAM :
759 enc = kTextEncodingMacMalayalam ;
760 break ;
761 case wxFONTENCODING_MACSINHALESE :
762 enc = kTextEncodingMacSinhalese ;
763 break ;
764 case wxFONTENCODING_MACBURMESE :
765 enc = kTextEncodingMacBurmese ;
766 break ;
767 case wxFONTENCODING_MACKHMER :
768 enc = kTextEncodingMacKhmer ;
769 break ;
770 case wxFONTENCODING_MACTHAI :
771 enc = kTextEncodingMacThai ;
772 break ;
773 case wxFONTENCODING_MACLAOTIAN :
774 enc = kTextEncodingMacLaotian ;
775 break ;
776 case wxFONTENCODING_MACGEORGIAN :
777 enc = kTextEncodingMacGeorgian ;
778 break ;
779 case wxFONTENCODING_MACARMENIAN :
780 enc = kTextEncodingMacArmenian ;
781 break ;
782 case wxFONTENCODING_MACCHINESESIMP :
783 enc = kTextEncodingMacChineseSimp ;
784 break ;
785 case wxFONTENCODING_MACTIBETAN :
786 enc = kTextEncodingMacTibetan ;
787 break ;
788 case wxFONTENCODING_MACMONGOLIAN :
789 enc = kTextEncodingMacMongolian ;
790 break ;
791 case wxFONTENCODING_MACETHIOPIC :
792 enc = kTextEncodingMacEthiopic ;
793 break ;
794 case wxFONTENCODING_MACCENTRALEUR :
795 enc = kTextEncodingMacCentralEurRoman ;
796 break ;
797 case wxFONTENCODING_MACVIATNAMESE :
798 enc = kTextEncodingMacVietnamese ;
799 break ;
800 case wxFONTENCODING_MACARABICEXT :
801 enc = kTextEncodingMacExtArabic ;
802 break ;
803 case wxFONTENCODING_MACSYMBOL :
804 enc = kTextEncodingMacSymbol ;
805 break ;
806 case wxFONTENCODING_MACDINGBATS :
807 enc = kTextEncodingMacDingbats ;
808 break ;
809 case wxFONTENCODING_MACTURKISH :
810 enc = kTextEncodingMacTurkish ;
811 break ;
812 case wxFONTENCODING_MACCROATIAN :
813 enc = kTextEncodingMacCroatian ;
814 break ;
815 case wxFONTENCODING_MACICELANDIC :
816 enc = kTextEncodingMacIcelandic ;
817 break ;
818 case wxFONTENCODING_MACROMANIAN :
819 enc = kTextEncodingMacRomanian ;
820 break ;
821 case wxFONTENCODING_MACCELTIC :
822 enc = kTextEncodingMacCeltic ;
823 break ;
824 case wxFONTENCODING_MACGAELIC :
825 enc = kTextEncodingMacGaelic ;
826 break ;
827 case wxFONTENCODING_MACKEYBOARD :
828 enc = kTextEncodingMacKeyboardGlyphs ;
829 break ;
830 default :
831 // to make gcc happy
832 break ;
833 } ;
834 return enc ;
835 }
836
837 wxFontEncoding wxMacGetFontEncFromSystemEnc(wxUint32 encoding)
838 {
839 wxFontEncoding enc = wxFONTENCODING_DEFAULT ;
840
841 switch( encoding)
842 {
843 case kTextEncodingISOLatin1 :
844 enc = wxFONTENCODING_ISO8859_1 ;
845 break ;
846 case kTextEncodingISOLatin2 :
847 enc = wxFONTENCODING_ISO8859_2;
848 break ;
849 case kTextEncodingISOLatin3 :
850 enc = wxFONTENCODING_ISO8859_3 ;
851 break ;
852 case kTextEncodingISOLatin4 :
853 enc = wxFONTENCODING_ISO8859_4;
854 break ;
855 case kTextEncodingISOLatinCyrillic :
856 enc = wxFONTENCODING_ISO8859_5;
857 break ;
858 case kTextEncodingISOLatinArabic :
859 enc = wxFONTENCODING_ISO8859_6;
860 break ;
861 case kTextEncodingISOLatinGreek :
862 enc = wxFONTENCODING_ISO8859_7;
863 break ;
864 case kTextEncodingISOLatinHebrew :
865 enc = wxFONTENCODING_ISO8859_8;
866 break ;
867 case kTextEncodingISOLatin5 :
868 enc = wxFONTENCODING_ISO8859_9;
869 break ;
870 case kTextEncodingISOLatin6 :
871 enc = wxFONTENCODING_ISO8859_10;
872 break ;
873 case kTextEncodingISOLatin7 :
874 enc = wxFONTENCODING_ISO8859_13;
875 break ;
876 case kTextEncodingISOLatin8 :
877 enc = wxFONTENCODING_ISO8859_14;
878 break ;
879 case kTextEncodingISOLatin9 :
880 enc =wxFONTENCODING_ISO8859_15 ;
881 break ;
882
883 case kTextEncodingKOI8_R :
884 enc = wxFONTENCODING_KOI8;
885 break ;
886 /*
887 case :
888 enc = wxFONTENCODING_BULGARIAN;
889 break ;
890 */
891 case kTextEncodingDOSLatinUS :
892 enc = wxFONTENCODING_CP437;
893 break ;
894 case kTextEncodingDOSLatin1 :
895 enc = wxFONTENCODING_CP850;
896 break ;
897 case kTextEncodingDOSLatin2 :
898 enc =wxFONTENCODING_CP852 ;
899 break ;
900 case kTextEncodingDOSCyrillic :
901 enc = wxFONTENCODING_CP855;
902 break ;
903 case kTextEncodingDOSRussian :
904 enc = wxFONTENCODING_CP866;
905 break ;
906 case kTextEncodingDOSThai :
907 enc =wxFONTENCODING_CP874 ;
908 break ;
909 case kTextEncodingDOSJapanese :
910 enc = wxFONTENCODING_CP932;
911 break ;
912 case kTextEncodingDOSChineseSimplif :
913 enc = wxFONTENCODING_CP936;
914 break ;
915 case kTextEncodingDOSKorean :
916 enc = wxFONTENCODING_CP949;
917 break ;
918 case kTextEncodingDOSChineseTrad :
919 enc = wxFONTENCODING_CP950;
920 break ;
921
922 case kTextEncodingWindowsLatin2 :
923 enc = wxFONTENCODING_CP1250;
924 break ;
925 case kTextEncodingWindowsCyrillic :
926 enc = wxFONTENCODING_CP1251;
927 break ;
928 case kTextEncodingWindowsLatin1 :
929 enc = wxFONTENCODING_CP1252;
930 break ;
931 case kTextEncodingWindowsGreek :
932 enc = wxFONTENCODING_CP1253;
933 break ;
934 case kTextEncodingWindowsLatin5 :
935 enc = wxFONTENCODING_CP1254;
936 break ;
937 case kTextEncodingWindowsHebrew :
938 enc = wxFONTENCODING_CP1255;
939 break ;
940 case kTextEncodingWindowsArabic :
941 enc = wxFONTENCODING_CP1256;
942 break ;
943 case kTextEncodingWindowsBalticRim :
944 enc =wxFONTENCODING_CP1257 ;
945 break ;
946 case kTextEncodingEUC_JP :
947 enc = wxFONTENCODING_EUC_JP;
948 break ;
949 /*
950 case wxFONTENCODING_UTF7 :
951 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF7Format) ;
952 break ;
953 case wxFONTENCODING_UTF8 :
954 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicodeUTF8Format) ;
955 break ;
956 case wxFONTENCODING_UTF16BE :
957 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
958 break ;
959 case wxFONTENCODING_UTF16LE :
960 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
961 break ;
962 case wxFONTENCODING_UTF32BE :
963 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
964 break ;
965 case wxFONTENCODING_UTF32LE :
966 enc = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode32BitFormat) ;
967 break ;
968 */
969 case kTextEncodingMacRoman :
970 enc = wxFONTENCODING_MACROMAN ;
971 break ;
972 case kTextEncodingMacJapanese :
973 enc = wxFONTENCODING_MACJAPANESE ;
974 break ;
975 case kTextEncodingMacChineseTrad :
976 enc = wxFONTENCODING_MACCHINESETRAD ;
977 break ;
978 case kTextEncodingMacKorean :
979 enc = wxFONTENCODING_MACKOREAN ;
980 break ;
981 case kTextEncodingMacArabic :
982 enc =wxFONTENCODING_MACARABIC ;
983 break ;
984 case kTextEncodingMacHebrew :
985 enc = wxFONTENCODING_MACHEBREW ;
986 break ;
987 case kTextEncodingMacGreek :
988 enc = wxFONTENCODING_MACGREEK ;
989 break ;
990 case kTextEncodingMacCyrillic :
991 enc = wxFONTENCODING_MACCYRILLIC ;
992 break ;
993 case kTextEncodingMacDevanagari :
994 enc = wxFONTENCODING_MACDEVANAGARI ;
995 break ;
996 case kTextEncodingMacGurmukhi :
997 enc = wxFONTENCODING_MACGURMUKHI ;
998 break ;
999 case kTextEncodingMacGujarati :
1000 enc = wxFONTENCODING_MACGUJARATI ;
1001 break ;
1002 case kTextEncodingMacOriya :
1003 enc =wxFONTENCODING_MACORIYA ;
1004 break ;
1005 case kTextEncodingMacBengali :
1006 enc =wxFONTENCODING_MACBENGALI ;
1007 break ;
1008 case kTextEncodingMacTamil :
1009 enc = wxFONTENCODING_MACTAMIL ;
1010 break ;
1011 case kTextEncodingMacTelugu :
1012 enc = wxFONTENCODING_MACTELUGU ;
1013 break ;
1014 case kTextEncodingMacKannada :
1015 enc = wxFONTENCODING_MACKANNADA ;
1016 break ;
1017 case kTextEncodingMacMalayalam :
1018 enc = wxFONTENCODING_MACMALAJALAM ;
1019 break ;
1020 case kTextEncodingMacSinhalese :
1021 enc = wxFONTENCODING_MACSINHALESE ;
1022 break ;
1023 case kTextEncodingMacBurmese :
1024 enc = wxFONTENCODING_MACBURMESE ;
1025 break ;
1026 case kTextEncodingMacKhmer :
1027 enc = wxFONTENCODING_MACKHMER ;
1028 break ;
1029 case kTextEncodingMacThai :
1030 enc = wxFONTENCODING_MACTHAI ;
1031 break ;
1032 case kTextEncodingMacLaotian :
1033 enc = wxFONTENCODING_MACLAOTIAN ;
1034 break ;
1035 case kTextEncodingMacGeorgian :
1036 enc = wxFONTENCODING_MACGEORGIAN ;
1037 break ;
1038 case kTextEncodingMacArmenian :
1039 enc = wxFONTENCODING_MACARMENIAN ;
1040 break ;
1041 case kTextEncodingMacChineseSimp :
1042 enc = wxFONTENCODING_MACCHINESESIMP ;
1043 break ;
1044 case kTextEncodingMacTibetan :
1045 enc = wxFONTENCODING_MACTIBETAN ;
1046 break ;
1047 case kTextEncodingMacMongolian :
1048 enc = wxFONTENCODING_MACMONGOLIAN ;
1049 break ;
1050 case kTextEncodingMacEthiopic :
1051 enc = wxFONTENCODING_MACETHIOPIC ;
1052 break ;
1053 case kTextEncodingMacCentralEurRoman:
1054 enc = wxFONTENCODING_MACCENTRALEUR ;
1055 break ;
1056 case kTextEncodingMacVietnamese:
1057 enc = wxFONTENCODING_MACVIATNAMESE ;
1058 break ;
1059 case kTextEncodingMacExtArabic :
1060 enc = wxFONTENCODING_MACARABICEXT ;
1061 break ;
1062 case kTextEncodingMacSymbol :
1063 enc = wxFONTENCODING_MACSYMBOL ;
1064 break ;
1065 case kTextEncodingMacDingbats :
1066 enc = wxFONTENCODING_MACDINGBATS ;
1067 break ;
1068 case kTextEncodingMacTurkish :
1069 enc = wxFONTENCODING_MACTURKISH ;
1070 break ;
1071 case kTextEncodingMacCroatian :
1072 enc = wxFONTENCODING_MACCROATIAN ;
1073 break ;
1074 case kTextEncodingMacIcelandic :
1075 enc = wxFONTENCODING_MACICELANDIC ;
1076 break ;
1077 case kTextEncodingMacRomanian :
1078 enc = wxFONTENCODING_MACROMANIAN ;
1079 break ;
1080 case kTextEncodingMacCeltic :
1081 enc = wxFONTENCODING_MACCELTIC ;
1082 break ;
1083 case kTextEncodingMacGaelic :
1084 enc = wxFONTENCODING_MACGAELIC ;
1085 break ;
1086 case kTextEncodingMacKeyboardGlyphs :
1087 enc = wxFONTENCODING_MACKEYBOARD ;
1088 break ;
1089 } ;
1090 return enc ;
1091 }
1092
1093 #endif // wxUSE_BASE
1094
1095 #if wxUSE_GUI
1096
1097
1098 //
1099 // CFStringRefs (Carbon only)
1100 //
1101
1102 #if TARGET_CARBON
1103
1104 // converts this string into a carbon foundation string with optional pc 2 mac encoding
1105 void wxMacCFStringHolder::Assign( const wxString &st , wxFontEncoding encoding )
1106 {
1107 Release() ;
1108
1109 wxString str = st ;
1110 wxMacConvertNewlines13To10( &str ) ;
1111 #if wxUSE_UNICODE
1112 #if SIZEOF_WCHAR_T == 2
1113 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault,
1114 (UniChar*)str.wc_str() , str.Len() );
1115 #else
1116 wxMBConvUTF16BE converter ;
1117 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1118 UniChar *unibuf = new UniChar[ unicharlen / sizeof(UniChar) + 1 ] ;
1119 converter.WC2MB( (char*)unibuf , str.wc_str() , unicharlen ) ;
1120 m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault ,
1121 unibuf , unicharlen / sizeof(UniChar) ) ;
1122 delete[] unibuf ;
1123 #endif
1124 #else // not wxUSE_UNICODE
1125 m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
1126 wxMacGetSystemEncFromFontEnc( encoding ) ) ;
1127 #endif
1128 m_release = true ;
1129 }
1130
1131 wxString wxMacCFStringHolder::AsString(wxFontEncoding encoding)
1132 {
1133 Size cflen = CFStringGetLength( m_cfs ) ;
1134 size_t noChars ;
1135 wxChar* buf = NULL ;
1136
1137 #if wxUSE_UNICODE
1138 #if SIZEOF_WCHAR_T == 2
1139 buf = new wxChar[ cflen + 1 ] ;
1140 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) buf ) ;
1141 noChars = cflen ;
1142 #else
1143 UniChar* unibuf = new UniChar[ cflen + 1 ] ;
1144 CFStringGetCharacters( m_cfs , CFRangeMake( 0 , cflen ) , (UniChar*) unibuf ) ;
1145 unibuf[cflen] = 0 ;
1146 wxMBConvUTF16BE converter ;
1147 noChars = converter.MB2WC( NULL , (const char*)unibuf , 0 ) ;
1148 buf = new wxChar[ noChars + 1 ] ;
1149 converter.MB2WC( buf , (const char*)unibuf , noChars ) ;
1150 delete[] unibuf ;
1151 #endif
1152 #else
1153 CFIndex cStrLen ;
1154 CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) ,
1155 '?' , false , NULL , 0 , &cStrLen ) ;
1156 buf = new wxChar[ cStrLen + 1 ] ;
1157 CFStringGetBytes( m_cfs , CFRangeMake(0, cflen) , wxMacGetSystemEncFromFontEnc( encoding ) ,
1158 '?' , false , (unsigned char*) buf , cStrLen , &cStrLen) ;
1159 noChars = cStrLen ;
1160 #endif
1161
1162 buf[noChars] = 0 ;
1163 wxMacConvertNewlines10To13( buf ) ;
1164 wxString result(buf) ;
1165 delete[] buf ;
1166 return result ;
1167 }
1168
1169 #endif //TARGET_CARBON
1170
1171 void wxMacConvertNewlines13To10( char * data )
1172 {
1173 char * buf = data ;
1174 while( (buf=strchr(buf,0x0d)) != NULL )
1175 {
1176 *buf = 0x0a ;
1177 buf++ ;
1178 }
1179 }
1180
1181 void wxMacConvertNewlines10To13( char * data )
1182 {
1183 char * buf = data ;
1184 while( (buf=strchr(buf,0x0a)) != NULL )
1185 {
1186 *buf = 0x0d ;
1187 buf++ ;
1188 }
1189 }
1190
1191 void wxMacConvertNewlines13To10( wxString * data )
1192 {
1193 size_t len = data->Length() ;
1194
1195 if ( len == 0 || wxStrchr(data->c_str(),0x0d)==NULL)
1196 return ;
1197
1198 wxString temp(*data) ;
1199 wxStringBuffer buf(*data,len ) ;
1200 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
1201
1202 wxMacConvertNewlines13To10( buf ) ;
1203 }
1204
1205 void wxMacConvertNewlines10To13( wxString * data )
1206 {
1207 size_t len = data->Length() ;
1208
1209 if ( data->Length() == 0 || wxStrchr(data->c_str(),0x0a)==NULL)
1210 return ;
1211
1212 wxString temp(*data) ;
1213 wxStringBuffer buf(*data,len ) ;
1214 memcpy( buf , temp.c_str() , (len+1)*sizeof(wxChar) ) ;
1215 wxMacConvertNewlines10To13( buf ) ;
1216 }
1217
1218
1219 #if wxUSE_UNICODE
1220 void wxMacConvertNewlines13To10( wxChar * data )
1221 {
1222 wxChar * buf = data ;
1223 while( (buf=wxStrchr(buf,0x0d)) != NULL )
1224 {
1225 *buf = 0x0a ;
1226 buf++ ;
1227 }
1228 }
1229
1230 void wxMacConvertNewlines10To13( wxChar * data )
1231 {
1232 wxChar * buf = data ;
1233 while( (buf=wxStrchr(buf,0x0a)) != NULL )
1234 {
1235 *buf = 0x0d ;
1236 buf++ ;
1237 }
1238 }
1239 #endif
1240
1241 // ----------------------------------------------------------------------------
1242 // debugging support
1243 // ----------------------------------------------------------------------------
1244
1245 #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
1246
1247 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
1248
1249 #ifndef __MetroNubUtils__
1250 #include "MetroNubUtils.h"
1251 #endif
1252
1253 #ifndef __GESTALT__
1254 #include <Gestalt.h>
1255 #endif
1256
1257 #if TARGET_API_MAC_CARBON
1258
1259 #include <CodeFragments.h>
1260
1261 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
1262
1263 ProcPtr gCallUniversalProc_Proc = NULL;
1264
1265 #endif
1266
1267 static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
1268
1269 static long fRunOnce = false;
1270
1271 /* ---------------------------------------------------------------------------
1272 IsMetroNubInstalled
1273 --------------------------------------------------------------------------- */
1274
1275 Boolean IsMetroNubInstalled()
1276 {
1277 if (!fRunOnce)
1278 {
1279 long result, value;
1280
1281 fRunOnce = true;
1282 gMetroNubEntry = NULL;
1283
1284 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
1285 {
1286 /* look for MetroNub's Gestalt selector */
1287 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
1288 {
1289
1290 #if TARGET_API_MAC_CARBON
1291 if (gCallUniversalProc_Proc == NULL)
1292 {
1293 CFragConnectionID connectionID;
1294 Ptr mainAddress;
1295 Str255 errorString;
1296 ProcPtr symbolAddress;
1297 OSErr err;
1298 CFragSymbolClass symbolClass;
1299
1300 symbolAddress = NULL;
1301 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
1302 &connectionID, &mainAddress, errorString);
1303
1304 if (err != noErr)
1305 {
1306 gCallUniversalProc_Proc = NULL;
1307 goto end;
1308 }
1309
1310 err = FindSymbol(connectionID, "\pCallUniversalProc",
1311 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
1312
1313 if (err != noErr)
1314 {
1315 gCallUniversalProc_Proc = NULL;
1316 goto end;
1317 }
1318 }
1319 #endif
1320
1321 {
1322 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
1323
1324 /* make sure the version of the API is compatible */
1325 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
1326 kMetroNubUserAPIVersion <= block->apiHiVersion)
1327 gMetroNubEntry = block; /* success! */
1328 }
1329
1330 }
1331 }
1332 }
1333
1334 end:
1335
1336 #if TARGET_API_MAC_CARBON
1337 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
1338 #else
1339 return (gMetroNubEntry != NULL);
1340 #endif
1341 }
1342
1343 /* ---------------------------------------------------------------------------
1344 IsMWDebuggerRunning [v1 API]
1345 --------------------------------------------------------------------------- */
1346
1347 Boolean IsMWDebuggerRunning()
1348 {
1349 if (IsMetroNubInstalled())
1350 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
1351 else
1352 return false;
1353 }
1354
1355 /* ---------------------------------------------------------------------------
1356 AmIBeingMWDebugged [v1 API]
1357 --------------------------------------------------------------------------- */
1358
1359 Boolean AmIBeingMWDebugged()
1360 {
1361 if (IsMetroNubInstalled())
1362 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
1363 else
1364 return false;
1365 }
1366
1367 extern bool WXDLLEXPORT wxIsDebuggerRunning()
1368 {
1369 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
1370 }
1371
1372 #else
1373
1374 extern bool WXDLLEXPORT wxIsDebuggerRunning()
1375 {
1376 return false;
1377 }
1378
1379 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
1380
1381 #endif // wxUSE_GUI
1382