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