]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
Fixed the background colour bug.
[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 #include "MoreFiles.h"
30 #include "MoreFilesExtras.h"
31
32 #ifndef __DARWIN__
33 #include <Threads.h>
34 #include <Sound.h>
35 #endif
36
37 #ifndef __DARWIN__
38 // defined in unix/utilsunx.cpp for Mac OS X
39
40 // get full hostname (with domain name if possible)
41 bool wxGetFullHostName(wxChar *buf, int maxSize)
42 {
43 return wxGetHostName(buf, maxSize);
44 }
45
46 // Get hostname only (without domain name)
47 bool wxGetHostName(char *buf, int maxSize)
48 {
49 // Gets Chooser name of user by examining a System resource.
50
51 const short kComputerNameID = -16413;
52
53 short oldResFile = CurResFile() ;
54 UseResFile(0);
55 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
56 UseResFile(oldResFile);
57
58 if (chooserName && *chooserName)
59 {
60 int length = (*chooserName)[0] ;
61 if ( length + 1 > maxSize )
62 {
63 length = maxSize - 1 ;
64 }
65 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
66 buf[length] = 0 ;
67 }
68 else
69 buf[0] = 0 ;
70
71 return TRUE;
72 }
73
74 // Get user ID e.g. jacs
75 bool wxGetUserId(char *buf, int maxSize)
76 {
77 return wxGetUserName( buf , maxSize ) ;
78 }
79
80 const wxChar* wxGetHomeDir(wxString *pstr)
81 {
82 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
83 return pstr->c_str() ;
84 }
85
86 // Get user name e.g. AUTHOR
87 bool wxGetUserName(char *buf, int maxSize)
88 {
89 // Gets Chooser name of user by examining a System resource.
90
91 const short kChooserNameID = -16096;
92
93 short oldResFile = CurResFile() ;
94 UseResFile(0);
95 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
96 UseResFile(oldResFile);
97
98 if (chooserName && *chooserName)
99 {
100 int length = (*chooserName)[0] ;
101 if ( length + 1 > maxSize )
102 {
103 length = maxSize - 1 ;
104 }
105 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
106 buf[length] = 0 ;
107 }
108 else
109 buf[0] = 0 ;
110
111 return TRUE;
112 }
113
114 int wxKill(long pid, wxSignal sig , wxKillError *rc )
115 {
116 // TODO
117 return 0;
118 }
119
120 WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
121 {
122 // TODO : under classic there is no environement support, under X yes
123 return false ;
124 }
125
126 // set the env var name to the given value, return TRUE on success
127 WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
128 {
129 // TODO : under classic there is no environement support, under X yes
130 return false ;
131 }
132
133 //
134 // Execute a program in an Interactive Shell
135 //
136 bool wxShell(const wxString& command)
137 {
138 // TODO
139 return FALSE;
140 }
141
142 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
143 long wxGetFreeMemory()
144 {
145 return FreeMem() ;
146 }
147
148 void wxUsleep(unsigned long milliseconds)
149 {
150 clock_t start = clock() ;
151 do
152 {
153 YieldToAnyThread() ;
154 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
155 }
156
157 void wxSleep(int nSecs)
158 {
159 wxUsleep(1000*nSecs);
160 }
161
162 // Consume all events until no more left
163 void wxFlushEvents()
164 {
165 }
166
167 #if WXWIN_COMPATIBILITY_2_2
168
169 // Output a debug message, in a system dependent fashion.
170 void wxDebugMsg(const char *fmt ...)
171 {
172 va_list ap;
173 static char buffer[512];
174
175 if (!wxTheApp->GetWantDebugOutput())
176 return ;
177
178 va_start(ap, fmt);
179
180 vsprintf(buffer,fmt,ap) ;
181 strcat(buffer,";g") ;
182 c2pstr(buffer) ;
183 DebugStr((unsigned char*) buffer) ;
184
185 va_end(ap);
186 }
187
188 // Non-fatal error: pop up message box and (possibly) continue
189 void wxError(const wxString& msg, const wxString& title)
190 {
191 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
192 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
193 wxExit();
194 }
195
196 // Fatal error: pop up message box and abort
197 void wxFatalError(const wxString& msg, const wxString& title)
198 {
199 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
200 wxMessageBox(wxBuffer);
201 wxExit();
202 }
203
204 #endif // WXWIN_COMPATIBILITY_2_2
205
206 #endif // !__DARWIN__
207
208 // Emit a beeeeeep
209 void wxBell()
210 {
211 SysBeep(30);
212 }
213
214 int wxGetOsVersion(int *majorVsn, int *minorVsn)
215 {
216 long theSystem ;
217
218 // are there x-platform conventions ?
219
220 Gestalt(gestaltSystemVersion, &theSystem) ;
221 if (minorVsn != NULL) {
222 *minorVsn = (theSystem & 0xFF ) ;
223 }
224 if (majorVsn != NULL) {
225 *majorVsn = (theSystem >> 8 ) ;
226 }
227 #ifdef __DARWIN__
228 return wxMAC_DARWIN;
229 #else
230 return wxMAC;
231 #endif
232 }
233
234 // Reading and writing resources (eg WIN.INI, .Xdefaults)
235 #if wxUSE_RESOURCES
236 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
237 {
238 // TODO
239 return FALSE;
240 }
241
242 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
243 {
244 char buf[50];
245 sprintf(buf, "%.4f", value);
246 return wxWriteResource(section, entry, buf, file);
247 }
248
249 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
250 {
251 char buf[50];
252 sprintf(buf, "%ld", value);
253 return wxWriteResource(section, entry, buf, file);
254 }
255
256 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
257 {
258 char buf[50];
259 sprintf(buf, "%d", value);
260 return wxWriteResource(section, entry, buf, file);
261 }
262
263 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
264 {
265 // TODO
266 return FALSE;
267 }
268
269 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
270 {
271 char *s = NULL;
272 bool succ = wxGetResource(section, entry, (char **)&s, file);
273 if (succ)
274 {
275 *value = (float)strtod(s, NULL);
276 delete[] s;
277 return TRUE;
278 }
279 else return FALSE;
280 }
281
282 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
283 {
284 char *s = NULL;
285 bool succ = wxGetResource(section, entry, (char **)&s, file);
286 if (succ)
287 {
288 *value = strtol(s, NULL, 10);
289 delete[] s;
290 return TRUE;
291 }
292 else return FALSE;
293 }
294
295 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
296 {
297 char *s = NULL;
298 bool succ = wxGetResource(section, entry, (char **)&s, file);
299 if (succ)
300 {
301 *value = (int)strtol(s, NULL, 10);
302 delete[] s;
303 return TRUE;
304 }
305 else return FALSE;
306 }
307 #endif // wxUSE_RESOURCES
308
309 int wxBusyCursorCount = 0;
310 extern CursHandle gMacCurrentCursor ;
311 CursHandle gMacStoredActiveCursor = NULL ;
312
313 // Set the cursor to the busy cursor for all windows
314 void wxBeginBusyCursor(wxCursor *cursor)
315 {
316 wxBusyCursorCount ++;
317 if (wxBusyCursorCount == 1)
318 {
319 gMacStoredActiveCursor = gMacCurrentCursor ;
320 ::SetCursor( *::GetCursor( watchCursor ) ) ;
321 }
322 else
323 {
324 // TODO
325 }
326 }
327
328 // Restore cursor to normal
329 void wxEndBusyCursor()
330 {
331 if (wxBusyCursorCount == 0)
332 return;
333
334 wxBusyCursorCount --;
335 if (wxBusyCursorCount == 0)
336 {
337 if ( gMacStoredActiveCursor )
338 ::SetCursor( *gMacStoredActiveCursor ) ;
339 else
340 {
341 Cursor MacArrow ;
342 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
343 }
344 gMacStoredActiveCursor = NULL ;
345 }
346 }
347
348 // TRUE if we're between the above two calls
349 bool wxIsBusy()
350 {
351 return (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 #if TARGET_CARBON
463 SInt16 mheight ;
464 GetThemeMenuBarHeight( &mheight ) ;
465 *height -= mheight ;
466 #else
467 *height -= LMGetMBarHeight() ;
468 #endif
469 }
470
471 void wxDisplaySizeMM(int *width, int *height)
472 {
473 wxDisplaySize(width, height);
474 }
475
476 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
477 {
478 // This is supposed to return desktop dimensions minus any window
479 // manager panels, menus, taskbars, etc. If there is a way to do that
480 // for this platform please fix this function, otherwise it defaults
481 // to the entire desktop.
482 if (x) *x = 0;
483 if (y) *y = 0;
484 wxDisplaySize(width, height);
485 }
486
487 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
488 {
489 return wxGenericFindWindowAtPoint(pt);
490 }
491
492 wxString wxGetOsDescription()
493 {
494 #ifdef WXWIN_OS_DESCRIPTION
495 // use configure generated description if available
496 return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")");
497 #else
498 return "MacOS" ; //TODO:define further
499 #endif
500 }
501