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