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