1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/utils.cpp
3 // Purpose: Various utilities
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
21 #include "wx/toplevel.h"
26 #include "wx/apptrait.h"
35 // #include "MoreFilesX.h"
37 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
38 #include <AudioToolbox/AudioServices.h>
41 #include "wx/osx/private.h"
43 #include "wx/osx/private/timer.h"
46 #include "wx/evtloop.h"
48 #if defined(__MWERKS__) && wxUSE_UNICODE
49 #if __MWERKS__ < 0x4100
55 // TODO BEGIN move to utils_osx.cpp
60 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
62 // TODO : try to find out ...
66 #if wxOSX_USE_COCOA_OR_CARBON
68 // our OS version is the same in non GUI and GUI cases
69 wxOperatingSystemId
wxGetOsVersion(int *majorVsn
, int *minorVsn
)
72 Gestalt(gestaltSystemVersion
, &theSystem
);
74 if ( majorVsn
!= NULL
)
75 *majorVsn
= (theSystem
>> 8);
77 if ( minorVsn
!= NULL
)
78 *minorVsn
= (theSystem
& 0xFF);
80 return wxOS_MAC_OSX_DARWIN
;
83 #include <sys/utsname.h>
85 wxString
wxGetOsDescription()
89 return wxString::Format(_T("Mac OS X (%s %s %s)"),
90 wxString::FromAscii(name
.sysname
).c_str(),
91 wxString::FromAscii(name
.release
).c_str(),
92 wxString::FromAscii(name
.machine
).c_str());
95 #endif // wxOSX_USE_COCOA_OR_CARBON
98 //---------------------------------------------------------------------------
99 // wxMac Specific utility functions
100 //---------------------------------------------------------------------------
102 void wxMacStringToPascal( const wxString
&from
, StringPtr to
)
104 wxCharBuffer buf
= from
.mb_str( wxConvLocal
);
105 int len
= strlen(buf
);
110 memcpy( (char*) &to
[1] , buf
, len
);
113 wxString
wxMacMakeStringFromPascal( ConstStringPtr from
)
115 return wxString( (char*) &from
[1] , wxConvLocal
, from
[0] );
122 // Check whether this window wants to process messages, e.g. Stop button
123 // in long calculations.
124 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
130 // Return true if we have a colour display
131 bool wxColourDisplay()
136 #if wxOSX_USE_COCOA_OR_CARBON
137 // Returns depth of screen
140 int theDepth
= (int) CGDisplayBitsPerPixel(CGMainDisplayID());
144 // Get size of display
145 void wxDisplaySize(int *width
, int *height
)
147 // TODO adapt for multi-displays
148 CGRect bounds
= CGDisplayBounds(CGMainDisplayID());
150 *width
= (int)bounds
.size
.width
;
152 *height
= (int)bounds
.size
.height
;
156 void wxDisplaySizeMM(int *width
, int *height
)
158 wxDisplaySize(width
, height
);
159 // on mac 72 is fixed (at least now;-)
160 double cvPt2Mm
= 25.4 / 72;
163 *width
= int( *width
* cvPt2Mm
);
166 *height
= int( *height
* cvPt2Mm
);
170 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
172 // We suppose that toolkit version is the same as OS version under Mac
173 wxGetOsVersion(verMaj
, verMin
);
178 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
180 return new wxEventLoop
;
183 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
185 return wxGenericFindWindowAtPoint(pt
);
189 Return the generic RGB color space. This is a 'get' function and the caller should
190 not release the returned value unless the caller retains it first. Usually callers
191 of this routine will immediately use the returned colorspace with CoreGraphics
192 so they typically do not need to retain it themselves.
194 This function creates the generic RGB color space once and hangs onto it so it can
195 return it whenever this function is called.
198 CGColorSpaceRef
wxMacGetGenericRGBColorSpace()
200 static wxCFRef
<CGColorSpaceRef
> genericRGBColorSpace
;
202 if (genericRGBColorSpace
== NULL
)
205 genericRGBColorSpace
.reset( CGColorSpaceCreateDeviceRGB() );
207 genericRGBColorSpace
.reset( CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB
) );
211 return genericRGBColorSpace
;
214 #if wxOSX_USE_COCOA_OR_CARBON
216 CGColorRef
wxMacCreateCGColorFromHITheme( ThemeBrush brush
)
219 HIThemeBrushCreateCGColor( brush
, &color
);
223 #endif // wxOSX_USE_COCOA_OR_CARBON
225 IMPLEMENT_ABSTRACT_CLASS( wxWidgetImpl
, wxObject
)
227 wxWidgetImpl::wxWidgetImpl( wxWindowMac
* peer
, bool isRootControl
)
230 m_isRootControl
= isRootControl
;
234 wxWidgetImpl::wxWidgetImpl()
239 wxWidgetImpl::~wxWidgetImpl()
243 void wxWidgetImpl::Init()
245 m_isRootControl
= false;
247 m_needsFocusRect
= false;
250 void wxWidgetImpl::Destroy()
254 void wxWidgetImpl::SetNeedsFocusRect( bool needs
)
256 m_needsFocusRect
= needs
;
259 bool wxWidgetImpl::NeedsFocusRect() const
261 return m_needsFocusRect
;
267 // TODO END move to utils_osx.cpp
281 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
282 if ( AudioServicesPlayAlertSound
!= NULL
)
283 AudioServicesPlayAlertSound(kUserPreferredAlert
);
286 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
298 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
300 return new wxCarbonTimerImpl(timer
);
303 int gs_wxBusyCursorCount
= 0;
304 extern wxCursor gMacCurrentCursor
;
305 wxCursor gMacStoredActiveCursor
;
307 // Set the cursor to the busy cursor for all windows
308 void wxBeginBusyCursor(const wxCursor
*cursor
)
310 if (gs_wxBusyCursorCount
++ == 0)
312 gMacStoredActiveCursor
= gMacCurrentCursor
;
313 cursor
->MacInstall();
315 wxSetCursor(*cursor
);
317 //else: nothing to do, already set
320 // Restore cursor to normal
321 void wxEndBusyCursor()
323 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
324 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
326 if (--gs_wxBusyCursorCount
== 0)
328 gMacStoredActiveCursor
.MacInstall();
329 gMacStoredActiveCursor
= wxNullCursor
;
331 wxSetCursor(wxNullCursor
);
335 // true if we're between the above two calls
338 return (gs_wxBusyCursorCount
> 0);
345 wxString
wxMacFindFolderNoSeparator( short vol
,
347 Boolean createFolder
)
352 if ( FSFindFolder( vol
, folderType
, createFolder
, &fsRef
) == noErr
)
354 strDir
= wxMacFSRefToPath( &fsRef
);
360 wxString
wxMacFindFolder( short vol
,
362 Boolean createFolder
)
364 return wxMacFindFolderNoSeparator(vol
, folderType
, createFolder
) + wxFILE_SEP_PATH
;
371 void wxGetMousePosition( int* x
, int* y
)
381 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
383 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
385 HIWindowGetAvailablePositioningBounds(kCGNullDirectDisplay
,kHICoordSpace72DPIGlobal
,
388 *x
= bounds
.origin
.x
;
390 *y
= bounds
.origin
.y
;
392 *width
= bounds
.size
.width
;
394 *height
= bounds
.size
.height
;
397 GetAvailableWindowPositioningBounds( GetMainDevice() , &r
);
403 *width
= r
.right
- r
.left
;
405 *height
= r
.bottom
- r
.top
;
412 // ----------------------------------------------------------------------------
413 // Common Event Support
414 // ----------------------------------------------------------------------------
418 OSStatus err
= noErr
;
422 // lead sometimes to race conditions, although all calls used should be thread safe ...
423 static wxMacCarbonEvent s_wakeupEvent
;
424 if ( !s_wakeupEvent
.IsValid() )
426 err
= s_wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
427 kEventAttributeNone
);
432 if ( IsEventInQueue( GetMainEventQueue() , s_wakeupEvent
) )
434 s_wakeupEvent
.SetCurrentTime();
435 err
= PostEventToQueue(GetMainEventQueue(), s_wakeupEvent
,
436 kEventPriorityHigh
);
439 wxMacCarbonEvent wakeupEvent
;
440 wakeupEvent
.Create( 'WXMC', 'WXMC', GetCurrentEventTime(),
441 kEventAttributeNone
);
442 err
= PostEventToQueue(GetMainEventQueue(), wakeupEvent
,
443 kEventPriorityHigh
);
452 // ----------------------------------------------------------------------------
453 // Native Struct Conversions
454 // ----------------------------------------------------------------------------
456 void wxMacRectToNative( const wxRect
*wx
, Rect
*n
)
460 n
->right
= wx
->x
+ wx
->width
;
461 n
->bottom
= wx
->y
+ wx
->height
;
464 void wxMacNativeToRect( const Rect
*n
, wxRect
* wx
)
468 wx
->width
= n
->right
- n
->left
;
469 wx
->height
= n
->bottom
- n
->top
;
472 void wxMacPointToNative( const wxPoint
* wx
, Point
*n
)
478 void wxMacNativeToPoint( const Point
*n
, wxPoint
* wx
)
484 // ----------------------------------------------------------------------------
485 // Carbon Event Support
486 // ----------------------------------------------------------------------------
488 OSStatus
wxMacCarbonEvent::GetParameter(EventParamName inName
, EventParamType inDesiredType
, UInt32 inBufferSize
, void * outData
)
490 return ::GetEventParameter( m_eventRef
, inName
, inDesiredType
, NULL
, inBufferSize
, NULL
, outData
);
493 OSStatus
wxMacCarbonEvent::SetParameter(EventParamName inName
, EventParamType inType
, UInt32 inBufferSize
, const void * inData
)
495 return ::SetEventParameter( m_eventRef
, inName
, inType
, inBufferSize
, inData
);
498 // ----------------------------------------------------------------------------
499 // Control Access Support
500 // ----------------------------------------------------------------------------
503 // ============================================================================
504 // DataBrowser Wrapper
505 // ============================================================================
507 // basing on DataBrowserItemIDs
510 IMPLEMENT_ABSTRACT_CLASS( wxMacDataBrowserControl
, wxMacControl
)
512 pascal void wxMacDataBrowserControl::DataBrowserItemNotificationProc(
514 DataBrowserItemID itemID
,
515 DataBrowserItemNotification message
,
516 DataBrowserItemDataRef itemData
)
518 wxMacDataBrowserControl
* ctl
= wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser
), wxMacDataBrowserControl
);
521 ctl
->ItemNotification(itemID
, message
, itemData
);
525 pascal OSStatus
wxMacDataBrowserControl::DataBrowserGetSetItemDataProc(
527 DataBrowserItemID itemID
,
528 DataBrowserPropertyID property
,
529 DataBrowserItemDataRef itemData
,
530 Boolean changeValue
)
532 OSStatus err
= errDataBrowserPropertyNotSupported
;
533 wxMacDataBrowserControl
* ctl
= wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser
), wxMacDataBrowserControl
);
536 err
= ctl
->GetSetItemData(itemID
, property
, itemData
, changeValue
);
541 pascal Boolean
wxMacDataBrowserControl::DataBrowserCompareProc(
543 DataBrowserItemID itemOneID
,
544 DataBrowserItemID itemTwoID
,
545 DataBrowserPropertyID sortProperty
)
547 wxMacDataBrowserControl
* ctl
= wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser
), wxMacDataBrowserControl
);
550 return ctl
->CompareItems(itemOneID
, itemTwoID
, sortProperty
);
555 DataBrowserItemDataUPP gDataBrowserItemDataUPP
= NULL
;
556 DataBrowserItemNotificationUPP gDataBrowserItemNotificationUPP
= NULL
;
557 DataBrowserItemCompareUPP gDataBrowserItemCompareUPP
= NULL
;
559 wxMacDataBrowserControl::wxMacDataBrowserControl( wxWindow
* peer
,
562 long WXUNUSED(style
))
563 : wxMacControl( peer
)
565 Rect bounds
= wxMacGetBoundsForControl( peer
, pos
, size
);
566 OSStatus err
= ::CreateDataBrowserControl(
567 MAC_WXHWND(peer
->MacGetTopLevelWindowRef()),
568 &bounds
, kDataBrowserListView
, &m_controlRef
);
569 SetReferenceInNativeControl();
571 if ( gDataBrowserItemCompareUPP
== NULL
)
572 gDataBrowserItemCompareUPP
= NewDataBrowserItemCompareUPP(DataBrowserCompareProc
);
573 if ( gDataBrowserItemDataUPP
== NULL
)
574 gDataBrowserItemDataUPP
= NewDataBrowserItemDataUPP(DataBrowserGetSetItemDataProc
);
575 if ( gDataBrowserItemNotificationUPP
== NULL
)
577 gDataBrowserItemNotificationUPP
=
578 (DataBrowserItemNotificationUPP
) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc
);
581 DataBrowserCallbacks callbacks
;
582 InitializeDataBrowserCallbacks( &callbacks
, kDataBrowserLatestCallbacks
);
584 callbacks
.u
.v1
.itemDataCallback
= gDataBrowserItemDataUPP
;
585 callbacks
.u
.v1
.itemCompareCallback
= gDataBrowserItemCompareUPP
;
586 callbacks
.u
.v1
.itemNotificationCallback
= gDataBrowserItemNotificationUPP
;
587 SetCallbacks( &callbacks
);
591 OSStatus
wxMacDataBrowserControl::GetItemCount( DataBrowserItemID container
,
593 DataBrowserItemState state
,
594 ItemCount
*numItems
) const
596 return GetDataBrowserItemCount( m_controlRef
, container
, recurse
, state
, numItems
);
599 OSStatus
wxMacDataBrowserControl::GetItems( DataBrowserItemID container
,
601 DataBrowserItemState state
,
604 return GetDataBrowserItems( m_controlRef
, container
, recurse
, state
, items
);
607 OSStatus
wxMacDataBrowserControl::SetSelectionFlags( DataBrowserSelectionFlags options
)
609 return SetDataBrowserSelectionFlags( m_controlRef
, options
);
612 OSStatus
wxMacDataBrowserControl::AddColumn( DataBrowserListViewColumnDesc
*columnDesc
,
613 DataBrowserTableViewColumnIndex position
)
615 return AddDataBrowserListViewColumn( m_controlRef
, columnDesc
, position
);
618 OSStatus
wxMacDataBrowserControl::GetColumnIDFromIndex( DataBrowserTableViewColumnIndex position
, DataBrowserTableViewColumnID
* id
){
619 return GetDataBrowserTableViewColumnProperty( m_controlRef
, position
, id
);
622 OSStatus
wxMacDataBrowserControl::RemoveColumn( DataBrowserTableViewColumnIndex position
)
624 DataBrowserTableViewColumnID id
;
625 GetColumnIDFromIndex( position
, &id
);
626 return RemoveDataBrowserTableViewColumn( m_controlRef
, id
);
629 OSStatus
wxMacDataBrowserControl::AutoSizeColumns()
631 return AutoSizeDataBrowserListViewColumns(m_controlRef
);
634 OSStatus
wxMacDataBrowserControl::SetHasScrollBars( bool horiz
, bool vert
)
636 return SetDataBrowserHasScrollBars( m_controlRef
, horiz
, vert
);
639 OSStatus
wxMacDataBrowserControl::SetHiliteStyle( DataBrowserTableViewHiliteStyle hiliteStyle
)
641 return SetDataBrowserTableViewHiliteStyle( m_controlRef
, hiliteStyle
);
644 OSStatus
wxMacDataBrowserControl::SetHeaderButtonHeight(UInt16 height
)
646 return SetDataBrowserListViewHeaderBtnHeight( m_controlRef
, height
);
649 OSStatus
wxMacDataBrowserControl::GetHeaderButtonHeight(UInt16
*height
)
651 return GetDataBrowserListViewHeaderBtnHeight( m_controlRef
, height
);
654 OSStatus
wxMacDataBrowserControl::SetCallbacks(const DataBrowserCallbacks
*callbacks
)
656 return SetDataBrowserCallbacks( m_controlRef
, callbacks
);
659 OSStatus
wxMacDataBrowserControl::UpdateItems(
660 DataBrowserItemID container
,
662 const DataBrowserItemID
*items
,
663 DataBrowserPropertyID preSortProperty
,
664 DataBrowserPropertyID propertyID
) const
666 return UpdateDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
, propertyID
);
669 bool wxMacDataBrowserControl::IsItemSelected( DataBrowserItemID item
) const
671 return IsDataBrowserItemSelected( m_controlRef
, item
);
674 OSStatus
wxMacDataBrowserControl::AddItems(
675 DataBrowserItemID container
,
677 const DataBrowserItemID
*items
,
678 DataBrowserPropertyID preSortProperty
)
680 return AddDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
);
683 OSStatus
wxMacDataBrowserControl::RemoveItems(
684 DataBrowserItemID container
,
686 const DataBrowserItemID
*items
,
687 DataBrowserPropertyID preSortProperty
)
689 return RemoveDataBrowserItems( m_controlRef
, container
, numItems
, items
, preSortProperty
);
692 OSStatus
wxMacDataBrowserControl::RevealItem(
693 DataBrowserItemID item
,
694 DataBrowserPropertyID propertyID
,
695 DataBrowserRevealOptions options
) const
697 return RevealDataBrowserItem( m_controlRef
, item
, propertyID
, options
);
700 OSStatus
wxMacDataBrowserControl::SetSelectedItems(
702 const DataBrowserItemID
*items
,
703 DataBrowserSetOption operation
)
705 return SetDataBrowserSelectedItems( m_controlRef
, numItems
, items
, operation
);
708 OSStatus
wxMacDataBrowserControl::GetSelectionAnchor( DataBrowserItemID
*first
, DataBrowserItemID
*last
) const
710 return GetDataBrowserSelectionAnchor( m_controlRef
, first
, last
);
713 OSStatus
wxMacDataBrowserControl::GetItemID( DataBrowserTableViewRowIndex row
, DataBrowserItemID
* item
) const
715 return GetDataBrowserTableViewItemID( m_controlRef
, row
, item
);
718 OSStatus
wxMacDataBrowserControl::GetItemRow( DataBrowserItemID item
, DataBrowserTableViewRowIndex
* row
) const
720 return GetDataBrowserTableViewItemRow( m_controlRef
, item
, row
);
723 OSStatus
wxMacDataBrowserControl::SetDefaultRowHeight( UInt16 height
)
725 return SetDataBrowserTableViewRowHeight( m_controlRef
, height
);
728 OSStatus
wxMacDataBrowserControl::GetDefaultRowHeight( UInt16
* height
) const
730 return GetDataBrowserTableViewRowHeight( m_controlRef
, height
);
733 OSStatus
wxMacDataBrowserControl::SetRowHeight( DataBrowserItemID item
, UInt16 height
)
735 return SetDataBrowserTableViewItemRowHeight( m_controlRef
, item
, height
);
738 OSStatus
wxMacDataBrowserControl::GetRowHeight( DataBrowserItemID item
, UInt16
*height
) const
740 return GetDataBrowserTableViewItemRowHeight( m_controlRef
, item
, height
);
743 OSStatus
wxMacDataBrowserControl::GetColumnWidth( DataBrowserPropertyID column
, UInt16
*width
) const
745 return GetDataBrowserTableViewNamedColumnWidth( m_controlRef
, column
, width
);
748 OSStatus
wxMacDataBrowserControl::SetColumnWidth( DataBrowserPropertyID column
, UInt16 width
)
750 return SetDataBrowserTableViewNamedColumnWidth( m_controlRef
, column
, width
);
753 OSStatus
wxMacDataBrowserControl::GetDefaultColumnWidth( UInt16
*width
) const
755 return GetDataBrowserTableViewColumnWidth( m_controlRef
, width
);
758 OSStatus
wxMacDataBrowserControl::SetDefaultColumnWidth( UInt16 width
)
760 return SetDataBrowserTableViewColumnWidth( m_controlRef
, width
);
763 OSStatus
wxMacDataBrowserControl::GetColumnCount(UInt32
* numColumns
) const
765 return GetDataBrowserTableViewColumnCount( m_controlRef
, numColumns
);
768 OSStatus
wxMacDataBrowserControl::GetColumnPosition( DataBrowserPropertyID column
,
769 DataBrowserTableViewColumnIndex
*position
) const
771 return GetDataBrowserTableViewColumnPosition( m_controlRef
, column
, position
);
774 OSStatus
wxMacDataBrowserControl::SetColumnPosition( DataBrowserPropertyID column
, DataBrowserTableViewColumnIndex position
)
776 return SetDataBrowserTableViewColumnPosition( m_controlRef
, column
, position
);
779 OSStatus
wxMacDataBrowserControl::GetScrollPosition( UInt32
*top
, UInt32
*left
) const
781 return GetDataBrowserScrollPosition( m_controlRef
, top
, left
);
784 OSStatus
wxMacDataBrowserControl::SetScrollPosition( UInt32 top
, UInt32 left
)
786 return SetDataBrowserScrollPosition( m_controlRef
, top
, left
);
789 OSStatus
wxMacDataBrowserControl::GetSortProperty( DataBrowserPropertyID
*column
) const
791 return GetDataBrowserSortProperty( m_controlRef
, column
);
794 OSStatus
wxMacDataBrowserControl::SetSortProperty( DataBrowserPropertyID column
)
796 return SetDataBrowserSortProperty( m_controlRef
, column
);
799 OSStatus
wxMacDataBrowserControl::GetSortOrder( DataBrowserSortOrder
*order
) const
801 return GetDataBrowserSortOrder( m_controlRef
, order
);
804 OSStatus
wxMacDataBrowserControl::SetSortOrder( DataBrowserSortOrder order
)
806 return SetDataBrowserSortOrder( m_controlRef
, order
);
809 OSStatus
wxMacDataBrowserControl::GetPropertyFlags( DataBrowserPropertyID property
,
810 DataBrowserPropertyFlags
*flags
) const
812 return GetDataBrowserPropertyFlags( m_controlRef
, property
, flags
);
815 OSStatus
wxMacDataBrowserControl::SetPropertyFlags( DataBrowserPropertyID property
,
816 DataBrowserPropertyFlags flags
)
818 return SetDataBrowserPropertyFlags( m_controlRef
, property
, flags
);
821 OSStatus
wxMacDataBrowserControl::GetHeaderDesc( DataBrowserPropertyID property
,
822 DataBrowserListViewHeaderDesc
*desc
) const
824 return GetDataBrowserListViewHeaderDesc( m_controlRef
, property
, desc
);
827 OSStatus
wxMacDataBrowserControl::SetHeaderDesc( DataBrowserPropertyID property
,
828 DataBrowserListViewHeaderDesc
*desc
)
830 return SetDataBrowserListViewHeaderDesc( m_controlRef
, property
, desc
);
833 OSStatus
wxMacDataBrowserControl::SetDisclosureColumn( DataBrowserPropertyID property
,
834 Boolean expandableRows
)
836 return SetDataBrowserListViewDisclosureColumn( m_controlRef
, property
, expandableRows
);
839 // ============================================================================
840 // Higher-level Databrowser
841 // ============================================================================
843 // basing on data item objects
846 wxMacDataItem::wxMacDataItem()
851 m_colId
= kTextColumnId
; // for compat with existing wx*ListBox impls.
854 wxMacDataItem::~wxMacDataItem()
858 void wxMacDataItem::SetOrder( SInt32 order
)
863 SInt32
wxMacDataItem::GetOrder() const
868 void wxMacDataItem::SetData( void* data
)
873 void* wxMacDataItem::GetData() const
878 short wxMacDataItem::GetColumn()
883 void wxMacDataItem::SetColumn( short col
)
888 void wxMacDataItem::SetLabel( const wxString
& str
)
891 m_cfLabel
= wxCFStringRef( str
, wxLocale::GetSystemEncoding());
894 const wxString
& wxMacDataItem::GetLabel() const
899 bool wxMacDataItem::IsLessThan(wxMacDataItemBrowserControl
*WXUNUSED(owner
) ,
900 const wxMacDataItem
* rhs
,
901 DataBrowserPropertyID sortProperty
) const
903 const wxMacDataItem
* otherItem
= wx_const_cast(wxMacDataItem
*,rhs
);
906 if ( sortProperty
== m_colId
){
907 retval
= m_label
.CmpNoCase( otherItem
->m_label
) < 0;
910 else if ( sortProperty
== kNumericOrderColumnId
)
911 retval
= m_order
< otherItem
->m_order
;
916 OSStatus
wxMacDataItem::GetSetData( wxMacDataItemBrowserControl
*WXUNUSED(owner
) ,
917 DataBrowserPropertyID property
,
918 DataBrowserItemDataRef itemData
,
921 OSStatus err
= errDataBrowserPropertyNotSupported
;
924 if ( property
== m_colId
){
925 err
= ::SetDataBrowserItemDataText( itemData
, m_cfLabel
);
928 else if ( property
== kNumericOrderColumnId
){
929 err
= ::SetDataBrowserItemDataValue( itemData
, m_order
);
939 // no editable props here
948 void wxMacDataItem::Notification(wxMacDataItemBrowserControl
*WXUNUSED(owner
) ,
949 DataBrowserItemNotification
WXUNUSED(message
),
950 DataBrowserItemDataRef
WXUNUSED(itemData
) ) const
954 IMPLEMENT_DYNAMIC_CLASS( wxMacDataItemBrowserControl
, wxMacDataBrowserControl
)
956 wxMacDataItemBrowserControl::wxMacDataItemBrowserControl( wxWindow
* peer
, const wxPoint
& pos
, const wxSize
& size
, long style
) :
957 wxMacDataBrowserControl( peer
, pos
, size
, style
)
959 m_suppressSelection
= false;
960 m_sortOrder
= SortOrder_None
;
961 m_clientDataItemsType
= wxClientData_None
;
964 wxMacDataItem
* wxMacDataItemBrowserControl::CreateItem()
966 return new wxMacDataItem();
969 wxMacDataItemBrowserSelectionSuppressor::wxMacDataItemBrowserSelectionSuppressor(wxMacDataItemBrowserControl
*browser
)
971 m_former
= browser
->SuppressSelection(true);
975 wxMacDataItemBrowserSelectionSuppressor::~wxMacDataItemBrowserSelectionSuppressor()
977 m_browser
->SuppressSelection(m_former
);
980 bool wxMacDataItemBrowserControl::SuppressSelection( bool suppress
)
982 bool former
= m_suppressSelection
;
983 m_suppressSelection
= suppress
;
988 Boolean
wxMacDataItemBrowserControl::CompareItems(DataBrowserItemID itemOneID
,
989 DataBrowserItemID itemTwoID
,
990 DataBrowserPropertyID sortProperty
)
992 wxMacDataItem
* itemOne
= (wxMacDataItem
*) itemOneID
;
993 wxMacDataItem
* itemTwo
= (wxMacDataItem
*) itemTwoID
;
994 return CompareItems( itemOne
, itemTwo
, sortProperty
);
997 Boolean
wxMacDataItemBrowserControl::CompareItems(const wxMacDataItem
* itemOne
,
998 const wxMacDataItem
* itemTwo
,
999 DataBrowserPropertyID sortProperty
)
1001 Boolean retval
= false;
1002 if ( itemOne
!= NULL
)
1003 retval
= itemOne
->IsLessThan( this , itemTwo
, sortProperty
);
1007 OSStatus
wxMacDataItemBrowserControl::GetSetItemData(
1008 DataBrowserItemID itemID
,
1009 DataBrowserPropertyID property
,
1010 DataBrowserItemDataRef itemData
,
1011 Boolean changeValue
)
1013 wxMacDataItem
* item
= (wxMacDataItem
*) itemID
;
1014 return GetSetItemData(item
, property
, itemData
, changeValue
);
1017 OSStatus
wxMacDataItemBrowserControl::GetSetItemData(
1018 wxMacDataItem
* item
,
1019 DataBrowserPropertyID property
,
1020 DataBrowserItemDataRef itemData
,
1021 Boolean changeValue
)
1023 OSStatus err
= errDataBrowserPropertyNotSupported
;
1026 case kDataBrowserContainerIsClosableProperty
:
1027 case kDataBrowserContainerIsSortableProperty
:
1028 case kDataBrowserContainerIsOpenableProperty
:
1029 // right now default behaviour on these
1033 if ( item
!= NULL
){
1034 err
= item
->GetSetData( this, property
, itemData
, changeValue
);
1042 void wxMacDataItemBrowserControl::ItemNotification(
1043 DataBrowserItemID itemID
,
1044 DataBrowserItemNotification message
,
1045 DataBrowserItemDataRef itemData
)
1047 wxMacDataItem
* item
= (wxMacDataItem
*) itemID
;
1048 ItemNotification( item
, message
, itemData
);
1051 void wxMacDataItemBrowserControl::ItemNotification(
1052 const wxMacDataItem
* item
,
1053 DataBrowserItemNotification message
,
1054 DataBrowserItemDataRef itemData
)
1057 item
->Notification( this, message
, itemData
);
1060 unsigned int wxMacDataItemBrowserControl::GetItemCount(const wxMacDataItem
* container
,
1061 bool recurse
, DataBrowserItemState state
) const
1063 ItemCount numItems
= 0;
1064 verify_noerr( wxMacDataBrowserControl::GetItemCount( (DataBrowserItemID
)container
,
1065 recurse
, state
, &numItems
) );
1069 unsigned int wxMacDataItemBrowserControl::GetSelectedItemCount( const wxMacDataItem
* container
,
1070 bool recurse
) const
1072 return GetItemCount( container
, recurse
, kDataBrowserItemIsSelected
);
1076 void wxMacDataItemBrowserControl::GetItems(const wxMacDataItem
* container
,
1077 bool recurse
, DataBrowserItemState state
, wxArrayMacDataItemPtr
&items
) const
1079 Handle handle
= NewHandle(0);
1080 verify_noerr( wxMacDataBrowserControl::GetItems( (DataBrowserItemID
)container
,
1081 recurse
, state
, handle
) );
1083 int itemCount
= GetHandleSize(handle
)/sizeof(DataBrowserItemID
);
1085 wxMacDataItemPtr
* itemsArray
= (wxMacDataItemPtr
*) *handle
;
1086 for ( int i
= 0; i
< itemCount
; ++i
)
1088 items
.Add(itemsArray
[i
]);
1091 DisposeHandle( handle
);
1094 unsigned int wxMacDataItemBrowserControl::GetLineFromItem(const wxMacDataItem
* item
) const
1096 DataBrowserTableViewRowIndex row
;
1097 OSStatus err
= GetItemRow( (DataBrowserItemID
) item
, &row
);
1098 wxCHECK( err
== noErr
, (unsigned)-1 );
1102 wxMacDataItem
* wxMacDataItemBrowserControl::GetItemFromLine(unsigned int n
) const
1104 DataBrowserItemID id
;
1105 OSStatus err
= GetItemID( (DataBrowserTableViewRowIndex
) n
, &id
);
1106 wxCHECK( err
== noErr
, NULL
);
1107 return (wxMacDataItem
*) id
;
1110 void wxMacDataItemBrowserControl::UpdateItem(const wxMacDataItem
*container
,
1111 const wxMacDataItem
*item
, DataBrowserPropertyID property
) const
1113 verify_noerr( wxMacDataBrowserControl::UpdateItems((DataBrowserItemID
)container
, 1,
1114 (DataBrowserItemID
*) &item
, kDataBrowserItemNoProperty
/* notSorted */, property
) );
1117 void wxMacDataItemBrowserControl::UpdateItems(const wxMacDataItem
*container
,
1118 wxArrayMacDataItemPtr
&itemArray
, DataBrowserPropertyID property
) const
1120 unsigned int noItems
= itemArray
.GetCount();
1121 DataBrowserItemID
*items
= new DataBrowserItemID
[noItems
];
1122 for ( unsigned int i
= 0; i
< noItems
; ++i
)
1123 items
[i
] = (DataBrowserItemID
) itemArray
[i
];
1125 verify_noerr( wxMacDataBrowserControl::UpdateItems((DataBrowserItemID
)container
, noItems
,
1126 items
, kDataBrowserItemNoProperty
/* notSorted */, property
) );
1130 void wxMacDataItemBrowserControl::InsertColumn(int colId
, DataBrowserPropertyType colType
,
1131 const wxString
& title
, SInt16 just
, int defaultWidth
)
1133 DataBrowserListViewColumnDesc columnDesc
;
1134 columnDesc
.headerBtnDesc
.titleOffset
= 0;
1135 columnDesc
.headerBtnDesc
.version
= kDataBrowserListViewLatestHeaderDesc
;
1137 columnDesc
.headerBtnDesc
.btnFontStyle
.flags
=
1138 kControlUseFontMask
| kControlUseJustMask
;
1140 columnDesc
.headerBtnDesc
.btnContentInfo
.contentType
= kControlContentTextOnly
;
1141 columnDesc
.headerBtnDesc
.btnFontStyle
.just
= just
;
1142 columnDesc
.headerBtnDesc
.btnFontStyle
.font
= kControlFontViewSystemFont
;
1143 columnDesc
.headerBtnDesc
.btnFontStyle
.style
= normal
;
1145 // TODO: Why is m_font not defined when we enter wxLC_LIST mode, but is
1146 // defined for other modes?
1149 enc
= m_font
.GetEncoding();
1151 enc
= wxLocale::GetSystemEncoding();
1152 wxCFStringRef
cfTitle( title
, enc
);
1153 columnDesc
.headerBtnDesc
.titleString
= cfTitle
;
1155 columnDesc
.headerBtnDesc
.minimumWidth
= 0;
1156 columnDesc
.headerBtnDesc
.maximumWidth
= 30000;
1158 columnDesc
.propertyDesc
.propertyID
= (kMinColumnId
+ colId
);
1159 columnDesc
.propertyDesc
.propertyType
= colType
;
1160 columnDesc
.propertyDesc
.propertyFlags
= kDataBrowserListViewSortableColumn
;
1161 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewTypeSelectColumn
;
1162 columnDesc
.propertyDesc
.propertyFlags
|= kDataBrowserListViewNoGapForIconInHeaderButton
;
1164 verify_noerr( AddColumn( &columnDesc
, kDataBrowserListViewAppendColumn
) );
1166 if (defaultWidth
> 0){
1167 SetColumnWidth(colId
, defaultWidth
);
1172 void wxMacDataItemBrowserControl::SetColumnWidth(int colId
, int width
)
1174 DataBrowserPropertyID id
;
1175 GetColumnIDFromIndex(colId
, &id
);
1176 verify_noerr( wxMacDataBrowserControl::SetColumnWidth(id
, width
));
1179 int wxMacDataItemBrowserControl::GetColumnWidth(int colId
)
1181 DataBrowserPropertyID id
;
1182 GetColumnIDFromIndex(colId
, &id
);
1184 verify_noerr( wxMacDataBrowserControl::GetColumnWidth(id
, &result
));
1188 void wxMacDataItemBrowserControl::AddItem(wxMacDataItem
*container
, wxMacDataItem
*item
)
1190 verify_noerr( wxMacDataBrowserControl::AddItems( (DataBrowserItemID
)container
, 1,
1191 (DataBrowserItemID
*) &item
, kDataBrowserItemNoProperty
) );
1194 void wxMacDataItemBrowserControl::AddItems(wxMacDataItem
*container
, wxArrayMacDataItemPtr
&itemArray
)
1196 unsigned int noItems
= itemArray
.GetCount();
1197 DataBrowserItemID
*items
= new DataBrowserItemID
[noItems
];
1198 for ( unsigned int i
= 0; i
< noItems
; ++i
)
1199 items
[i
] = (DataBrowserItemID
) itemArray
[i
];
1201 verify_noerr( wxMacDataBrowserControl::AddItems( (DataBrowserItemID
)container
, noItems
,
1202 (DataBrowserItemID
*) items
, kDataBrowserItemNoProperty
) );
1206 void wxMacDataItemBrowserControl::RemoveItem(wxMacDataItem
*container
, wxMacDataItem
* item
)
1208 OSStatus err
= wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID
)container
, 1,
1209 (DataBrowserItemID
*) &item
, kDataBrowserItemNoProperty
);
1210 verify_noerr( err
);
1213 void wxMacDataItemBrowserControl::RemoveItems(wxMacDataItem
*container
, wxArrayMacDataItemPtr
&itemArray
)
1215 unsigned int noItems
= itemArray
.GetCount();
1216 DataBrowserItemID
*items
= new DataBrowserItemID
[noItems
];
1217 for ( unsigned int i
= 0; i
< noItems
; ++i
)
1218 items
[i
] = (DataBrowserItemID
) itemArray
[i
];
1220 OSStatus err
= wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID
)container
, noItems
,
1221 (DataBrowserItemID
*) items
, kDataBrowserItemNoProperty
);
1222 verify_noerr( err
);
1226 void wxMacDataItemBrowserControl::RemoveAllItems(wxMacDataItem
*container
)
1228 OSStatus err
= wxMacDataBrowserControl::RemoveItems( (DataBrowserItemID
)container
, 0 , NULL
, kDataBrowserItemNoProperty
);
1229 verify_noerr( err
);
1232 void wxMacDataItemBrowserControl::SetSelectedItem(wxMacDataItem
* item
, DataBrowserSetOption option
)
1234 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( 1, (DataBrowserItemID
*) &item
, option
));
1237 void wxMacDataItemBrowserControl::SetSelectedAllItems(DataBrowserSetOption option
)
1239 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( 0 , NULL
, option
));
1242 void wxMacDataItemBrowserControl::SetSelectedItems(wxArrayMacDataItemPtr
&itemArray
, DataBrowserSetOption option
)
1244 unsigned int noItems
= itemArray
.GetCount();
1245 DataBrowserItemID
*items
= new DataBrowserItemID
[noItems
];
1246 for ( unsigned int i
= 0; i
< noItems
; ++i
)
1247 items
[i
] = (DataBrowserItemID
) itemArray
[i
];
1249 verify_noerr(wxMacDataBrowserControl::SetSelectedItems( noItems
, (DataBrowserItemID
*) items
, option
));
1253 Boolean
wxMacDataItemBrowserControl::IsItemSelected( const wxMacDataItem
* item
) const
1255 return wxMacDataBrowserControl::IsItemSelected( (DataBrowserItemID
) item
);
1258 void wxMacDataItemBrowserControl::RevealItem( wxMacDataItem
* item
, DataBrowserRevealOptions options
)
1260 verify_noerr(wxMacDataBrowserControl::RevealItem( (DataBrowserItemID
) item
, kDataBrowserNoItem
, options
) );
1263 void wxMacDataItemBrowserControl::GetSelectionAnchor( wxMacDataItemPtr
* first
, wxMacDataItemPtr
* last
) const
1265 verify_noerr(wxMacDataBrowserControl::GetSelectionAnchor( (DataBrowserItemID
*) first
, (DataBrowserItemID
*) last
) );
1268 wxClientDataType
wxMacDataItemBrowserControl::GetClientDataType() const
1270 return m_clientDataItemsType
;
1272 void wxMacDataItemBrowserControl::SetClientDataType(wxClientDataType clientDataItemsType
)
1274 m_clientDataItemsType
= clientDataItemsType
;
1277 unsigned int wxMacDataItemBrowserControl::MacGetCount() const
1279 return GetItemCount(wxMacDataBrowserRootContainer
,false,kDataBrowserItemAnyState
);
1282 void wxMacDataItemBrowserControl::MacDelete( unsigned int n
)
1284 wxMacDataItem
* item
= (wxMacDataItem
*)GetItemFromLine( n
);
1285 RemoveItem( wxMacDataBrowserRootContainer
, item
);
1288 void wxMacDataItemBrowserControl::MacInsert( unsigned int n
,
1289 const wxArrayStringsAdapter
& items
,
1292 size_t itemsCount
= items
.GetCount();
1293 if ( itemsCount
== 0 )
1296 SInt32 frontLineOrder
= 0;
1298 if ( m_sortOrder
== SortOrder_None
)
1300 // increase the order of the lines to be shifted
1301 unsigned int lines
= MacGetCount();
1302 for ( unsigned int i
= n
; i
< lines
; ++i
)
1304 wxMacDataItem
* iter
= (wxMacDataItem
*) GetItemFromLine(i
);
1305 iter
->SetOrder( iter
->GetOrder() + itemsCount
);
1309 wxMacDataItem
* iter
= (wxMacDataItem
*) GetItemFromLine(n
-1);
1310 frontLineOrder
= iter
->GetOrder();
1314 wxArrayMacDataItemPtr ids
;
1315 ids
.SetCount( itemsCount
);
1317 for ( unsigned int i
= 0; i
< itemsCount
; ++i
)
1319 wxMacDataItem
* item
= CreateItem();
1320 item
->SetLabel( items
[i
]);
1322 item
->SetColumn( kMinColumnId
+ column
);
1324 if ( m_sortOrder
== SortOrder_None
)
1325 item
->SetOrder( frontLineOrder
+ 1 + i
);
1330 AddItems( wxMacDataBrowserRootContainer
, ids
);
1333 int wxMacDataItemBrowserControl::MacAppend( const wxString
& text
)
1335 wxMacDataItem
* item
= CreateItem();
1336 item
->SetLabel( text
);
1337 if ( m_sortOrder
== SortOrder_None
)
1339 unsigned int lines
= MacGetCount();
1341 item
->SetOrder( 1 );
1344 wxMacDataItem
* frontItem
= (wxMacDataItem
*) GetItemFromLine(lines
-1);
1345 item
->SetOrder( frontItem
->GetOrder() + 1 );
1348 AddItem( wxMacDataBrowserRootContainer
, item
);
1350 return GetLineFromItem(item
);
1353 void wxMacDataItemBrowserControl::MacClear()
1355 wxMacDataItemBrowserSelectionSuppressor
suppressor(this);
1356 RemoveAllItems(wxMacDataBrowserRootContainer
);
1359 void wxMacDataItemBrowserControl::MacDeselectAll()
1361 wxMacDataItemBrowserSelectionSuppressor
suppressor(this);
1362 SetSelectedAllItems( kDataBrowserItemsRemove
);
1365 void wxMacDataItemBrowserControl::MacSetSelection( unsigned int n
, bool select
, bool multi
)
1367 wxMacDataItem
* item
= (wxMacDataItem
*) GetItemFromLine(n
);
1368 wxMacDataItemBrowserSelectionSuppressor
suppressor(this);
1370 if ( IsItemSelected( item
) != select
)
1373 SetSelectedItem( item
, multi
? kDataBrowserItemsAdd
: kDataBrowserItemsAssign
);
1375 SetSelectedItem( item
, kDataBrowserItemsRemove
);
1381 bool wxMacDataItemBrowserControl::MacIsSelected( unsigned int n
) const
1383 wxMacDataItem
* item
= (wxMacDataItem
*) GetItemFromLine(n
);
1384 return IsItemSelected( item
);
1387 int wxMacDataItemBrowserControl::MacGetSelection() const
1389 wxMacDataItemPtr first
, last
;
1390 GetSelectionAnchor( &first
, &last
);
1392 if ( first
!= NULL
)
1394 return GetLineFromItem( first
);
1400 int wxMacDataItemBrowserControl::MacGetSelections( wxArrayInt
& aSelections
) const
1402 aSelections
.Empty();
1403 wxArrayMacDataItemPtr selectedItems
;
1404 GetItems( wxMacDataBrowserRootContainer
, false , kDataBrowserItemIsSelected
, selectedItems
);
1406 int count
= selectedItems
.GetCount();
1408 for ( int i
= 0; i
< count
; ++i
)
1410 aSelections
.Add(GetLineFromItem(selectedItems
[i
]));
1416 void wxMacDataItemBrowserControl::MacSetString( unsigned int n
, const wxString
& text
)
1418 // as we don't store the strings we only have to issue a redraw
1419 wxMacDataItem
* item
= (wxMacDataItem
*) GetItemFromLine( n
);
1420 item
->SetLabel( text
);
1421 UpdateItem( wxMacDataBrowserRootContainer
, item
, kTextColumnId
);
1424 wxString
wxMacDataItemBrowserControl::MacGetString( unsigned int n
) const
1426 wxMacDataItem
* item
= (wxMacDataItem
*) GetItemFromLine( n
);
1427 return item
->GetLabel();
1430 void wxMacDataItemBrowserControl::MacSetClientData( unsigned int n
, void * data
)
1432 wxMacDataItem
* item
= (wxMacDataItem
*) GetItemFromLine( n
);
1433 item
->SetData( data
);
1434 // not displayed, therefore no Update infos to DataBrowser
1437 void * wxMacDataItemBrowserControl::MacGetClientData( unsigned int n
) const
1439 wxMacDataItem
* item
= (wxMacDataItem
*) GetItemFromLine( n
);
1440 return item
->GetData();
1443 void wxMacDataItemBrowserControl::MacScrollTo( unsigned int n
)
1446 GetScrollPosition( &top
, &left
) ;
1447 wxMacDataItem
* item
= (wxMacDataItem
*) GetItemFromLine( n
);
1449 // there is a bug in RevealItem that leads to situations
1450 // in large lists, where the item does not get scrolled
1451 // into sight, so we do a pre-scroll if necessary
1453 GetRowHeight( (DataBrowserItemID
) item
, &height
) ;
1454 UInt32 linetop
= n
* ((UInt32
) height
);
1455 UInt32 linebottom
= linetop
+ height
;
1457 GetControlBounds( m_controlRef
, &rect
);
1459 if ( linetop
< top
|| linebottom
> (top
+ rect
.bottom
- rect
.top
) )
1460 SetScrollPosition( wxMax( n
-2, 0 ) * ((UInt32
)height
) , left
) ;
1462 RevealItem( item
, kDataBrowserRevealWithoutSelecting
);
1467 #endif // wxOSX_USE_CARBON