]> git.saurik.com Git - wxWidgets.git/blob - src/mac/utils.cpp
changed wxSystemSettings::HasFrameDecorations to more general GetCapability(index)
[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 Gestalt(gestaltSystemVersion, &theSystem) ;
195 *minorVsn = (theSystem & 0xFF ) ;
196 *majorVsn = (theSystem >> 8 ) ; // are there x-platform conventions ?
197 return wxMACINTOSH;
198 }
199
200 // Reading and writing resources (eg WIN.INI, .Xdefaults)
201 #if wxUSE_RESOURCES
202 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
203 {
204 // TODO
205 return FALSE;
206 }
207
208 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
209 {
210 char buf[50];
211 sprintf(buf, "%.4f", value);
212 return wxWriteResource(section, entry, buf, file);
213 }
214
215 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
216 {
217 char buf[50];
218 sprintf(buf, "%ld", value);
219 return wxWriteResource(section, entry, buf, file);
220 }
221
222 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
223 {
224 char buf[50];
225 sprintf(buf, "%d", value);
226 return wxWriteResource(section, entry, buf, file);
227 }
228
229 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
230 {
231 // TODO
232 return FALSE;
233 }
234
235 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
236 {
237 char *s = NULL;
238 bool succ = wxGetResource(section, entry, (char **)&s, file);
239 if (succ)
240 {
241 *value = (float)strtod(s, NULL);
242 delete[] s;
243 return TRUE;
244 }
245 else return FALSE;
246 }
247
248 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
249 {
250 char *s = NULL;
251 bool succ = wxGetResource(section, entry, (char **)&s, file);
252 if (succ)
253 {
254 *value = strtol(s, NULL, 10);
255 delete[] s;
256 return TRUE;
257 }
258 else return FALSE;
259 }
260
261 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
262 {
263 char *s = NULL;
264 bool succ = wxGetResource(section, entry, (char **)&s, file);
265 if (succ)
266 {
267 *value = (int)strtol(s, NULL, 10);
268 delete[] s;
269 return TRUE;
270 }
271 else return FALSE;
272 }
273 #endif // wxUSE_RESOURCES
274
275 int wxBusyCursorCount = 0;
276 extern CursHandle gMacCurrentCursor ;
277 CursHandle gMacStoredActiveCursor = NULL ;
278
279 // Set the cursor to the busy cursor for all windows
280 void wxBeginBusyCursor(wxCursor *cursor)
281 {
282 wxBusyCursorCount ++;
283 if (wxBusyCursorCount == 1)
284 {
285 gMacStoredActiveCursor = gMacCurrentCursor ;
286 ::SetCursor( *::GetCursor( watchCursor ) ) ;
287 }
288 else
289 {
290 // TODO
291 }
292 }
293
294 // Restore cursor to normal
295 void wxEndBusyCursor()
296 {
297 if (wxBusyCursorCount == 0)
298 return;
299
300 wxBusyCursorCount --;
301 if (wxBusyCursorCount == 0)
302 {
303 if ( gMacStoredActiveCursor )
304 ::SetCursor( *gMacStoredActiveCursor ) ;
305 else
306 {
307 Cursor MacArrow ;
308 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
309 }
310 gMacStoredActiveCursor = NULL ;
311 }
312 }
313
314 // TRUE if we're between the above two calls
315 bool wxIsBusy()
316 {
317 return (wxBusyCursorCount > 0);
318 }
319
320 wxString wxMacFindFolder( short vol,
321 OSType folderType,
322 Boolean createFolder)
323 {
324 short vRefNum ;
325 long dirID ;
326 wxString strDir ;
327
328 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
329 {
330 FSSpec file ;
331 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
332 {
333 strDir = wxMacFSSpec2MacFilename( &file ) + wxFILE_SEP_PATH ;
334 }
335 }
336 return strDir ;
337 }
338
339 #ifndef __DARWIN__
340 char *wxGetUserHome (const wxString& user)
341 {
342 // TODO
343 return NULL;
344 }
345
346 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
347 {
348 if ( path.empty() )
349 return FALSE;
350
351 wxString p = path ;
352 if (p[0] == ':' ) {
353 p = wxGetCwd() + p ;
354 }
355
356 int pos = p.Find(':') ;
357 if ( pos != wxNOT_FOUND ) {
358 p = p.Mid(1,pos) ;
359 }
360
361 p = p + ":" ;
362
363 Str255 volumeName ;
364 XVolumeParam pb ;
365
366 wxMacStringToPascal( p , volumeName ) ;
367 OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ;
368 if ( err == noErr ) {
369 if ( pTotal ) {
370 (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ;
371 }
372 if ( pFree ) {
373 (*pFree) = wxLongLong( pb.ioVFreeBytes ) ;
374 }
375 }
376
377 return err == noErr ;
378 }
379 #endif
380
381 // Check whether this window wants to process messages, e.g. Stop button
382 // in long calculations.
383 bool wxCheckForInterrupt(wxWindow *wnd)
384 {
385 // TODO
386 return FALSE;
387 }
388
389 void wxGetMousePosition( int* x, int* y )
390 {
391 Point pt ;
392
393 GetMouse( &pt ) ;
394 LocalToGlobal( &pt ) ;
395 *x = pt.h ;
396 *y = pt.v ;
397 };
398
399 // Return TRUE if we have a colour display
400 bool wxColourDisplay()
401 {
402 return TRUE;
403 }
404
405 // Returns depth of screen
406 int wxDisplayDepth()
407 {
408 Rect globRect ;
409 SetRect(&globRect, -32760, -32760, 32760, 32760);
410 GDHandle theMaxDevice;
411
412 int theDepth = 8;
413 theMaxDevice = GetMaxDevice(&globRect);
414 if (theMaxDevice != nil)
415 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
416
417 return theDepth ;
418 }
419
420 // Get size of display
421 void wxDisplaySize(int *width, int *height)
422 {
423 BitMap screenBits;
424 GetQDGlobalsScreenBits( &screenBits );
425
426 *width = screenBits.bounds.right - screenBits.bounds.left ;
427 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
428 #if TARGET_CARBON
429 SInt16 mheight ;
430 GetThemeMenuBarHeight( &mheight ) ;
431 *height -= mheight ;
432 #else
433 *height -= LMGetMBarHeight() ;
434 #endif
435 }
436
437 void wxDisplaySizeMM(int *width, int *height)
438 {
439 wxDisplaySize(width, height);
440 }
441
442 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
443 {
444 // This is supposed to return desktop dimensions minus any window
445 // manager panels, menus, taskbars, etc. If there is a way to do that
446 // for this platform please fix this function, otherwise it defaults
447 // to the entire desktop.
448 if (x) *x = 0;
449 if (y) *y = 0;
450 wxDisplaySize(width, height);
451 }
452
453 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
454 {
455 return wxGenericFindWindowAtPoint(pt);
456 }