]>
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 | ||
2d4e4f80 GD |
29 | #ifdef __DARWIN__ |
30 | # include "MoreFilesX.h" | |
31 | #else | |
32 | # include "MoreFiles.h" | |
33 | # include "MoreFilesExtras.h" | |
34 | #endif | |
518af45b | 35 | |
66a09d47 SC |
36 | #ifndef __DARWIN__ |
37 | #include <Threads.h> | |
38 | #include <Sound.h> | |
39 | #endif | |
40 | ||
f5c6eb5c | 41 | #ifndef __DARWIN__ |
03e11df5 GD |
42 | // defined in unix/utilsunx.cpp for Mac OS X |
43 | ||
2f1ae414 SC |
44 | // get full hostname (with domain name if possible) |
45 | bool wxGetFullHostName(wxChar *buf, int maxSize) | |
46 | { | |
47 | return wxGetHostName(buf, maxSize); | |
48 | } | |
49 | ||
50 | // Get hostname only (without domain name) | |
e9576ca5 SC |
51 | bool wxGetHostName(char *buf, int maxSize) |
52 | { | |
0a67a93b SC |
53 | // Gets Chooser name of user by examining a System resource. |
54 | ||
55 | const short kComputerNameID = -16413; | |
56 | ||
57 | short oldResFile = CurResFile() ; | |
58 | UseResFile(0); | |
59 | StringHandle chooserName = (StringHandle)::GetString(kComputerNameID); | |
60 | UseResFile(oldResFile); | |
61 | ||
62 | if (chooserName && *chooserName) | |
63 | { | |
64 | int length = (*chooserName)[0] ; | |
65 | if ( length + 1 > maxSize ) | |
66 | { | |
67 | length = maxSize - 1 ; | |
68 | } | |
69 | strncpy( buf , (char*) &(*chooserName)[1] , length ) ; | |
70 | buf[length] = 0 ; | |
71 | } | |
72 | else | |
73 | buf[0] = 0 ; | |
74 | ||
75 | return TRUE; | |
e9576ca5 SC |
76 | } |
77 | ||
78 | // Get user ID e.g. jacs | |
79 | bool wxGetUserId(char *buf, int maxSize) | |
80 | { | |
0a67a93b | 81 | return wxGetUserName( buf , maxSize ) ; |
e9576ca5 SC |
82 | } |
83 | ||
5b781a67 SC |
84 | const wxChar* wxGetHomeDir(wxString *pstr) |
85 | { | |
86 | *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ; | |
87 | return pstr->c_str() ; | |
88 | } | |
89 | ||
e9576ca5 SC |
90 | // Get user name e.g. AUTHOR |
91 | bool wxGetUserName(char *buf, int maxSize) | |
92 | { | |
0a67a93b SC |
93 | // Gets Chooser name of user by examining a System resource. |
94 | ||
95 | const short kChooserNameID = -16096; | |
96 | ||
97 | short oldResFile = CurResFile() ; | |
98 | UseResFile(0); | |
99 | StringHandle chooserName = (StringHandle)::GetString(kChooserNameID); | |
100 | UseResFile(oldResFile); | |
101 | ||
102 | if (chooserName && *chooserName) | |
103 | { | |
104 | int length = (*chooserName)[0] ; | |
105 | if ( length + 1 > maxSize ) | |
106 | { | |
107 | length = maxSize - 1 ; | |
108 | } | |
109 | strncpy( buf , (char*) &(*chooserName)[1] , length ) ; | |
110 | buf[length] = 0 ; | |
111 | } | |
112 | else | |
113 | buf[0] = 0 ; | |
114 | ||
115 | return TRUE; | |
e9576ca5 SC |
116 | } |
117 | ||
5dbb17e2 | 118 | int wxKill(long pid, wxSignal sig , wxKillError *rc ) |
e9576ca5 SC |
119 | { |
120 | // TODO | |
121 | return 0; | |
122 | } | |
123 | ||
5dbb17e2 SC |
124 | WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value) |
125 | { | |
126 | // TODO : under classic there is no environement support, under X yes | |
127 | return false ; | |
128 | } | |
129 | ||
130 | // set the env var name to the given value, return TRUE on success | |
131 | WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value) | |
132 | { | |
133 | // TODO : under classic there is no environement support, under X yes | |
134 | return false ; | |
135 | } | |
136 | ||
e9576ca5 SC |
137 | // |
138 | // Execute a program in an Interactive Shell | |
139 | // | |
140 | bool wxShell(const wxString& command) | |
141 | { | |
142 | // TODO | |
143 | return FALSE; | |
144 | } | |
145 | ||
f6ba47d9 VZ |
146 | // Shutdown or reboot the PC |
147 | bool wxShutdown(wxShutdownFlags wFlags) | |
148 | { | |
149 | // TODO | |
150 | return FALSE; | |
151 | } | |
152 | ||
e9576ca5 SC |
153 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) |
154 | long wxGetFreeMemory() | |
155 | { | |
0a67a93b SC |
156 | return FreeMem() ; |
157 | } | |
158 | ||
159 | void wxUsleep(unsigned long milliseconds) | |
160 | { | |
e7e1b01e GD |
161 | clock_t start = clock() ; |
162 | do | |
163 | { | |
164 | YieldToAnyThread() ; | |
165 | } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ; | |
e9576ca5 SC |
166 | } |
167 | ||
168 | void wxSleep(int nSecs) | |
169 | { | |
0a67a93b | 170 | wxUsleep(1000*nSecs); |
e9576ca5 SC |
171 | } |
172 | ||
173 | // Consume all events until no more left | |
174 | void wxFlushEvents() | |
175 | { | |
176 | } | |
177 | ||
73deed44 VZ |
178 | #if WXWIN_COMPATIBILITY_2_2 |
179 | ||
e9576ca5 SC |
180 | // Output a debug message, in a system dependent fashion. |
181 | void wxDebugMsg(const char *fmt ...) | |
182 | { | |
183 | va_list ap; | |
184 | static char buffer[512]; | |
185 | ||
186 | if (!wxTheApp->GetWantDebugOutput()) | |
187 | return ; | |
188 | ||
189 | va_start(ap, fmt); | |
190 | ||
0a67a93b SC |
191 | vsprintf(buffer,fmt,ap) ; |
192 | strcat(buffer,";g") ; | |
9ff647cf SC |
193 | c2pstr(buffer) ; |
194 | DebugStr((unsigned char*) buffer) ; | |
e9576ca5 SC |
195 | |
196 | va_end(ap); | |
197 | } | |
198 | ||
199 | // Non-fatal error: pop up message box and (possibly) continue | |
200 | void wxError(const wxString& msg, const wxString& title) | |
201 | { | |
0a67a93b SC |
202 | wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg); |
203 | if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO ) | |
e9576ca5 SC |
204 | wxExit(); |
205 | } | |
206 | ||
207 | // Fatal error: pop up message box and abort | |
208 | void wxFatalError(const wxString& msg, const wxString& title) | |
209 | { | |
0a67a93b SC |
210 | wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg); |
211 | wxMessageBox(wxBuffer); | |
212 | wxExit(); | |
e9576ca5 | 213 | } |
73deed44 VZ |
214 | |
215 | #endif // WXWIN_COMPATIBILITY_2_2 | |
216 | ||
f5c6eb5c | 217 | #endif // !__DARWIN__ |
e9576ca5 SC |
218 | |
219 | // Emit a beeeeeep | |
220 | void wxBell() | |
221 | { | |
0a67a93b | 222 | SysBeep(30); |
e9576ca5 SC |
223 | } |
224 | ||
225 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
226 | { | |
ff8fda36 GD |
227 | long theSystem ; |
228 | ||
229 | // are there x-platform conventions ? | |
230 | ||
231 | Gestalt(gestaltSystemVersion, &theSystem) ; | |
232 | if (minorVsn != NULL) { | |
233 | *minorVsn = (theSystem & 0xFF ) ; | |
234 | } | |
235 | if (majorVsn != NULL) { | |
236 | *majorVsn = (theSystem >> 8 ) ; | |
237 | } | |
238 | #ifdef __DARWIN__ | |
239 | return wxMAC_DARWIN; | |
240 | #else | |
241 | return wxMAC; | |
242 | #endif | |
e9576ca5 SC |
243 | } |
244 | ||
245 | // Reading and writing resources (eg WIN.INI, .Xdefaults) | |
246 | #if wxUSE_RESOURCES | |
247 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) | |
248 | { | |
249 | // TODO | |
250 | return FALSE; | |
251 | } | |
252 | ||
253 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
254 | { | |
255 | char buf[50]; | |
256 | sprintf(buf, "%.4f", value); | |
257 | return wxWriteResource(section, entry, buf, file); | |
258 | } | |
259 | ||
260 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
261 | { | |
262 | char buf[50]; | |
263 | sprintf(buf, "%ld", value); | |
264 | return wxWriteResource(section, entry, buf, file); | |
265 | } | |
266 | ||
267 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
268 | { | |
269 | char buf[50]; | |
270 | sprintf(buf, "%d", value); | |
271 | return wxWriteResource(section, entry, buf, file); | |
272 | } | |
273 | ||
274 | bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file) | |
275 | { | |
276 | // TODO | |
277 | return FALSE; | |
278 | } | |
279 | ||
280 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
281 | { | |
282 | char *s = NULL; | |
283 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
284 | if (succ) | |
285 | { | |
286 | *value = (float)strtod(s, NULL); | |
287 | delete[] s; | |
288 | return TRUE; | |
289 | } | |
290 | else return FALSE; | |
291 | } | |
292 | ||
293 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
294 | { | |
295 | char *s = NULL; | |
296 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
297 | if (succ) | |
298 | { | |
299 | *value = strtol(s, NULL, 10); | |
300 | delete[] s; | |
301 | return TRUE; | |
302 | } | |
303 | else return FALSE; | |
304 | } | |
305 | ||
306 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
307 | { | |
308 | char *s = NULL; | |
309 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
310 | if (succ) | |
311 | { | |
312 | *value = (int)strtol(s, NULL, 10); | |
ec5d7799 | 313 | delete[] s; |
e9576ca5 SC |
314 | return TRUE; |
315 | } | |
316 | else return FALSE; | |
317 | } | |
318 | #endif // wxUSE_RESOURCES | |
319 | ||
6b57b49a SC |
320 | int gs_wxBusyCursorCount = 0; |
321 | extern wxCursor gMacCurrentCursor ; | |
322 | wxCursor gMacStoredActiveCursor ; | |
e9576ca5 SC |
323 | |
324 | // Set the cursor to the busy cursor for all windows | |
325 | void wxBeginBusyCursor(wxCursor *cursor) | |
326 | { | |
6b57b49a | 327 | if (gs_wxBusyCursorCount++ == 0) |
e9576ca5 | 328 | { |
519cb848 | 329 | gMacStoredActiveCursor = gMacCurrentCursor ; |
6b57b49a | 330 | cursor->MacInstall() ; |
e9576ca5 | 331 | } |
6b57b49a | 332 | //else: nothing to do, already set |
e9576ca5 SC |
333 | } |
334 | ||
335 | // Restore cursor to normal | |
336 | void wxEndBusyCursor() | |
337 | { | |
6b57b49a SC |
338 | wxCHECK_RET( gs_wxBusyCursorCount > 0, |
339 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); | |
ec5d7799 | 340 | |
6b57b49a | 341 | if (--gs_wxBusyCursorCount == 0) |
e9576ca5 | 342 | { |
6b57b49a SC |
343 | gMacStoredActiveCursor.MacInstall() ; |
344 | gMacStoredActiveCursor = wxNullCursor ; | |
e9576ca5 SC |
345 | } |
346 | } | |
347 | ||
348 | // TRUE if we're between the above two calls | |
349 | bool wxIsBusy() | |
350 | { | |
6b57b49a | 351 | return (gs_wxBusyCursorCount > 0); |
ec5d7799 | 352 | } |
e9576ca5 | 353 | |
e7e1b01e GD |
354 | wxString wxMacFindFolder( short vol, |
355 | OSType folderType, | |
356 | Boolean createFolder) | |
2f1ae414 | 357 | { |
2d4e4f80 GD |
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 ; | |
2f1ae414 SC |
371 | } |
372 | ||
f5c6eb5c | 373 | #ifndef __DARWIN__ |
e9576ca5 SC |
374 | char *wxGetUserHome (const wxString& user) |
375 | { | |
376 | // TODO | |
377 | return NULL; | |
378 | } | |
379 | ||
518af45b SC |
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 | } | |
e7e1b01e | 413 | #endif |
518af45b | 414 | |
e9576ca5 SC |
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 | { | |
519cb848 | 425 | Point pt ; |
ec5d7799 | 426 | |
519cb848 SC |
427 | GetMouse( &pt ) ; |
428 | LocalToGlobal( &pt ) ; | |
429 | *x = pt.h ; | |
430 | *y = pt.v ; | |
e9576ca5 SC |
431 | }; |
432 | ||
433 | // Return TRUE if we have a colour display | |
434 | bool wxColourDisplay() | |
435 | { | |
e9576ca5 SC |
436 | return TRUE; |
437 | } | |
438 | ||
439 | // Returns depth of screen | |
440 | int wxDisplayDepth() | |
441 | { | |
ec5d7799 | 442 | Rect globRect ; |
2f1ae414 SC |
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; | |
ec5d7799 | 450 | |
2f1ae414 | 451 | return theDepth ; |
e9576ca5 SC |
452 | } |
453 | ||
454 | // Get size of display | |
455 | void wxDisplaySize(int *width, int *height) | |
456 | { | |
2b5f62a0 VZ |
457 | BitMap screenBits; |
458 | GetQDGlobalsScreenBits( &screenBits ); | |
459 | ||
460 | *width = screenBits.bounds.right - screenBits.bounds.left ; | |
461 | *height = screenBits.bounds.bottom - screenBits.bounds.top ; | |
e9576ca5 SC |
462 | } |
463 | ||
5fde6fcc GD |
464 | void wxDisplaySizeMM(int *width, int *height) |
465 | { | |
5b028d57 SC |
466 | wxDisplaySize(width, height); |
467 | // on mac 72 is fixed (at least now ;-) | |
468 | float cvPt2Mm = 25.4 / 72; | |
469 | *width = int( *width * cvPt2Mm ); | |
470 | *height = int( *height * cvPt2Mm ); | |
5fde6fcc GD |
471 | } |
472 | ||
ec5d7799 RD |
473 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) |
474 | { | |
2d4e4f80 GD |
475 | BitMap screenBits; |
476 | GetQDGlobalsScreenBits( &screenBits ); | |
7cfebe05 | 477 | |
ec5d7799 RD |
478 | if (x) *x = 0; |
479 | if (y) *y = 0; | |
7cfebe05 SC |
480 | |
481 | *width = screenBits.bounds.right - screenBits.bounds.left ; | |
482 | *height = screenBits.bounds.bottom - screenBits.bounds.top ; | |
483 | ||
2d4e4f80 GD |
484 | SInt16 mheight ; |
485 | #if TARGET_CARBON | |
486 | GetThemeMenuBarHeight( &mheight ) ; | |
487 | #else | |
7cfebe05 | 488 | mheight = LMGetMBarHeight() ; |
2d4e4f80 | 489 | #endif |
7cfebe05 SC |
490 | *height -= mheight ; |
491 | if ( y ) | |
2d4e4f80 | 492 | *y = mheight ; |
ec5d7799 RD |
493 | } |
494 | ||
57591e0e JS |
495 | wxWindow* wxFindWindowAtPoint(const wxPoint& pt) |
496 | { | |
497 | return wxGenericFindWindowAtPoint(pt); | |
498 | } | |
5dbb17e2 SC |
499 | |
500 | wxString wxGetOsDescription() | |
501 | { | |
6e73695c GD |
502 | #ifdef WXWIN_OS_DESCRIPTION |
503 | // use configure generated description if available | |
504 | return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")"); | |
505 | #else | |
506 | return "MacOS" ; //TODO:define further | |
507 | #endif | |
508 | } | |
509 | ||
3d963f81 SC |
510 | //--------------------------------------------------------------------------- |
511 | // wxMac Specific utility functions | |
512 | //--------------------------------------------------------------------------- | |
513 | ||
514 | #if TARGET_CARBON | |
515 | // converts this string into a carbon foundation string with optional pc 2 mac encoding | |
516 | CFStringRef wxMacCreateCFString( const wxString &str , bool pc2macEncoding ) | |
517 | { | |
518 | return CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() , | |
519 | pc2macEncoding ? | |
520 | kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ; | |
521 | } | |
522 | ||
523 | #endif //TARGET_CARBON | |
524 |