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