]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
added wxApp::Yield()
[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
15 // #pragma implementation "utils.h"
16 #endif
17
18 #include "wx/setup.h"
19 #include "wx/utils.h"
20 #include "wx/app.h"
21 #include "wx/mac/uma.h"
22
23 #include <ctype.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29
30 #include "MoreFiles.h"
31 #include "MoreFilesExtras.h"
32
33 #ifndef __DARWIN__
34 // defined in unix/utilsunx.cpp for Mac OS X
35
36 // get full hostname (with domain name if possible)
37 bool wxGetFullHostName(wxChar *buf, int maxSize)
38 {
39 return wxGetHostName(buf, maxSize);
40 }
41
42 // Get hostname only (without domain name)
43 bool wxGetHostName(char *buf, int maxSize)
44 {
45 // Gets Chooser name of user by examining a System resource.
46
47 const short kComputerNameID = -16413;
48
49 short oldResFile = CurResFile() ;
50 UseResFile(0);
51 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
52 UseResFile(oldResFile);
53
54 if (chooserName && *chooserName)
55 {
56 int length = (*chooserName)[0] ;
57 if ( length + 1 > maxSize )
58 {
59 length = maxSize - 1 ;
60 }
61 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
62 buf[length] = 0 ;
63 }
64 else
65 buf[0] = 0 ;
66
67 return TRUE;
68 }
69
70 // Get user ID e.g. jacs
71 bool wxGetUserId(char *buf, int maxSize)
72 {
73 return wxGetUserName( buf , maxSize ) ;
74 }
75
76 const wxChar* wxGetHomeDir(wxString *pstr)
77 {
78 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
79 return pstr->c_str() ;
80 }
81
82 // Get user name e.g. AUTHOR
83 bool wxGetUserName(char *buf, int maxSize)
84 {
85 // Gets Chooser name of user by examining a System resource.
86
87 const short kChooserNameID = -16096;
88
89 short oldResFile = CurResFile() ;
90 UseResFile(0);
91 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
92 UseResFile(oldResFile);
93
94 if (chooserName && *chooserName)
95 {
96 int length = (*chooserName)[0] ;
97 if ( length + 1 > maxSize )
98 {
99 length = maxSize - 1 ;
100 }
101 strncpy( buf , (char*) &(*chooserName)[1] , length ) ;
102 buf[length] = 0 ;
103 }
104 else
105 buf[0] = 0 ;
106
107 return TRUE;
108 }
109
110 int wxKill(long pid, wxSignal sig)
111 {
112 // TODO
113 return 0;
114 }
115
116 //
117 // Execute a program in an Interactive Shell
118 //
119 bool wxShell(const wxString& command)
120 {
121 // TODO
122 return FALSE;
123 }
124
125 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
126 long wxGetFreeMemory()
127 {
128 return FreeMem() ;
129 }
130
131 void wxUsleep(unsigned long milliseconds)
132 {
133 clock_t start = clock() ;
134 do
135 {
136 YieldToAnyThread() ;
137 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
138 }
139
140 void wxSleep(int nSecs)
141 {
142 wxUsleep(1000*nSecs);
143 }
144
145 // Consume all events until no more left
146 void wxFlushEvents()
147 {
148 }
149
150 // Output a debug message, in a system dependent fashion.
151 void wxDebugMsg(const char *fmt ...)
152 {
153 va_list ap;
154 static char buffer[512];
155
156 if (!wxTheApp->GetWantDebugOutput())
157 return ;
158
159 va_start(ap, fmt);
160
161 vsprintf(buffer,fmt,ap) ;
162 strcat(buffer,";g") ;
163 c2pstr(buffer) ;
164 DebugStr((unsigned char*) buffer) ;
165
166 va_end(ap);
167 }
168
169 // Non-fatal error: pop up message box and (possibly) continue
170 void wxError(const wxString& msg, const wxString& title)
171 {
172 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
173 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
174 wxExit();
175 }
176
177 // Fatal error: pop up message box and abort
178 void wxFatalError(const wxString& msg, const wxString& title)
179 {
180 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
181 wxMessageBox(wxBuffer);
182 wxExit();
183 }
184 #endif // !__DARWIN__
185
186 // Emit a beeeeeep
187 void wxBell()
188 {
189 SysBeep(30);
190 }
191
192 int wxGetOsVersion(int *majorVsn, int *minorVsn)
193 {
194 long theSystem ;
195 Gestalt(gestaltSystemVersion, &theSystem) ;
196 *minorVsn = (theSystem & 0xFF ) ;
197 *majorVsn = (theSystem >> 8 ) ; // are there x-platform conventions ?
198 return wxMACINTOSH;
199 }
200
201 // Reading and writing resources (eg WIN.INI, .Xdefaults)
202 #if wxUSE_RESOURCES
203 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
204 {
205 // TODO
206 return FALSE;
207 }
208
209 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
210 {
211 char buf[50];
212 sprintf(buf, "%.4f", value);
213 return wxWriteResource(section, entry, buf, file);
214 }
215
216 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
217 {
218 char buf[50];
219 sprintf(buf, "%ld", value);
220 return wxWriteResource(section, entry, buf, file);
221 }
222
223 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
224 {
225 char buf[50];
226 sprintf(buf, "%d", value);
227 return wxWriteResource(section, entry, buf, file);
228 }
229
230 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
231 {
232 // TODO
233 return FALSE;
234 }
235
236 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
237 {
238 char *s = NULL;
239 bool succ = wxGetResource(section, entry, (char **)&s, file);
240 if (succ)
241 {
242 *value = (float)strtod(s, NULL);
243 delete[] s;
244 return TRUE;
245 }
246 else return FALSE;
247 }
248
249 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
250 {
251 char *s = NULL;
252 bool succ = wxGetResource(section, entry, (char **)&s, file);
253 if (succ)
254 {
255 *value = strtol(s, NULL, 10);
256 delete[] s;
257 return TRUE;
258 }
259 else return FALSE;
260 }
261
262 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
263 {
264 char *s = NULL;
265 bool succ = wxGetResource(section, entry, (char **)&s, file);
266 if (succ)
267 {
268 *value = (int)strtol(s, NULL, 10);
269 delete[] s;
270 return TRUE;
271 }
272 else return FALSE;
273 }
274 #endif // wxUSE_RESOURCES
275
276 int wxBusyCursorCount = 0;
277 extern CursHandle gMacCurrentCursor ;
278 CursHandle gMacStoredActiveCursor = NULL ;
279
280 // Set the cursor to the busy cursor for all windows
281 void wxBeginBusyCursor(wxCursor *cursor)
282 {
283 wxBusyCursorCount ++;
284 if (wxBusyCursorCount == 1)
285 {
286 gMacStoredActiveCursor = gMacCurrentCursor ;
287 ::SetCursor( *::GetCursor( watchCursor ) ) ;
288 }
289 else
290 {
291 // TODO
292 }
293 }
294
295 // Restore cursor to normal
296 void wxEndBusyCursor()
297 {
298 if (wxBusyCursorCount == 0)
299 return;
300
301 wxBusyCursorCount --;
302 if (wxBusyCursorCount == 0)
303 {
304 if ( gMacStoredActiveCursor )
305 ::SetCursor( *gMacStoredActiveCursor ) ;
306 else
307 {
308 Cursor MacArrow ;
309 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
310 }
311 gMacStoredActiveCursor = NULL ;
312 }
313 }
314
315 // TRUE if we're between the above two calls
316 bool wxIsBusy()
317 {
318 return (wxBusyCursorCount > 0);
319 }
320
321 wxString wxMacFindFolder( short vol,
322 OSType folderType,
323 Boolean createFolder)
324 {
325 short vRefNum ;
326 long dirID ;
327 wxString strDir ;
328
329 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
330 {
331 FSSpec file ;
332 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
333 {
334 strDir = wxMacFSSpec2MacFilename( &file ) + ":" ;
335 }
336 }
337 return strDir ;
338 }
339
340 #ifndef __DARWIN__
341 char *wxGetUserHome (const wxString& user)
342 {
343 // TODO
344 return NULL;
345 }
346 #endif
347
348 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
349 {
350 if ( path.empty() )
351 return FALSE;
352
353 wxString p = path ;
354 if (p[0] == ':' ) {
355 p = wxGetCwd() + p ;
356 }
357
358 int pos = p.Find(':') ;
359 if ( pos != wxNOT_FOUND ) {
360 p = p.Mid(1,pos) ;
361 }
362
363 p = p + ":" ;
364
365 Str255 volumeName ;
366 XVolumeParam pb ;
367
368 wxMacStringToPascal( p , volumeName ) ;
369 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
370 if ( err == noErr ) {
371 if ( pTotal ) {
372 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
373 }
374 if ( pFree ) {
375 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
376 }
377 }
378
379 return err == noErr ;
380 }
381
382 // Check whether this window wants to process messages, e.g. Stop button
383 // in long calculations.
384 bool wxCheckForInterrupt(wxWindow *wnd)
385 {
386 // TODO
387 return FALSE;
388 }
389
390 void wxGetMousePosition( int* x, int* y )
391 {
392 Point pt ;
393
394 GetMouse( &pt ) ;
395 LocalToGlobal( &pt ) ;
396 *x = pt.h ;
397 *y = pt.v ;
398 };
399
400 // Return TRUE if we have a colour display
401 bool wxColourDisplay()
402 {
403 return TRUE;
404 }
405
406 // Returns depth of screen
407 int wxDisplayDepth()
408 {
409 Rect globRect ;
410 SetRect(&globRect, -32760, -32760, 32760, 32760);
411 GDHandle theMaxDevice;
412
413 int theDepth = 8;
414 theMaxDevice = GetMaxDevice(&globRect);
415 if (theMaxDevice != nil)
416 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
417
418 return theDepth ;
419 }
420
421 // Get size of display
422 void wxDisplaySize(int *width, int *height)
423 {
424 BitMap screenBits;
425 GetQDGlobalsScreenBits( &screenBits );
426
427 *width = screenBits.bounds.right - screenBits.bounds.left ;
428 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
429 #if TARGET_CARBON
430 SInt16 mheight ;
431 GetThemeMenuBarHeight( &mheight ) ;
432 *height -= mheight ;
433 #else
434 *height -= LMGetMBarHeight() ;
435 #endif
436 }
437
438 void wxDisplaySizeMM(int *width, int *height)
439 {
440 wxDisplaySize(width, height);
441 }
442
443 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
444 {
445 // This is supposed to return desktop dimensions minus any window
446 // manager panels, menus, taskbars, etc. If there is a way to do that
447 // for this platform please fix this function, otherwise it defaults
448 // to the entire desktop.
449 if (x) *x = 0;
450 if (y) *y = 0;
451 wxDisplaySize(width, height);
452 }
453
454 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
455 {
456 return wxGenericFindWindowAtPoint(pt);
457 }