]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
mini-patch for better layout under OSX
[wxWidgets.git] / src / mac / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
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 #ifndef __DARWIN__
42 // defined in unix/utilsunx.cpp for Mac OS X
43
44 // get full hostname (with domain name if possible)
45 bool wxGetFullHostName(wxChar *buf, int maxSize)
46 {
47 return wxGetHostName(buf, maxSize);
48 }
49
50 // Get hostname only (without domain name)
51 bool wxGetHostName(char *buf, int maxSize)
52 {
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 ;
74
75 return TRUE;
76 }
77
78 // Get user ID e.g. jacs
79 bool wxGetUserId(char *buf, int maxSize)
80 {
81 return wxGetUserName( buf , maxSize ) ;
82 }
83
84 const wxChar* wxGetHomeDir(wxString *pstr)
85 {
86 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
87 return pstr->c_str() ;
88 }
89
90 // Get user name e.g. AUTHOR
91 bool wxGetUserName(char *buf, int maxSize)
92 {
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 ;
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 / 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 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
203 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
204 wxExit();
205 }
206
207 // Fatal error: pop up message box and abort
208 void wxFatalError(const wxString& msg, const wxString& title)
209 {
210 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
211 wxMessageBox(wxBuffer);
212 wxExit();
213 }
214
215 #endif // WXWIN_COMPATIBILITY_2_2
216
217 #endif // !__DARWIN__
218
219 // Emit a beeeeeep
220 void wxBell()
221 {
222 SysBeep(30);
223 }
224
225 int wxGetOsVersion(int *majorVsn, int *minorVsn)
226 {
227 long theSystem ;
228
229 // are there x-platform conventions ?
230
231 Gestalt(gestaltSystemVersion, &theSystem) ;
232 if (minorVsn != NULL) {
233 *minorVsn = (theSystem & 0xFF ) ;
234 }
235 if (majorVsn != NULL) {
236 *majorVsn = (theSystem >> 8 ) ;
237 }
238 #ifdef __DARWIN__
239 return wxMAC_DARWIN;
240 #else
241 return wxMAC;
242 #endif
243 }
244
245 // Reading and writing resources (eg WIN.INI, .Xdefaults)
246 #if wxUSE_RESOURCES
247 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
248 {
249 // TODO
250 return FALSE;
251 }
252
253 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
254 {
255 char buf[50];
256 sprintf(buf, "%.4f", value);
257 return wxWriteResource(section, entry, buf, file);
258 }
259
260 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
261 {
262 char buf[50];
263 sprintf(buf, "%ld", value);
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 char buf[50];
270 sprintf(buf, "%d", value);
271 return wxWriteResource(section, entry, buf, file);
272 }
273
274 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
275 {
276 // TODO
277 return FALSE;
278 }
279
280 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
281 {
282 char *s = NULL;
283 bool succ = wxGetResource(section, entry, (char **)&s, file);
284 if (succ)
285 {
286 *value = (float)strtod(s, NULL);
287 delete[] s;
288 return TRUE;
289 }
290 else return FALSE;
291 }
292
293 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
294 {
295 char *s = NULL;
296 bool succ = wxGetResource(section, entry, (char **)&s, file);
297 if (succ)
298 {
299 *value = strtol(s, NULL, 10);
300 delete[] s;
301 return TRUE;
302 }
303 else return FALSE;
304 }
305
306 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
307 {
308 char *s = NULL;
309 bool succ = wxGetResource(section, entry, (char **)&s, file);
310 if (succ)
311 {
312 *value = (int)strtol(s, NULL, 10);
313 delete[] s;
314 return TRUE;
315 }
316 else return FALSE;
317 }
318 #endif // wxUSE_RESOURCES
319
320 int gs_wxBusyCursorCount = 0;
321 extern wxCursor gMacCurrentCursor ;
322 wxCursor gMacStoredActiveCursor ;
323
324 // Set the cursor to the busy cursor for all windows
325 void wxBeginBusyCursor(wxCursor *cursor)
326 {
327 if (gs_wxBusyCursorCount++ == 0)
328 {
329 gMacStoredActiveCursor = gMacCurrentCursor ;
330 cursor->MacInstall() ;
331 }
332 //else: nothing to do, already set
333 }
334
335 // Restore cursor to normal
336 void wxEndBusyCursor()
337 {
338 wxCHECK_RET( gs_wxBusyCursorCount > 0,
339 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
340
341 if (--gs_wxBusyCursorCount == 0)
342 {
343 gMacStoredActiveCursor.MacInstall() ;
344 gMacStoredActiveCursor = wxNullCursor ;
345 }
346 }
347
348 // TRUE if we're between the above two calls
349 bool wxIsBusy()
350 {
351 return (gs_wxBusyCursorCount > 0);
352 }
353
354 wxString wxMacFindFolder( short vol,
355 OSType folderType,
356 Boolean createFolder)
357 {
358 short vRefNum ;
359 long dirID ;
360 wxString strDir ;
361
362 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
363 {
364 FSSpec file ;
365 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
366 {
367 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
368 }
369 }
370 return strDir ;
371 }
372
373 #ifndef __DARWIN__
374 char *wxGetUserHome (const wxString& user)
375 {
376 // TODO
377 return NULL;
378 }
379
380 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
381 {
382 if ( path.empty() )
383 return FALSE;
384
385 wxString p = path ;
386 if (p[0] == ':' ) {
387 p = wxGetCwd() + p ;
388 }
389
390 int pos = p.Find(':') ;
391 if ( pos != wxNOT_FOUND ) {
392 p = p.Mid(1,pos) ;
393 }
394
395 p = p + ":" ;
396
397 Str255 volumeName ;
398 XVolumeParam pb ;
399
400 wxMacStringToPascal( p , volumeName ) ;
401 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
402 if ( err == noErr ) {
403 if ( pTotal ) {
404 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
405 }
406 if ( pFree ) {
407 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
408 }
409 }
410
411 return err == noErr ;
412 }
413 #endif
414
415 // Check whether this window wants to process messages, e.g. Stop button
416 // in long calculations.
417 bool wxCheckForInterrupt(wxWindow *wnd)
418 {
419 // TODO
420 return FALSE;
421 }
422
423 void wxGetMousePosition( int* x, int* y )
424 {
425 Point pt ;
426
427 GetMouse( &pt ) ;
428 LocalToGlobal( &pt ) ;
429 *x = pt.h ;
430 *y = pt.v ;
431 };
432
433 // Return TRUE if we have a colour display
434 bool wxColourDisplay()
435 {
436 return TRUE;
437 }
438
439 // Returns depth of screen
440 int wxDisplayDepth()
441 {
442 Rect globRect ;
443 SetRect(&globRect, -32760, -32760, 32760, 32760);
444 GDHandle theMaxDevice;
445
446 int theDepth = 8;
447 theMaxDevice = GetMaxDevice(&globRect);
448 if (theMaxDevice != nil)
449 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
450
451 return theDepth ;
452 }
453
454 // Get size of display
455 void wxDisplaySize(int *width, int *height)
456 {
457 BitMap screenBits;
458 GetQDGlobalsScreenBits( &screenBits );
459
460 *width = screenBits.bounds.right - screenBits.bounds.left ;
461 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
462 }
463
464 void wxDisplaySizeMM(int *width, int *height)
465 {
466 wxDisplaySize(width, height);
467 // on mac 72 is fixed (at least now ;-)
468 float cvPt2Mm = 25.4 / 72;
469 *width = int( *width * cvPt2Mm );
470 *height = int( *height * cvPt2Mm );
471 }
472
473 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
474 {
475 BitMap screenBits;
476 GetQDGlobalsScreenBits( &screenBits );
477
478 if (x) *x = 0;
479 if (y) *y = 0;
480
481 *width = screenBits.bounds.right - screenBits.bounds.left ;
482 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
483
484 SInt16 mheight ;
485 #if TARGET_CARBON
486 GetThemeMenuBarHeight( &mheight ) ;
487 #else
488 mheight = LMGetMBarHeight() ;
489 #endif
490 *height -= mheight ;
491 if ( y )
492 *y = mheight ;
493 }
494
495 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
496 {
497 return wxGenericFindWindowAtPoint(pt);
498 }
499
500 wxString wxGetOsDescription()
501 {
502 #ifdef WXWIN_OS_DESCRIPTION
503 // use configure generated description if available
504 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
505 #else
506 return "MacOS" ; //TODO:define further
507 #endif
508 }
509
510 //---------------------------------------------------------------------------
511 // wxMac Specific utility functions
512 //---------------------------------------------------------------------------
513
514 #if TARGET_CARBON
515 // converts this string into a carbon foundation string with optional pc 2 mac encoding
516 CFStringRef wxMacCreateCFString( const wxString &str , bool pc2macEncoding )
517 {
518 return CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() ,
519 pc2macEncoding ?
520 kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ;
521 }
522
523 #endif //TARGET_CARBON
524