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