]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/utils.cpp
we are always compositing on the supported OS versions
[wxWidgets.git] / src / mac / carbon / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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 #include "wx/wxprec.h"
13
14 #include "wx/utils.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/app.h"
19 #if wxUSE_GUI
20 #include "wx/toplevel.h"
21 #include "wx/font.h"
22 #endif
23 #endif
24
25 #include "wx/apptrait.h"
26
27 #if wxUSE_GUI
28 #include "wx/mac/uma.h"
29 #endif
30
31 #include <ctype.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdarg.h>
37
38 #include "MoreFilesX.h"
39
40 #ifndef __DARWIN__
41 #include <Threads.h>
42 #include <Sound.h>
43 #endif
44
45 #if wxUSE_GUI
46 #if TARGET_API_MAC_OSX
47 #include <CoreServices/CoreServices.h>
48 #else
49 #include <DriverServices.h>
50 #include <Multiprocessing.h>
51 #endif
52
53 #ifdef __DARWIN__
54 #include <Carbon/Carbon.h>
55 #else
56 #include <ATSUnicode.h>
57 #include <TextCommon.h>
58 #include <TextEncodingConverter.h>
59 #endif
60 #endif // wxUSE_GUI
61
62 #include "wx/mac/private.h"
63
64 #if defined(__MWERKS__) && wxUSE_UNICODE
65 #if __MWERKS__ < 0x4100 || !defined(__DARWIN__)
66 #include <wtime.h>
67 #endif
68 #endif
69
70 // ---------------------------------------------------------------------------
71 // code used in both base and GUI compilation
72 // ---------------------------------------------------------------------------
73
74 // our OS version is the same in non GUI and GUI cases
75 static int DoGetOSVersion(int *majorVsn, int *minorVsn)
76 {
77 long theSystem;
78
79 // are there x-platform conventions ?
80
81 Gestalt(gestaltSystemVersion, &theSystem);
82 if (minorVsn != NULL)
83 *minorVsn = (theSystem & 0xFF);
84
85 if (majorVsn != NULL)
86 *majorVsn = (theSystem >> 8);
87
88 #ifdef __DARWIN__
89 return wxMAC_DARWIN;
90 #else
91 return wxMAC;
92 #endif
93 }
94
95
96 #if wxUSE_BASE
97
98 // ----------------------------------------------------------------------------
99 // debugging support
100 // ----------------------------------------------------------------------------
101
102 #if defined(__WXDEBUG__) && defined(__WXMAC__) && !defined(__DARWIN__) && defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
103
104 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
105
106 #ifndef __MetroNubUtils__
107 #include "MetroNubUtils.h"
108 #endif
109
110 #ifndef __GESTALT__
111 #include <Gestalt.h>
112 #endif
113
114 #if TARGET_API_MAC_CARBON
115
116 #include <CodeFragments.h>
117
118 extern "C" long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
119
120 ProcPtr gCallUniversalProc_Proc = NULL;
121
122 #endif
123
124 static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
125
126 static long fRunOnce = false;
127
128
129 Boolean IsMetroNubInstalled()
130 {
131 if (!fRunOnce)
132 {
133 long result, value;
134
135 fRunOnce = true;
136 gMetroNubEntry = NULL;
137
138 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
139 {
140 // look for MetroNub's Gestalt selector
141 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
142 {
143 #if TARGET_API_MAC_CARBON
144 if (gCallUniversalProc_Proc == NULL)
145 {
146 CFragConnectionID connectionID;
147 Ptr mainAddress;
148 Str255 errorString;
149 ProcPtr symbolAddress;
150 OSErr err;
151 CFragSymbolClass symbolClass;
152
153 symbolAddress = NULL;
154 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
155 &connectionID, &mainAddress, errorString);
156
157 if (err != noErr)
158 {
159 gCallUniversalProc_Proc = NULL;
160 goto end;
161 }
162
163 err = FindSymbol(connectionID, "\pCallUniversalProc",
164 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
165
166 if (err != noErr)
167 {
168 gCallUniversalProc_Proc = NULL;
169 goto end;
170 }
171 }
172 #endif
173
174 {
175 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
176
177 // make sure the version of the API is compatible
178 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
179 kMetroNubUserAPIVersion <= block->apiHiVersion)
180 {
181 // success!
182 gMetroNubEntry = block;
183 }
184 }
185 }
186 }
187 }
188
189 end:
190
191 #if TARGET_API_MAC_CARBON
192 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
193 #else
194 return (gMetroNubEntry != NULL);
195 #endif
196 }
197
198 Boolean IsMWDebuggerRunning()
199 {
200 if (IsMetroNubInstalled())
201 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
202
203 return false;
204 }
205
206 Boolean AmIBeingMWDebugged()
207 {
208 if (IsMetroNubInstalled())
209 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
210
211 return false;
212 }
213
214 extern bool WXDLLEXPORT wxIsDebuggerRunning()
215 {
216 return IsMWDebuggerRunning() && AmIBeingMWDebugged();
217 }
218
219 #else
220
221 extern bool WXDLLEXPORT wxIsDebuggerRunning()
222 {
223 return false;
224 }
225
226 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ >= 0x2400)
227
228
229 #ifndef __DARWIN__
230 // defined in unix/utilsunx.cpp for Mac OS X
231
232 // get full hostname (with domain name if possible)
233 bool wxGetFullHostName(wxChar *buf, int maxSize)
234 {
235 return wxGetHostName(buf, maxSize);
236 }
237
238 // Get user ID e.g. jacs
239 bool wxGetUserId(wxChar *buf, int maxSize)
240 {
241 return wxGetUserName( buf , maxSize );
242 }
243
244 const wxChar* wxGetHomeDir(wxString *pstr)
245 {
246 *pstr = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder );
247 return pstr->c_str();
248 }
249
250 // Get hostname only (without domain name)
251 bool wxGetHostName(wxChar *buf, int maxSize)
252 {
253 // Gets Chooser name of user by examining a System resource.
254 buf[0] = 0;
255
256 const short kComputerNameID = -16413;
257
258 short oldResFile = CurResFile();
259 UseResFile(0);
260 StringHandle chooserName = (StringHandle)::GetString(kComputerNameID);
261 UseResFile(oldResFile);
262
263 if (chooserName && *chooserName)
264 {
265 HLock( (Handle) chooserName );
266 wxString name = wxMacMakeStringFromPascal( *chooserName );
267 HUnlock( (Handle) chooserName );
268 ReleaseResource( (Handle) chooserName );
269 wxStrncpy( buf , name , maxSize - 1 );
270 }
271
272 return true;
273 }
274
275 // Get user name e.g. Stefan Csomor
276 bool wxGetUserName(wxChar *buf, int maxSize)
277 {
278 // Gets Chooser name of user by examining a System resource.
279 buf[0] = 0;
280
281 const short kChooserNameID = -16096;
282
283 short oldResFile = CurResFile();
284 UseResFile(0);
285 StringHandle chooserName = (StringHandle)::GetString(kChooserNameID);
286 UseResFile(oldResFile);
287
288 if (chooserName && *chooserName)
289 {
290 HLock( (Handle) chooserName );
291 wxString name = wxMacMakeStringFromPascal( *chooserName );
292 HUnlock( (Handle) chooserName );
293 ReleaseResource( (Handle) chooserName );
294 wxStrncpy( buf , name , maxSize - 1 );
295 }
296
297 return true;
298 }
299
300 int wxKill(long pid, wxSignal sig , wxKillError *rc, int flags)
301 {
302 // TODO
303 return 0;
304 }
305
306 WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value)
307 {
308 // TODO : under classic there is no environement support, under X yes
309 return false;
310 }
311
312 // set the env var name to the given value, return true on success
313 WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value)
314 {
315 // TODO : under classic there is no environement support, under X yes
316 return false;
317 }
318
319 // Execute a program in an Interactive Shell
320 bool wxShell(const wxString& command)
321 {
322 // TODO
323 return false;
324 }
325
326 // Shutdown or reboot the PC
327 bool wxShutdown(wxShutdownFlags wFlags)
328 {
329 // TODO
330 return false;
331 }
332
333 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
334 wxMemorySize wxGetFreeMemory()
335 {
336 return (wxMemorySize)FreeMem();
337 }
338
339 #ifndef __DARWIN__
340
341 void wxMicroSleep(unsigned long microseconds)
342 {
343 AbsoluteTime wakeup = AddDurationToAbsolute( microseconds * durationMicrosecond , UpTime());
344 MPDelayUntil( & wakeup);
345 }
346
347 void wxMilliSleep(unsigned long milliseconds)
348 {
349 AbsoluteTime wakeup = AddDurationToAbsolute( milliseconds, UpTime());
350 MPDelayUntil( & wakeup);
351 }
352
353 void wxSleep(int nSecs)
354 {
355 wxMilliSleep(1000*nSecs);
356 }
357
358 #endif
359
360 // Consume all events until no more left
361 void wxFlushEvents()
362 {
363 }
364
365 #endif // !__DARWIN__
366
367 // Emit a beeeeeep
368 void wxBell()
369 {
370 SysBeep(30);
371 }
372
373 wxToolkitInfo& wxConsoleAppTraits::GetToolkitInfo()
374 {
375 static wxToolkitInfo info;
376
377 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
378 info.name = _T("wxBase");
379
380 return info;
381 }
382
383 #endif // wxUSE_BASE
384
385 #if wxUSE_GUI
386
387 wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
388 {
389 static wxToolkitInfo info;
390
391 info.os = DoGetOSVersion(&info.versionMajor, &info.versionMinor);
392 info.shortName = _T("mac");
393 info.name = _T("wxMac");
394
395 #ifdef __WXUNIVERSAL__
396 info.shortName << _T("univ");
397 info.name << _T("/wxUniversal");
398 #endif
399
400 return info;
401 }
402
403 // Reading and writing resources (eg WIN.INI, .Xdefaults)
404 #if wxUSE_RESOURCES
405 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
406 {
407 // TODO
408 return false;
409 }
410
411 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
412 {
413 wxString buf;
414 buf.Printf(wxT("%.4f"), value);
415
416 return wxWriteResource(section, entry, buf, file);
417 }
418
419 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
420 {
421 wxString buf;
422 buf.Printf(wxT("%ld"), value);
423
424 return wxWriteResource(section, entry, buf, file);
425 }
426
427 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
428 {
429 wxString buf;
430 buf.Printf(wxT("%d"), value);
431
432 return wxWriteResource(section, entry, buf, file);
433 }
434
435 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
436 {
437 // TODO
438 return false;
439 }
440
441 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
442 {
443 char *s = NULL;
444 bool succ = wxGetResource(section, entry, (char **)&s, file);
445 if (succ)
446 {
447 *value = (float)strtod(s, NULL);
448 delete[] s;
449 }
450
451 return succ;
452 }
453
454 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
455 {
456 char *s = NULL;
457 bool succ = wxGetResource(section, entry, (char **)&s, file);
458 if (succ)
459 {
460 *value = strtol(s, NULL, 10);
461 delete[] s;
462 }
463
464 return succ;
465 }
466
467 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
468 {
469 char *s = NULL;
470 bool succ = wxGetResource(section, entry, (char **)&s, file);
471 if (succ)
472 {
473 *value = (int)strtol(s, NULL, 10);
474 delete[] s;
475 }
476
477 return succ;
478 }
479 #endif // wxUSE_RESOURCES
480
481 int gs_wxBusyCursorCount = 0;
482 extern wxCursor gMacCurrentCursor;
483 wxCursor gMacStoredActiveCursor;
484
485 // Set the cursor to the busy cursor for all windows
486 void wxBeginBusyCursor(const wxCursor *cursor)
487 {
488 if (gs_wxBusyCursorCount++ == 0)
489 {
490 gMacStoredActiveCursor = gMacCurrentCursor;
491 cursor->MacInstall();
492 }
493 //else: nothing to do, already set
494 }
495
496 // Restore cursor to normal
497 void wxEndBusyCursor()
498 {
499 wxCHECK_RET( gs_wxBusyCursorCount > 0,
500 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
501
502 if (--gs_wxBusyCursorCount == 0)
503 {
504 gMacStoredActiveCursor.MacInstall();
505 gMacStoredActiveCursor = wxNullCursor;
506 }
507 }
508
509 // true if we're between the above two calls
510 bool wxIsBusy()
511 {
512 return (gs_wxBusyCursorCount > 0);
513 }
514
515 #endif // wxUSE_GUI
516
517 #if wxUSE_BASE
518
519 wxString wxMacFindFolderNoSeparator( short vol,
520 OSType folderType,
521 Boolean createFolder)
522 {
523 FSRef fsRef;
524 wxString strDir;
525
526 if ( FSFindFolder( vol, folderType, createFolder, &fsRef) == noErr)
527 {
528 strDir = wxMacFSRefToPath( &fsRef );
529 }
530
531 return strDir;
532 }
533
534 wxString wxMacFindFolder( short vol,
535 OSType folderType,
536 Boolean createFolder)
537 {
538 return wxMacFindFolderNoSeparator(vol, folderType, createFolder) + wxFILE_SEP_PATH;
539 }
540
541 #endif // wxUSE_BASE
542
543 #if wxUSE_GUI
544
545 // Check whether this window wants to process messages, e.g. Stop button
546 // in long calculations.
547 bool wxCheckForInterrupt(wxWindow *wnd)
548 {
549 // TODO
550 return false;
551 }
552
553 void wxGetMousePosition( int* x, int* y )
554 {
555 Point pt;
556
557 GetMouse( &pt );
558 LocalToGlobal( &pt );
559 *x = pt.h;
560 *y = pt.v;
561 };
562
563 // Return true if we have a colour display
564 bool wxColourDisplay()
565 {
566 return true;
567 }
568
569 // Returns depth of screen
570 int wxDisplayDepth()
571 {
572 Rect globRect;
573 SetRect(&globRect, -32760, -32760, 32760, 32760);
574 GDHandle theMaxDevice;
575
576 int theDepth = 8;
577 theMaxDevice = GetMaxDevice(&globRect);
578 if (theMaxDevice != NULL)
579 theDepth = (**(**theMaxDevice).gdPMap).pixelSize;
580
581 return theDepth;
582 }
583
584 // Get size of display
585 void wxDisplaySize(int *width, int *height)
586 {
587 BitMap screenBits;
588 GetQDGlobalsScreenBits( &screenBits );
589
590 if (width != NULL)
591 *width = screenBits.bounds.right - screenBits.bounds.left;
592
593 if (height != NULL)
594 *height = screenBits.bounds.bottom - screenBits.bounds.top;
595 }
596
597 void wxDisplaySizeMM(int *width, int *height)
598 {
599 wxDisplaySize(width, height);
600 // on mac 72 is fixed (at least now;-)
601 float cvPt2Mm = 25.4 / 72;
602
603 if (width != NULL)
604 *width = int( *width * cvPt2Mm );
605
606 if (height != NULL)
607 *height = int( *height * cvPt2Mm );
608 }
609
610 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
611 {
612 Rect r;
613
614 GetAvailableWindowPositioningBounds( GetMainDevice() , &r );
615 if ( x )
616 *x = r.left;
617 if ( y )
618 *y = r.top;
619 if ( width )
620 *width = r.right - r.left;
621 if ( height )
622 *height = r.bottom - r.top;
623 }
624
625 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
626 {
627 return wxGenericFindWindowAtPoint(pt);
628 }
629
630 #endif // wxUSE_GUI
631
632 #if wxUSE_BASE
633
634 wxString wxGetOsDescription()
635 {
636 #ifdef WXWIN_OS_DESCRIPTION
637 // use configure generated description if available
638 return wxString(wxT("MacOS (")) + wxT(WXWIN_OS_DESCRIPTION) + wxString(wxT(")"));
639 #else
640 return wxT("MacOS"); //TODO:define further
641 #endif
642 }
643
644 #ifndef __DARWIN__
645 wxChar *wxGetUserHome (const wxString& user)
646 {
647 // TODO
648 return NULL;
649 }
650
651 bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree)
652 {
653 if ( path.empty() )
654 return false;
655
656 wxString p = path;
657 if (p[0u] == ':' )
658 p = wxGetCwd() + p;
659
660 int pos = p.Find(':');
661 if ( pos != wxNOT_FOUND )
662 p = p.Mid(1,pos);
663
664 p = p + wxT(":");
665
666 OSErr err = noErr;
667
668 FSRef fsRef;
669 err = wxMacPathToFSRef( p , &fsRef );
670 if ( noErr == err )
671 {
672 FSVolumeRefNum vRefNum;
673 err = FSGetVRefNum( &fsRef , &vRefNum );
674 if ( noErr == err )
675 {
676 UInt64 freeBytes , totalBytes;
677 err = FSGetVInfo( vRefNum , NULL , &freeBytes , &totalBytes );
678 if ( noErr == err )
679 {
680 if ( pTotal )
681 *pTotal = wxDiskspaceSize_t( totalBytes );
682 if ( pFree )
683 *pFree = wxDiskspaceSize_t( freeBytes );
684 }
685 }
686 }
687
688 return err == noErr;
689 }
690 #endif // !__DARWIN__
691
692 //---------------------------------------------------------------------------
693 // wxMac Specific utility functions
694 //---------------------------------------------------------------------------
695
696 void wxMacStringToPascal( const wxString&from , StringPtr to )
697 {
698 wxCharBuffer buf = from.mb_str( wxConvLocal );
699 int len = strlen(buf);
700
701 if ( len > 255 )
702 len = 255;
703 to[0] = len;
704 memcpy( (char*) &to[1] , buf , len );
705 }
706
707 wxString wxMacMakeStringFromPascal( ConstStringPtr from )
708 {
709 return wxString( (char*) &from[1] , wxConvLocal , from[0] );
710 }
711
712 // ----------------------------------------------------------------------------
713 // Common Event Support
714 // ----------------------------------------------------------------------------
715
716 extern ProcessSerialNumber gAppProcess;
717
718 void wxMacWakeUp()
719 {
720 ProcessSerialNumber psn;
721 Boolean isSame;
722 psn.highLongOfPSN = 0;
723 psn.lowLongOfPSN = kCurrentProcess;
724 SameProcess( &gAppProcess , &psn , &isSame );
725 if ( isSame )
726 {
727 #if TARGET_CARBON
728 OSStatus err = noErr;
729
730 #if 0
731 // lead sometimes to race conditions, although all calls used should be thread safe ...
732 static wxMacCarbonEvent s_wakeupEvent;
733 if ( !s_wakeupEvent.IsValid() )
734 {
735 err = s_wakeupEvent.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
736 kEventAttributeNone );
737 }
738 if ( err == noErr )
739 {
740
741 if ( IsEventInQueue( GetMainEventQueue() , s_wakeupEvent ) )
742 return;
743 s_wakeupEvent.SetCurrentTime();
744 err = PostEventToQueue(GetMainEventQueue(), s_wakeupEvent,
745 kEventPriorityHigh );
746 }
747 #else
748 wxMacCarbonEvent wakeupEvent;
749 wakeupEvent.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
750 kEventAttributeNone );
751 err = PostEventToQueue(GetMainEventQueue(), wakeupEvent,
752 kEventPriorityHigh );
753 #endif
754 #else
755 PostEvent( nullEvent , 0 );
756 #endif
757 }
758 else
759 {
760 WakeUpProcess( &gAppProcess );
761 }
762 }
763
764 #endif // wxUSE_BASE
765
766 #if wxUSE_GUI
767
768 // ----------------------------------------------------------------------------
769 // Native Struct Conversions
770 // ----------------------------------------------------------------------------
771
772 void wxMacRectToNative( const wxRect *wx , Rect *n )
773 {
774 n->left = wx->x;
775 n->top = wx->y;
776 n->right = wx->x + wx->width;
777 n->bottom = wx->y + wx->height;
778 }
779
780 void wxMacNativeToRect( const Rect *n , wxRect* wx )
781 {
782 wx->x = n->left;
783 wx->y = n->top;
784 wx->width = n->right - n->left;
785 wx->height = n->bottom - n->top;
786 }
787
788 void wxMacPointToNative( const wxPoint* wx , Point *n )
789 {
790 n->h = wx->x;
791 n->v = wx->y;
792 }
793
794 void wxMacNativeToPoint( const Point *n , wxPoint* wx )
795 {
796 wx->x = n->h;
797 wx->y = n->v;
798 }
799
800 // ----------------------------------------------------------------------------
801 // Carbon Event Support
802 // ----------------------------------------------------------------------------
803
804 OSStatus wxMacCarbonEvent::GetParameter(EventParamName inName, EventParamType inDesiredType, UInt32 inBufferSize, void * outData)
805 {
806 return ::GetEventParameter( m_eventRef , inName , inDesiredType , NULL , inBufferSize , NULL , outData );
807 }
808
809 OSStatus wxMacCarbonEvent::SetParameter(EventParamName inName, EventParamType inType, UInt32 inBufferSize, const void * inData)
810 {
811 return ::SetEventParameter( m_eventRef , inName , inType , inBufferSize , inData );
812 }
813
814 // ----------------------------------------------------------------------------
815 // Control Access Support
816 // ----------------------------------------------------------------------------
817
818 wxMacControl::wxMacControl(wxWindow* peer , bool isRootControl )
819 {
820 Init();
821 m_peer = peer;
822 m_isRootControl = isRootControl;
823 }
824
825 wxMacControl::wxMacControl( wxWindow* peer , ControlRef control )
826 {
827 Init();
828 m_peer = peer;
829 m_controlRef = control;
830 }
831
832 wxMacControl::wxMacControl( wxWindow* peer , WXWidget control )
833 {
834 Init();
835 m_peer = peer;
836 m_controlRef = (ControlRef) control;
837 }
838
839 wxMacControl::~wxMacControl()
840 {
841 }
842
843 void wxMacControl::Init()
844 {
845 m_peer = NULL;
846 m_controlRef = NULL;
847 m_needsFocusRect = false;
848 m_isRootControl = false;
849 }
850
851 void wxMacControl::Dispose()
852 {
853 ::DisposeControl( m_controlRef );
854 m_controlRef = NULL;
855 }
856
857 void wxMacControl::SetReference( SInt32 data )
858 {
859 SetControlReference( m_controlRef , data );
860 }
861
862 OSStatus wxMacControl::GetData(ControlPartCode inPartCode , ResType inTag , Size inBufferSize , void * inOutBuffer , Size * outActualSize ) const
863 {
864 return ::GetControlData( m_controlRef , inPartCode , inTag , inBufferSize , inOutBuffer , outActualSize );
865 }
866
867 OSStatus wxMacControl::GetDataSize(ControlPartCode inPartCode , ResType inTag , Size * outActualSize ) const
868 {
869 return ::GetControlDataSize( m_controlRef , inPartCode , inTag , outActualSize );
870 }
871
872 OSStatus wxMacControl::SetData(ControlPartCode inPartCode , ResType inTag , Size inSize , const void * inData)
873 {
874 return ::SetControlData( m_controlRef , inPartCode , inTag , inSize , inData );
875 }
876
877 OSStatus wxMacControl::SendEvent( EventRef event , OptionBits inOptions )
878 {
879 #if TARGET_API_MAC_OSX
880 return SendEventToEventTargetWithOptions( event,
881 HIObjectGetEventTarget( (HIObjectRef) m_controlRef ), inOptions );
882 #else
883 #pragma unused(inOptions)
884 return SendEventToEventTarget(event,GetControlEventTarget( m_controlRef ) );
885 #endif
886 }
887
888 OSStatus wxMacControl::SendHICommand( HICommand &command , OptionBits inOptions )
889 {
890 wxMacCarbonEvent event( kEventClassCommand , kEventCommandProcess );
891
892 event.SetParameter<HICommand>(kEventParamDirectObject,command);
893
894 return SendEvent( event , inOptions );
895 }
896
897 OSStatus wxMacControl::SendHICommand( UInt32 commandID , OptionBits inOptions )
898 {
899 HICommand command;
900
901 memset( &command, 0 , sizeof(command) );
902 command.commandID = commandID;
903 return SendHICommand( command , inOptions );
904 }
905
906 void wxMacControl::Flash( ControlPartCode part , UInt32 ticks )
907 {
908 unsigned long finalTicks;
909
910 HiliteControl( m_controlRef , part );
911 Delay( ticks , &finalTicks );
912 HiliteControl( m_controlRef , kControlNoPart );
913 }
914
915 SInt32 wxMacControl::GetValue() const
916 {
917 return ::GetControl32BitValue( m_controlRef );
918 }
919
920 SInt32 wxMacControl::GetMaximum() const
921 {
922 return ::GetControl32BitMaximum( m_controlRef );
923 }
924
925 SInt32 wxMacControl::GetMinimum() const
926 {
927 return ::GetControl32BitMinimum( m_controlRef );
928 }
929
930 void wxMacControl::SetValue( SInt32 v )
931 {
932 ::SetControl32BitValue( m_controlRef , v );
933 }
934
935 void wxMacControl::SetMinimum( SInt32 v )
936 {
937 ::SetControl32BitMinimum( m_controlRef , v );
938 }
939
940 void wxMacControl::SetMaximum( SInt32 v )
941 {
942 ::SetControl32BitMaximum( m_controlRef , v );
943 }
944
945 void wxMacControl::SetValueAndRange( SInt32 value , SInt32 minimum , SInt32 maximum )
946 {
947 ::SetControl32BitMinimum( m_controlRef , minimum );
948 ::SetControl32BitMaximum( m_controlRef , maximum );
949 ::SetControl32BitValue( m_controlRef , value );
950 }
951
952 OSStatus wxMacControl::SetFocus( ControlFocusPart focusPart )
953 {
954 return SetKeyboardFocus( GetControlOwner( m_controlRef ), m_controlRef, focusPart );
955 }
956
957 bool wxMacControl::HasFocus() const
958 {
959 ControlRef control;
960 GetKeyboardFocus( GetUserFocusWindow() , &control );
961 return control == m_controlRef;
962 }
963
964 void wxMacControl::SetNeedsFocusRect( bool needs )
965 {
966 m_needsFocusRect = needs;
967 }
968
969 bool wxMacControl::NeedsFocusRect() const
970 {
971 return m_needsFocusRect;
972 }
973
974 void wxMacControl::VisibilityChanged(bool shown)
975 {
976 }
977
978 void wxMacControl::SuperChangedPosition()
979 {
980 }
981
982 void wxMacControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
983 {
984 m_font = font;
985 ControlFontStyleRec fontStyle;
986 if ( font.MacGetThemeFontID() != kThemeCurrentPortFont )
987 {
988 switch ( font.MacGetThemeFontID() )
989 {
990 case kThemeSmallSystemFont :
991 fontStyle.font = kControlFontSmallSystemFont;
992 break;
993
994 case 109 : // mini font
995 fontStyle.font = -5;
996 break;
997
998 case kThemeSystemFont :
999 fontStyle.font = kControlFontBigSystemFont;
1000 break;
1001
1002 default :
1003 fontStyle.font = kControlFontBigSystemFont;
1004 break;
1005 }
1006
1007 fontStyle.flags = kControlUseFontMask;
1008 }
1009 else
1010 {
1011 fontStyle.font = font.MacGetFontNum();
1012 fontStyle.style = font.MacGetFontStyle();
1013 fontStyle.size = font.MacGetFontSize();
1014 fontStyle.flags = kControlUseFontMask | kControlUseFaceMask | kControlUseSizeMask;
1015 }
1016
1017 fontStyle.just = teJustLeft;
1018 fontStyle.flags |= kControlUseJustMask;
1019 if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_CENTER_HORIZONTAL )
1020 fontStyle.just = teJustCenter;
1021 else if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_RIGHT )
1022 fontStyle.just = teJustRight;
1023
1024
1025 // we only should do this in case of a non-standard color, as otherwise 'disabled' controls
1026 // won't get grayed out by the system anymore
1027
1028 if ( foreground != *wxBLACK )
1029 {
1030 fontStyle.foreColor = MAC_WXCOLORREF( foreground.GetPixel() );
1031 fontStyle.flags |= kControlUseForeColorMask;
1032 }
1033
1034 ::SetControlFontStyle( m_controlRef , &fontStyle );
1035 }
1036
1037 void wxMacControl::SetBackground( const wxBrush &WXUNUSED(brush) )
1038 {
1039 // TODO
1040 // setting up a color proc is not recommended anymore
1041 }
1042
1043 void wxMacControl::SetRange( SInt32 minimum , SInt32 maximum )
1044 {
1045 ::SetControl32BitMinimum( m_controlRef , minimum );
1046 ::SetControl32BitMaximum( m_controlRef , maximum );
1047 }
1048
1049 short wxMacControl::HandleKey( SInt16 keyCode, SInt16 charCode, EventModifiers modifiers )
1050 {
1051 return HandleControlKey( m_controlRef , keyCode , charCode , modifiers );
1052 }
1053
1054 void wxMacControl::SetActionProc( ControlActionUPP actionProc )
1055 {
1056 SetControlAction( m_controlRef , actionProc );
1057 }
1058
1059 void wxMacControl::SetViewSize( SInt32 viewSize )
1060 {
1061 SetControlViewSize(m_controlRef , viewSize );
1062 }
1063
1064 SInt32 wxMacControl::GetViewSize() const
1065 {
1066 return GetControlViewSize( m_controlRef );
1067 }
1068
1069 bool wxMacControl::IsVisible() const
1070 {
1071 return IsControlVisible( m_controlRef );
1072 }
1073
1074 void wxMacControl::SetVisibility( bool visible , bool redraw )
1075 {
1076 SetControlVisibility( m_controlRef , visible , redraw );
1077 }
1078
1079 bool wxMacControl::IsEnabled() const
1080 {
1081 #if TARGET_API_MAC_OSX
1082 return IsControlEnabled( m_controlRef );
1083 #else
1084 return IsControlActive( m_controlRef );
1085 #endif
1086 }
1087
1088 bool wxMacControl::IsActive() const
1089 {
1090 return IsControlActive( m_controlRef );
1091 }
1092
1093 void wxMacControl::Enable( bool enable )
1094 {
1095 if ( enable )
1096 EnableControl( m_controlRef );
1097 else
1098 DisableControl( m_controlRef );
1099 }
1100
1101 void wxMacControl::SetDrawingEnabled( bool enable )
1102 {
1103 HIViewSetDrawingEnabled( m_controlRef , enable );
1104 }
1105
1106 bool wxMacControl::GetNeedsDisplay() const
1107 {
1108 return HIViewGetNeedsDisplay( m_controlRef );
1109 }
1110
1111 void wxMacControl::SetNeedsDisplay( RgnHandle where )
1112 {
1113 if ( !IsVisible() )
1114 return;
1115
1116 HIViewSetNeedsDisplayInRegion( m_controlRef , where , true );
1117 }
1118
1119 void wxMacControl::SetNeedsDisplay( Rect* where )
1120 {
1121 if ( !IsVisible() )
1122 return;
1123
1124 if ( where != NULL )
1125 {
1126 RgnHandle update = NewRgn();
1127 RectRgn( update , where );
1128 HIViewSetNeedsDisplayInRegion( m_controlRef , update , true );
1129 DisposeRgn( update );
1130 }
1131 else
1132 HIViewSetNeedsDisplay( m_controlRef , true );
1133 }
1134
1135 void wxMacControl::Convert( wxPoint *pt , wxMacControl *from , wxMacControl *to )
1136 {
1137 HIPoint hiPoint;
1138
1139 hiPoint.x = pt->x;
1140 hiPoint.y = pt->y;
1141 HIViewConvertPoint( &hiPoint , from->m_controlRef , to->m_controlRef );
1142 pt->x = (int)hiPoint.x;
1143 pt->y = (int)hiPoint.y;
1144 }
1145
1146 void wxMacControl::SetRect( Rect *r )
1147 {
1148 //A HIRect is actually a CGRect on OSX - which consists of two structures -
1149 //CGPoint and CGSize, which have two floats each
1150 HIRect hir = { { r->left , r->top }, { r->right - r->left , r->bottom - r->top } };
1151 HIViewSetFrame ( m_controlRef , &hir );
1152 // eventuall we might have to do a SetVisibility( false , true );
1153 // before and a SetVisibility( true , true ); after
1154 }
1155
1156 void wxMacControl::GetRect( Rect *r )
1157 {
1158 GetControlBounds( m_controlRef , r );
1159 }
1160
1161 void wxMacControl::GetRectInWindowCoords( Rect *r )
1162 {
1163 UMAGetControlBoundsInWindowCoords( m_controlRef , r );
1164 }
1165
1166 void wxMacControl::GetBestRect( Rect *r )
1167 {
1168 short baselineoffset;
1169
1170 GetBestControlRect( m_controlRef , r , &baselineoffset );
1171 }
1172
1173 void wxMacControl::SetLabel( const wxString &title )
1174 {
1175 wxFontEncoding encoding;
1176
1177 if ( m_font.Ok() )
1178 encoding = m_font.GetEncoding();
1179 else
1180 encoding = wxFont::GetDefaultEncoding();
1181
1182 UMASetControlTitle( m_controlRef , title , encoding );
1183 }
1184
1185 void wxMacControl::GetFeatures( UInt32 * features )
1186 {
1187 GetControlFeatures( m_controlRef , features );
1188 }
1189
1190 OSStatus wxMacControl::GetRegion( ControlPartCode partCode , RgnHandle region )
1191 {
1192 OSStatus err = GetControlRegion( m_controlRef , partCode , region );
1193 return err;
1194 }
1195
1196 OSStatus wxMacControl::SetZOrder( bool above , wxMacControl* other )
1197 {
1198 #if TARGET_API_MAC_OSX
1199 return HIViewSetZOrder( m_controlRef,above ? kHIViewZOrderAbove : kHIViewZOrderBelow,
1200 (other != NULL) ? other->m_controlRef : NULL);
1201 #else
1202 return 0;
1203 #endif
1204 }
1205
1206 #if TARGET_API_MAC_OSX
1207 // SetNeedsDisplay would not invalidate the children
1208 static void InvalidateControlAndChildren( HIViewRef control )
1209 {
1210 HIViewSetNeedsDisplay( control , true );
1211 UInt16 childrenCount = 0;
1212 OSStatus err = CountSubControls( control , &childrenCount );
1213 if ( err == errControlIsNotEmbedder )
1214 return;
1215
1216 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") );
1217
1218 for ( UInt16 i = childrenCount; i >=1; --i )
1219 {
1220 HIViewRef child;
1221
1222 err = GetIndexedSubControl( control , i , & child );
1223 if ( err == errControlIsNotEmbedder )
1224 return;
1225
1226 InvalidateControlAndChildren( child );
1227 }
1228 }
1229 #endif
1230
1231 void wxMacControl::InvalidateWithChildren()
1232 {
1233 #if TARGET_API_MAC_OSX
1234 InvalidateControlAndChildren( m_controlRef );
1235 #endif
1236 }
1237
1238 void wxMacControl::ScrollRect( wxRect *r , int dx , int dy )
1239 {
1240 wxASSERT( r != NULL );
1241
1242 HIRect scrollarea = CGRectMake( r->x , r->y , r->width , r->height);
1243 HIViewScrollRect ( m_controlRef , &scrollarea , dx ,dy );
1244 }
1245
1246 OSType wxMacCreator = 'WXMC';
1247 OSType wxMacControlProperty = 'MCCT';
1248
1249 void wxMacControl::SetReferenceInNativeControl()
1250 {
1251 void * data = this;
1252 verify_noerr( SetControlProperty ( m_controlRef ,
1253 wxMacCreator,wxMacControlProperty, sizeof(data), &data ) );
1254 }
1255
1256 wxMacControl* wxMacControl::GetReferenceFromNativeControl(ControlRef control)
1257 {
1258 wxMacControl* ctl = NULL;
1259 UInt32 actualSize;
1260 if ( GetControlProperty( control ,wxMacCreator,wxMacControlProperty, sizeof(ctl) ,
1261 &actualSize , &ctl ) == noErr )
1262 {
1263 return ctl;
1264 }
1265 return NULL;
1266 }
1267
1268 // ============================================================================
1269 // DataBrowser Wrapper
1270 // ============================================================================
1271 //
1272 // basing on DataBrowserItemIDs
1273 //
1274
1275 pascal void wxMacDataBrowserControl::DataBrowserItemNotificationProc(
1276 ControlRef browser,
1277 DataBrowserItemID itemID,
1278 DataBrowserItemNotification message,
1279 DataBrowserItemDataRef itemData )
1280 {
1281 wxMacDataBrowserControl* ctl = dynamic_cast<wxMacDataBrowserControl*>( wxMacControl::GetReferenceFromNativeControl( browser ) );
1282 if ( ctl != 0 )
1283 {
1284 ctl->ItemNotification(itemID, message, itemData);
1285 }
1286 }
1287
1288 pascal OSStatus wxMacDataBrowserControl::DataBrowserGetSetItemDataProc(
1289 ControlRef browser,
1290 DataBrowserItemID itemID,
1291 DataBrowserPropertyID property,
1292 DataBrowserItemDataRef itemData,
1293 Boolean changeValue )
1294 {
1295 OSStatus err = errDataBrowserPropertyNotSupported;
1296
1297 wxMacDataBrowserControl* ctl = dynamic_cast<wxMacDataBrowserControl*>( wxMacControl::GetReferenceFromNativeControl( browser ) );
1298 if ( ctl != 0 )
1299 {
1300 err = ctl->GetSetItemData(itemID, property, itemData, changeValue);
1301 }
1302 return err;
1303 }
1304
1305 pascal Boolean wxMacDataBrowserControl::DataBrowserCompareProc(
1306 ControlRef browser,
1307 DataBrowserItemID itemOneID,
1308 DataBrowserItemID itemTwoID,
1309 DataBrowserPropertyID sortProperty)
1310 {
1311 wxMacDataBrowserControl* ctl = dynamic_cast<wxMacDataBrowserControl*>( wxMacControl::GetReferenceFromNativeControl( browser ) );
1312 if ( ctl != 0 )
1313 {
1314 return ctl->CompareItems(itemOneID, itemTwoID, sortProperty);
1315 }
1316 return false;
1317 }
1318
1319 DataBrowserItemDataUPP gDataBrowserItemDataUPP = NULL;
1320 DataBrowserItemNotificationUPP gDataBrowserItemNotificationUPP = NULL;
1321 DataBrowserItemCompareUPP gDataBrowserItemCompareUPP = NULL;
1322
1323 wxMacDataBrowserControl::wxMacDataBrowserControl( wxWindow* peer, const wxPoint& pos, const wxSize& size, long style) : wxMacControl( peer )
1324 {
1325 Rect bounds = wxMacGetBoundsForControl( peer, pos, size );
1326 OSStatus err = ::CreateDataBrowserControl(
1327 MAC_WXHWND(peer->MacGetTopLevelWindowRef()),
1328 &bounds, kDataBrowserListView, &m_controlRef );
1329 SetReferenceInNativeControl();
1330 verify_noerr( err );
1331 if ( gDataBrowserItemCompareUPP == NULL )
1332 gDataBrowserItemCompareUPP = NewDataBrowserItemCompareUPP(DataBrowserCompareProc);
1333 if ( gDataBrowserItemDataUPP == NULL )
1334 gDataBrowserItemDataUPP = NewDataBrowserItemDataUPP(DataBrowserGetSetItemDataProc);
1335 if ( gDataBrowserItemNotificationUPP == NULL )
1336 {
1337 gDataBrowserItemNotificationUPP =
1338 #if TARGET_API_MAC_OSX
1339 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc);
1340 #else
1341 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc);
1342 #endif
1343 }
1344
1345 DataBrowserCallbacks callbacks;
1346 InitializeDataBrowserCallbacks( &callbacks, kDataBrowserLatestCallbacks );
1347
1348 callbacks.u.v1.itemDataCallback = gDataBrowserItemDataUPP;
1349 callbacks.u.v1.itemCompareCallback = gDataBrowserItemCompareUPP;
1350 callbacks.u.v1.itemNotificationCallback = gDataBrowserItemNotificationUPP;
1351 SetCallbacks( &callbacks );
1352
1353 }
1354
1355 OSStatus wxMacDataBrowserControl::GetItemCount( DataBrowserItemID container,
1356 Boolean recurse,
1357 DataBrowserItemState state,
1358 UInt32 *numItems) const
1359 {
1360 return GetDataBrowserItemCount( m_controlRef, container, recurse, state, numItems );
1361 }
1362
1363 OSStatus wxMacDataBrowserControl::GetItems( DataBrowserItemID container,
1364 Boolean recurse,
1365 DataBrowserItemState state,
1366 Handle items) const
1367 {
1368 return GetDataBrowserItems( m_controlRef, container, recurse, state, items );
1369 }
1370
1371 OSStatus wxMacDataBrowserControl::SetSelectionFlags( DataBrowserSelectionFlags options )
1372 {
1373 return SetDataBrowserSelectionFlags( m_controlRef, options );
1374 }
1375
1376 OSStatus wxMacDataBrowserControl::AddColumn( DataBrowserListViewColumnDesc *columnDesc,
1377 DataBrowserTableViewColumnIndex position )
1378 {
1379 return AddDataBrowserListViewColumn( m_controlRef, columnDesc, position );
1380 }
1381
1382 OSStatus wxMacDataBrowserControl::AutoSizeColumns()
1383 {
1384 return AutoSizeDataBrowserListViewColumns(m_controlRef);
1385 }
1386
1387 OSStatus wxMacDataBrowserControl::SetHasScrollBars( bool horiz, bool vert )
1388 {
1389 return SetDataBrowserHasScrollBars( m_controlRef, horiz, vert );
1390 }
1391
1392 OSStatus wxMacDataBrowserControl::SetHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle )
1393 {
1394 return SetDataBrowserTableViewHiliteStyle( m_controlRef, hiliteStyle );
1395 }
1396
1397 OSStatus wxMacDataBrowserControl::SetHeaderButtonHeight(UInt16 height)
1398 {
1399 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef, height );
1400 }
1401
1402 OSStatus wxMacDataBrowserControl::GetHeaderButtonHeight(UInt16 *height)
1403 {
1404 return GetDataBrowserListViewHeaderBtnHeight( m_controlRef, height );
1405 }
1406
1407 OSStatus wxMacDataBrowserControl::SetCallbacks(const DataBrowserCallbacks *callbacks)
1408 {
1409 return SetDataBrowserCallbacks( m_controlRef, callbacks );
1410 }
1411
1412 OSStatus wxMacDataBrowserControl::UpdateItems(
1413 DataBrowserItemID container,
1414 UInt32 numItems,
1415 const DataBrowserItemID *items,
1416 DataBrowserPropertyID preSortProperty,
1417 DataBrowserPropertyID propertyID ) const
1418 {
1419 return UpdateDataBrowserItems( m_controlRef, container, numItems, items, preSortProperty, propertyID );
1420 }
1421
1422 bool wxMacDataBrowserControl::IsItemSelected( DataBrowserItemID item ) const
1423 {
1424 return IsDataBrowserItemSelected( m_controlRef, item );
1425 }
1426
1427 OSStatus wxMacDataBrowserControl::AddItems(
1428 DataBrowserItemID container,
1429 UInt32 numItems,
1430 const DataBrowserItemID *items,
1431 DataBrowserPropertyID preSortProperty )
1432 {
1433 return AddDataBrowserItems( m_controlRef, container, numItems, items, preSortProperty );
1434 }
1435
1436 OSStatus wxMacDataBrowserControl::RemoveItems(
1437 DataBrowserItemID container,
1438 UInt32 numItems,
1439 const DataBrowserItemID *items,
1440 DataBrowserPropertyID preSortProperty )
1441 {
1442 return RemoveDataBrowserItems( m_controlRef, container, numItems, items, preSortProperty );
1443 }
1444
1445 OSStatus wxMacDataBrowserControl::RevealItem(
1446 DataBrowserItemID item,
1447 DataBrowserPropertyID propertyID,
1448 DataBrowserRevealOptions options ) const
1449 {
1450 return RevealDataBrowserItem( m_controlRef, item, propertyID, options );
1451 }
1452
1453 OSStatus wxMacDataBrowserControl::SetSelectedItems(
1454 UInt32 numItems,
1455 const DataBrowserItemID *items,
1456 DataBrowserSetOption operation )
1457 {
1458 return SetDataBrowserSelectedItems( m_controlRef, numItems, items, operation );
1459 }
1460
1461 OSStatus wxMacDataBrowserControl::GetSelectionAnchor( DataBrowserItemID *first, DataBrowserItemID *last ) const
1462 {
1463 return GetDataBrowserSelectionAnchor( m_controlRef, first, last );
1464 }
1465
1466 OSStatus wxMacDataBrowserControl::GetItemID( DataBrowserTableViewRowIndex row, DataBrowserItemID * item ) const
1467 {
1468 return GetDataBrowserTableViewItemID( m_controlRef, row, item );
1469 }
1470
1471 OSStatus wxMacDataBrowserControl::GetItemRow( DataBrowserItemID item, DataBrowserTableViewRowIndex * row ) const
1472 {
1473 return GetDataBrowserTableViewItemRow( m_controlRef, item, row );
1474 }
1475
1476 OSStatus wxMacDataBrowserControl::SetDefaultRowHeight( UInt16 height )
1477 {
1478 return SetDataBrowserTableViewRowHeight( m_controlRef , height );
1479 }
1480
1481 OSStatus wxMacDataBrowserControl::GetDefaultRowHeight( UInt16 * height ) const
1482 {
1483 return GetDataBrowserTableViewRowHeight( m_controlRef, height );
1484 }
1485
1486 OSStatus wxMacDataBrowserControl::SetRowHeight( DataBrowserItemID item , UInt16 height)
1487 {
1488 return SetDataBrowserTableViewItemRowHeight( m_controlRef, item , height );
1489 }
1490
1491 OSStatus wxMacDataBrowserControl::GetRowHeight( DataBrowserItemID item , UInt16 *height) const
1492 {
1493 return GetDataBrowserTableViewItemRowHeight( m_controlRef, item , height);
1494 }
1495
1496 OSStatus wxMacDataBrowserControl::GetColumnWidth( DataBrowserPropertyID column , UInt16 *width ) const
1497 {
1498 return GetDataBrowserTableViewNamedColumnWidth( m_controlRef , column , width );
1499 }
1500
1501 OSStatus wxMacDataBrowserControl::SetColumnWidth( DataBrowserPropertyID column , UInt16 width )
1502 {
1503 return SetDataBrowserTableViewNamedColumnWidth( m_controlRef , column , width );
1504 }
1505
1506 OSStatus wxMacDataBrowserControl::GetDefaultColumnWidth( UInt16 *width ) const
1507 {
1508 return GetDataBrowserTableViewColumnWidth( m_controlRef , width );
1509 }
1510
1511 OSStatus wxMacDataBrowserControl::SetDefaultColumnWidth( UInt16 width )
1512 {
1513 return SetDataBrowserTableViewColumnWidth( m_controlRef , width );
1514 }
1515
1516 OSStatus wxMacDataBrowserControl::GetColumnCount(UInt32* numColumns) const
1517 {
1518 return GetDataBrowserTableViewColumnCount( m_controlRef, numColumns);
1519 }
1520
1521 OSStatus wxMacDataBrowserControl::GetColumnPosition( DataBrowserPropertyID column,
1522 UInt32 *position) const
1523 {
1524 return GetDataBrowserTableViewColumnPosition( m_controlRef , column , position);
1525 }
1526
1527 OSStatus wxMacDataBrowserControl::SetColumnPosition( DataBrowserPropertyID column, UInt32 position)
1528 {
1529 return SetDataBrowserTableViewColumnPosition( m_controlRef , column , position);
1530 }
1531
1532 OSStatus wxMacDataBrowserControl::GetScrollPosition( UInt32 *top , UInt32 *left ) const
1533 {
1534 return GetDataBrowserScrollPosition( m_controlRef , top , left );
1535 }
1536
1537 OSStatus wxMacDataBrowserControl::SetScrollPosition( UInt32 top , UInt32 left )
1538 {
1539 return SetDataBrowserScrollPosition( m_controlRef , top , left );
1540 }
1541
1542 OSStatus wxMacDataBrowserControl::GetSortProperty( DataBrowserPropertyID *column ) const
1543 {
1544 return GetDataBrowserSortProperty( m_controlRef , column );
1545 }
1546
1547 OSStatus wxMacDataBrowserControl::SetSortProperty( DataBrowserPropertyID column )
1548 {
1549 return SetDataBrowserSortProperty( m_controlRef , column );
1550 }
1551
1552 OSStatus wxMacDataBrowserControl::GetSortOrder( DataBrowserSortOrder *order ) const
1553 {
1554 return GetDataBrowserSortOrder( m_controlRef , order );
1555 }
1556
1557 OSStatus wxMacDataBrowserControl::SetSortOrder( DataBrowserSortOrder order )
1558 {
1559 return SetDataBrowserSortOrder( m_controlRef , order );
1560 }
1561
1562 OSStatus wxMacDataBrowserControl::GetPropertyFlags( DataBrowserPropertyID property,
1563 DataBrowserPropertyFlags *flags ) const
1564 {
1565 return GetDataBrowserPropertyFlags( m_controlRef , property , flags );
1566 }
1567
1568 OSStatus wxMacDataBrowserControl::SetPropertyFlags( DataBrowserPropertyID property,
1569 DataBrowserPropertyFlags flags )
1570 {
1571 return SetDataBrowserPropertyFlags( m_controlRef , property , flags );
1572 }
1573
1574 OSStatus wxMacDataBrowserControl::GetHeaderDesc( DataBrowserPropertyID property,
1575 DataBrowserListViewHeaderDesc *desc ) const
1576 {
1577 return GetDataBrowserListViewHeaderDesc( m_controlRef , property , desc );
1578 }
1579
1580 OSStatus wxMacDataBrowserControl::SetHeaderDesc( DataBrowserPropertyID property,
1581 DataBrowserListViewHeaderDesc *desc )
1582 {
1583 return SetDataBrowserListViewHeaderDesc( m_controlRef , property , desc );
1584 }
1585
1586 OSStatus wxMacDataBrowserControl::SetDisclosureColumn( DataBrowserPropertyID property ,
1587 Boolean expandableRows )
1588 {
1589 return SetDataBrowserListViewDisclosureColumn( m_controlRef, property, expandableRows);
1590 }
1591
1592 // ============================================================================
1593 // Higher-level Databrowser
1594 // ============================================================================
1595 //
1596 // basing on data item objects
1597 //
1598
1599 wxMacDataItem::wxMacDataItem()
1600 {
1601 }
1602
1603 wxMacDataItem::~wxMacDataItem()
1604 {
1605 }
1606
1607 bool wxMacDataItem::IsLessThan(wxMacDataItemBrowserControl *owner ,
1608 const wxMacDataItem*,
1609 DataBrowserPropertyID property) const
1610 {
1611 return false;
1612 }
1613
1614 OSStatus wxMacDataItem::GetSetData(wxMacDataItemBrowserControl *owner ,
1615 DataBrowserPropertyID property,
1616 DataBrowserItemDataRef itemData,
1617 bool changeValue )
1618 {
1619 return errDataBrowserPropertyNotSupported;
1620 }
1621
1622 void wxMacDataItem::Notification(wxMacDataItemBrowserControl *owner ,
1623 DataBrowserItemNotification message,
1624 DataBrowserItemDataRef itemData ) const
1625 {
1626 }
1627
1628
1629 wxMacDataItemBrowserControl::wxMacDataItemBrowserControl( wxWindow* peer , const wxPoint& pos, const wxSize& size, long style) :
1630 wxMacDataBrowserControl( peer, pos, size, style )
1631 {
1632 m_suppressSelection = false;
1633 }
1634
1635 wxMacDataItemBrowserSelectionSuppressor::wxMacDataItemBrowserSelectionSuppressor(wxMacDataItemBrowserControl *browser)
1636 {
1637 m_former = browser->SuppressSelection(true);
1638 m_browser = browser;
1639 }
1640
1641 wxMacDataItemBrowserSelectionSuppressor::~wxMacDataItemBrowserSelectionSuppressor()
1642 {
1643 m_browser->SuppressSelection(m_former);
1644 }
1645
1646 bool wxMacDataItemBrowserControl::SuppressSelection( bool suppress )
1647 {
1648 bool former = m_suppressSelection;
1649 m_suppressSelection = suppress;
1650
1651 return former;
1652 }
1653
1654 Boolean wxMacDataItemBrowserControl::CompareItems(DataBrowserItemID itemOneID,
1655 DataBrowserItemID itemTwoID,
1656 DataBrowserPropertyID sortProperty)
1657 {
1658 wxMacDataItem* itemOne = (wxMacDataItem*) itemOneID;
1659 wxMacDataItem* itemTwo = (wxMacDataItem*) itemTwoID;
1660 return CompareItems( itemOne , itemTwo , sortProperty );
1661 }
1662
1663 Boolean wxMacDataItemBrowserControl::CompareItems(const wxMacDataItem* itemOne,
1664 const wxMacDataItem* itemTwo,
1665 DataBrowserPropertyID sortProperty)
1666 {
1667 Boolean retval = false;
1668 if ( itemOne != NULL )
1669 retval = itemOne->IsLessThan( this , itemTwo , sortProperty);
1670 return retval;
1671 }
1672
1673 OSStatus wxMacDataItemBrowserControl::GetSetItemData(
1674 DataBrowserItemID itemID,
1675 DataBrowserPropertyID property,
1676 DataBrowserItemDataRef itemData,
1677 Boolean changeValue )
1678 {
1679 wxMacDataItem* item = (wxMacDataItem*) itemID;
1680 return GetSetItemData(item, property, itemData , changeValue );
1681 }
1682
1683 OSStatus wxMacDataItemBrowserControl::GetSetItemData(
1684 wxMacDataItem* item,
1685 DataBrowserPropertyID property,
1686 DataBrowserItemDataRef itemData,
1687 Boolean changeValue )
1688 {
1689 OSStatus err = errDataBrowserPropertyNotSupported;
1690 switch( property )
1691 {
1692 case kDataBrowserContainerIsClosableProperty :
1693 case kDataBrowserContainerIsSortableProperty :
1694 case kDataBrowserContainerIsOpenableProperty :
1695 // right now default behaviour on these
1696 break;
1697 default :
1698 if ( item != NULL )
1699 err = item->GetSetData( this, property , itemData , changeValue );
1700 break;
1701
1702 }
1703 return err;
1704 }
1705
1706 void wxMacDataItemBrowserControl::ItemNotification(
1707 DataBrowserItemID itemID,
1708 DataBrowserItemNotification message,
1709 DataBrowserItemDataRef itemData)
1710 {
1711 wxMacDataItem* item = (wxMacDataItem*) itemID;
1712 ItemNotification( item , message, itemData);
1713 }
1714
1715 void wxMacDataItemBrowserControl::ItemNotification(
1716 const wxMacDataItem* item,
1717 DataBrowserItemNotification message,
1718 DataBrowserItemDataRef itemData)
1719 {
1720 if (item != NULL)
1721 item->Notification( this, message, itemData);
1722 }
1723
1724 unsigned int wxMacDataItemBrowserControl::GetItemCount(const wxMacDataItem* container,
1725 bool recurse , DataBrowserItemState state) const
1726 {
1727 UInt32 numItems = 0;
1728 verify_noerr( wxMacDataBrowserControl::GetItemCount( (DataBrowserItemID)container,
1729 recurse, state, &numItems ) );
1730 return numItems;
1731 }
1732
1733 void wxMacDataItemBrowserControl::GetItems(const wxMacDataItem* container,
1734 bool recurse , DataBrowserItemState state, wxArrayMacDataItemPtr &items) const
1735 {
1736 Handle handle = NewHandle(0);
1737 verify_noerr( wxMacDataBrowserControl::GetItems( (DataBrowserItemID)container ,
1738 recurse , state, handle) );
1739
1740 int itemCount = GetHandleSize(handle)/sizeof(DataBrowserItemID);
1741 HLock( handle );
1742 wxMacDataItemPtr* itemsArray = (wxMacDataItemPtr*) *handle;
1743 for ( int i = 0; i < itemCount; ++i)
1744 {
1745 items.Add(itemsArray[i]);
1746 }
1747 HUnlock( handle );
1748 DisposeHandle( handle );
1749 }
1750
1751 unsigned int wxMacDataItemBrowserControl::GetLineFromItem(const wxMacDataItem* item) const
1752 {
1753 DataBrowserTableViewRowIndex row;
1754 OSStatus err = GetItemRow( (DataBrowserItemID) item , &row);
1755 wxASSERT( err == noErr);
1756 return row;
1757 }
1758
1759 wxMacDataItem* wxMacDataItemBrowserControl::GetItemFromLine(unsigned int n) const
1760 {
1761 DataBrowserItemID id;
1762 OSStatus err = GetItemID( (DataBrowserTableViewRowIndex) n , &id);
1763 wxASSERT( err == noErr);
1764 return (wxMacDataItem*) id;
1765 }
1766
1767 void wxMacDataItemBrowserControl::UpdateItem(const wxMacDataItem *container,
1768 const wxMacDataItem *item , DataBrowserPropertyID property) const
1769 {
1770 verify_noerr( wxMacDataBrowserControl::UpdateItems((DataBrowserItemID)container, 1,
1771 (DataBrowserItemID*) &item, kDataBrowserItemNoProperty /* notSorted */, property ) );
1772 }
1773
1774 void wxMacDataItemBrowserControl::UpdateItems(const wxMacDataItem *container,
1775 wxArrayMacDataItemPtr &itemArray , DataBrowserPropertyID property) const
1776 {
1777 unsigned int noItems = itemArray.GetCount();
1778 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1779 for ( unsigned int i = 0; i < noItems; ++i )
1780 items[i] = (DataBrowserItemID) itemArray[i];
1781
1782 verify_noerr( wxMacDataBrowserControl::UpdateItems((DataBrowserItemID)container, noItems,
1783 items, kDataBrowserItemNoProperty /* notSorted */, property ) );
1784 delete [] items;
1785 }
1786
1787 void wxMacDataItemBrowserControl::AddItem(wxMacDataItem *container, wxMacDataItem *item)
1788 {
1789 verify_noerr( wxMacDataBrowserControl::AddItems( (DataBrowserItemID)container, 1,
1790 (DataBrowserItemID*) &item, kDataBrowserItemNoProperty ) );
1791 }
1792
1793 void wxMacDataItemBrowserControl::AddItems(wxMacDataItem *container, wxArrayMacDataItemPtr &itemArray )
1794 {
1795 unsigned int noItems = itemArray.GetCount();
1796 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1797 for ( unsigned int i = 0; i < noItems; ++i )
1798 items[i] = (DataBrowserItemID) itemArray[i];
1799
1800 verify_noerr( wxMacDataBrowserControl::AddItems( (DataBrowserItemID)container, noItems,
1801 (DataBrowserItemID*) items, kDataBrowserItemNoProperty ) );
1802 delete [] items;
1803 }
1804
1805 void wxMacDataItemBrowserControl::RemoveItem(wxMacDataItem *container, wxMacDataItem* item)
1806 {
1807 OSStatus err = wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID)container, 1,
1808 (UInt32*) &item, kDataBrowserItemNoProperty );
1809 verify_noerr( err );
1810 }
1811
1812 void wxMacDataItemBrowserControl::RemoveItems(wxMacDataItem *container, wxArrayMacDataItemPtr &itemArray)
1813 {
1814 unsigned int noItems = itemArray.GetCount();
1815 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1816 for ( unsigned int i = 0; i < noItems; ++i )
1817 items[i] = (DataBrowserItemID) itemArray[i];
1818
1819 OSStatus err = wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID)container, noItems,
1820 (UInt32*) items, kDataBrowserItemNoProperty );
1821 verify_noerr( err );
1822 delete [] items;
1823 }
1824
1825 void wxMacDataItemBrowserControl::RemoveAllItems(wxMacDataItem *container)
1826 {
1827 OSStatus err = wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID)container, 0 , NULL , kDataBrowserItemNoProperty );
1828 verify_noerr( err );
1829 }
1830
1831 void wxMacDataItemBrowserControl::SetSelectedItem(wxMacDataItem* item , DataBrowserSetOption option)
1832 {
1833 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( 1, (DataBrowserItemID*) &item, option ));
1834 }
1835
1836 void wxMacDataItemBrowserControl::SetSelectedAllItems(DataBrowserSetOption option)
1837 {
1838 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( 0 , NULL , option ));
1839 }
1840
1841 void wxMacDataItemBrowserControl::SetSelectedItems(wxArrayMacDataItemPtr &itemArray , DataBrowserSetOption option)
1842 {
1843 unsigned int noItems = itemArray.GetCount();
1844 DataBrowserItemID *items = new DataBrowserItemID[noItems];
1845 for ( unsigned int i = 0; i < noItems; ++i )
1846 items[i] = (DataBrowserItemID) itemArray[i];
1847
1848 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( noItems, (DataBrowserItemID*) items, option ));
1849 delete [] items;
1850 }
1851
1852 Boolean wxMacDataItemBrowserControl::IsItemSelected( const wxMacDataItem* item) const
1853 {
1854 return wxMacDataBrowserControl::IsItemSelected( (DataBrowserItemID) item);
1855 }
1856
1857 void wxMacDataItemBrowserControl::RevealItem( wxMacDataItem* item, DataBrowserRevealOptions options)
1858 {
1859 verify_noerr(wxMacDataBrowserControl::RevealItem( (DataBrowserItemID) item, kDataBrowserNoItem , options ) );
1860 }
1861
1862 void wxMacDataItemBrowserControl::GetSelectionAnchor( wxMacDataItemPtr* first , wxMacDataItemPtr* last) const
1863 {
1864 verify_noerr(wxMacDataBrowserControl::GetSelectionAnchor( (DataBrowserItemID*) first, (DataBrowserItemID*) last) );
1865 }
1866
1867
1868
1869 //
1870 // Tab Control
1871 //
1872
1873 OSStatus wxMacControl::SetTabEnabled( SInt16 tabNo , bool enable )
1874 {
1875 return ::SetTabEnabled( m_controlRef , tabNo , enable );
1876 }
1877
1878 //
1879 // Quartz Support
1880 //
1881
1882 #ifdef __WXMAC_OSX__
1883 // snippets from Sketch Sample from Apple :
1884
1885 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
1886
1887 /*
1888 This function locates, opens, and returns the profile reference for the calibrated
1889 Generic RGB color space. It is up to the caller to call CMCloseProfile when done
1890 with the profile reference this function returns.
1891 */
1892 CMProfileRef wxMacOpenGenericProfile()
1893 {
1894 static CMProfileRef cachedRGBProfileRef = NULL;
1895
1896 // we only create the profile reference once
1897 if (cachedRGBProfileRef == NULL)
1898 {
1899 CMProfileLocation loc;
1900
1901 loc.locType = cmPathBasedProfile;
1902 strcpy(loc.u.pathLoc.path, kGenericRGBProfilePathStr);
1903
1904 verify_noerr( CMOpenProfile(&cachedRGBProfileRef, &loc) );
1905 }
1906
1907 // clone the profile reference so that the caller has their own reference, not our cached one
1908 if (cachedRGBProfileRef)
1909 CMCloneProfileRef(cachedRGBProfileRef);
1910
1911 return cachedRGBProfileRef;
1912 }
1913
1914 /*
1915 Return the generic RGB color space. This is a 'get' function and the caller should
1916 not release the returned value unless the caller retains it first. Usually callers
1917 of this routine will immediately use the returned colorspace with CoreGraphics
1918 so they typically do not need to retain it themselves.
1919
1920 This function creates the generic RGB color space once and hangs onto it so it can
1921 return it whenever this function is called.
1922 */
1923
1924 CGColorSpaceRef wxMacGetGenericRGBColorSpace()
1925 {
1926 static wxMacCFRefHolder<CGColorSpaceRef> genericRGBColorSpace;
1927
1928 if (genericRGBColorSpace == NULL)
1929 {
1930 if ( UMAGetSystemVersion() >= 0x1040 )
1931 {
1932 genericRGBColorSpace.Set( CGColorSpaceCreateWithName( CFSTR("kCGColorSpaceGenericRGB") ) );
1933 }
1934 else
1935 {
1936 CMProfileRef genericRGBProfile = wxMacOpenGenericProfile();
1937
1938 if (genericRGBProfile)
1939 {
1940 genericRGBColorSpace.Set( CGColorSpaceCreateWithPlatformColorSpace(genericRGBProfile) );
1941
1942 wxASSERT_MSG( genericRGBColorSpace != NULL, wxT("couldn't create the generic RGB color space") );
1943
1944 // we opened the profile so it is up to us to close it
1945 CMCloseProfile(genericRGBProfile);
1946 }
1947 }
1948 }
1949
1950 return genericRGBColorSpace;
1951 }
1952 #endif
1953
1954 wxMacPortSaver::wxMacPortSaver( GrafPtr port )
1955 {
1956 ::GetPort( &m_port );
1957 ::SetPort( port );
1958 }
1959
1960 wxMacPortSaver::~wxMacPortSaver()
1961 {
1962 ::SetPort( m_port );
1963 }
1964
1965 #endif // wxUSE_GUI