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