]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/utils.cpp
blind fix for GCC compilation when #pragmas are honored
[wxWidgets.git] / src / mac / carbon / utils.cpp
CommitLineData
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
65571936 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"
fe929141
SC
24 #include "wx/font.h"
25#else
26 #include "wx/intl.h"
b6ed2b86 27#endif
e9576ca5
SC
28
29#include <ctype.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <stdarg.h>
35
a2b77260 36#include "MoreFilesX.h"
518af45b 37
66a09d47
SC
38#ifndef __DARWIN__
39#include <Threads.h>
40#include <Sound.h>
41#endif
42
7d9bf1f1 43#if wxUSE_GUI
29d91661
SC
44#if TARGET_API_MAC_OSX
45#include <CoreServices/CoreServices.h>
46#else
47#include <DriverServices.h>
48#include <Multiprocessing.h>
49#endif
50
7f0c3a63
SC
51#include <ATSUnicode.h>
52#include <TextCommon.h>
53#include <TextEncodingConverter.h>
7d9bf1f1 54#endif // wxUSE_GUI
427ff662 55
8aa701ed 56#include "wx/mac/private.h" // includes mac headers
a434b43f
VZ
57
58#if defined(__MWERKS__) && wxUSE_UNICODE
59 #include <wtime.h>
60#endif
61
659b3e8b
VZ
62// ---------------------------------------------------------------------------
63// code used in both base and GUI compilation
64// ---------------------------------------------------------------------------
65
66// our OS version is the same in non GUI and GUI cases
67static int DoGetOSVersion(int *majorVsn, int *minorVsn)
68{
69 long theSystem ;
70
71 // are there x-platform conventions ?
72
73 Gestalt(gestaltSystemVersion, &theSystem) ;
74 if (minorVsn != NULL) {
75 *minorVsn = (theSystem & 0xFF ) ;
76 }
77 if (majorVsn != NULL) {
78 *majorVsn = (theSystem >> 8 ) ;
79 }
80#ifdef __DARWIN__
81 return wxMAC_DARWIN;
82#else
83 return wxMAC;
84#endif
85}
86
ee8bf34f
RD
87
88#if wxUSE_BASE
89
e5a9c663
SC
90// ----------------------------------------------------------------------------
91// debugging support
92// ----------------------------------------------------------------------------
93
94#if defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
95
96// MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
97
98#ifndef __MetroNubUtils__
99#include "MetroNubUtils.h"
100#endif
101
102#ifndef __GESTALT__
103#include <Gestalt.h>
104#endif
105
106#if TARGET_API_MAC_CARBON
107
108 #include <CodeFragments.h>
109
110 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
111
112 ProcPtr gCallUniversalProc_Proc = NULL;
113
114#endif
115
116static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
117
118static long fRunOnce = false;
119
120/* ---------------------------------------------------------------------------
121 IsMetroNubInstalled
122 --------------------------------------------------------------------------- */
123
124Boolean IsMetroNubInstalled()
125{
126 if (!fRunOnce)
127 {
128 long result, value;
129
130 fRunOnce = true;
131 gMetroNubEntry = NULL;
132
133 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
134 {
135 /* look for MetroNub's Gestalt selector */
136 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
137 {
138
139 #if TARGET_API_MAC_CARBON
140 if (gCallUniversalProc_Proc == NULL)
141 {
142 CFragConnectionID connectionID;
143 Ptr mainAddress;
144 Str255 errorString;
145 ProcPtr symbolAddress;
146 OSErr err;
147 CFragSymbolClass symbolClass;
148
149 symbolAddress = NULL;
150 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
151 &connectionID, &mainAddress, errorString);
152
153 if (err != noErr)
154 {
155 gCallUniversalProc_Proc = NULL;
156 goto end;
157 }
158
159 err = FindSymbol(connectionID, "\pCallUniversalProc",
160 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
161
162 if (err != noErr)
163 {
164 gCallUniversalProc_Proc = NULL;
165 goto end;
166 }
167 }
168 #endif
169
170 {
171 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
172
173 /* make sure the version of the API is compatible */
174 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
175 kMetroNubUserAPIVersion <= block->apiHiVersion)
176 gMetroNubEntry = block; /* success! */
177 }
178
179 }
180 }
181 }
182
183end:
184
185#if TARGET_API_MAC_CARBON
186 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
187#else
188 return (gMetroNubEntry != NULL);
189#endif
190}
191
192/* ---------------------------------------------------------------------------
193 IsMWDebuggerRunning [v1 API]
194 --------------------------------------------------------------------------- */
195
196Boolean IsMWDebuggerRunning()
197{
198 if (IsMetroNubInstalled())
199 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
200 else
201 return false;
202}
203
204/* ---------------------------------------------------------------------------
205 AmIBeingMWDebugged [v1 API]
206 --------------------------------------------------------------------------- */
207
208Boolean AmIBeingMWDebugged()
209{
210 if (IsMetroNubInstalled())
211 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
212 else
213 return false;
214}
215
216extern bool WXDLLEXPORT wxIsDebuggerRunning()
217{
218 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
219}
220
221#else
222
223extern bool WXDLLEXPORT wxIsDebuggerRunning()
224{
225 return false;
226}
227
228#endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
229
a434b43f 230
f5c6eb5c 231#ifndef __DARWIN__
03e11df5
GD
232// defined in unix/utilsunx.cpp for Mac OS X
233
2f1ae414
SC
234// get full hostname (with domain name if possible)
235bool wxGetFullHostName(wxChar *buf, int maxSize)
236{
237 return wxGetHostName(buf, maxSize);
238}
239
240// Get hostname only (without domain name)
427ff662 241bool wxGetHostName(wxChar *buf, int maxSize)
e9576ca5 242{
e40298d5
JS
243 // Gets Chooser name of user by examining a System resource.
244
245 const short kComputerNameID = -16413;
5be55d56 246
e40298d5
JS
247 short oldResFile = CurResFile() ;
248 UseResFile(0);
249 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
250 UseResFile(oldResFile);
251
252 if (chooserName && *chooserName)
253 {
427ff662
SC
254 HLock( (Handle) chooserName ) ;
255 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
256 HUnlock( (Handle) chooserName ) ;
257 ReleaseResource( (Handle) chooserName ) ;
258 wxStrncpy( buf , name , maxSize - 1 ) ;
e40298d5
JS
259 }
260 else
261 buf[0] = 0 ;
0a67a93b
SC
262
263 return TRUE;
e9576ca5
SC
264}
265
266// Get user ID e.g. jacs
427ff662 267bool wxGetUserId(wxChar *buf, int maxSize)
e9576ca5 268{
0a67a93b 269 return wxGetUserName( buf , maxSize ) ;
e9576ca5
SC
270}
271
5b781a67
SC
272const wxChar* wxGetHomeDir(wxString *pstr)
273{
e40298d5
JS
274 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
275 return pstr->c_str() ;
5b781a67
SC
276}
277
a31a5f85 278// Get user name e.g. Stefan Csomor
427ff662 279bool wxGetUserName(wxChar *buf, int maxSize)
e9576ca5 280{
e40298d5
JS
281 // Gets Chooser name of user by examining a System resource.
282
283 const short kChooserNameID = -16096;
5be55d56 284
e40298d5
JS
285 short oldResFile = CurResFile() ;
286 UseResFile(0);
287 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
288 UseResFile(oldResFile);
289
290 if (chooserName && *chooserName)
291 {
427ff662
SC
292 HLock( (Handle) chooserName ) ;
293 wxString name = wxMacMakeStringFromPascal( *chooserName ) ;
294 HUnlock( (Handle) chooserName ) ;
295 ReleaseResource( (Handle) chooserName ) ;
296 wxStrncpy( buf , name , maxSize - 1 ) ;
e40298d5
JS
297 }
298 else
299 buf[0] = 0 ;
0a67a93b
SC
300
301 return TRUE;
e9576ca5
SC
302}
303
e0f6b731 304int wxKill(long pid, wxSignal sig , wxKillError *rc, int flags)
e9576ca5
SC
305{
306 // TODO
307 return 0;
308}
309
5dbb17e2
SC
310WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
311{
e40298d5
JS
312 // TODO : under classic there is no environement support, under X yes
313 return false ;
5dbb17e2
SC
314}
315
316// set the env var name to the given value, return TRUE on success
317WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
318{
e40298d5
JS
319 // TODO : under classic there is no environement support, under X yes
320 return false ;
5dbb17e2
SC
321}
322
e9576ca5
SC
323//
324// Execute a program in an Interactive Shell
325//
326bool wxShell(const wxString& command)
327{
328 // TODO
329 return FALSE;
330}
331
5be55d56 332// Shutdown or reboot the PC
f6ba47d9
VZ
333bool wxShutdown(wxShutdownFlags wFlags)
334{
335 // TODO
336 return FALSE;
337}
338
e9576ca5
SC
339// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
340long wxGetFreeMemory()
341{
0a67a93b
SC
342 return FreeMem() ;
343}
344
7f7b52b4
SC
345#ifndef __DARWIN__
346
347void wxMicroSleep(unsigned long microseconds)
0a67a93b 348{
7f7b52b4
SC
349 AbsoluteTime wakeup = AddDurationToAbsolute( microseconds * durationMicrosecond , UpTime());
350 MPDelayUntil( & wakeup);
351}
352
353void wxMilliSleep(unsigned long milliseconds)
354{
355 AbsoluteTime wakeup = AddDurationToAbsolute( milliseconds, UpTime());
356 MPDelayUntil( & wakeup);
e9576ca5
SC
357}
358
359void wxSleep(int nSecs)
360{
7f7b52b4 361 wxMilliSleep(1000*nSecs);
e9576ca5
SC
362}
363
7f7b52b4
SC
364#endif
365
e9576ca5
SC
366// Consume all events until no more left
367void wxFlushEvents()
368{
369}
370
f5c6eb5c 371#endif // !__DARWIN__
e9576ca5
SC
372
373// Emit a beeeeeep
374void wxBell()
375{
0a67a93b 376 SysBeep(30);
e9576ca5
SC
377}
378
324899f6 379wxToolkitInfo& wxConsoleAppTraits::GetToolkitInfo()
29c99ad3 380{
a8eaaeb2 381 static wxToolkitInfo info;
66b6b57c 382 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
a8eaaeb2 383 info.name = _T("wxBase");
324899f6 384 return info;
29c99ad3
VZ
385}
386
b6ed2b86
VZ
387#endif // wxUSE_BASE
388
389#if wxUSE_GUI
536732e4 390
324899f6 391wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
29c99ad3 392{
a8eaaeb2 393 static wxToolkitInfo info;
66b6b57c 394 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
a8eaaeb2
VS
395 info.shortName = _T("mac");
396 info.name = _T("wxMac");
397#ifdef __WXUNIVERSAL__
398 info.shortName << _T("univ");
399 info.name << _T("/wxUniversal");
400#endif
324899f6 401 return info;
29c99ad3
VZ
402}
403
e9576ca5
SC
404// Reading and writing resources (eg WIN.INI, .Xdefaults)
405#if wxUSE_RESOURCES
406bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
407{
408 // TODO
409 return FALSE;
410}
411
412bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
413{
427ff662
SC
414 wxString buf;
415 buf.Printf(wxT("%.4f"), value);
416
e40298d5 417 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
418}
419
420bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
421{
427ff662
SC
422 wxString buf;
423 buf.Printf(wxT("%ld"), value);
424
e40298d5 425 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
426}
427
428bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
429{
427ff662
SC
430 wxString buf;
431 buf.Printf(wxT("%d"), value);
432
e40298d5 433 return wxWriteResource(section, entry, buf, file);
e9576ca5
SC
434}
435
436bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
437{
438 // TODO
439 return FALSE;
440}
441
442bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
443{
e40298d5
JS
444 char *s = NULL;
445 bool succ = wxGetResource(section, entry, (char **)&s, file);
446 if (succ)
447 {
448 *value = (float)strtod(s, NULL);
449 delete[] s;
450 return TRUE;
451 }
452 else return FALSE;
e9576ca5
SC
453}
454
455bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
456{
e40298d5
JS
457 char *s = NULL;
458 bool succ = wxGetResource(section, entry, (char **)&s, file);
459 if (succ)
460 {
461 *value = strtol(s, NULL, 10);
462 delete[] s;
463 return TRUE;
464 }
465 else return FALSE;
e9576ca5
SC
466}
467
468bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
469{
e40298d5
JS
470 char *s = NULL;
471 bool succ = wxGetResource(section, entry, (char **)&s, file);
472 if (succ)
473 {
474 *value = (int)strtol(s, NULL, 10);
475 delete[] s;
476 return TRUE;
477 }
478 else return FALSE;
e9576ca5
SC
479}
480#endif // wxUSE_RESOURCES
481
6b57b49a 482int gs_wxBusyCursorCount = 0;
e40298d5
JS
483extern wxCursor gMacCurrentCursor ;
484wxCursor gMacStoredActiveCursor ;
e9576ca5
SC
485
486// Set the cursor to the busy cursor for all windows
487void wxBeginBusyCursor(wxCursor *cursor)
488{
e40298d5
JS
489 if (gs_wxBusyCursorCount++ == 0)
490 {
491 gMacStoredActiveCursor = gMacCurrentCursor ;
492 cursor->MacInstall() ;
493 }
494 //else: nothing to do, already set
e9576ca5
SC
495}
496
497// Restore cursor to normal
498void wxEndBusyCursor()
499{
6b57b49a 500 wxCHECK_RET( gs_wxBusyCursorCount > 0,
e40298d5 501 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
5be55d56 502
e40298d5
JS
503 if (--gs_wxBusyCursorCount == 0)
504 {
505 gMacStoredActiveCursor.MacInstall() ;
506 gMacStoredActiveCursor = wxNullCursor ;
507 }
e9576ca5
SC
508}
509
510// TRUE if we're between the above two calls
511bool wxIsBusy()
512{
e40298d5 513 return (gs_wxBusyCursorCount > 0);
ec5d7799 514}
e9576ca5 515
2dbc444a
RD
516#endif // wxUSE_GUI
517
518#if wxUSE_BASE
519
e7e1b01e 520wxString wxMacFindFolder( short vol,
e40298d5
JS
521 OSType folderType,
522 Boolean createFolder)
2f1ae414 523{
a2b77260 524 FSRef fsRef ;
2d4e4f80 525 wxString strDir ;
5be55d56 526
a2b77260 527 if ( FSFindFolder( vol, folderType, createFolder, &fsRef) == noErr)
814af667 528 strDir = wxMacFSRefToPath( &fsRef ) + wxFILE_SEP_PATH ;
a2b77260 529
2d4e4f80 530 return strDir ;
2f1ae414
SC
531}
532
2dbc444a
RD
533#endif // wxUSE_BASE
534
535#if wxUSE_GUI
536
e9576ca5
SC
537// Check whether this window wants to process messages, e.g. Stop button
538// in long calculations.
539bool wxCheckForInterrupt(wxWindow *wnd)
540{
541 // TODO
542 return FALSE;
543}
544
545void wxGetMousePosition( int* x, int* y )
546{
519cb848 547 Point pt ;
ec5d7799 548
519cb848
SC
549 GetMouse( &pt ) ;
550 LocalToGlobal( &pt ) ;
551 *x = pt.h ;
552 *y = pt.v ;
e9576ca5
SC
553};
554
555// Return TRUE if we have a colour display
556bool wxColourDisplay()
557{
e9576ca5
SC
558 return TRUE;
559}
560
561// Returns depth of screen
562int wxDisplayDepth()
563{
e40298d5
JS
564 Rect globRect ;
565 SetRect(&globRect, -32760, -32760, 32760, 32760);
566 GDHandle theMaxDevice;
2f1ae414 567
e40298d5
JS
568 int theDepth = 8;
569 theMaxDevice = GetMaxDevice(&globRect);
570 if (theMaxDevice != nil)
571 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
ec5d7799 572
e40298d5 573 return theDepth ;
e9576ca5
SC
574}
575
576// Get size of display
577void wxDisplaySize(int *width, int *height)
578{
e40298d5
JS
579 BitMap screenBits;
580 GetQDGlobalsScreenBits( &screenBits );
5be55d56
VZ
581
582 if (width != NULL) {
e8ca7105
GD
583 *width = screenBits.bounds.right - screenBits.bounds.left ;
584 }
5be55d56 585 if (height != NULL) {
e8ca7105
GD
586 *height = screenBits.bounds.bottom - screenBits.bounds.top ;
587 }
e9576ca5
SC
588}
589
5fde6fcc
GD
590void wxDisplaySizeMM(int *width, int *height)
591{
5b028d57
SC
592 wxDisplaySize(width, height);
593 // on mac 72 is fixed (at least now ;-)
594 float cvPt2Mm = 25.4 / 72;
e8ca7105 595
5be55d56 596 if (width != NULL) {
e8ca7105
GD
597 *width = int( *width * cvPt2Mm );
598 }
5be55d56 599 if (height != NULL) {
e8ca7105
GD
600 *height = int( *height * cvPt2Mm );
601 }
5fde6fcc
GD
602}
603
ec5d7799
RD
604void wxClientDisplayRect(int *x, int *y, int *width, int *height)
605{
ee658398
SC
606 Rect r ;
607 GetAvailableWindowPositioningBounds( GetMainDevice() , &r ) ;
608 if ( x )
609 *x = r.left ;
610 if ( y )
611 *y = r.top ;
612 if ( width )
613 *width = r.right - r.left ;
614 if ( height )
615 *height = r.bottom - r.top ;
ec5d7799
RD
616}
617
57591e0e
JS
618wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
619{
620 return wxGenericFindWindowAtPoint(pt);
621}
5dbb17e2 622
b6ed2b86
VZ
623#endif // wxUSE_GUI
624
625#if wxUSE_BASE
626
5dbb17e2
SC
627wxString wxGetOsDescription()
628{
6e73695c
GD
629#ifdef WXWIN_OS_DESCRIPTION
630 // use configure generated description if available
5f3f0f17 631 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION) + wxString(wxT(")"));
6e73695c 632#else
427ff662 633 return wxT("MacOS") ; //TODO:define further
6e73695c
GD
634#endif
635}
636
b6ed2b86
VZ
637#ifndef __DARWIN__
638wxChar *wxGetUserHome (const wxString& user)
639{
640 // TODO
641 return NULL;
642}
643
644bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
645{
646 if ( path.empty() )
647 return FALSE;
648
649 wxString p = path ;
71111c40 650 if (p[0u] == ':' ) {
b6ed2b86
VZ
651 p = wxGetCwd() + p ;
652 }
653
654 int pos = p.Find(':') ;
655 if ( pos != wxNOT_FOUND ) {
656 p = p.Mid(1,pos) ;
657 }
658
659 p = p + wxT(":") ;
660
a2b77260
SC
661 OSErr err = noErr ;
662
663 FSRef fsRef ;
664 err = wxMacPathToFSRef( p , &fsRef ) ;
665 if ( noErr == err )
666 {
667 FSVolumeRefNum vRefNum ;
668 err = FSGetVRefNum( &fsRef , &vRefNum ) ;
669 if ( noErr == err )
670 {
671 UInt64 freeBytes , totalBytes ;
672 err = FSGetVInfo( vRefNum , NULL , &freeBytes , &totalBytes ) ;
673 if ( noErr == err )
674 {
675 if ( pTotal )
676 *pTotal = wxLongLong( totalBytes ) ;
677 if ( pFree )
678 *pFree = wxLongLong( freeBytes ) ;
679 }
680 }
b6ed2b86 681 }
a2b77260 682
b6ed2b86
VZ
683 return err == noErr ;
684}
685#endif // !__DARWIN__
686
3d963f81
SC
687//---------------------------------------------------------------------------
688// wxMac Specific utility functions
689//---------------------------------------------------------------------------
690
5be55d56 691void wxMacStringToPascal( const wxString&from , StringPtr to )
427ff662 692{
939fba6c 693 wxCharBuffer buf = from.mb_str( wxConvLocal ) ;
427ff662
SC
694 int len = strlen(buf) ;
695
696 if ( len > 255 )
697 len = 255 ;
698 to[0] = len ;
699 memcpy( (char*) &to[1] , buf , len ) ;
700}
701
5be55d56 702wxString wxMacMakeStringFromPascal( ConstStringPtr from )
427ff662 703{
939fba6c 704 return wxString( (char*) &from[1] , wxConvLocal , from[0] ) ;
427ff662
SC
705}
706
e50d5284 707
73280e05
SC
708// ----------------------------------------------------------------------------
709// Common Event Support
710// ----------------------------------------------------------------------------
711
d13ea1bd 712
73280e05
SC
713extern ProcessSerialNumber gAppProcess ;
714
715void wxMacWakeUp()
716{
717 ProcessSerialNumber psn ;
718 Boolean isSame ;
719 psn.highLongOfPSN = 0 ;
720 psn.lowLongOfPSN = kCurrentProcess ;
721 SameProcess( &gAppProcess , &psn , &isSame ) ;
722 if ( isSame )
723 {
724#if TARGET_CARBON
8dfef0c2
SC
725 static wxMacCarbonEvent s_wakeupEvent ;
726 OSStatus err = noErr ;
727 if ( !s_wakeupEvent.IsValid() )
d6c69b17 728 {
8dfef0c2
SC
729 err = s_wakeupEvent.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
730 kEventAttributeNone ) ;
d6c69b17 731 }
8dfef0c2 732 if ( err == noErr )
73280e05 733 {
8dfef0c2
SC
734 if ( IsEventInQueue( GetMainEventQueue() , s_wakeupEvent ) )
735 return ;
736 s_wakeupEvent.SetTime(0) ;
d6c69b17 737 err = PostEventToQueue(GetMainEventQueue(), s_wakeupEvent,
73280e05 738 kEventPriorityHigh);
8dfef0c2 739 }
73280e05
SC
740#else
741 PostEvent( nullEvent , 0 ) ;
742#endif
743 }
744 else
745 {
746 WakeUpProcess( &gAppProcess ) ;
747 }
748}
749
d13ea1bd
RD
750#endif // wxUSE_BASE
751
752#if wxUSE_GUI
753
754
facd6764
SC
755// ----------------------------------------------------------------------------
756// Carbon Event Support
757// ----------------------------------------------------------------------------
758
759
760OSStatus wxMacCarbonEvent::GetParameter(EventParamName inName, EventParamType inDesiredType, UInt32 inBufferSize, void * outData)
761{
762 return ::GetEventParameter( m_eventRef , inName , inDesiredType , NULL , inBufferSize , NULL , outData ) ;
763}
764
21fd5529 765OSStatus wxMacCarbonEvent::SetParameter(EventParamName inName, EventParamType inType, UInt32 inBufferSize, const void * inData)
facd6764
SC
766{
767 return ::SetEventParameter( m_eventRef , inName , inType , inBufferSize , inData ) ;
768}
769
86d8b744
SC
770// ----------------------------------------------------------------------------
771// Control Access Support
772// ----------------------------------------------------------------------------
773
5ca0d812
SC
774void wxMacControl::Dispose()
775{
776 ::DisposeControl( m_controlRef ) ;
777 m_controlRef = NULL ;
778}
779
780void wxMacControl::SetReference( SInt32 data )
781{
782 SetControlReference( m_controlRef , data ) ;
783}
784
29d91661 785OSStatus wxMacControl::GetData(ControlPartCode inPartCode , ResType inTag , Size inBufferSize , void * inOutBuffer , Size * outActualSize ) const
86d8b744
SC
786{
787 return ::GetControlData( m_controlRef , inPartCode , inTag , inBufferSize , inOutBuffer , outActualSize ) ;
788}
789
29d91661 790OSStatus wxMacControl::GetDataSize(ControlPartCode inPartCode , ResType inTag , Size * outActualSize ) const
86d8b744
SC
791{
792 return ::GetControlDataSize( m_controlRef , inPartCode , inTag , outActualSize ) ;
793}
794
795OSStatus wxMacControl::SetData(ControlPartCode inPartCode , ResType inTag , Size inSize , const void * inData)
796{
797 return ::SetControlData( m_controlRef , inPartCode , inTag , inSize , inData ) ;
798}
799
21fd5529
SC
800OSStatus wxMacControl::SendEvent( EventRef event , OptionBits inOptions )
801{
e996f509 802#if TARGET_API_MAC_OSX
21fd5529 803 return SendEventToEventTargetWithOptions( event,
e996f509
SC
804 HIObjectGetEventTarget( (HIObjectRef) m_controlRef ), inOptions );
805#else
806 #pragma unused(inOptions)
807 return SendEventToEventTarget(event,GetControlEventTarget( m_controlRef ) ) ;
808#endif
21fd5529
SC
809}
810
811OSStatus wxMacControl::SendHICommand( HICommand &command , OptionBits inOptions )
812{
813 wxMacCarbonEvent event( kEventClassCommand , kEventCommandProcess ) ;
814 event.SetParameter<HICommand>(kEventParamDirectObject,command) ;
815 return SendEvent( event , inOptions ) ;
816}
817
818OSStatus wxMacControl::SendHICommand( UInt32 commandID , OptionBits inOptions )
819{
820 HICommand command ;
821 memset( &command, 0 , sizeof(command) ) ;
822 command.commandID = commandID ;
823 return SendHICommand( command , inOptions ) ;
824}
825
826void wxMacControl::Flash( ControlPartCode part , UInt32 ticks )
827{
828 HiliteControl( m_controlRef , part ) ;
829 unsigned long finalTicks ;
830 Delay( ticks , &finalTicks ) ;
831 HiliteControl( m_controlRef , kControlNoPart ) ;
832}
833
7f7b52b4
SC
834SInt32 wxMacControl::GetValue() const
835{
836 return ::GetControl32BitValue( m_controlRef ) ;
837}
838
839SInt32 wxMacControl::GetMaximum() const
840{
841 return ::GetControl32BitMaximum( m_controlRef ) ;
842}
843
844SInt32 wxMacControl::GetMinimum() const
845{
846 return ::GetControl32BitMinimum( m_controlRef ) ;
847}
848
849void wxMacControl::SetValue( SInt32 v )
850{
851 ::SetControl32BitValue( m_controlRef , v ) ;
852}
853
854void wxMacControl::SetMinimum( SInt32 v )
855{
856 ::SetControl32BitMinimum( m_controlRef , v ) ;
857}
858
859void wxMacControl::SetMaximum( SInt32 v )
860{
861 ::SetControl32BitMaximum( m_controlRef , v ) ;
862}
863
864void wxMacControl::SetValueAndRange( SInt32 value , SInt32 minimum , SInt32 maximum )
865{
866 ::SetControl32BitMinimum( m_controlRef , minimum ) ;
867 ::SetControl32BitMaximum( m_controlRef , maximum ) ;
868 ::SetControl32BitValue( m_controlRef , value ) ;
869}
870
5ca0d812
SC
871OSStatus wxMacControl::SetFocus( ControlFocusPart focusPart )
872{
873 return SetKeyboardFocus( GetControlOwner( m_controlRef ) ,
874 m_controlRef , focusPart ) ;
875}
876
877bool wxMacControl::HasFocus() const
878{
879 ControlRef control ;
880 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
881 return control == m_controlRef ;
882}
883
884bool wxMacControl::NeedsFocusRect() const
885{
886 return false ;
887}
888
29d91661
SC
889void wxMacControl::VisibilityChanged(bool shown)
890{
891}
892
893void wxMacControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
894{
895 m_font = font ;
896 ControlFontStyleRec fontStyle;
897 if ( font.MacGetThemeFontID() != kThemeCurrentPortFont )
898 {
899 switch( font.MacGetThemeFontID() )
900 {
901 case kThemeSmallSystemFont : fontStyle.font = kControlFontSmallSystemFont ; break ;
902 case 109 /*mini font */ : fontStyle.font = -5 ; break ;
903 case kThemeSystemFont : fontStyle.font = kControlFontBigSystemFont ; break ;
904 default : fontStyle.font = kControlFontBigSystemFont ; break ;
905 }
906 fontStyle.flags = kControlUseFontMask ;
907 }
908 else
909 {
910 fontStyle.font = font.MacGetFontNum() ;
911 fontStyle.style = font.MacGetFontStyle() ;
912 fontStyle.size = font.MacGetFontSize() ;
913 fontStyle.flags = kControlUseFontMask | kControlUseFaceMask | kControlUseSizeMask ;
914 }
915
916 fontStyle.just = teJustLeft ;
917 fontStyle.flags |= kControlUseJustMask ;
918 if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_CENTER_HORIZONTAL )
919 fontStyle.just = teJustCenter ;
920 else if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_RIGHT )
921 fontStyle.just = teJustRight ;
922
923
924 // we only should do this in case of a non-standard color, as otherwise 'disabled' controls
925 // won't get grayed out by the system anymore
926
927 if ( foreground != *wxBLACK )
928 {
929 fontStyle.foreColor = MAC_WXCOLORREF(foreground.GetPixel() ) ;
930 fontStyle.flags |= kControlUseForeColorMask ;
931 }
932
933 ::SetControlFontStyle( m_controlRef , &fontStyle );
934}
935
7ea087b7
SC
936void wxMacControl::SetBackground( const wxBrush &WXUNUSED(brush) )
937{
938 // TODO
939 // setting up a color proc is not recommended anymore
940}
941
7f7b52b4
SC
942void wxMacControl::SetRange( SInt32 minimum , SInt32 maximum )
943{
944 ::SetControl32BitMinimum( m_controlRef , minimum ) ;
945 ::SetControl32BitMaximum( m_controlRef , maximum ) ;
946}
947
5ca0d812
SC
948short wxMacControl::HandleKey( SInt16 keyCode, SInt16 charCode, EventModifiers modifiers )
949{
950 return HandleControlKey( m_controlRef , keyCode , charCode , modifiers ) ;
951}
952
953void wxMacControl::SetActionProc( ControlActionUPP actionProc )
954{
955 SetControlAction( m_controlRef , actionProc ) ;
956}
957
958void wxMacControl::SetViewSize( SInt32 viewSize )
959{
960 SetControlViewSize(m_controlRef , viewSize ) ;
961}
962
963SInt32 wxMacControl::GetViewSize() const
964{
965 return GetControlViewSize( m_controlRef ) ;
966}
967
968bool wxMacControl::IsVisible() const
969{
970 return IsControlVisible( m_controlRef ) ;
971}
972
973void wxMacControl::SetVisibility( bool visible , bool redraw )
974{
975 SetControlVisibility( m_controlRef , visible , redraw ) ;
976}
977
978bool wxMacControl::IsEnabled() const
979{
980#if TARGET_API_MAC_OSX
981 return IsControlEnabled( m_controlRef ) ;
982#else
983 return IsControlActive( m_controlRef ) ;
984#endif
985}
986
987bool wxMacControl::IsActive() const
988{
989 return IsControlActive( m_controlRef ) ;
990}
991
992void wxMacControl::Enable( bool enable )
993{
994#if TARGET_API_MAC_OSX
995 if ( enable )
996 EnableControl( m_controlRef ) ;
997 else
998 DisableControl( m_controlRef ) ;
999#else
1000 if ( enable )
1001 ActivateControl( m_controlRef ) ;
1002 else
1003 DeactivateControl( m_controlRef ) ;
1004#endif
1005}
1006
1007void wxMacControl::SetDrawingEnabled( bool enable )
1008{
1009#if TARGET_API_MAC_OSX
1010 HIViewSetDrawingEnabled( m_controlRef , enable ) ;
1011#endif
1012}
1013
1014bool wxMacControl::GetNeedsDisplay() const
1015{
1016#if TARGET_API_MAC_OSX
1017 return HIViewGetNeedsDisplay( m_controlRef ) ;
1018#else
1019 return false ;
1020#endif
1021}
1022
1023void wxMacControl::SetNeedsDisplay( bool needsDisplay , RgnHandle where )
1024{
1025#if TARGET_API_MAC_OSX
1026 if ( where != NULL )
1027 HIViewSetNeedsDisplayInRegion( m_controlRef , where , needsDisplay ) ;
1028 else
1029 HIViewSetNeedsDisplay( m_controlRef , needsDisplay ) ;
1030#endif
1031}
1032
1033void wxMacControl::Convert( wxPoint *pt , wxMacControl *from , wxMacControl *to )
1034{
1035#if TARGET_API_MAC_OSX
1036 HIPoint hiPoint ;
1037 hiPoint.x = pt->x ;
1038 hiPoint.y = pt->y ;
1039 HIViewConvertPoint( &hiPoint , from->m_controlRef , to->m_controlRef ) ;
6ea4a266
VZ
1040 pt->x = (int)hiPoint.x ;
1041 pt->y = (int)hiPoint.y ;
5ca0d812
SC
1042#endif
1043}
1044
1045void wxMacControl::SetRect( Rect *r )
1046{
1047#if TARGET_API_MAC_OSX
af4fc79b
RN
1048 //A HIRect is actually a CGRect on OSX - which consists of two structures -
1049 //CGPoint and CGSize, which have two floats each
1050 HIRect hir = { { r->left , r->top }, { r->right - r->left , r->bottom - r->top } } ;
5ca0d812
SC
1051 HIViewSetFrame ( m_controlRef , &hir ) ;
1052#else
1053 SetControlBounds( m_controlRef , r ) ;
1054#endif
1055
1056}
1057
1058void wxMacControl::GetRect( Rect *r )
1059{
1060 GetControlBounds( m_controlRef , r ) ;
1061}
1062
1063void wxMacControl::GetRectInWindowCoords( Rect *r )
1064{
1065 UMAGetControlBoundsInWindowCoords( m_controlRef , r ) ;
1066}
1067
1068void wxMacControl::GetBestRect( Rect *r )
1069{
5ca0d812
SC
1070 short baselineoffset ;
1071 GetBestControlRect( m_controlRef , r , &baselineoffset ) ;
1072}
1073
1074void wxMacControl::SetTitle( const wxString &title )
1075{
10a59880
RD
1076 wxFontEncoding encoding;
1077
1078 if ( m_font.Ok() )
1079 encoding = m_font.GetEncoding();
1080 else
1081 encoding = wxFont::GetDefaultEncoding();
1082
1083 UMASetControlTitle( m_controlRef , title , encoding ) ;
5ca0d812
SC
1084}
1085
1086void wxMacControl::GetFeatures( UInt32 * features )
1087{
1088 GetControlFeatures( m_controlRef , features ) ;
1089}
1090
1091OSStatus wxMacControl::GetRegion( ControlPartCode partCode , RgnHandle region )
1092{
1093 return GetControlRegion( m_controlRef , partCode , region ) ;
1094}
1095
1096OSStatus wxMacControl::SetZOrder( bool above , wxMacControl* other )
1097{
1098#if TARGET_API_MAC_OSX
1099 return HIViewSetZOrder( m_controlRef,above ? kHIViewZOrderAbove : kHIViewZOrderBelow,
1100 (other != NULL) ? other->m_controlRef : NULL) ;
1101#else
1102 return 0 ;
1103#endif
1104}
1105
1106
1107#if TARGET_API_MAC_OSX
1108// SetNeedsDisplay would not invalidate the children
1109static void InvalidateControlAndChildren( HIViewRef control )
1110{
1111 HIViewSetNeedsDisplay( control , true ) ;
1112 UInt16 childrenCount = 0 ;
1113 OSStatus err = CountSubControls( control , &childrenCount ) ;
1114 if ( err == errControlIsNotEmbedder )
1115 return ;
1116 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
1117
1118 for ( UInt16 i = childrenCount ; i >=1 ; --i )
1119 {
1120 HIViewRef child ;
1121 err = GetIndexedSubControl( control , i , & child ) ;
1122 if ( err == errControlIsNotEmbedder )
1123 return ;
1124 InvalidateControlAndChildren( child ) ;
1125 }
1126}
1127#endif
1128
1129void wxMacControl::InvalidateWithChildren()
1130{
1131#if TARGET_API_MAC_OSX
1132 InvalidateControlAndChildren( m_controlRef ) ;
1133#endif
1134}
1135
1136void wxMacControl::ScrollRect( const wxRect &r , int dx , int dy )
1137{
1138#if TARGET_API_MAC_OSX
1139 HIRect scrollarea = CGRectMake( r.x , r.y , r.width , r.height) ;
1140 HIViewScrollRect ( m_controlRef , &scrollarea , dx ,dy ) ;
1141#endif
1142}
1143
1144
1145// SetNeedsDisplay would not invalidate the children
1146
1147//
1148// Databrowser
1149//
1150
1151OSStatus wxMacControl::SetSelectionFlags( DataBrowserSelectionFlags options )
1152{
1153 return SetDataBrowserSelectionFlags( m_controlRef , options ) ;
1154}
1155
1156OSStatus wxMacControl::AddListViewColumn( DataBrowserListViewColumnDesc *columnDesc,
1157 DataBrowserTableViewColumnIndex position )
1158{
1159 return AddDataBrowserListViewColumn( m_controlRef , columnDesc, position ) ;
1160}
1161
1162OSStatus wxMacControl::AutoSizeListViewColumns()
1163{
1164 return AutoSizeDataBrowserListViewColumns(m_controlRef) ;
1165}
1166
1167OSStatus wxMacControl::SetHasScrollBars( bool horiz , bool vert )
1168{
1169 return SetDataBrowserHasScrollBars( m_controlRef , horiz , vert ) ;
1170}
1171
1172OSStatus wxMacControl::SetTableViewHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle )
1173{
1174 return SetDataBrowserTableViewHiliteStyle( m_controlRef , hiliteStyle ) ;
1175}
1176
1177OSStatus wxMacControl::SetListViewHeaderBtnHeight(UInt16 height)
1178{
1179 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef ,height ) ;
1180}
1181
1182OSStatus wxMacControl::SetCallbacks(const DataBrowserCallbacks * callbacks)
1183{
1184 return SetDataBrowserCallbacks( m_controlRef , callbacks ) ;
1185}
1186
1187OSStatus wxMacControl::UpdateItems( DataBrowserItemID container, UInt32 numItems,
1188 const DataBrowserItemID* items,
1189 DataBrowserPropertyID preSortProperty,
1190 DataBrowserPropertyID propertyID )
1191{
1192 return UpdateDataBrowserItems( m_controlRef , container, numItems, items, preSortProperty, propertyID ) ;
1193}
1194
1195bool wxMacControl::IsItemSelected( DataBrowserItemID item )
1196{
1197 return IsDataBrowserItemSelected( m_controlRef , item ) ;
1198}
1199
1200OSStatus wxMacControl::AddItems( DataBrowserItemID container, UInt32 numItems,
1201 const DataBrowserItemID* items,
1202 DataBrowserPropertyID preSortProperty )
1203{
1204 return AddDataBrowserItems( m_controlRef , container, numItems, items, preSortProperty ) ;
1205}
1206
1207OSStatus wxMacControl::RemoveItems( DataBrowserItemID container, UInt32 numItems,
1208 const DataBrowserItemID* items,
1209 DataBrowserPropertyID preSortProperty )
1210{
1211 return RemoveDataBrowserItems( m_controlRef , container, numItems, items, preSortProperty ) ;
1212}
1213
1214OSStatus wxMacControl::RevealItem( DataBrowserItemID item,
1215 DataBrowserPropertyID propertyID,
1216 DataBrowserRevealOptions options )
1217{
1218 return RevealDataBrowserItem( m_controlRef , item , propertyID , options ) ;
1219}
1220
1221OSStatus wxMacControl::SetSelectedItems(UInt32 numItems,
1222 const DataBrowserItemID * items,
1223 DataBrowserSetOption operation )
1224{
1225 return SetDataBrowserSelectedItems( m_controlRef , numItems , items, operation ) ;
1226}
1227
c020f421
SC
1228OSStatus wxMacControl::GetSelectionAnchor( DataBrowserItemID * first, DataBrowserItemID * last )
1229{
1230 return GetDataBrowserSelectionAnchor( m_controlRef , first , last ) ;
1231}
1232
5ca0d812
SC
1233//
1234// Tab Control
1235//
1236
1237OSStatus wxMacControl::SetTabEnabled( SInt16 tabNo , bool enable )
1238{
1239 return ::SetTabEnabled( m_controlRef , tabNo , enable ) ;
1240}
1241
b6ed2b86
VZ
1242#endif // wxUSE_GUI
1243