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