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