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