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