]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/utils.cpp
96e20cfc3d9a42d6da920a578e10d8e54d9c796e
[wxWidgets.git] / src / mac / carbon / 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 "morefile.h"
31 #include "moreextr.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 #ifndef __DARWIN__
322 wxString wxMacFindFolder( short vol,
323 OSType folderType,
324 Boolean createFolder)
325 {
326 short vRefNum ;
327 long dirID ;
328 wxString strDir ;
329
330 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
331 {
332 FSSpec file ;
333 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
334 {
335 strDir = wxMacFSSpec2MacFilename( &file ) + ":" ;
336 }
337 }
338 return strDir ;
339 }
340 #endif
341
342 #ifndef __DARWIN__
343 char *wxGetUserHome (const wxString& user)
344 {
345 // TODO
346 return NULL;
347 }
348 #endif
349
350 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
351 {
352 if ( path.empty() )
353 return FALSE;
354
355 wxString p = path ;
356 if (p[0] == ':' ) {
357 p = wxGetCwd() + p ;
358 }
359
360 int pos = p.Find(':') ;
361 if ( pos != wxNOT_FOUND ) {
362 p = p.Mid(1,pos) ;
363 }
364
365 p = p + ":" ;
366
367 Str255 volumeName ;
368 XVolumeParam pb ;
369
370 wxMacStringToPascal( p , volumeName ) ;
371 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
372 if ( err == noErr ) {
373 if ( pTotal ) {
374 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
375 }
376 if ( pFree ) {
377 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
378 }
379 }
380
381 return err == noErr ;
382 }
383
384 // Check whether this window wants to process messages, e.g. Stop button
385 // in long calculations.
386 bool wxCheckForInterrupt(wxWindow *wnd)
387 {
388 // TODO
389 return FALSE;
390 }
391
392 void wxGetMousePosition( int* x, int* y )
393 {
394 Point pt ;
395
396 GetMouse( &pt ) ;
397 LocalToGlobal( &pt ) ;
398 *x = pt.h ;
399 *y = pt.v ;
400 };
401
402 // Return TRUE if we have a colour display
403 bool wxColourDisplay()
404 {
405 return TRUE;
406 }
407
408 // Returns depth of screen
409 int wxDisplayDepth()
410 {
411 Rect globRect ;
412 SetRect(&globRect, -32760, -32760, 32760, 32760);
413 GDHandle theMaxDevice;
414
415 int theDepth = 8;
416 theMaxDevice = GetMaxDevice(&globRect);
417 if (theMaxDevice != nil)
418 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
419
420 return theDepth ;
421 }
422
423 // Get size of display
424 void wxDisplaySize(int *width, int *height)
425 {
426 BitMap screenBits;
427 GetQDGlobalsScreenBits( &screenBits );
428
429 *width = screenBits.bounds.right - screenBits.bounds.left ;
430 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
431 #if TARGET_CARBON
432 SInt16 mheight ;
433 GetThemeMenuBarHeight( &mheight ) ;
434 *height -= mheight ;
435 #else
436 *height -= LMGetMBarHeight() ;
437 #endif
438 }
439
440 void wxDisplaySizeMM(int *width, int *height)
441 {
442 wxDisplaySize(width, height);
443 }
444
445 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
446 {
447 // This is supposed to return desktop dimensions minus any window
448 // manager panels, menus, taskbars, etc. If there is a way to do that
449 // for this platform please fix this function, otherwise it defaults
450 // to the entire desktop.
451 if (x) *x = 0;
452 if (y) *y = 0;
453 wxDisplaySize(width, height);
454 }
455
456 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
457 {
458 return wxGenericFindWindowAtPoint(pt);
459 }