]> git.saurik.com Git - wxWidgets.git/blame - src/mac/utils.cpp
added generic dir dlg
[wxWidgets.git] / src / mac / utils.cpp
CommitLineData
e9576ca5
SC
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"
2f1ae414 21#include "wx/mac/uma.h"
e9576ca5
SC
22
23#include <ctype.h>
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdarg.h>
29
03e11df5
GD
30#ifndef __UNIX__
31// defined in unix/utilsunx.cpp for Mac OS X
32
2f1ae414
SC
33// get full hostname (with domain name if possible)
34bool wxGetFullHostName(wxChar *buf, int maxSize)
35{
36 return wxGetHostName(buf, maxSize);
37}
38
39// Get hostname only (without domain name)
e9576ca5
SC
40bool wxGetHostName(char *buf, int maxSize)
41{
0a67a93b
SC
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;
e9576ca5
SC
65}
66
67// Get user ID e.g. jacs
68bool wxGetUserId(char *buf, int maxSize)
69{
0a67a93b 70 return wxGetUserName( buf , maxSize ) ;
e9576ca5
SC
71}
72
5b781a67
SC
73const wxChar* wxGetHomeDir(wxString *pstr)
74{
75 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
76 return pstr->c_str() ;
77}
78
e9576ca5
SC
79// Get user name e.g. AUTHOR
80bool wxGetUserName(char *buf, int maxSize)
81{
0a67a93b
SC
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;
e9576ca5
SC
105}
106
107int wxKill(long pid, int sig)
108{
109 // TODO
110 return 0;
111}
112
113//
114// Execute a program in an Interactive Shell
115//
116bool 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)
123long wxGetFreeMemory()
124{
0a67a93b
SC
125 return FreeMem() ;
126}
127
128void wxUsleep(unsigned long milliseconds)
129{
130 clock_t start = clock() ;
131 do
132 {
133 YieldToAnyThread() ;
134 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
e9576ca5
SC
135}
136
137void wxSleep(int nSecs)
138{
0a67a93b 139 wxUsleep(1000*nSecs);
e9576ca5
SC
140}
141
142// Consume all events until no more left
143void wxFlushEvents()
144{
145}
146
147// Output a debug message, in a system dependent fashion.
148void 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
0a67a93b
SC
158 vsprintf(buffer,fmt,ap) ;
159 strcat(buffer,";g") ;
9ff647cf
SC
160 c2pstr(buffer) ;
161 DebugStr((unsigned char*) buffer) ;
e9576ca5
SC
162
163 va_end(ap);
164}
165
166// Non-fatal error: pop up message box and (possibly) continue
167void wxError(const wxString& msg, const wxString& title)
168{
0a67a93b
SC
169 wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg);
170 if (wxMessageBox(wxBuffer, title, wxYES_NO) == wxID_NO )
e9576ca5
SC
171 wxExit();
172}
173
174// Fatal error: pop up message box and abort
175void wxFatalError(const wxString& msg, const wxString& title)
176{
0a67a93b
SC
177 wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg);
178 wxMessageBox(wxBuffer);
179 wxExit();
e9576ca5 180}
03e11df5 181#endif // !__UNIX__
e9576ca5
SC
182
183// Emit a beeeeeep
184void wxBell()
185{
0a67a93b 186 SysBeep(30);
e9576ca5
SC
187}
188
189int wxGetOsVersion(int *majorVsn, int *minorVsn)
190{
0a67a93b
SC
191 long theSystem ;
192 Gestalt(gestaltSystemVersion, &theSystem) ;
193 *minorVsn = (theSystem & 0xFF ) ;
194 *majorVsn = (theSystem >> 8 ) ; // are there x-platform conventions ?
195 return wxMACINTOSH;
e9576ca5
SC
196}
197
198// Reading and writing resources (eg WIN.INI, .Xdefaults)
199#if wxUSE_RESOURCES
200bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
201{
202 // TODO
203 return FALSE;
204}
205
206bool 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
213bool 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
220bool 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
227bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
228{
229 // TODO
230 return FALSE;
231}
232
233bool 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
246bool 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
259bool 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);
ec5d7799 266 delete[] s;
e9576ca5
SC
267 return TRUE;
268 }
269 else return FALSE;
270}
271#endif // wxUSE_RESOURCES
272
519cb848
SC
273int wxBusyCursorCount = 0;
274extern CursHandle gMacCurrentCursor ;
275CursHandle gMacStoredActiveCursor = NULL ;
e9576ca5
SC
276
277// Set the cursor to the busy cursor for all windows
278void wxBeginBusyCursor(wxCursor *cursor)
279{
280 wxBusyCursorCount ++;
281 if (wxBusyCursorCount == 1)
282 {
519cb848
SC
283 gMacStoredActiveCursor = gMacCurrentCursor ;
284 ::SetCursor( *::GetCursor( watchCursor ) ) ;
e9576ca5
SC
285 }
286 else
287 {
288 // TODO
289 }
290}
291
292// Restore cursor to normal
293void wxEndBusyCursor()
294{
295 if (wxBusyCursorCount == 0)
296 return;
ec5d7799 297
e9576ca5
SC
298 wxBusyCursorCount --;
299 if (wxBusyCursorCount == 0)
300 {
519cb848
SC
301 if ( gMacStoredActiveCursor )
302 ::SetCursor( *gMacStoredActiveCursor ) ;
303 else
2f1ae414
SC
304 {
305 Cursor MacArrow ;
306 ::SetCursor( GetQDGlobalsArrow( &MacArrow ) ) ;
307 }
519cb848 308 gMacStoredActiveCursor = NULL ;
e9576ca5
SC
309 }
310}
311
312// TRUE if we're between the above two calls
313bool wxIsBusy()
314{
315 return (wxBusyCursorCount > 0);
ec5d7799 316}
e9576ca5 317
5fde6fcc 318#ifndef __UNIX__
2f1ae414
SC
319wxString wxMacFindFolder( short vol,
320 OSType folderType,
321 Boolean createFolder)
322{
323 short vRefNum ;
324 long dirID ;
325 wxString strDir ;
ec5d7799 326
2f1ae414
SC
327 if ( FindFolder( vol, folderType, createFolder, &vRefNum, &dirID) == noErr)
328 {
329 FSSpec file ;
330 if ( FSMakeFSSpec( vRefNum , dirID , "\p" , &file ) == noErr )
331 {
332 strDir = wxMacFSSpec2UnixFilename( &file ) + "/" ;
333 }
334 }
335 return strDir ;
336}
5fde6fcc 337#endif
2f1ae414 338
03e11df5 339#ifndef __UNIX__
e9576ca5
SC
340char *wxGetUserHome (const wxString& user)
341{
342 // TODO
343 return NULL;
344}
03e11df5 345#endif
e9576ca5
SC
346
347// Check whether this window wants to process messages, e.g. Stop button
348// in long calculations.
349bool wxCheckForInterrupt(wxWindow *wnd)
350{
351 // TODO
352 return FALSE;
353}
354
355void wxGetMousePosition( int* x, int* y )
356{
519cb848 357 Point pt ;
ec5d7799 358
519cb848
SC
359 GetMouse( &pt ) ;
360 LocalToGlobal( &pt ) ;
361 *x = pt.h ;
362 *y = pt.v ;
e9576ca5
SC
363};
364
365// Return TRUE if we have a colour display
366bool wxColourDisplay()
367{
e9576ca5
SC
368 return TRUE;
369}
370
371// Returns depth of screen
372int wxDisplayDepth()
373{
ec5d7799 374 Rect globRect ;
2f1ae414
SC
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;
ec5d7799 382
2f1ae414 383 return theDepth ;
e9576ca5
SC
384}
385
386// Get size of display
387void wxDisplaySize(int *width, int *height)
388{
2f1ae414
SC
389 BitMap screenBits;
390 GetQDGlobalsScreenBits( &screenBits );
391
392 *width = screenBits.bounds.right - screenBits.bounds.left ;
ec5d7799 393 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
03e11df5 394#if TARGET_CARBON
2f1ae414
SC
395 SInt16 mheight ;
396 GetThemeMenuBarHeight( &mheight ) ;
397 *height -= mheight ;
398#else
399 *height -= LMGetMBarHeight() ;
03e11df5 400#endif
e9576ca5
SC
401}
402
5fde6fcc
GD
403void wxDisplaySizeMM(int *width, int *height)
404{
405 wxDisplaySize(width, height);
406}
407
ec5d7799
RD
408void 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
57591e0e
JS
419wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
420{
421 return wxGenericFindWindowAtPoint(pt);
422}