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