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