]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utils.cpp | |
3 | // Purpose: Various utilities | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
e40298d5 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | // Note: this is done in utilscmn.cpp now. | |
e9576ca5 SC |
14 | // #pragma implementation "utils.h" |
15 | #endif | |
16 | ||
17 | #include "wx/setup.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/app.h" | |
2bf2d09e | 20 | #include "wx/apptrait.h" |
b6ed2b86 VZ |
21 | |
22 | #if wxUSE_GUI | |
23 | #include "wx/mac/uma.h" | |
24 | #endif | |
e9576ca5 SC |
25 | |
26 | #include <ctype.h> | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <stdarg.h> | |
32 | ||
2d4e4f80 GD |
33 | #ifdef __DARWIN__ |
34 | # include "MoreFilesX.h" | |
35 | #else | |
36 | # include "MoreFiles.h" | |
37 | # include "MoreFilesExtras.h" | |
38 | #endif | |
518af45b | 39 | |
66a09d47 SC |
40 | #ifndef __DARWIN__ |
41 | #include <Threads.h> | |
42 | #include <Sound.h> | |
43 | #endif | |
44 | ||
427ff662 SC |
45 | #include "ATSUnicode.h" |
46 | #include "TextCommon.h" | |
47 | #include "TextEncodingConverter.h" | |
48 | ||
a434b43f VZ |
49 | #if defined(__WXMAC__) |
50 | #include "wx/mac/private.h" // includes mac headers | |
51 | #endif | |
52 | ||
53 | #if defined(__MWERKS__) && wxUSE_UNICODE | |
54 | #include <wtime.h> | |
55 | #endif | |
56 | ||
b6ed2b86 | 57 | #if wxUSE_BASE |
a434b43f | 58 | |
f5c6eb5c | 59 | #ifndef __DARWIN__ |
03e11df5 GD |
60 | // defined in unix/utilsunx.cpp for Mac OS X |
61 | ||
2f1ae414 SC |
62 | // get full hostname (with domain name if possible) |
63 | bool wxGetFullHostName(wxChar *buf, int maxSize) | |
64 | { | |
65 | return wxGetHostName(buf, maxSize); | |
66 | } | |
67 | ||
68 | // Get hostname only (without domain name) | |
427ff662 | 69 | bool wxGetHostName(wxChar *buf, int maxSize) |
e9576ca5 | 70 | { |
e40298d5 JS |
71 | // Gets Chooser name of user by examining a System resource. |
72 | ||
73 | const short kComputerNameID = -16413; | |
5be55d56 | 74 | |
e40298d5 JS |
75 | short oldResFile = CurResFile() ; |
76 | UseResFile(0); | |
77 | StringHandle chooserName = (StringHandle)::GetString(kComputerNameID); | |
78 | UseResFile(oldResFile); | |
79 | ||
80 | if (chooserName && *chooserName) | |
81 | { | |
427ff662 SC |
82 | HLock( (Handle) chooserName ) ; |
83 | wxString name = wxMacMakeStringFromPascal( *chooserName ) ; | |
84 | HUnlock( (Handle) chooserName ) ; | |
85 | ReleaseResource( (Handle) chooserName ) ; | |
86 | wxStrncpy( buf , name , maxSize - 1 ) ; | |
e40298d5 JS |
87 | } |
88 | else | |
89 | buf[0] = 0 ; | |
0a67a93b SC |
90 | |
91 | return TRUE; | |
e9576ca5 SC |
92 | } |
93 | ||
94 | // Get user ID e.g. jacs | |
427ff662 | 95 | bool wxGetUserId(wxChar *buf, int maxSize) |
e9576ca5 | 96 | { |
0a67a93b | 97 | return wxGetUserName( buf , maxSize ) ; |
e9576ca5 SC |
98 | } |
99 | ||
5b781a67 SC |
100 | const wxChar* wxGetHomeDir(wxString *pstr) |
101 | { | |
e40298d5 JS |
102 | *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ; |
103 | return pstr->c_str() ; | |
5b781a67 SC |
104 | } |
105 | ||
a31a5f85 | 106 | // Get user name e.g. Stefan Csomor |
427ff662 | 107 | bool wxGetUserName(wxChar *buf, int maxSize) |
e9576ca5 | 108 | { |
e40298d5 JS |
109 | // Gets Chooser name of user by examining a System resource. |
110 | ||
111 | const short kChooserNameID = -16096; | |
5be55d56 | 112 | |
e40298d5 JS |
113 | short oldResFile = CurResFile() ; |
114 | UseResFile(0); | |
115 | StringHandle chooserName = (StringHandle)::GetString(kChooserNameID); | |
116 | UseResFile(oldResFile); | |
117 | ||
118 | if (chooserName && *chooserName) | |
119 | { | |
427ff662 SC |
120 | HLock( (Handle) chooserName ) ; |
121 | wxString name = wxMacMakeStringFromPascal( *chooserName ) ; | |
122 | HUnlock( (Handle) chooserName ) ; | |
123 | ReleaseResource( (Handle) chooserName ) ; | |
124 | wxStrncpy( buf , name , maxSize - 1 ) ; | |
e40298d5 JS |
125 | } |
126 | else | |
127 | buf[0] = 0 ; | |
0a67a93b SC |
128 | |
129 | return TRUE; | |
e9576ca5 SC |
130 | } |
131 | ||
5dbb17e2 | 132 | int wxKill(long pid, wxSignal sig , wxKillError *rc ) |
e9576ca5 SC |
133 | { |
134 | // TODO | |
135 | return 0; | |
136 | } | |
137 | ||
5dbb17e2 SC |
138 | WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value) |
139 | { | |
e40298d5 JS |
140 | // TODO : under classic there is no environement support, under X yes |
141 | return false ; | |
5dbb17e2 SC |
142 | } |
143 | ||
144 | // set the env var name to the given value, return TRUE on success | |
145 | WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value) | |
146 | { | |
e40298d5 JS |
147 | // TODO : under classic there is no environement support, under X yes |
148 | return false ; | |
5dbb17e2 SC |
149 | } |
150 | ||
e9576ca5 SC |
151 | // |
152 | // Execute a program in an Interactive Shell | |
153 | // | |
154 | bool wxShell(const wxString& command) | |
155 | { | |
156 | // TODO | |
157 | return FALSE; | |
158 | } | |
159 | ||
5be55d56 | 160 | // Shutdown or reboot the PC |
f6ba47d9 VZ |
161 | bool wxShutdown(wxShutdownFlags wFlags) |
162 | { | |
163 | // TODO | |
164 | return FALSE; | |
165 | } | |
166 | ||
e9576ca5 SC |
167 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) |
168 | long wxGetFreeMemory() | |
169 | { | |
0a67a93b SC |
170 | return FreeMem() ; |
171 | } | |
172 | ||
173 | void wxUsleep(unsigned long milliseconds) | |
174 | { | |
e7e1b01e | 175 | clock_t start = clock() ; |
5be55d56 | 176 | do |
e7e1b01e | 177 | { |
e40298d5 | 178 | YieldToAnyThread() ; |
2a616188 | 179 | } while( clock() - start < milliseconds / 1000.0 * CLOCKS_PER_SEC ) ; |
e9576ca5 SC |
180 | } |
181 | ||
182 | void wxSleep(int nSecs) | |
183 | { | |
0a67a93b | 184 | wxUsleep(1000*nSecs); |
e9576ca5 SC |
185 | } |
186 | ||
187 | // Consume all events until no more left | |
188 | void wxFlushEvents() | |
189 | { | |
190 | } | |
191 | ||
f5c6eb5c | 192 | #endif // !__DARWIN__ |
e9576ca5 SC |
193 | |
194 | // Emit a beeeeeep | |
195 | void wxBell() | |
196 | { | |
0a67a93b | 197 | SysBeep(30); |
e9576ca5 SC |
198 | } |
199 | ||
536732e4 | 200 | int wxAppTraits::GetOSVersion(int *majorVsn, int *minorVsn) |
e9576ca5 | 201 | { |
ff8fda36 | 202 | long theSystem ; |
5be55d56 | 203 | |
ff8fda36 | 204 | // are there x-platform conventions ? |
5be55d56 | 205 | |
ff8fda36 GD |
206 | Gestalt(gestaltSystemVersion, &theSystem) ; |
207 | if (minorVsn != NULL) { | |
e40298d5 | 208 | *minorVsn = (theSystem & 0xFF ) ; |
ff8fda36 GD |
209 | } |
210 | if (majorVsn != NULL) { | |
e40298d5 | 211 | *majorVsn = (theSystem >> 8 ) ; |
ff8fda36 GD |
212 | } |
213 | #ifdef __DARWIN__ | |
214 | return wxMAC_DARWIN; | |
215 | #else | |
216 | return wxMAC; | |
217 | #endif | |
e9576ca5 SC |
218 | } |
219 | ||
b6ed2b86 VZ |
220 | #endif // wxUSE_BASE |
221 | ||
222 | #if wxUSE_GUI | |
536732e4 | 223 | |
e9576ca5 SC |
224 | // Reading and writing resources (eg WIN.INI, .Xdefaults) |
225 | #if wxUSE_RESOURCES | |
226 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) | |
227 | { | |
228 | // TODO | |
229 | return FALSE; | |
230 | } | |
231 | ||
232 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
233 | { | |
427ff662 SC |
234 | wxString buf; |
235 | buf.Printf(wxT("%.4f"), value); | |
236 | ||
e40298d5 | 237 | return wxWriteResource(section, entry, buf, file); |
e9576ca5 SC |
238 | } |
239 | ||
240 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
241 | { | |
427ff662 SC |
242 | wxString buf; |
243 | buf.Printf(wxT("%ld"), value); | |
244 | ||
e40298d5 | 245 | return wxWriteResource(section, entry, buf, file); |
e9576ca5 SC |
246 | } |
247 | ||
248 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
249 | { | |
427ff662 SC |
250 | wxString buf; |
251 | buf.Printf(wxT("%d"), value); | |
252 | ||
e40298d5 | 253 | return wxWriteResource(section, entry, buf, file); |
e9576ca5 SC |
254 | } |
255 | ||
256 | bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file) | |
257 | { | |
258 | // TODO | |
259 | return FALSE; | |
260 | } | |
261 | ||
262 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
263 | { | |
e40298d5 JS |
264 | char *s = NULL; |
265 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
266 | if (succ) | |
267 | { | |
268 | *value = (float)strtod(s, NULL); | |
269 | delete[] s; | |
270 | return TRUE; | |
271 | } | |
272 | else return FALSE; | |
e9576ca5 SC |
273 | } |
274 | ||
275 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
276 | { | |
e40298d5 JS |
277 | char *s = NULL; |
278 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
279 | if (succ) | |
280 | { | |
281 | *value = strtol(s, NULL, 10); | |
282 | delete[] s; | |
283 | return TRUE; | |
284 | } | |
285 | else return FALSE; | |
e9576ca5 SC |
286 | } |
287 | ||
288 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
289 | { | |
e40298d5 JS |
290 | char *s = NULL; |
291 | bool succ = wxGetResource(section, entry, (char **)&s, file); | |
292 | if (succ) | |
293 | { | |
294 | *value = (int)strtol(s, NULL, 10); | |
295 | delete[] s; | |
296 | return TRUE; | |
297 | } | |
298 | else return FALSE; | |
e9576ca5 SC |
299 | } |
300 | #endif // wxUSE_RESOURCES | |
301 | ||
6b57b49a | 302 | int gs_wxBusyCursorCount = 0; |
e40298d5 JS |
303 | extern wxCursor gMacCurrentCursor ; |
304 | wxCursor gMacStoredActiveCursor ; | |
e9576ca5 SC |
305 | |
306 | // Set the cursor to the busy cursor for all windows | |
307 | void wxBeginBusyCursor(wxCursor *cursor) | |
308 | { | |
e40298d5 JS |
309 | if (gs_wxBusyCursorCount++ == 0) |
310 | { | |
311 | gMacStoredActiveCursor = gMacCurrentCursor ; | |
312 | cursor->MacInstall() ; | |
313 | } | |
314 | //else: nothing to do, already set | |
e9576ca5 SC |
315 | } |
316 | ||
317 | // Restore cursor to normal | |
318 | void wxEndBusyCursor() | |
319 | { | |
6b57b49a | 320 | wxCHECK_RET( gs_wxBusyCursorCount > 0, |
e40298d5 | 321 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); |
5be55d56 | 322 | |
e40298d5 JS |
323 | if (--gs_wxBusyCursorCount == 0) |
324 | { | |
325 | gMacStoredActiveCursor.MacInstall() ; | |
326 | gMacStoredActiveCursor = wxNullCursor ; | |
327 | } | |
e9576ca5 SC |
328 | } |
329 | ||
330 | // TRUE if we're between the above two calls | |
331 | bool wxIsBusy() | |
332 | { | |
e40298d5 | 333 | return (gs_wxBusyCursorCount > 0); |
ec5d7799 | 334 | } |
e9576ca5 | 335 | |
e7e1b01e | 336 | wxString wxMacFindFolder( short vol, |
e40298d5 JS |
337 | OSType folderType, |
338 | Boolean createFolder) | |
2f1ae414 | 339 | { |
2d4e4f80 GD |
340 | short vRefNum ; |
341 | long dirID ; | |
342 | wxString strDir ; | |
5be55d56 | 343 | |
2d4e4f80 GD |
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 ; | |
2f1ae414 SC |
353 | } |
354 | ||
e9576ca5 SC |
355 | // Check whether this window wants to process messages, e.g. Stop button |
356 | // in long calculations. | |
357 | bool wxCheckForInterrupt(wxWindow *wnd) | |
358 | { | |
359 | // TODO | |
360 | return FALSE; | |
361 | } | |
362 | ||
363 | void wxGetMousePosition( int* x, int* y ) | |
364 | { | |
519cb848 | 365 | Point pt ; |
ec5d7799 | 366 | |
519cb848 SC |
367 | GetMouse( &pt ) ; |
368 | LocalToGlobal( &pt ) ; | |
369 | *x = pt.h ; | |
370 | *y = pt.v ; | |
e9576ca5 SC |
371 | }; |
372 | ||
373 | // Return TRUE if we have a colour display | |
374 | bool wxColourDisplay() | |
375 | { | |
e9576ca5 SC |
376 | return TRUE; |
377 | } | |
378 | ||
379 | // Returns depth of screen | |
380 | int wxDisplayDepth() | |
381 | { | |
e40298d5 JS |
382 | Rect globRect ; |
383 | SetRect(&globRect, -32760, -32760, 32760, 32760); | |
384 | GDHandle theMaxDevice; | |
2f1ae414 | 385 | |
e40298d5 JS |
386 | int theDepth = 8; |
387 | theMaxDevice = GetMaxDevice(&globRect); | |
388 | if (theMaxDevice != nil) | |
389 | theDepth = (**(**theMaxDevice).gdPMap).pixelSize; | |
ec5d7799 | 390 | |
e40298d5 | 391 | return theDepth ; |
e9576ca5 SC |
392 | } |
393 | ||
394 | // Get size of display | |
395 | void wxDisplaySize(int *width, int *height) | |
396 | { | |
e40298d5 JS |
397 | BitMap screenBits; |
398 | GetQDGlobalsScreenBits( &screenBits ); | |
5be55d56 VZ |
399 | |
400 | if (width != NULL) { | |
e8ca7105 GD |
401 | *width = screenBits.bounds.right - screenBits.bounds.left ; |
402 | } | |
5be55d56 | 403 | if (height != NULL) { |
e8ca7105 GD |
404 | *height = screenBits.bounds.bottom - screenBits.bounds.top ; |
405 | } | |
e9576ca5 SC |
406 | } |
407 | ||
5fde6fcc GD |
408 | void wxDisplaySizeMM(int *width, int *height) |
409 | { | |
5b028d57 SC |
410 | wxDisplaySize(width, height); |
411 | // on mac 72 is fixed (at least now ;-) | |
412 | float cvPt2Mm = 25.4 / 72; | |
e8ca7105 | 413 | |
5be55d56 | 414 | if (width != NULL) { |
e8ca7105 GD |
415 | *width = int( *width * cvPt2Mm ); |
416 | } | |
5be55d56 | 417 | if (height != NULL) { |
e8ca7105 GD |
418 | *height = int( *height * cvPt2Mm ); |
419 | } | |
5fde6fcc GD |
420 | } |
421 | ||
ec5d7799 RD |
422 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) |
423 | { | |
2d4e4f80 GD |
424 | BitMap screenBits; |
425 | GetQDGlobalsScreenBits( &screenBits ); | |
7cfebe05 | 426 | |
ec5d7799 RD |
427 | if (x) *x = 0; |
428 | if (y) *y = 0; | |
7cfebe05 | 429 | |
5be55d56 | 430 | if (width != NULL) { |
e8ca7105 GD |
431 | *width = screenBits.bounds.right - screenBits.bounds.left ; |
432 | } | |
5be55d56 | 433 | if (height != NULL) { |
e8ca7105 GD |
434 | *height = screenBits.bounds.bottom - screenBits.bounds.top ; |
435 | } | |
7cfebe05 | 436 | |
2d4e4f80 GD |
437 | SInt16 mheight ; |
438 | #if TARGET_CARBON | |
439 | GetThemeMenuBarHeight( &mheight ) ; | |
440 | #else | |
7cfebe05 | 441 | mheight = LMGetMBarHeight() ; |
2d4e4f80 | 442 | #endif |
5be55d56 | 443 | if (height != NULL) { |
e8ca7105 GD |
444 | *height -= mheight ; |
445 | } | |
446 | if (y) | |
2d4e4f80 | 447 | *y = mheight ; |
ec5d7799 RD |
448 | } |
449 | ||
57591e0e JS |
450 | wxWindow* wxFindWindowAtPoint(const wxPoint& pt) |
451 | { | |
452 | return wxGenericFindWindowAtPoint(pt); | |
453 | } | |
5dbb17e2 | 454 | |
b6ed2b86 VZ |
455 | #endif // wxUSE_GUI |
456 | ||
457 | #if wxUSE_BASE | |
458 | ||
5dbb17e2 SC |
459 | wxString wxGetOsDescription() |
460 | { | |
6e73695c GD |
461 | #ifdef WXWIN_OS_DESCRIPTION |
462 | // use configure generated description if available | |
463 | return wxString("MacOS (") + WXWIN_OS_DESCRIPTION + wxString(")"); | |
464 | #else | |
427ff662 | 465 | return wxT("MacOS") ; //TODO:define further |
6e73695c GD |
466 | #endif |
467 | } | |
468 | ||
b6ed2b86 VZ |
469 | #ifndef __DARWIN__ |
470 | wxChar *wxGetUserHome (const wxString& user) | |
471 | { | |
472 | // TODO | |
473 | return NULL; | |
474 | } | |
475 | ||
476 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) | |
477 | { | |
478 | if ( path.empty() ) | |
479 | return FALSE; | |
480 | ||
481 | wxString p = path ; | |
482 | if (p[0] == ':' ) { | |
483 | p = wxGetCwd() + p ; | |
484 | } | |
485 | ||
486 | int pos = p.Find(':') ; | |
487 | if ( pos != wxNOT_FOUND ) { | |
488 | p = p.Mid(1,pos) ; | |
489 | } | |
490 | ||
491 | p = p + wxT(":") ; | |
492 | ||
493 | Str255 volumeName ; | |
494 | XVolumeParam pb ; | |
495 | ||
496 | wxMacStringToPascal( p , volumeName ) ; | |
497 | OSErr err = XGetVolumeInfoNoName( volumeName , 0 , &pb ) ; | |
498 | if ( err == noErr ) { | |
499 | if ( pTotal ) { | |
500 | (*pTotal) = wxLongLong( pb.ioVTotalBytes ) ; | |
501 | } | |
502 | if ( pFree ) { | |
503 | (*pFree) = wxLongLong( pb.ioVFreeBytes ) ; | |
504 | } | |
505 | } | |
506 | ||
507 | return err == noErr ; | |
508 | } | |
509 | #endif // !__DARWIN__ | |
510 | ||
511 | #endif // wxUSE_BASE | |
512 | ||
513 | #if wxUSE_GUI | |
514 | ||
3d963f81 SC |
515 | //--------------------------------------------------------------------------- |
516 | // wxMac Specific utility functions | |
517 | //--------------------------------------------------------------------------- | |
518 | ||
4c200e8d SC |
519 | char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" |
520 | "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" | |
521 | "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf" | |
522 | "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf" | |
523 | "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf" | |
524 | "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ; | |
525 | ||
526 | char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8" | |
527 | "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC" | |
528 | "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8" | |
529 | "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8" | |
530 | "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C" | |
531 | "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ; | |
532 | ||
533 | void wxMacConvertFromPC( const char *from , char *to , int len ) | |
534 | { | |
535 | char *c ; | |
536 | if ( from == to ) | |
537 | { | |
538 | for( int i = 0 ; i < len ; ++ i ) | |
539 | { | |
540 | c = strchr( StringANSI , *from ) ; | |
541 | if ( c != NULL ) | |
542 | { | |
543 | *to = StringMac[ c - StringANSI] ; | |
544 | } | |
545 | ++to ; | |
546 | ++from ; | |
547 | } | |
548 | } | |
549 | else | |
550 | { | |
551 | for( int i = 0 ; i < len ; ++ i ) | |
552 | { | |
553 | c = strchr( StringANSI , *from ) ; | |
554 | if ( c != NULL ) | |
555 | { | |
556 | *to = StringMac[ c - StringANSI] ; | |
557 | } | |
558 | else | |
559 | { | |
560 | *to = *from ; | |
561 | } | |
562 | ++to ; | |
563 | ++from ; | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
568 | void wxMacConvertToPC( const char *from , char *to , int len ) | |
569 | { | |
570 | char *c ; | |
571 | if ( from == to ) | |
572 | { | |
573 | for( int i = 0 ; i < len ; ++ i ) | |
574 | { | |
575 | c = strchr( StringMac , *from ) ; | |
576 | if ( c != NULL ) | |
577 | { | |
578 | *to = StringANSI[ c - StringMac] ; | |
579 | } | |
580 | ++to ; | |
581 | ++from ; | |
582 | } | |
583 | } | |
584 | else | |
585 | { | |
586 | for( int i = 0 ; i < len ; ++ i ) | |
587 | { | |
588 | c = strchr( StringMac , *from ) ; | |
589 | if ( c != NULL ) | |
590 | { | |
591 | *to = StringANSI[ c - StringMac] ; | |
592 | } | |
593 | else | |
594 | { | |
595 | *to = *from ; | |
596 | } | |
597 | ++to ; | |
598 | ++from ; | |
599 | } | |
600 | } | |
601 | } | |
602 | ||
427ff662 SC |
603 | TECObjectRef s_TECNativeCToUnicode = NULL ; |
604 | TECObjectRef s_TECUnicodeToNativeC = NULL ; | |
c0ef107e SC |
605 | TECObjectRef s_TECPlatformToNativeC = NULL ; |
606 | TECObjectRef s_TECNativeCToPlatform = NULL ; | |
5be55d56 | 607 | void wxMacSetupConverters() |
4c200e8d | 608 | { |
5be55d56 VZ |
609 | // if we assume errors are happening here we need low level debugging |
610 | // since the high level assert will use the encoders that are not yet | |
611 | // setup... | |
612 | const int kEncoding = wxApp::s_macDefaultEncodingIsPC | |
613 | ? (int)kTextEncodingWindowsLatin1 | |
614 | : (int)kTextEncodingMacRoman; | |
615 | ||
427ff662 | 616 | OSStatus status = noErr ; |
5be55d56 VZ |
617 | status = TECCreateConverter(&s_TECNativeCToUnicode, |
618 | kEncoding, | |
619 | kTextEncodingUnicodeDefault); | |
427ff662 SC |
620 | |
621 | ||
5be55d56 VZ |
622 | status = TECCreateConverter(&s_TECUnicodeToNativeC, |
623 | kTextEncodingUnicodeDefault, | |
624 | kEncoding); | |
427ff662 | 625 | |
c0ef107e | 626 | if ( wxApp::s_macDefaultEncodingIsPC ) |
427ff662 | 627 | { |
5be55d56 VZ |
628 | status = TECCreateConverter(&s_TECPlatformToNativeC, |
629 | kTextEncodingMacRoman, | |
630 | kTextEncodingWindowsLatin1); | |
631 | ||
427ff662 | 632 | |
5be55d56 VZ |
633 | status = TECCreateConverter(&s_TECNativeCToPlatform, |
634 | kTextEncodingWindowsLatin1, | |
635 | kTextEncodingMacRoman); | |
427ff662 SC |
636 | } |
637 | } | |
638 | ||
639 | void wxMacCleanupConverters() | |
640 | { | |
641 | OSStatus status = noErr ; | |
642 | status = TECDisposeConverter(s_TECNativeCToUnicode); | |
643 | ||
644 | status = TECDisposeConverter(s_TECUnicodeToNativeC); | |
645 | ||
c0ef107e | 646 | status = TECDisposeConverter(s_TECPlatformToNativeC); |
427ff662 | 647 | |
c0ef107e | 648 | status = TECDisposeConverter(s_TECNativeCToPlatform); |
427ff662 SC |
649 | } |
650 | ||
5be55d56 | 651 | wxWCharBuffer wxMacStringToWString( const wxString &from ) |
427ff662 SC |
652 | { |
653 | #if wxUSE_UNICODE | |
654 | wxWCharBuffer result( from.wc_str() ) ; | |
655 | #else | |
656 | OSStatus status = noErr ; | |
657 | ByteCount byteOutLen ; | |
658 | ByteCount byteInLen = from.Length() ; | |
659 | ByteCount byteBufferLen = byteInLen *2 ; | |
660 | wxWCharBuffer result( from.Length() ) ; | |
661 | status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from.c_str() , byteInLen, &byteInLen, | |
662 | (TextPtr)result.data(), byteBufferLen, &byteOutLen); | |
663 | result.data()[byteOutLen/2] = 0 ; | |
664 | #endif | |
665 | return result ; | |
666 | } | |
667 | ||
5be55d56 | 668 | wxString wxMacMakeStringFromCString( const char * from , int len ) |
427ff662 SC |
669 | { |
670 | OSStatus status = noErr ; | |
e40298d5 | 671 | wxString result ; |
427ff662 SC |
672 | wxChar* buf = result.GetWriteBuf( len ) ; |
673 | #if wxUSE_UNICODE | |
674 | ByteCount byteOutLen ; | |
675 | ByteCount byteInLen = len ; | |
676 | ByteCount byteBufferLen = len *2 ; | |
677 | ||
678 | status = TECConvertText(s_TECNativeCToUnicode, (ConstTextPtr)from , byteInLen, &byteInLen, | |
679 | (TextPtr)buf, byteBufferLen, &byteOutLen); | |
680 | #else | |
c0ef107e | 681 | if ( !wxApp::s_macDefaultEncodingIsPC ) |
427ff662 SC |
682 | memcpy( buf , from , len ) ; |
683 | else | |
684 | { | |
427ff662 SC |
685 | ByteCount byteOutLen ; |
686 | ByteCount byteInLen = len ; | |
687 | ByteCount byteBufferLen = byteInLen ; | |
688 | ||
c0ef107e | 689 | status = TECConvertText(s_TECPlatformToNativeC, (ConstTextPtr)from , byteInLen, &byteInLen, |
427ff662 SC |
690 | (TextPtr)buf, byteBufferLen, &byteOutLen); |
691 | } | |
692 | #endif | |
693 | buf[len] = 0 ; | |
694 | result.UngetWriteBuf() ; | |
695 | return result ; | |
696 | } | |
697 | ||
698 | wxString wxMacMakeStringFromCString( const char * from ) | |
699 | { | |
700 | return wxMacMakeStringFromCString( from , strlen(from) ) ; | |
701 | } | |
702 | ||
5be55d56 | 703 | wxCharBuffer wxMacStringToCString( const wxString &from ) |
427ff662 SC |
704 | { |
705 | #if wxUSE_UNICODE | |
706 | OSStatus status = noErr ; | |
707 | ByteCount byteOutLen ; | |
708 | ByteCount byteInLen = from.Length() * 2 ; | |
709 | ByteCount byteBufferLen = from.Length() ; | |
710 | wxCharBuffer result( from.Length() ) ; | |
711 | status = TECConvertText(s_TECUnicodeToNativeC , (ConstTextPtr)from.wc_str() , byteInLen, &byteInLen, | |
712 | (TextPtr)result.data(), byteBufferLen, &byteOutLen); | |
713 | return result ; | |
714 | #else | |
c0ef107e | 715 | if ( !wxApp::s_macDefaultEncodingIsPC ) |
427ff662 SC |
716 | return wxCharBuffer( from.c_str() ) ; |
717 | else | |
718 | { | |
719 | wxCharBuffer result( from.Length() ) ; | |
720 | OSStatus status = noErr ; | |
721 | ByteCount byteOutLen ; | |
722 | ByteCount byteInLen = from.Length() ; | |
723 | ByteCount byteBufferLen = byteInLen ; | |
724 | ||
c0ef107e | 725 | status = TECConvertText(s_TECNativeCToPlatform, (ConstTextPtr)from.c_str() , byteInLen, &byteInLen, |
427ff662 SC |
726 | (TextPtr)result.data(), byteBufferLen, &byteOutLen); |
727 | return result ; | |
728 | } | |
729 | #endif | |
730 | } | |
731 | ||
5be55d56 | 732 | void wxMacStringToPascal( const wxString&from , StringPtr to ) |
427ff662 SC |
733 | { |
734 | wxCharBuffer buf = wxMacStringToCString( from ) ; | |
735 | int len = strlen(buf) ; | |
736 | ||
737 | if ( len > 255 ) | |
738 | len = 255 ; | |
739 | to[0] = len ; | |
740 | memcpy( (char*) &to[1] , buf , len ) ; | |
741 | } | |
742 | ||
5be55d56 | 743 | wxString wxMacMakeStringFromPascal( ConstStringPtr from ) |
427ff662 SC |
744 | { |
745 | return wxMacMakeStringFromCString( (char*) &from[1] , from[0] ) ; | |
746 | } | |
747 | ||
5be55d56 | 748 | // |
427ff662 SC |
749 | // CFStringRefs (Carbon only) |
750 | // | |
751 | ||
752 | #if TARGET_CARBON | |
753 | // converts this string into a carbon foundation string with optional pc 2 mac encoding | |
5be55d56 | 754 | void wxMacCFStringHolder::Assign( const wxString &str ) |
427ff662 SC |
755 | { |
756 | #if wxUSE_UNICODE | |
5be55d56 | 757 | m_cfs = CFStringCreateWithCharacters( kCFAllocatorDefault, |
427ff662 SC |
758 | (const unsigned short*)str.wc_str(), str.Len() ); |
759 | #else | |
760 | m_cfs = CFStringCreateWithCString( kCFAllocatorSystemDefault , str.c_str() , | |
5be55d56 | 761 | wxApp::s_macDefaultEncodingIsPC ? |
427ff662 SC |
762 | kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ; |
763 | #endif | |
764 | m_release = true ; | |
765 | } | |
766 | ||
5be55d56 | 767 | wxString wxMacCFStringHolder::AsString() |
427ff662 SC |
768 | { |
769 | wxString result ; | |
770 | Size len = CFStringGetLength( m_cfs ) ; | |
771 | wxChar* buf = result.GetWriteBuf( len ) ; | |
772 | #if wxUSE_UNICODE | |
773 | CFStringGetCharacters( m_cfs , CFRangeMake( 0 , len ) , (UniChar*) buf ) ; | |
774 | #else | |
5be55d56 | 775 | CFStringGetCString( m_cfs , buf , len+1 , wxApp::s_macDefaultEncodingIsPC ? |
427ff662 SC |
776 | kCFStringEncodingWindowsLatin1 : CFStringGetSystemEncoding() ) ; |
777 | #endif | |
778 | buf[len] = 0 ; | |
779 | result.UngetWriteBuf() ; | |
780 | return result ; | |
781 | } | |
782 | ||
783 | #if 0 | |
784 | ||
785 | wxString wxMacMakeMacStringFromPC( const wxChar * p ) | |
786 | { | |
787 | wxString result ; | |
788 | int len = wxStrlen ( p ) ; | |
e40298d5 JS |
789 | if ( len > 0 ) |
790 | { | |
791 | wxChar* ptr = result.GetWriteBuf(len) ; | |
792 | wxMacConvertFromPC( p , ptr , len ) ; | |
793 | ptr[len] = 0 ; | |
794 | result.UngetWriteBuf( len ) ; | |
795 | } | |
4c200e8d SC |
796 | return result ; |
797 | } | |
798 | ||
427ff662 | 799 | wxString wxMacMakePCStringFromMac( const wxChar * p ) |
4c200e8d | 800 | { |
e40298d5 | 801 | wxString result ; |
427ff662 | 802 | int len = wxStrlen ( p ) ; |
e40298d5 JS |
803 | if ( len > 0 ) |
804 | { | |
805 | wxChar* ptr = result.GetWriteBuf(len) ; | |
806 | wxMacConvertToPC( p , ptr , len ) ; | |
807 | ptr[len] = 0 ; | |
808 | result.UngetWriteBuf( len ) ; | |
809 | } | |
4c200e8d SC |
810 | return result ; |
811 | } | |
812 | ||
427ff662 | 813 | wxString wxMacMakeStringFromMacString( const wxChar* from , bool mac2pcEncoding ) |
4c200e8d SC |
814 | { |
815 | if (mac2pcEncoding) | |
816 | { | |
817 | return wxMacMakePCStringFromMac( from ) ; | |
818 | } | |
819 | else | |
820 | { | |
821 | return wxString( from ) ; | |
822 | } | |
823 | } | |
824 | ||
5be55d56 | 825 | // |
4c200e8d SC |
826 | // Pascal Strings |
827 | // | |
828 | ||
829 | wxString wxMacMakeStringFromPascal( ConstStringPtr from , bool mac2pcEncoding ) | |
830 | { | |
e40298d5 JS |
831 | // this is safe since a pascal string can never be larger than 256 bytes |
832 | char s[256] ; | |
833 | CopyPascalStringToC( from , s ) ; | |
4c200e8d SC |
834 | if (mac2pcEncoding) |
835 | { | |
836 | return wxMacMakePCStringFromMac( s ) ; | |
837 | } | |
838 | else | |
839 | { | |
840 | return wxString( s ) ; | |
841 | } | |
842 | } | |
843 | ||
427ff662 | 844 | void wxMacStringToPascal( const wxChar * from , StringPtr to , bool pc2macEncoding ) |
4c200e8d SC |
845 | { |
846 | if (pc2macEncoding) | |
847 | { | |
848 | CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ; | |
849 | } | |
850 | else | |
851 | { | |
852 | CopyCStringToPascal( from , to ) ; | |
853 | } | |
854 | } | |
427ff662 | 855 | #endif |
4c200e8d | 856 | |
3d963f81 SC |
857 | |
858 | #endif //TARGET_CARBON | |
859 | ||
a434b43f VZ |
860 | // ---------------------------------------------------------------------------- |
861 | // debugging support | |
862 | // ---------------------------------------------------------------------------- | |
863 | ||
864 | #if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400) | |
865 | ||
866 | // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds... | |
867 | ||
868 | #ifndef __MetroNubUtils__ | |
869 | #include "MetroNubUtils.h" | |
870 | #endif | |
871 | ||
872 | #ifndef __GESTALT__ | |
873 | #include <Gestalt.h> | |
874 | #endif | |
875 | ||
876 | #if TARGET_API_MAC_CARBON | |
877 | ||
878 | #include <CodeFragments.h> | |
879 | ||
536732e4 | 880 | extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...); |
a434b43f VZ |
881 | |
882 | ProcPtr gCallUniversalProc_Proc = NULL; | |
883 | ||
884 | #endif | |
885 | ||
886 | static MetroNubUserEntryBlock* gMetroNubEntry = NULL; | |
887 | ||
888 | static long fRunOnce = false; | |
889 | ||
890 | /* --------------------------------------------------------------------------- | |
891 | IsMetroNubInstalled | |
892 | --------------------------------------------------------------------------- */ | |
893 | ||
894 | Boolean IsMetroNubInstalled() | |
895 | { | |
896 | if (!fRunOnce) | |
897 | { | |
898 | long result, value; | |
899 | ||
900 | fRunOnce = true; | |
901 | gMetroNubEntry = NULL; | |
902 | ||
903 | if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000) | |
904 | { | |
905 | /* look for MetroNub's Gestalt selector */ | |
906 | if (Gestalt(kMetroNubUserSignature, &result) == noErr) | |
907 | { | |
908 | ||
909 | #if TARGET_API_MAC_CARBON | |
910 | if (gCallUniversalProc_Proc == NULL) | |
911 | { | |
912 | CFragConnectionID connectionID; | |
913 | Ptr mainAddress; | |
914 | Str255 errorString; | |
915 | ProcPtr symbolAddress; | |
916 | OSErr err; | |
917 | CFragSymbolClass symbolClass; | |
918 | ||
919 | symbolAddress = NULL; | |
920 | err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag, | |
921 | &connectionID, &mainAddress, errorString); | |
922 | ||
923 | if (err != noErr) | |
924 | { | |
925 | gCallUniversalProc_Proc = NULL; | |
926 | goto end; | |
927 | } | |
928 | ||
929 | err = FindSymbol(connectionID, "\pCallUniversalProc", | |
930 | (Ptr *) &gCallUniversalProc_Proc, &symbolClass); | |
931 | ||
932 | if (err != noErr) | |
933 | { | |
934 | gCallUniversalProc_Proc = NULL; | |
935 | goto end; | |
936 | } | |
937 | } | |
938 | #endif | |
939 | ||
940 | { | |
941 | MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result; | |
942 | ||
943 | /* make sure the version of the API is compatible */ | |
944 | if (block->apiLowVersion <= kMetroNubUserAPIVersion && | |
945 | kMetroNubUserAPIVersion <= block->apiHiVersion) | |
946 | gMetroNubEntry = block; /* success! */ | |
947 | } | |
948 | ||
949 | } | |
950 | } | |
951 | } | |
952 | ||
953 | end: | |
954 | ||
955 | #if TARGET_API_MAC_CARBON | |
956 | return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL); | |
957 | #else | |
958 | return (gMetroNubEntry != NULL); | |
959 | #endif | |
960 | } | |
961 | ||
962 | /* --------------------------------------------------------------------------- | |
963 | IsMWDebuggerRunning [v1 API] | |
964 | --------------------------------------------------------------------------- */ | |
965 | ||
966 | Boolean IsMWDebuggerRunning() | |
967 | { | |
968 | if (IsMetroNubInstalled()) | |
969 | return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning); | |
970 | else | |
971 | return false; | |
972 | } | |
973 | ||
974 | /* --------------------------------------------------------------------------- | |
975 | AmIBeingMWDebugged [v1 API] | |
976 | --------------------------------------------------------------------------- */ | |
977 | ||
978 | Boolean AmIBeingMWDebugged() | |
979 | { | |
980 | if (IsMetroNubInstalled()) | |
981 | return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged); | |
982 | else | |
983 | return false; | |
984 | } | |
985 | ||
986 | extern bool WXDLLEXPORT wxIsDebuggerRunning() | |
987 | { | |
988 | return IsMWDebuggerRunning() && AmIBeingMWDebugged(); | |
989 | } | |
990 | ||
991 | #else | |
992 | ||
993 | extern bool WXDLLEXPORT wxIsDebuggerRunning() | |
994 | { | |
995 | return false; | |
996 | } | |
997 | ||
998 | #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400) | |
999 | ||
b6ed2b86 VZ |
1000 | #endif // wxUSE_GUI |
1001 |