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