]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/utils.cpp
28c069a63abecaa35c86c4e17391d770cd7f7884
[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 #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 debugstr(buffer) ;
161
162 va_end(ap);
163 }
164
165 // Non-fatal error: pop up message box and (possibly) continue
166 void wxError(const wxString& msg, const wxString& title)
167 {
168 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
169 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
170 wxExit();
171 }
172
173 // Fatal error: pop up message box and abort
174 void wxFatalError(const wxString& msg, const wxString& title)
175 {
176 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
177 wxMessageBox(wxBuffer);
178 wxExit();
179 }
180 #endif // !__UNIX__
181
182 // Emit a beeeeeep
183 void wxBell()
184 {
185 SysBeep(30);
186 }
187
188 int wxGetOsVersion(int *majorVsn, int *minorVsn)
189 {
190 long theSystem ;
191 Gestalt(gestaltSystemVersion, &theSystem) ;
192 *minorVsn = (theSystem & 0xFF ) ;
193 *majorVsn = (theSystem >> 8 ) ; // are there x-platform conventions ?
194 return wxMACINTOSH;
195 }
196
197 // Reading and writing resources (eg WIN.INI, .Xdefaults)
198 #if wxUSE_RESOURCES
199 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
200 {
201 // TODO
202 return FALSE;
203 }
204
205 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
206 {
207 char buf[50];
208 sprintf(buf, "%.4f", value);
209 return wxWriteResource(section, entry, buf, file);
210 }
211
212 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
213 {
214 char buf[50];
215 sprintf(buf, "%ld", value);
216 return wxWriteResource(section, entry, buf, file);
217 }
218
219 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
220 {
221 char buf[50];
222 sprintf(buf, "%d", value);
223 return wxWriteResource(section, entry, buf, file);
224 }
225
226 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
227 {
228 // TODO
229 return FALSE;
230 }
231
232 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
233 {
234 char *s = NULL;
235 bool succ = wxGetResource(section, entry, (char **)&s, file);
236 if (succ)
237 {
238 *value = (float)strtod(s, NULL);
239 delete[] s;
240 return TRUE;
241 }
242 else return FALSE;
243 }
244
245 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
246 {
247 char *s = NULL;
248 bool succ = wxGetResource(section, entry, (char **)&s, file);
249 if (succ)
250 {
251 *value = strtol(s, NULL, 10);
252 delete[] s;
253 return TRUE;
254 }
255 else return FALSE;
256 }
257
258 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
259 {
260 char *s = NULL;
261 bool succ = wxGetResource(section, entry, (char **)&s, file);
262 if (succ)
263 {
264 *value = (int)strtol(s, NULL, 10);
265 delete[] s;
266 return TRUE;
267 }
268 else return FALSE;
269 }
270 #endif // wxUSE_RESOURCES
271
272 int wxBusyCursorCount = 0;
273 extern CursHandle gMacCurrentCursor ;
274 CursHandle gMacStoredActiveCursor = NULL ;
275
276 // Set the cursor to the busy cursor for all windows
277 void wxBeginBusyCursor(wxCursor *cursor)
278 {
279 wxBusyCursorCount ++;
280 if (wxBusyCursorCount == 1)
281 {
282 gMacStoredActiveCursor = gMacCurrentCursor ;
283 ::SetCursor( *::GetCursor( watchCursor ) ) ;
284 }
285 else
286 {
287 // TODO
288 }
289 }
290
291 // Restore cursor to normal
292 void wxEndBusyCursor()
293 {
294 if (wxBusyCursorCount == 0)
295 return;
296
297 wxBusyCursorCount --;
298 if (wxBusyCursorCount == 0)
299 {
300 if ( gMacStoredActiveCursor )
301 ::SetCursor( *gMacStoredActiveCursor ) ;
302 else
303 {
304 Cursor MacArrow ;
305 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
306 }
307 gMacStoredActiveCursor = NULL ;
308 }
309 }
310
311 // TRUE if we're between the above two calls
312 bool wxIsBusy()
313 {
314 return (wxBusyCursorCount > 0);
315 }
316
317 #ifndef __UNIX__
318 wxString wxMacFindFolder( short vol,
319 OSType folderType,
320 Boolean createFolder)
321 {
322 short vRefNum ;
323 long dirID ;
324 wxString strDir ;
325
326 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
327 {
328 FSSpec file ;
329 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
330 {
331 strDir = wxMacFSSpec2UnixFilename( &file ) + "/" ;
332 }
333 }
334 return strDir ;
335 }
336 #endif
337
338 #ifndef __UNIX__
339 char *wxGetUserHome (const wxString& user)
340 {
341 // TODO
342 return NULL;
343 }
344 #endif
345
346 // Check whether this window wants to process messages, e.g. Stop button
347 // in long calculations.
348 bool wxCheckForInterrupt(wxWindow *wnd)
349 {
350 // TODO
351 return FALSE;
352 }
353
354 void wxGetMousePosition( int* x, int* y )
355 {
356 Point pt ;
357
358 GetMouse( &pt ) ;
359 LocalToGlobal( &pt ) ;
360 *x = pt.h ;
361 *y = pt.v ;
362 };
363
364 // Return TRUE if we have a colour display
365 bool wxColourDisplay()
366 {
367 return TRUE;
368 }
369
370 // Returns depth of screen
371 int wxDisplayDepth()
372 {
373 Rect globRect ;
374 SetRect(&globRect, -32760, -32760, 32760, 32760);
375 GDHandle theMaxDevice;
376
377 int theDepth = 8;
378 theMaxDevice = GetMaxDevice(&globRect);
379 if (theMaxDevice != nil)
380 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
381
382 return theDepth ;
383 }
384
385 // Get size of display
386 void wxDisplaySize(int *width, int *height)
387 {
388 BitMap screenBits;
389 GetQDGlobalsScreenBits( &screenBits );
390
391 *width = screenBits.bounds.right - screenBits.bounds.left ;
392 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
393 #if TARGET_CARBON
394 SInt16 mheight ;
395 GetThemeMenuBarHeight( &mheight ) ;
396 *height -= mheight ;
397 #else
398 *height -= LMGetMBarHeight() ;
399 #endif
400 }
401
402 void wxDisplaySizeMM(int *width, int *height)
403 {
404 wxDisplaySize(width, height);
405 }
406
407 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
408 {
409 // This is supposed to return desktop dimensions minus any window
410 // manager panels, menus, taskbars, etc. If there is a way to do that
411 // for this platform please fix this function, otherwise it defaults
412 // to the entire desktop.
413 if (x) *x = 0;
414 if (y) *y = 0;
415 wxDisplaySize(width, height);
416 }
417
418 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
419 {
420 return wxGenericFindWindowAtPoint(pt);
421 }