]> git.saurik.com Git - wxWidgets.git/blame - src/mac/utils.cpp
Added helper functions for string -> XmString conversion.
[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
f5c6eb5c 41#ifndef __DARWIN__
03e11df5
GD
42// defined in unix/utilsunx.cpp for Mac OS X
43
2f1ae414
SC
44// get full hostname (with domain name if possible)
45bool wxGetFullHostName(wxChar *buf, int maxSize)
46{
47 return wxGetHostName(buf, maxSize);
48}
49
50// Get hostname only (without domain name)
e9576ca5
SC
51bool wxGetHostName(char *buf, int maxSize)
52{
e40298d5
JS
53 // Gets Chooser name of user by examining a System resource.
54
55 const short kComputerNameID = -16413;
56
57 short oldResFile = CurResFile() ;
58 UseResFile(0);
59 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
60 UseResFile(oldResFile);
61
62 if (chooserName && *chooserName)
63 {
64 int length = (*chooserName)[0] ;
65 if ( length + 1 > maxSize )
66 {
67 length = maxSize - 1 ;
68 }
69 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
70 buf[length] = 0 ;
71 }
72 else
73 buf[0] = 0 ;
0a67a93b
SC
74
75 return TRUE;
e9576ca5
SC
76}
77
78// Get user ID e.g. jacs
79bool wxGetUserId(char *buf, int maxSize)
80{
0a67a93b 81 return wxGetUserName( buf , maxSize ) ;
e9576ca5
SC
82}
83
5b781a67
SC
84const wxChar* wxGetHomeDir(wxString *pstr)
85{
e40298d5
JS
86 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
87 return pstr->c_str() ;
5b781a67
SC
88}
89
a31a5f85 90// Get user name e.g. Stefan Csomor
e9576ca5
SC
91bool wxGetUserName(char *buf, int maxSize)
92{
e40298d5
JS
93 // Gets Chooser name of user by examining a System resource.
94
95 const short kChooserNameID = -16096;
96
97 short oldResFile = CurResFile() ;
98 UseResFile(0);
99 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
100 UseResFile(oldResFile);
101
102 if (chooserName && *chooserName)
103 {
104 int length = (*chooserName)[0] ;
105 if ( length + 1 > maxSize )
106 {
107 length = maxSize - 1 ;
108 }
109 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
110 buf[length] = 0 ;
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
f6ba47d9
VZ
146// Shutdown or reboot the PC
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
GD
161 clock_t start = clock() ;
162 do
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];
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);
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
GD
225 long theSystem ;
226
227 // are there x-platform conventions ?
e40298d5 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{
e40298d5
JS
253 char buf[50];
254 sprintf(buf, "%.4f", value);
255 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
256}
257
258bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
259{
e40298d5
JS
260 char buf[50];
261 sprintf(buf, "%ld", value);
262 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
263}
264
265bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
266{
e40298d5
JS
267 char buf[50];
268 sprintf(buf, "%d", value);
269 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
270}
271
272bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
273{
274 // TODO
275 return FALSE;
276}
277
278bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
279{
e40298d5
JS
280 char *s = NULL;
281 bool succ = wxGetResource(section, entry, (char **)&s, file);
282 if (succ)
283 {
284 *value = (float)strtod(s, NULL);
285 delete[] s;
286 return TRUE;
287 }
288 else return FALSE;
e9576ca5
SC
289}
290
291bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
292{
e40298d5
JS
293 char *s = NULL;
294 bool succ = wxGetResource(section, entry, (char **)&s, file);
295 if (succ)
296 {
297 *value = strtol(s, NULL, 10);
298 delete[] s;
299 return TRUE;
300 }
301 else return FALSE;
e9576ca5
SC
302}
303
304bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
305{
e40298d5
JS
306 char *s = NULL;
307 bool succ = wxGetResource(section, entry, (char **)&s, file);
308 if (succ)
309 {
310 *value = (int)strtol(s, NULL, 10);
311 delete[] s;
312 return TRUE;
313 }
314 else return FALSE;
e9576ca5
SC
315}
316#endif // wxUSE_RESOURCES
317
6b57b49a 318int gs_wxBusyCursorCount = 0;
e40298d5
JS
319extern wxCursor gMacCurrentCursor ;
320wxCursor gMacStoredActiveCursor ;
e9576ca5
SC
321
322// Set the cursor to the busy cursor for all windows
323void wxBeginBusyCursor(wxCursor *cursor)
324{
e40298d5
JS
325 if (gs_wxBusyCursorCount++ == 0)
326 {
327 gMacStoredActiveCursor = gMacCurrentCursor ;
328 cursor->MacInstall() ;
329 }
330 //else: nothing to do, already set
e9576ca5
SC
331}
332
333// Restore cursor to normal
334void wxEndBusyCursor()
335{
6b57b49a 336 wxCHECK_RET( gs_wxBusyCursorCount > 0,
e40298d5
JS
337 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
338
339 if (--gs_wxBusyCursorCount == 0)
340 {
341 gMacStoredActiveCursor.MacInstall() ;
342 gMacStoredActiveCursor = wxNullCursor ;
343 }
e9576ca5
SC
344}
345
346// TRUE if we're between the above two calls
347bool wxIsBusy()
348{
e40298d5 349 return (gs_wxBusyCursorCount > 0);
ec5d7799 350}
e9576ca5 351
e7e1b01e 352wxString wxMacFindFolder( short vol,
e40298d5
JS
353 OSType folderType,
354 Boolean createFolder)
2f1ae414 355{
2d4e4f80
GD
356 short vRefNum ;
357 long dirID ;
358 wxString strDir ;
359
360 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
361 {
362 FSSpec file ;
363 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
364 {
365 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
366 }
367 }
368 return strDir ;
2f1ae414
SC
369}
370
f5c6eb5c 371#ifndef __DARWIN__
e9576ca5
SC
372char *wxGetUserHome (const wxString& user)
373{
374 // TODO
375 return NULL;
376}
377
518af45b
SC
378bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
379{
380 if ( path.empty() )
381 return FALSE;
382
383 wxString p = path ;
384 if (p[0] == ':' ) {
385 p = wxGetCwd() + p ;
386 }
387
388 int pos = p.Find(':') ;
389 if ( pos != wxNOT_FOUND ) {
390 p = p.Mid(1,pos) ;
391 }
392
393 p = p + ":" ;
394
395 Str255 volumeName ;
396 XVolumeParam pb ;
397
398 wxMacStringToPascal( p , volumeName ) ;
399 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
400 if ( err == noErr ) {
401 if ( pTotal ) {
402 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
403 }
404 if ( pFree ) {
405 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
406 }
407 }
408
409 return err == noErr ;
410}
e7e1b01e 411#endif
518af45b 412
e9576ca5
SC
413// Check whether this window wants to process messages, e.g. Stop button
414// in long calculations.
415bool wxCheckForInterrupt(wxWindow *wnd)
416{
417 // TODO
418 return FALSE;
419}
420
421void wxGetMousePosition( int* x, int* y )
422{
519cb848 423 Point pt ;
ec5d7799 424
519cb848
SC
425 GetMouse( &pt ) ;
426 LocalToGlobal( &pt ) ;
427 *x = pt.h ;
428 *y = pt.v ;
e9576ca5
SC
429};
430
431// Return TRUE if we have a colour display
432bool wxColourDisplay()
433{
e9576ca5
SC
434 return TRUE;
435}
436
437// Returns depth of screen
438int wxDisplayDepth()
439{
e40298d5
JS
440 Rect globRect ;
441 SetRect(&globRect, -32760, -32760, 32760, 32760);
442 GDHandle theMaxDevice;
2f1ae414 443
e40298d5
JS
444 int theDepth = 8;
445 theMaxDevice = GetMaxDevice(&globRect);
446 if (theMaxDevice != nil)
447 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 448
e40298d5 449 return theDepth ;
e9576ca5
SC
450}
451
452// Get size of display
453void wxDisplaySize(int *width, int *height)
454{
e40298d5
JS
455 BitMap screenBits;
456 GetQDGlobalsScreenBits( &screenBits );
457
2b5f62a0
VZ
458 *width = screenBits.bounds.right - screenBits.bounds.left ;
459 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
e9576ca5
SC
460}
461
5fde6fcc
GD
462void wxDisplaySizeMM(int *width, int *height)
463{
5b028d57
SC
464 wxDisplaySize(width, height);
465 // on mac 72 is fixed (at least now ;-)
466 float cvPt2Mm = 25.4 / 72;
467 *width = int( *width * cvPt2Mm );
468 *height = int( *height * cvPt2Mm );
5fde6fcc
GD
469}
470
ec5d7799
RD
471void wxClientDisplayRect(int *x, int *y, int *width, int *height)
472{
2d4e4f80
GD
473 BitMap screenBits;
474 GetQDGlobalsScreenBits( &screenBits );
7cfebe05 475
ec5d7799
RD
476 if (x) *x = 0;
477 if (y) *y = 0;
7cfebe05
SC
478
479 *width = screenBits.bounds.right - screenBits.bounds.left ;
480 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
481
2d4e4f80
GD
482 SInt16 mheight ;
483#if TARGET_CARBON
484 GetThemeMenuBarHeight( &mheight ) ;
485#else
7cfebe05 486 mheight = LMGetMBarHeight() ;
2d4e4f80 487#endif
7cfebe05
SC
488 *height -= mheight ;
489 if ( y )
2d4e4f80 490 *y = mheight ;
ec5d7799
RD
491}
492
57591e0e
JS
493wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
494{
495 return wxGenericFindWindowAtPoint(pt);
496}
5dbb17e2
SC
497
498wxString wxGetOsDescription()
499{
6e73695c
GD
500#ifdef WXWIN_OS_DESCRIPTION
501 // use configure generated description if available
502 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
503#else
504 return "MacOS" ; //TODO:define further
505#endif
506}
507
3d963f81
SC
508//---------------------------------------------------------------------------
509// wxMac Specific utility functions
510//---------------------------------------------------------------------------
511
4c200e8d
SC
512char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
513 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
514 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
515 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
516 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
517 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
518
519char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
520 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
521 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
522 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
523 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
524 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
525
526void wxMacConvertFromPC( const char *from , char *to , int len )
527{
528 char *c ;
529 if ( from == to )
530 {
531 for( int i = 0 ; i < len ; ++ i )
532 {
533 c = strchr( StringANSI , *from ) ;
534 if ( c != NULL )
535 {
536 *to = StringMac[ c - StringANSI] ;
537 }
538 ++to ;
539 ++from ;
540 }
541 }
542 else
543 {
544 for( int i = 0 ; i < len ; ++ i )
545 {
546 c = strchr( StringANSI , *from ) ;
547 if ( c != NULL )
548 {
549 *to = StringMac[ c - StringANSI] ;
550 }
551 else
552 {
553 *to = *from ;
554 }
555 ++to ;
556 ++from ;
557 }
558 }
559}
560
561void wxMacConvertToPC( const char *from , char *to , int len )
562{
563 char *c ;
564 if ( from == to )
565 {
566 for( int i = 0 ; i < len ; ++ i )
567 {
568 c = strchr( StringMac , *from ) ;
569 if ( c != NULL )
570 {
571 *to = StringANSI[ c - StringMac] ;
572 }
573 ++to ;
574 ++from ;
575 }
576 }
577 else
578 {
579 for( int i = 0 ; i < len ; ++ i )
580 {
581 c = strchr( StringMac , *from ) ;
582 if ( c != NULL )
583 {
584 *to = StringANSI[ c - StringMac] ;
585 }
586 else
587 {
588 *to = *from ;
589 }
590 ++to ;
591 ++from ;
592 }
593 }
594}
595
596wxString wxMacMakeMacStringFromPC( const char * p )
597{
e40298d5 598 wxString result ;
4c200e8d 599 int len = strlen ( p ) ;
e40298d5
JS
600 if ( len > 0 )
601 {
602 wxChar* ptr = result.GetWriteBuf(len) ;
603 wxMacConvertFromPC( p , ptr , len ) ;
604 ptr[len] = 0 ;
605 result.UngetWriteBuf( len ) ;
606 }
4c200e8d
SC
607 return result ;
608}
609
610wxString wxMacMakePCStringFromMac( const char * p )
611{
e40298d5 612 wxString result ;
4c200e8d 613 int len = strlen ( p ) ;
e40298d5
JS
614 if ( len > 0 )
615 {
616 wxChar* ptr = result.GetWriteBuf(len) ;
617 wxMacConvertToPC( p , ptr , len ) ;
618 ptr[len] = 0 ;
619 result.UngetWriteBuf( len ) ;
620 }
4c200e8d
SC
621 return result ;
622}
623
624wxString wxMacMakeStringFromMacString( const char* from , bool mac2pcEncoding )
625{
626 if (mac2pcEncoding)
627 {
628 return wxMacMakePCStringFromMac( from ) ;
629 }
630 else
631 {
632 return wxString( from ) ;
633 }
634}
635
636//
637// Pascal Strings
638//
639
640wxString wxMacMakeStringFromPascal( ConstStringPtr from , bool mac2pcEncoding )
641{
e40298d5
JS
642 // this is safe since a pascal string can never be larger than 256 bytes
643 char s[256] ;
644 CopyPascalStringToC( from , s ) ;
4c200e8d
SC
645 if (mac2pcEncoding)
646 {
647 return wxMacMakePCStringFromMac( s ) ;
648 }
649 else
650 {
651 return wxString( s ) ;
652 }
653}
654
655void wxMacStringToPascal( const char * from , StringPtr to , bool pc2macEncoding )
656{
657 if (pc2macEncoding)
658 {
659 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
660 }
661 else
662 {
663 CopyCStringToPascal( from , to ) ;
664 }
665}
666
667//
668// CFStringRefs (Carbon only)
669//
670
3d963f81
SC
671#if TARGET_CARBON
672// converts this string into a carbon foundation string with optional pc 2 mac encoding
673CFStringRef wxMacCreateCFString( const wxString &str , bool pc2macEncoding )
674{
e40298d5
JS
675 return CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
676 pc2macEncoding ?
677 kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ;
3d963f81
SC
678}
679
680#endif //TARGET_CARBON
681