]>
Commit | Line | Data |
---|---|---|
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 | #endif | |
25 | ||
26 | #include <ctype.h> | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <stdarg.h> | |
32 | ||
33 | #ifdef __DARWIN__ | |
34 | # include "MoreFilesX.h" | |
35 | #else | |
36 | # include "MoreFiles.h" | |
37 | # include "MoreFilesExtras.h" | |
38 | #endif | |
39 | ||
40 | #ifndef __DARWIN__ | |
41 | #include <Threads.h> | |
42 | #include <Sound.h> | |
43 | #endif | |
44 | ||
45 | #include "ATSUnicode.h" | |
46 | #include "TextCommon.h" | |
47 | #include "TextEncodingConverter.h" | |
48 | ||
49 | #if defined(__WXMAC__) | |
50 | #include "wx/mac/private.h" // includes mac headers | |
51 | #endif | |
52 | ||
53 | #if defined(__MWERKS__) && wxUSE_UNICODE | |
54 | #include <wtime.h> | |
55 | #endif | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | // code used in both base and GUI compilation | |
59 | // --------------------------------------------------------------------------- | |
60 | ||
61 | // our OS version is the same in non GUI and GUI cases | |
62 | static int DoGetOSVersion(int *majorVsn, int *minorVsn) | |
63 | { | |
64 | long theSystem ; | |
65 | ||
66 | // are there x-platform conventions ? | |
67 | ||
68 | Gestalt(gestaltSystemVersion, &theSystem) ; | |
69 | if (minorVsn != NULL) { | |
70 | *minorVsn = (theSystem & 0xFF ) ; | |
71 | } | |
72 | if (majorVsn != NULL) { | |
73 | *majorVsn = (theSystem >> 8 ) ; | |
74 | } | |
75 | #ifdef __DARWIN__ | |
76 | return wxMAC_DARWIN; | |
77 | #else | |
78 | return wxMAC; | |
79 | #endif | |
80 | } | |
81 | ||
82 | #if wxUSE_BASE | |
83 | ||
84 | #ifndef __DARWIN__ | |
85 | // defined in unix/utilsunx.cpp for Mac OS X | |
86 | ||
87 | // get full hostname (with domain name if possible) | |
88 | bool wxGetFullHostName(wxChar *buf, int maxSize) | |
89 | { | |
90 | return wxGetHostName(buf, maxSize); | |
91 | } | |
92 | ||
93 | // Get hostname only (without domain name) | |
94 | bool wxGetHostName(wxChar *buf, int maxSize) | |
95 | { | |
96 | // Gets Chooser name of user by examining a System resource. | |
97 | ||
98 | const short kComputerNameID = -16413; | |
99 | ||
100 | short oldResFile = CurResFile() ; | |
101 | UseResFile(0); | |
102 | StringHandle chooserName = (StringHandle)::GetString(kComputerNameID); | |
103 | UseResFile(oldResFile); | |
104 | ||
105 | if (chooserName && *chooserName) | |
106 | { | |
107 | HLock( (Handle) chooserName ) ; | |
108 | wxString name = wxMacMakeStringFromPascal( *chooserName ) ; | |
109 | HUnlock( (Handle) chooserName ) ; | |
110 | ReleaseResource( (Handle) chooserName ) ; | |
111 | wxStrncpy( buf , name , maxSize - 1 ) ; | |
112 | } | |
113 | else | |
114 | buf[0] = 0 ; | |
115 | ||
116 | return TRUE; | |
117 | } | |
118 | ||
119 | // Get user ID e.g. jacs | |
120 | bool wxGetUserId(wxChar *buf, int maxSize) | |
121 | { | |
122 | return wxGetUserName( buf , maxSize ) ; | |
123 | } | |
124 | ||
125 | const wxChar* wxGetHomeDir(wxString *pstr) | |
126 | { | |
127 | *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ; | |
128 | return pstr->c_str() ; | |
129 | } | |
130 | ||
131 | // Get user name e.g. Stefan Csomor | |
132 | bool wxGetUserName(wxChar *buf, int maxSize) | |
133 | { | |
134 | // Gets Chooser name of user by examining a System resource. | |
135 | ||
136 | const short kChooserNameID = -16096; | |
137 | ||
138 | short oldResFile = CurResFile() ; | |
139 | UseResFile(0); | |
140 | StringHandle chooserName = (StringHandle)::GetString(kChooserNameID); | |
141 | UseResFile(oldResFile); | |
142 | ||
143 | if (chooserName && *chooserName) | |
144 | { | |
145 | HLock( (Handle) chooserName ) ; | |
146 | wxString name = wxMacMakeStringFromPascal( *chooserName ) ; | |
147 | HUnlock( (Handle) chooserName ) ; | |
148 | ReleaseResource( (Handle) chooserName ) ; | |
149 | wxStrncpy( buf , name , maxSize - 1 ) ; | |
150 | } | |
151 | else | |
152 | buf[0] = 0 ; | |
153 | ||
154 | return TRUE; | |
155 | } | |
156 | ||
157 | int wxKill(long pid, wxSignal sig , wxKillError *rc ) | |
158 | { | |
159 | // TODO | |
160 | return 0; | |
161 | } | |
162 | ||
163 | WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value) | |
164 | { | |
165 | // TODO : under classic there is no environement support, under X yes | |
166 | return false ; | |
167 | } | |
168 | ||
169 | // set the env var name to the given value, return TRUE on success | |
170 | WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value) | |
171 | { | |
172 | // TODO : under classic there is no environement support, under X yes | |
173 | return false ; | |
174 | } | |
175 | ||
176 | // | |
177 | // Execute a program in an Interactive Shell | |
178 | // | |
179 | bool wxShell(const wxString& command) | |
180 | { | |
181 | // TODO | |
182 | return FALSE; | |
183 | } | |
184 | ||
185 | // Shutdown or reboot the PC | |
186 | bool wxShutdown(wxShutdownFlags wFlags) | |
187 | { | |
188 | // TODO | |
189 | return FALSE; | |
190 | } | |
191 | ||
192 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
193 | long wxGetFreeMemory() | |
194 | { | |
195 | return FreeMem() ; | |
196 | } | |
197 | ||
198 | void wxUsleep(unsigned long milliseconds) | |
199 | { | |
200 | clock_t start = clock() ; | |
201 | do | |
202 | { | |
203 | YieldToAnyThread() ; | |
204 | } while( clock() - start < milliseconds / 1000.0 * CLOCKS_PER_SEC ) ; | |
205 | } | |
206 | ||
207 | void wxSleep(int nSecs) | |
208 | { | |
209 | wxUsleep(1000*nSecs); | |
210 | } | |
211 | ||
212 | // Consume all events until no more left | |
213 | void wxFlushEvents() | |
214 | { | |
215 | } | |
216 | ||
217 | #endif // !__DARWIN__ | |
218 | ||
219 | // Emit a beeeeeep | |
220 | void wxBell() | |
221 | { | |
222 | SysBeep(30); | |
223 | } | |
224 | ||
225 | wxToolkitInfo& wxConsoleAppTraits::GetToolkitInfo() | |
226 | { | |
227 | static wxToolkitInfo info; | |
228 | info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor); | |
229 | info.name = _T("wxBase"); | |
230 | return info; | |
231 | } | |
232 | ||
233 | #endif // wxUSE_BASE | |
234 | ||
235 | #if wxUSE_GUI | |
236 | ||
237 | wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo() | |
238 | { | |
239 | static wxToolkitInfo info; | |
240 | info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor); | |
241 | info.shortName = _T("mac"); | |
242 | info.name = _T("wxMac"); | |
243 | #ifdef __WXUNIVERSAL__ | |
244 | info.shortName << _T("univ"); | |
245 | info.name << _T("/wxUniversal"); | |
246 | #endif | |
247 | return info; | |
248 | } | |
249 | ||
250 | // Reading and writing resources (eg WIN.INI, .Xdefaults) | |
251 | #if wxUSE_RESOURCES | |
252 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) | |
253 | { | |
254 | // TODO | |
255 | return FALSE; | |
256 | } | |
257 | ||
258 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
259 | { | |
260 | wxString buf; | |
261 | buf.Printf(wxT("%.4f"), value); | |
262 | ||
263 | return wxWriteResource(section, entry, buf, file); | |
264 | } | |
265 | ||
266 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
267 | { | |
268 | wxString buf; | |
269 | buf.Printf(wxT("%ld"), value); | |
270 | ||
271 | return wxWriteResource(section, entry, buf, file); | |
272 | } | |
273 | ||
274 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
275 | { | |
276 | wxString buf; | |
277 | buf.Printf(wxT("%d"), value); | |
278 | ||
279 | return wxWriteResource(section, entry, buf, file); | |
280 | } | |
281 | ||
282 | bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file) | |
283 | { | |
284 | // TODO | |
285 | return FALSE; | |
286 | } | |
287 | ||
288 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
289 | { | |
290 | char *s = NULL; | |
291 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
292 | if (succ) | |
293 | { | |
294 | *value = (float)strtod(s, NULL); | |
295 | delete[] s; | |
296 | return TRUE; | |
297 | } | |
298 | else return FALSE; | |
299 | } | |
300 | ||
301 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
302 | { | |
303 | char *s = NULL; | |
304 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
305 | if (succ) | |
306 | { | |
307 | *value = strtol(s, NULL, 10); | |
308 | delete[] s; | |
309 | return TRUE; | |
310 | } | |
311 | else return FALSE; | |
312 | } | |
313 | ||
314 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
315 | { | |
316 | char *s = NULL; | |
317 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
318 | if (succ) | |
319 | { | |
320 | *value = (int)strtol(s, NULL, 10); | |
321 | delete[] s; | |
322 | return TRUE; | |
323 | } | |
324 | else return FALSE; | |
325 | } | |
326 | #endif // wxUSE_RESOURCES | |
327 | ||
328 | int gs_wxBusyCursorCount = 0; | |
329 | extern wxCursor gMacCurrentCursor ; | |
330 | wxCursor gMacStoredActiveCursor ; | |
331 | ||
332 | // Set the cursor to the busy cursor for all windows | |
333 | void wxBeginBusyCursor(wxCursor *cursor) | |
334 | { | |
335 | if (gs_wxBusyCursorCount++ == 0) | |
336 | { | |
337 | gMacStoredActiveCursor = gMacCurrentCursor ; | |
338 | cursor->MacInstall() ; | |
339 | } | |
340 | //else: nothing to do, already set | |
341 | } | |
342 | ||
343 | // Restore cursor to normal | |
344 | void wxEndBusyCursor() | |
345 | { | |
346 | wxCHECK_RET( gs_wxBusyCursorCount > 0, | |
347 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); | |
348 | ||
349 | if (--gs_wxBusyCursorCount == 0) | |
350 | { | |
351 | gMacStoredActiveCursor.MacInstall() ; | |
352 | gMacStoredActiveCursor = wxNullCursor ; | |
353 | } | |
354 | } | |
355 | ||
356 | // TRUE if we're between the above two calls | |
357 | bool wxIsBusy() | |
358 | { | |
359 | return (gs_wxBusyCursorCount > 0); | |
360 | } | |
361 | ||
362 | #endif // wxUSE_GUI | |
363 | ||
364 | #if wxUSE_BASE | |
365 | ||
366 | wxString wxMacFindFolder( short vol, | |
367 | OSType folderType, | |
368 | Boolean createFolder) | |
369 | { | |
370 | short vRefNum ; | |
371 | long dirID ; | |
372 | wxString strDir ; | |
373 | ||
374 | if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr) | |
375 | { | |
376 | FSSpec file ; | |
377 | if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr ) | |
378 | { | |
379 | strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ; | |
380 | } | |
381 | } | |
382 | return strDir ; | |
383 | } | |
384 | ||
385 | #endif // wxUSE_BASE | |
386 | ||
387 | #if wxUSE_GUI | |
388 | ||
389 | // Check whether this window wants to process messages, e.g. Stop button | |
390 | // in long calculations. | |
391 | bool wxCheckForInterrupt(wxWindow *wnd) | |
392 | { | |
393 | // TODO | |
394 | return FALSE; | |
395 | } | |
396 | ||
397 | void wxGetMousePosition( int* x, int* y ) | |
398 | { | |
399 | Point pt ; | |
400 | ||
401 | GetMouse( &pt ) ; | |
402 | LocalToGlobal( &pt ) ; | |
403 | *x = pt.h ; | |
404 | *y = pt.v ; | |
405 | }; | |
406 | ||
407 | // Return TRUE if we have a colour display | |
408 | bool wxColourDisplay() | |
409 | { | |
410 | return TRUE; | |
411 | } | |
412 | ||
413 | // Returns depth of screen | |
414 | int wxDisplayDepth() | |
415 | { | |
416 | Rect globRect ; | |
417 | SetRect(&globRect, -32760, -32760, 32760, 32760); | |
418 | GDHandle theMaxDevice; | |
419 | ||
420 | int theDepth = 8; | |
421 | theMaxDevice = GetMaxDevice(&globRect); | |
422 | if (theMaxDevice != nil) | |
423 | theDepth = (**(**theMaxDevice).gdPMap).pixelSize; | |
424 | ||
425 | return theDepth ; | |
426 | } | |
427 | ||
428 | // Get size of display | |
429 | void wxDisplaySize(int *width, int *height) | |
430 | { | |
431 | BitMap screenBits; | |
432 | GetQDGlobalsScreenBits( &screenBits ); | |
433 | ||
434 | if (width != NULL) { | |
435 | *width = screenBits.bounds.right - screenBits.bounds.left ; | |
436 | } | |
437 | if (height != NULL) { | |
438 | *height = screenBits.bounds.bottom - screenBits.bounds.top ; | |
439 | } | |
440 | } | |
441 | ||
442 | void wxDisplaySizeMM(int *width, int *height) | |
443 | { | |
444 | wxDisplaySize(width, height); | |
445 | // on mac 72 is fixed (at least now ;-) | |
446 | float cvPt2Mm = 25.4 / 72; | |
447 | ||
448 | if (width != NULL) { | |
449 | *width = int( *width * cvPt2Mm ); | |
450 | } | |
451 | if (height != NULL) { | |
452 | *height = int( *height * cvPt2Mm ); | |
453 | } | |
454 | } | |
455 | ||
456 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) | |
457 | { | |
458 | BitMap screenBits; | |
459 | GetQDGlobalsScreenBits( &screenBits ); | |
460 | ||
461 | if (x) *x = 0; | |
462 | if (y) *y = 0; | |
463 | ||
464 | if (width != NULL) { | |
465 | *width = screenBits.bounds.right - screenBits.bounds.left ; | |
466 | } | |
467 | if (height != NULL) { | |
468 | *height = screenBits.bounds.bottom - screenBits.bounds.top ; | |
469 | } | |
470 | ||
471 | SInt16 mheight ; | |
472 | #if TARGET_CARBON | |
473 | GetThemeMenuBarHeight( &mheight ) ; | |
474 | #else | |
475 | mheight = LMGetMBarHeight() ; | |
476 | #endif | |
477 | if (height != NULL) { | |
478 | *height -= mheight ; | |
479 | } | |
480 | if (y) | |
481 | *y = mheight ; | |
482 | } | |
483 | ||
484 | wxWindow* wxFindWindowAtPoint(const wxPoint& pt) | |
485 | { | |
486 | return wxGenericFindWindowAtPoint(pt); | |
487 | } | |
488 | ||
489 | #endif // wxUSE_GUI | |
490 | ||
491 | #if wxUSE_BASE | |
492 | ||
493 | wxString wxGetOsDescription() | |
494 | { | |
495 | #ifdef WXWIN_OS_DESCRIPTION | |
496 | // use configure generated description if available | |
497 | return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION) + wxString(wxT(")")); | |
498 | #else | |
499 | return wxT("MacOS") ; //TODO:define further | |
500 | #endif | |
501 | } | |
502 | ||
503 | #ifndef __DARWIN__ | |
504 | wxChar *wxGetUserHome (const wxString& user) | |
505 | { | |
506 | // TODO | |
507 | return NULL; | |
508 | } | |
509 | ||
510 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) | |
511 | { | |
512 | if ( path.empty() ) | |
513 | return FALSE; | |
514 | ||
515 | wxString p = path ; | |
516 | if (p[0] == ':' ) { | |
517 | p = wxGetCwd() + p ; | |
518 | } | |
519 | ||
520 | int pos = p.Find(':') ; | |
521 | if ( pos != wxNOT_FOUND ) { | |
522 | p = p.Mid(1,pos) ; | |
523 | } | |
524 | ||
525 | p = p + wxT(":") ; | |
526 | ||
527 | Str255 volumeName ; | |
528 | XVolumeParam pb ; | |
529 | ||
530 | wxMacStringToPascal( p , volumeName ) ; | |
531 | OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ; | |
532 | if ( err == noErr ) { | |
533 | if ( pTotal ) { | |
534 | (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ; | |
535 | } | |
536 | if ( pFree ) { | |
537 | (*pFree) = wxLongLong( pb.ioVFreeBytes ) ; | |
538 | } | |
539 | } | |
540 | ||
541 | return err == noErr ; | |
542 | } | |
543 | #endif // !__DARWIN__ | |
544 | ||
545 | //--------------------------------------------------------------------------- | |
546 | // wxMac Specific utility functions | |
547 | //--------------------------------------------------------------------------- | |
548 | ||
549 | #if 0 | |
550 | ||
551 | char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" | |
552 | "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" | |
553 | "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf" | |
554 | "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf" | |
555 | "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf" | |
556 | "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ; | |
557 | ||
558 | char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8" | |
559 | "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC" | |
560 | "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8" | |
561 | "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8" | |
562 | "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C" | |
563 | "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ; | |
564 | ||
565 | void wxMacConvertFromPC( const char *from , char *to , int len ) | |
566 | { | |
567 | char *c ; | |
568 | if ( from == to ) | |
569 | { | |
570 | for( int i = 0 ; i < len ; ++ i ) | |
571 | { | |
572 | c = strchr( StringANSI , *from ) ; | |
573 | if ( c != NULL ) | |
574 | { | |
575 | *to = StringMac[ c - StringANSI] ; | |
576 | } | |
577 | ++to ; | |
578 | ++from ; | |
579 | } | |
580 | } | |
581 | else | |
582 | { | |
583 | for( int i = 0 ; i < len ; ++ i ) | |
584 | { | |
585 | c = strchr( StringANSI , *from ) ; | |
586 | if ( c != NULL ) | |
587 | { | |
588 | *to = StringMac[ c - StringANSI] ; | |
589 | } | |
590 | else | |
591 | { | |
592 | *to = *from ; | |
593 | } | |
594 | ++to ; | |
595 | ++from ; | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | void wxMacConvertToPC( const char *from , char *to , int len ) | |
601 | { | |
602 | char *c ; | |
603 | if ( from == to ) | |
604 | { | |
605 | for( int i = 0 ; i < len ; ++ i ) | |
606 | { | |
607 | c = strchr( StringMac , *from ) ; | |
608 | if ( c != NULL ) | |
609 | { | |
610 | *to = StringANSI[ c - StringMac] ; | |
611 | } | |
612 | ++to ; | |
613 | ++from ; | |
614 | } | |
615 | } | |
616 | else | |
617 | { | |
618 | for( int i = 0 ; i < len ; ++ i ) | |
619 | { | |
620 | c = strchr( StringMac , *from ) ; | |
621 | if ( c != NULL ) | |
622 | { | |
623 | *to = StringANSI[ c - StringMac] ; | |
624 | } | |
625 | else | |
626 | { | |
627 | *to = *from ; | |
628 | } | |
629 | ++to ; | |
630 | ++from ; | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
635 | TECObjectRef s_TECNativeCToUnicode = NULL ; | |
636 | TECObjectRef s_TECUnicodeToNativeC = NULL ; | |
637 | ||
638 | void wxMacSetupConverters() | |
639 | { | |
640 | // if we assume errors are happening here we need low level debugging | |
641 | // since the high level assert will use the encoders that are not yet | |
642 | // setup... | |
643 | #if TARGET_CARBON | |
644 | const TextEncodingBase kEncoding = CFStringGetSystemEncoding(); | |
645 | #else | |
646 | const TextEncodingBase kEncoding = kTextEncodingMacRoman; | |
647 | #endif | |
648 | OSStatus status = noErr ; | |
649 | status = TECCreateConverter(&s_TECNativeCToUnicode, | |
650 | kEncoding, | |
651 | kTextEncodingUnicodeDefault); | |
652 | ||
653 | ||
654 | status = TECCreateConverter(&s_TECUnicodeToNativeC, | |
655 | kTextEncodingUnicodeDefault, | |
656 | kEncoding); | |
657 | } | |
658 | ||
659 | void wxMacCleanupConverters() | |
660 | { | |
661 | OSStatus status = noErr ; | |
662 | status = TECDisposeConverter(s_TECNativeCToUnicode); | |
663 | ||
664 | status = TECDisposeConverter(s_TECUnicodeToNativeC); | |
665 | } | |
666 | ||
667 | wxWCharBuffer wxMacStringToWString( const wxString &from ) | |
668 | { | |
669 | #if wxUSE_UNICODE | |
670 | wxWCharBuffer result( from.wc_str() ) ; | |
671 | #else | |
672 | OSStatus status = noErr ; | |
673 | ByteCount byteOutLen ; | |
674 | ByteCount byteInLen = from.Length() ; | |
675 | ByteCount byteBufferLen = byteInLen *2 ; | |
676 | wxWCharBuffer result( from.Length() ) ; | |
677 | status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from.c_str() , byteInLen, &byteInLen, | |
678 | (TextPtr)result.data(), byteBufferLen, &byteOutLen); | |
679 | result.data()[byteOutLen/2] = 0 ; | |
680 | #endif | |
681 | return result ; | |
682 | } | |
683 | ||
684 | ||
685 | wxString wxMacMakeStringFromCString( const char * from , int len ) | |
686 | { | |
687 | OSStatus status = noErr ; | |
688 | wxString result ; | |
689 | wxChar* buf = result.GetWriteBuf( len ) ; | |
690 | #if wxUSE_UNICODE | |
691 | ByteCount byteOutLen ; | |
692 | ByteCount byteInLen = len ; | |
693 | ByteCount byteBufferLen = len *2 ; | |
694 | ||
695 | status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from , byteInLen, &byteInLen, | |
696 | (TextPtr)buf, byteBufferLen, &byteOutLen); | |
697 | #else | |
698 | memcpy( buf , from , len ) ; | |
699 | #endif | |
700 | buf[len] = 0 ; | |
701 | result.UngetWriteBuf() ; | |
702 | return result ; | |
703 | } | |
704 | ||
705 | wxString wxMacMakeStringFromCString( const char * from ) | |
706 | { | |
707 | return wxMacMakeStringFromCString( from , strlen(from) ) ; | |
708 | } | |
709 | ||
710 | wxCharBuffer wxMacStringToCString( const wxString &from ) | |
711 | { | |
712 | #if wxUSE_UNICODE | |
713 | OSStatus status = noErr ; | |
714 | ByteCount byteOutLen ; | |
715 | ByteCount byteInLen = from.Length() * 2 ; | |
716 | ByteCount byteBufferLen = from.Length() ; | |
717 | wxCharBuffer result( from.Length() ) ; | |
718 | status = TECConvertText(s_TECUnicodeToNativeC , (ConstTextPtr)from.wc_str() , byteInLen, &byteInLen, | |
719 | (TextPtr)result.data(), byteBufferLen, &byteOutLen); | |
720 | return result ; | |
721 | #else | |
722 | return wxCharBuffer( from.c_str() ) ; | |
723 | #endif | |
724 | } | |
725 | #endif | |
726 | ||
727 | void wxMacStringToPascal( const wxString&from , StringPtr to ) | |
728 | { | |
729 | wxCharBuffer buf = from.mb_str( wxConvLocal ) ; | |
730 | int len = strlen(buf) ; | |
731 | ||
732 | if ( len > 255 ) | |
733 | len = 255 ; | |
734 | to[0] = len ; | |
735 | memcpy( (char*) &to[1] , buf , len ) ; | |
736 | } | |
737 | ||
738 | wxString wxMacMakeStringFromPascal( ConstStringPtr from ) | |
739 | { | |
740 | return wxString( (char*) &from[1] , wxConvLocal , from[0] ) ; | |
741 | } | |
742 | ||
743 | #endif // wxUSE_BASE | |
744 | ||
745 | #if wxUSE_GUI | |
746 | ||
747 | ||
748 | // | |
749 | // CFStringRefs (Carbon only) | |
750 | // | |
751 | ||
752 | #if TARGET_CARBON | |
753 | // converts this string into a carbon foundation string with optional pc 2 mac encoding | |
754 | void wxMacCFStringHolder::Assign( const wxString &str ) | |
755 | { | |
756 | #if wxUSE_UNICODE | |
757 | m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault, | |
758 | (const unsigned short*)str.wc_str(), str.Len() ); | |
759 | #else | |
760 | m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() , | |
761 | CFStringGetSystemEncoding() ) ; | |
762 | #endif | |
763 | m_release = true ; | |
764 | } | |
765 | ||
766 | wxString wxMacCFStringHolder::AsString() | |
767 | { | |
768 | wxString result ; | |
769 | Size len = CFStringGetLength( m_cfs ) ; | |
770 | wxChar* buf = result.GetWriteBuf( len ) ; | |
771 | #if wxUSE_UNICODE | |
772 | CFStringGetCharacters( m_cfs , CFRangeMake( 0 , len ) , (UniChar*) buf ) ; | |
773 | #else | |
774 | CFStringGetCString( m_cfs , buf , len+1 , CFStringGetSystemEncoding() ) ; | |
775 | #endif | |
776 | buf[len] = 0 ; | |
777 | result.UngetWriteBuf() ; | |
778 | return result ; | |
779 | } | |
780 | ||
781 | #endif //TARGET_CARBON | |
782 | ||
783 | // ---------------------------------------------------------------------------- | |
784 | // debugging support | |
785 | // ---------------------------------------------------------------------------- | |
786 | ||
787 | #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400) | |
788 | ||
789 | // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds... | |
790 | ||
791 | #ifndef __MetroNubUtils__ | |
792 | #include "MetroNubUtils.h" | |
793 | #endif | |
794 | ||
795 | #ifndef __GESTALT__ | |
796 | #include <Gestalt.h> | |
797 | #endif | |
798 | ||
799 | #if TARGET_API_MAC_CARBON | |
800 | ||
801 | #include <CodeFragments.h> | |
802 | ||
803 | extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...); | |
804 | ||
805 | ProcPtr gCallUniversalProc_Proc = NULL; | |
806 | ||
807 | #endif | |
808 | ||
809 | static MetroNubUserEntryBlock* gMetroNubEntry = NULL; | |
810 | ||
811 | static long fRunOnce = false; | |
812 | ||
813 | /* --------------------------------------------------------------------------- | |
814 | IsMetroNubInstalled | |
815 | --------------------------------------------------------------------------- */ | |
816 | ||
817 | Boolean IsMetroNubInstalled() | |
818 | { | |
819 | if (!fRunOnce) | |
820 | { | |
821 | long result, value; | |
822 | ||
823 | fRunOnce = true; | |
824 | gMetroNubEntry = NULL; | |
825 | ||
826 | if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000) | |
827 | { | |
828 | /* look for MetroNub's Gestalt selector */ | |
829 | if (Gestalt(kMetroNubUserSignature, &result) == noErr) | |
830 | { | |
831 | ||
832 | #if TARGET_API_MAC_CARBON | |
833 | if (gCallUniversalProc_Proc == NULL) | |
834 | { | |
835 | CFragConnectionID connectionID; | |
836 | Ptr mainAddress; | |
837 | Str255 errorString; | |
838 | ProcPtr symbolAddress; | |
839 | OSErr err; | |
840 | CFragSymbolClass symbolClass; | |
841 | ||
842 | symbolAddress = NULL; | |
843 | err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag, | |
844 | &connectionID, &mainAddress, errorString); | |
845 | ||
846 | if (err != noErr) | |
847 | { | |
848 | gCallUniversalProc_Proc = NULL; | |
849 | goto end; | |
850 | } | |
851 | ||
852 | err = FindSymbol(connectionID, "\pCallUniversalProc", | |
853 | (Ptr *) &gCallUniversalProc_Proc, &symbolClass); | |
854 | ||
855 | if (err != noErr) | |
856 | { | |
857 | gCallUniversalProc_Proc = NULL; | |
858 | goto end; | |
859 | } | |
860 | } | |
861 | #endif | |
862 | ||
863 | { | |
864 | MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result; | |
865 | ||
866 | /* make sure the version of the API is compatible */ | |
867 | if (block->apiLowVersion <= kMetroNubUserAPIVersion && | |
868 | kMetroNubUserAPIVersion <= block->apiHiVersion) | |
869 | gMetroNubEntry = block; /* success! */ | |
870 | } | |
871 | ||
872 | } | |
873 | } | |
874 | } | |
875 | ||
876 | end: | |
877 | ||
878 | #if TARGET_API_MAC_CARBON | |
879 | return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL); | |
880 | #else | |
881 | return (gMetroNubEntry != NULL); | |
882 | #endif | |
883 | } | |
884 | ||
885 | /* --------------------------------------------------------------------------- | |
886 | IsMWDebuggerRunning [v1 API] | |
887 | --------------------------------------------------------------------------- */ | |
888 | ||
889 | Boolean IsMWDebuggerRunning() | |
890 | { | |
891 | if (IsMetroNubInstalled()) | |
892 | return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning); | |
893 | else | |
894 | return false; | |
895 | } | |
896 | ||
897 | /* --------------------------------------------------------------------------- | |
898 | AmIBeingMWDebugged [v1 API] | |
899 | --------------------------------------------------------------------------- */ | |
900 | ||
901 | Boolean AmIBeingMWDebugged() | |
902 | { | |
903 | if (IsMetroNubInstalled()) | |
904 | return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged); | |
905 | else | |
906 | return false; | |
907 | } | |
908 | ||
909 | extern bool WXDLLEXPORT wxIsDebuggerRunning() | |
910 | { | |
911 | return IsMWDebuggerRunning() && AmIBeingMWDebugged(); | |
912 | } | |
913 | ||
914 | #else | |
915 | ||
916 | extern bool WXDLLEXPORT wxIsDebuggerRunning() | |
917 | { | |
918 | return false; | |
919 | } | |
920 | ||
921 | #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400) | |
922 | ||
923 | #endif // wxUSE_GUI | |
924 |