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