]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/utils.cpp
Add wx.ListBox.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 #include "wx/app.h"
16 #include "wx/apptrait.h"
17
18 #if wxUSE_GUI
19 #include "wx/mac/uma.h"
20 #include "wx/font.h"
21 #else
22 #include "wx/intl.h"
23 #endif
24
25 #include <ctype.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31
32 #ifdef __DARWIN__
33 # include "MoreFilesX.h"
34 #else
35 # include "MoreFiles.h"
36 # include "MoreFilesExtras.h"
37 #endif
38
39 #ifndef __DARWIN__
40 #include <Threads.h>
41 #include <Sound.h>
42 #endif
43
44 #include <ATSUnicode.h>
45 #include <TextCommon.h>
46 #include <TextEncodingConverter.h>
47
48 #include "wx/mac/private.h" // includes mac headers
49
50 #if defined(__MWERKS__) && wxUSE_UNICODE
51 #include <wtime.h>
52 #endif
53
54 // ---------------------------------------------------------------------------
55 // code used in both base and GUI compilation
56 // ---------------------------------------------------------------------------
57
58 // our OS version is the same in non GUI and GUI cases
59 static int DoGetOSVersion(int *majorVsn, int *minorVsn)
60 {
61 long theSystem ;
62
63 // are there x-platform conventions ?
64
65 Gestalt(gestaltSystemVersion, &theSystem) ;
66 if (minorVsn != NULL) {
67 *minorVsn = (theSystem & 0xFF ) ;
68 }
69 if (majorVsn != NULL) {
70 *majorVsn = (theSystem >> 8 ) ;
71 }
72 #ifdef __DARWIN__
73 return wxMAC_DARWIN;
74 #else
75 return wxMAC;
76 #endif
77 }
78
79 #if wxUSE_BASE
80
81 #ifndef __DARWIN__
82 // defined in unix/utilsunx.cpp for Mac OS X
83
84 // get full hostname (with domain name if possible)
85 bool wxGetFullHostName(wxChar *buf, int maxSize)
86 {
87 return wxGetHostName(buf, maxSize);
88 }
89
90 // Get hostname only (without domain name)
91 bool wxGetHostName(wxChar *buf, int maxSize)
92 {
93 // Gets Chooser name of user by examining a System resource.
94
95 const short kComputerNameID = -16413;
96
97 short oldResFile = CurResFile() ;
98 UseResFile(0);
99 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
100 UseResFile(oldResFile);
101
102 if (chooserName && *chooserName)
103 {
104 HLock( (Handle) chooserName ) ;
105 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
106 HUnlock( (Handle) chooserName ) ;
107 ReleaseResource( (Handle) chooserName ) ;
108 wxStrncpy( buf , name , maxSize - 1 ) ;
109 }
110 else
111 buf[0] = 0 ;
112
113 return true;
114 }
115
116 // Get user ID e.g. jacs
117 bool wxGetUserId(wxChar *buf, int maxSize)
118 {
119 return wxGetUserName( buf , maxSize ) ;
120 }
121
122 const wxChar* wxGetHomeDir(wxString *pstr)
123 {
124 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
125 return pstr->c_str() ;
126 }
127
128 // Get user name e.g. Stefan Csomor
129 bool wxGetUserName(wxChar *buf, int maxSize)
130 {
131 // Gets Chooser name of user by examining a System resource.
132
133 const short kChooserNameID = -16096;
134
135 short oldResFile = CurResFile() ;
136 UseResFile(0);
137 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
138 UseResFile(oldResFile);
139
140 if (chooserName && *chooserName)
141 {
142 HLock( (Handle) chooserName ) ;
143 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
144 HUnlock( (Handle) chooserName ) ;
145 ReleaseResource( (Handle) chooserName ) ;
146 wxStrncpy( buf , name , maxSize - 1 ) ;
147 }
148 else
149 buf[0] = 0 ;
150
151 return true;
152 }
153
154 int wxKill(long pid, wxSignal sig , wxKillError *rc, int flags)
155 {
156 // TODO
157 return 0;
158 }
159
160 WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
161 {
162 // TODO : under classic there is no environement support, under X yes
163 return false ;
164 }
165
166 // set the env var name to the given value, return true on success
167 WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
168 {
169 // TODO : under classic there is no environement support, under X yes
170 return false ;
171 }
172
173 //
174 // Execute a program in an Interactive Shell
175 //
176 bool wxShell(const wxString& command)
177 {
178 // TODO
179 return false;
180 }
181
182 // Shutdown or reboot the PC
183 bool wxShutdown(wxShutdownFlags wFlags)
184 {
185 // TODO
186 return false;
187 }
188
189 wxPowerType wxGetPowerType()
190 {
191 // TODO
192 return wxPOWER_UNKNOWN;
193 }
194
195 wxBatteryState wxGetBatteryState()
196 {
197 // TODO
198 return wxBATTERY_UNKNOWN_STATE;
199 }
200
201 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
202 wxMemorySize wxGetFreeMemory()
203 {
204 return (wxMemorySize)FreeMem() ;
205 }
206
207 void wxUsleep(unsigned long milliseconds)
208 {
209 clock_t start = clock() ;
210 do
211 {
212 YieldToAnyThread() ;
213 } while( clock() - start < milliseconds / 1000.0 * CLOCKS_PER_SEC ) ;
214 }
215
216 void wxSleep(int nSecs)
217 {
218 wxUsleep(1000*nSecs);
219 }
220
221 // Consume all events until no more left
222 void wxFlushEvents()
223 {
224 }
225
226 #endif // !__DARWIN__
227
228 // Emit a beeeeeep
229 void wxBell()
230 {
231 SysBeep(30);
232 }
233
234 wxToolkitInfo& wxConsoleAppTraits::GetToolkitInfo()
235 {
236 static wxToolkitInfo info;
237 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
238 info.name = _T("wxBase");
239 return info;
240 }
241
242 #endif // wxUSE_BASE
243
244 #if wxUSE_GUI
245
246 wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
247 {
248 static wxToolkitInfo info;
249 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
250 info.shortName = _T("mac");
251 info.name = _T("wxMac");
252 #ifdef __WXUNIVERSAL__
253 info.shortName << _T("univ");
254 info.name << _T("/wxUniversal");
255 #endif
256 return info;
257 }
258
259 // Reading and writing resources (eg WIN.INI, .Xdefaults)
260 #if wxUSE_RESOURCES
261 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
262 {
263 // TODO
264 return false;
265 }
266
267 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
268 {
269 wxString buf;
270 buf.Printf(wxT("%.4f"), value);
271
272 return wxWriteResource(section, entry, buf, file);
273 }
274
275 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
276 {
277 wxString buf;
278 buf.Printf(wxT("%ld"), value);
279
280 return wxWriteResource(section, entry, buf, file);
281 }
282
283 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
284 {
285 wxString buf;
286 buf.Printf(wxT("%d"), value);
287
288 return wxWriteResource(section, entry, buf, file);
289 }
290
291 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
292 {
293 // TODO
294 return false;
295 }
296
297 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
298 {
299 char *s = NULL;
300 bool succ = wxGetResource(section, entry, (char **)&s, file);
301 if (succ)
302 {
303 *value = (float)strtod(s, NULL);
304 delete[] s;
305 return true;
306 }
307 else return false;
308 }
309
310 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
311 {
312 char *s = NULL;
313 bool succ = wxGetResource(section, entry, (char **)&s, file);
314 if (succ)
315 {
316 *value = strtol(s, NULL, 10);
317 delete[] s;
318 return true;
319 }
320 else return false;
321 }
322
323 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
324 {
325 char *s = NULL;
326 bool succ = wxGetResource(section, entry, (char **)&s, file);
327 if (succ)
328 {
329 *value = (int)strtol(s, NULL, 10);
330 delete[] s;
331 return true;
332 }
333 else return false;
334 }
335 #endif // wxUSE_RESOURCES
336
337 int gs_wxBusyCursorCount = 0;
338 extern wxCursor gMacCurrentCursor ;
339 wxCursor gMacStoredActiveCursor ;
340
341 // Set the cursor to the busy cursor for all windows
342 void wxBeginBusyCursor(wxCursor *cursor)
343 {
344 if (gs_wxBusyCursorCount++ == 0)
345 {
346 gMacStoredActiveCursor = gMacCurrentCursor ;
347 cursor->MacInstall() ;
348 }
349 //else: nothing to do, already set
350 }
351
352 // Restore cursor to normal
353 void wxEndBusyCursor()
354 {
355 wxCHECK_RET( gs_wxBusyCursorCount > 0,
356 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
357
358 if (--gs_wxBusyCursorCount == 0)
359 {
360 gMacStoredActiveCursor.MacInstall() ;
361 gMacStoredActiveCursor = wxNullCursor ;
362 }
363 }
364
365 // true if we're between the above two calls
366 bool wxIsBusy()
367 {
368 return (gs_wxBusyCursorCount > 0);
369 }
370
371 #endif // wxUSE_GUI
372
373 #if wxUSE_BASE
374
375 wxString wxMacFindFolder( short vol,
376 OSType folderType,
377 Boolean createFolder)
378 {
379 short vRefNum ;
380 long dirID ;
381 wxString strDir ;
382
383 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
384 {
385 FSSpec file ;
386 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
387 {
388 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
389 }
390 }
391 return strDir ;
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->Length() == 0 || 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